0
votes

So I have a socket , I send some data on it using protobuf CodedOutputStream like this:

 int size = myMessage.getSerializedBytes();
 out.writeRawVarint32(size) ; // out = CodedOutputStream created from a java.io.OutputStream
 myMessage.writeTo(out);
 out.flush();

Client code compiles and writes. How do i read this correctly on server side? If i use DataInputStream readByte() to read size I get a negative value for size... If I use CodedInputStream to read size via readRawVarint32 I get a large value 10x the size of the serialized message i sent.

How do I read a protobuf message from an InputStream in java??

CodedInputStream ? DataInputStream ?

I read the docs cannot find this documented anywhere. Do i need to drop down to protocol level and start debugging bytes ?

1

1 Answers

0
votes

Be careful what you type in your IDE, I made this little blunder in my OutputStream definition:

   OutputStream out = new ObjectOutputStream( client.getOutputStream() );
 

I was figuring out how to serialize protobuf message. I guess at first I figured I should use ObjectOutputStream, added it and forgot about it. And left it, then was wondering why i could not read data on receiving end of protobuf. When I fixed it and used the right OutputStream with the protobuf methods to read/write data, everything started working.