Nikolay Kuznetsov
Nikolay Kuznetsov

Reputation: 9579

Parsing java InputStream which has several protobuf inside

Format of incoming message

I implement two protobuf messages in one network packet for flexibility.

This is how I am trying to parse the message:

ByteArrayInputStream is = new ByteArrayInputStream(buf.array());
System.out.println(is.available());
is.skip(1);
System.out.println(is.available());

MessageHeader header = MessageHeader.parseFrom(is);
System.out.println(is.available());

Output is

So the problem is that parseFrom tries to read the inputStream until the end and does not stop once first protobuf reading is done.

What would be the best way to parse the message having this kind of format?

Upvotes: 3

Views: 3110

Answers (1)

Nikolay Kuznetsov
Nikolay Kuznetsov

Reputation: 9579

When I write to and parse from now I use writeDelimitedTo and parseDelimitedFrom and it works.

Upvotes: 3

Related Questions