protobuf 解码中的非法组结束指示符错误 [英] Illegal group end indicator error in protobuf decode
问题描述
当我尝试使用 protobuf.js decode 解码 bytearray 时出现以下错误
I am getting the following error when i try to decode bytearray using protobuf.js decode
错误:消息的非法组结束指示符.SampleMessage:1749(不是组)在错误(本机)在 ProtoBuf.Reflect.MessagePrototype.decode (http://127.0.0.0.1:53259/libs/protobuf/dist/ProtoBuf.js:3168:31)在 Function.Message.decode (http://127.0.0.1:53259/libs/protobuf/dist/ProtoBuf.js:2896:37)
error: Illegal group end indicator for Message .SampleMessage: 1749 (not a group) at Error (native) at ProtoBuf.Reflect.MessagePrototype.decode (http://127.0.0.1:53259/libs/protobuf/dist/ProtoBuf.js:3168:31) at Function.Message.decode (http://127.0.0.1:53259/libs/protobuf/dist/ProtoBuf.js:2896:37)
代码片段:sample.proto - 文件
code snippet: sample.proto - file
message SampleMessage {
required string text = 1;
}
Java 代码用于编码:
SampleMessage msg = SampleProto.SampleMessage.newBuilder().setText("test data ").build();
ObjectOutputStream out = new ObjectOutputStream(response.getOutputStream());
out.writeObject(msg);
out.flush();
使用 javascript 解码:
var ProtoBuf = dcodeIO.ProtoBuf;
var SampleMessage = ProtoBuf.loadProtoFile("com/cm/model/sample.proto").build("SampleMessage");
var msg = SampleMessage.decode(response.data);
推荐答案
我强烈怀疑是这个问题:
I strongly suspect that this is the problem:
ObjectOutputStream out = new ObjectOutputStream(response.getOutputStream());
out.writeObject(msg);
你为什么使用 ObjectOutputStream
?这是针对 Java 的本机二进制序列化协议,它与协议缓冲区不同.尽管 protobuf some 支持 Java 的序列化(这样如果您已经在使用内置序列化,您仍然可以序列化 protobuf 消息)您不应该使用它,除非您正在使用双方的Java序列化.
Why are you using ObjectOutputStream
? That's for Java's native binary serialization protocol, which isn't the same as Protocol Buffers. Even though protobuf has some support for Java's serialization (so that if you're already using the built-in serialization, you can still serialize protobuf messages) you shouldn't be using that unless you're using Java serialization at both sides.
你应该使用
SampleMessage msg = SampleProto.SampleMessage.newBuilder().setText("test data ").build();
msg.writeTo(response.getOutputStream());
这篇关于protobuf 解码中的非法组结束指示符错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!