java反编译数组,无法从Java中反编译来自C ++的protobuf数据

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:

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

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

However by this I get this Exception:

com.google.protobuf.UninitializedMessageException: Message missing required fields: name

What am I missing?

The flawed code line is: 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.

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;

解决方案

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值