gpt4 book ai didi

javascript - protobuf 解码中出现非法组结束指示符错误

转载 作者:行者123 更新时间:2023-11-27 23:40:59 29 4
gpt4 key购买 nike

当我尝试使用 protobuf.js 解码来解码字节数组时,出现以下错误

错误:消息 .SampleMessage 的组结束指示符非法:1749(不是组) 错误时(本地) 在 ProtoBuf.Reflect.MessagePrototype.decode ( http://127.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 )

代码片段:样本.proto - 文件

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);

最佳答案

我强烈怀疑这就是问题所在:

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

为什么使用ObjectOutputStream?这是 Java 的 native 二进制序列化协议(protocol),它与 Protocol Buffer 不同。尽管 protobuf 对 Java 序列化有一些支持(因此,如果您已经在使用内置序列化,您仍然可以序列化 protobuf 消息),但您不应该使用它,除非您正在使用两侧均进行 Java 序列化。

你应该使用

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

关于javascript - protobuf 解码中出现非法组结束指示符错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33658581/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com