无法从Java中反编译来自C ++的protobuf数据 [英] Cannot deserialize protobuf data from C++ in Java
问题描述
我的问题是用C ++序列化protobuf数据并用Java反序列化数据。
这是我用于dcn给出的提示的代码:
My problem is to serialize protobuf data in C++ and deserialize the data in Java probably. Here is the code I use to the hints given by dcn:
这样我用C ++创建protobuf数据并将其写入ostream,即发送通过套接字。
With this I create the protobuf data in C++ and write it to an ostream which is send via socket.
Name name;
name.set_name("platzhirsch");
boost::asio::streambuf b;
std::ostream os(&b);
ZeroCopyOutputStream *raw_output = new OstreamOutputStream(&os);
CodedOutputStream *coded_output = new CodedOutputStream(raw_output);
coded_output->WriteLittleEndian32(name.ByteSize());
name.SerializeToCodedStream(coded_output);
socket.send(b);
这是我尝试解析它的Java端:
This is the Java side where I try to parse it:
NameProtos.Name name = NameProtos.Name.parseDelimitedFrom(socket.getInputStream());
System.out.println(name.newBuilder().build().toString());
但是我得到了这个例外:
com.google.protobuf。 UninitializedMessageException:消息缺少必填字段:名称
However by this I get this Exception: com.google.protobuf.UninitializedMessageException: Message missing required fields: name
我缺少什么?
有缺陷的代码行是: name.newBuilder()。build()。toString()
这将永远不会有效,使用未初始化的名称字段创建一个新实例。无论如何,这里的答案解决了我的其余问题。
This would have never worked, a new instance is created with uninitialized name field. Anyway the answer here solved the rest of my problem.
最后一件事,我在protobuf邮件列表中被告知:为了刷新CodedOutputStreams,对象必须被删除!
One last thing, which I was told in the protobuf mailinglist: In order to flush the CodedOutputStreams, the objects have to be deleted!
delete coded_output;
delete raw_output;
推荐答案
我不知道收到
在您的Java代码中,但您的问题可能是由于某些字符集转换。另请注意,protobuf在序列化时不会分隔消息。
I don't know what received
is in your Java code, but your problem may be due to some charset conversion. Note also that protobuf does not delimit the messages when serializing.
因此,您应该使用原始数据来传输消息(字节数组或直接(反)串行化/流到流)。
如果您打算发送许多消息,您还应该在发送实际消息之前发送大小。
Therefore you should use raw data to transmit the messages (byte array or directly (de)serialize from/to streams). If you intent to send many message you should also send the size before you send the actual messages.
在Java中,你可以通过 parseDelimitedFrom(InputStream)
和 writeDelimitedTo(OutputStream)直接完成)
。使用 CodedOutputStream
就可以在C ++中执行相同的操作
In Java you can do it directly via parseDelimitedFrom(InputStream)
and writeDelimitedTo(OutputStream)
. You can do the same in C++ a litte more complex using CodedOutputStream
like
codedOutput.WriteVarint32(protoMessage.ByteSize());
protoMessage.SerializeToCodedStream(&codedOutput);
参见这个更早的线程。
这篇关于无法从Java中反编译来自C ++的protobuf数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!