Cannot deserialize protobuf data from C++ in Java

我只是一个虾纸丫 提交于 2019-12-04 12:28:10
dcn

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.

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

See also this ealier thread.

You're writing two things to the stream, a size and the Name object, but only trying to read one.

As a general question: why do you feel the need to use CodedInputStream? To quote the docs:

Typically these classes will only be used internally by the protocol buffer library in order to encode and decode protocol buffers. Clients of the library only need to know about this class if they wish to write custom message parsing or serialization procedures

And to emphasize jtahlborn's comment: why little-endian? Java deals with big-endian values, so will have to convert on reading.

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