1

The Client sends a 1481 bytes array. The server can read all the 1481 bytes message without any problems but by parsing the given messsage from the received binary array i get this exeption:

com.google.protobuf.InvalidProtocolBufferException: Protocol message contained an invalid tag (zero).

The binary data is the same. I checked that I am using the right version of the proto files. I am a bit at a loss tbh. Any help appreciated.

Code

byte [] data= IOUtils.toByteArray(br1, "ASCII"); System.out.println("SIZE:" + data.length);
AddressBook adb1 = AddressBook.parseFrom(data); System.out.println("Server: Addressbook:" + adb1.getPersonCount()); System.out.println("Server: Addressbook:" + adb1.getPerson(0).getName());

Question:

I need to find a way to correctly parse the received Adressbook msg from the read 1481 bytes arry.

Thanks.

10
  • You mean 1481 bytes, not bits, right? When you say "the binary data is the same" - the same as what? It's hard to follow what's actually going on here. Commented Oct 25, 2012 at 12:39
  • i mean the length of byteArray is 1481. Commented Oct 25, 2012 at 12:49
  • Right. It's important to know the difference between bytes and bits, and use the right term. The rest of the question is still vague. Commented Oct 25, 2012 at 12:50
  • The received binary data is the same as the sent data Commented Oct 25, 2012 at 12:51
  • the Problem allthough the received binary data is the same as the sent data i get this Exception: com.google.protobuf.InvalidProtocolBufferException: Protocol message contained an invalid tag (zero). Commented Oct 25, 2012 at 13:00

2 Answers 2

2

This is the problem:

br1 = new InputStreamReader(s.getInputStream());

That's trying to treat opaque binary data as text. It's not text, it's binary data. So when you convert that Reader into a byte array, you've lost a load of the original data - no wonder it's an invalid protocol buffer.

Just use:

AddressBook adb1 = AddressBook.parseFrom(s.getInputStream());

and avoid the lossy text conversion. That's assuming you haven't got something equally broken on the C# side, of course.

If you must go via text, you should use base64 encoding on both sides.

Sign up to request clarification or add additional context in comments.

12 Comments

why i lost a load of the original data if i convert that Reader into a byte arry? I tried your proposal but i get the same exception : com.google.protobuf.InvalidProtocolBufferException: Protocol message contained an invalid tag (zero)
How could i use the base64 encoding on both sides? Thanks!
@Kaiser4you: No, you shouldn't use a Reader at all. That's the whole point. Readers are about text data. A protocol buffer message is binary data. Just don't do it! You should only resort to base64 if you absolutely have to use text - which is unlikely.
Thanks! Ok which I/O steam it is better to use in this case: BuffredInputStream or DataInputStream or ObjectInputStream?
@Kaiser4you: Unless you're using any of the features provided by DataInputStream or ObjectInputStream, don't use them. You may not even need to use BufferedInputStream, as IIRC the Protobuf implementation performs buffering. Just use s.getInputStream(), as per my answer.
|
0

Now it works I had same mistake by Serializing and Sending the Protocol Buffers Message

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.