Illegal group end indicator error in protobuf decode

旧时模样 提交于 2020-01-25 04:23:41

问题


I am getting the following error when i try to decode bytearray using protobuf.js decode

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)

code snippet: sample.proto - file

message SampleMessage {
    required string text = 1;
}

Java code For encode:

 SampleMessage msg = SampleProto.SampleMessage.newBuilder().setText("test data ").build();
 ObjectOutputStream out = new ObjectOutputStream(response.getOutputStream());
 out.writeObject(msg);
 out.flush();

Decode using javascript :

var ProtoBuf = dcodeIO.ProtoBuf;
var SampleMessage = ProtoBuf.loadProtoFile("com/cm/model/sample.proto").build("SampleMessage");  
var msg = SampleMessage.decode(response.data);

回答1:


I strongly suspect that this is the problem:

ObjectOutputStream out = new ObjectOutputStream(response.getOutputStream());
out.writeObject(msg);

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.

You should be using

SampleMessage msg = SampleProto.SampleMessage.newBuilder().setText("test data ").build();
msg.writeTo(response.getOutputStream());


来源:https://stackoverflow.com/questions/33658581/illegal-group-end-indicator-error-in-protobuf-decode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!