Handling multiple messages in Java

36 views
Skip to first unread message

Carlos Pereira

unread,
Oct 13, 2020, 9:25:07 AM10/13/20
to Protocol Buffers
Hi,

Currently I'm working with ProtoBuf at work. There's a client device that sends us messages using ProtoBuf, so we can't change the protocol. The first 3 bytes is the length of the ProtoBuf message with big endian bytes order and then after that is the message itself. However, the .proto file they sent us defines multiple types of messages.

I'm using a CodedInputStream to first read the length of the serialized message and then read this size.

My problem is that after having bytes of the message I don't know which class inside the protc generated file I should use to call the parseFrom.

Any idea?


Best Regards,

Carlos Pereira

unread,
Oct 13, 2020, 9:25:43 AM10/13/20
to Protocol Buffers
I forgot to mention that the file they sent us is using Protocol Buffers 2.

BR,

Adam Cozzette

unread,
Oct 13, 2020, 12:07:47 PM10/13/20
to Carlos Pereira, Protocol Buffers
Serialized protocol buffers don't contain any metadata to indicate what type they are, so you have to have some other mechanism to know what the type is. I would guess that your .proto file probably has a parent message containing the other messages, and the parent is probably the one that is always sent.

--
You received this message because you are subscribed to the Google Groups "Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to protobuf+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/protobuf/94523e6f-cc14-4928-a4f6-eb3d6a7f37e8n%40googlegroups.com.

Carlos Pereira

unread,
Oct 14, 2020, 3:09:04 AM10/14/20
to Adam Cozzette, Protocol Buffers
You're absolutely right! Thanks!

BR
--
Carlos Duarte Pereira
Reply all
Reply to author
Forward
0 new messages