First of all, you cannot analyze PB data without knowing the patterns. Initially, this scheme comes from the ".proto" file and is usually embedded in the code generated by protoc . However, you can also tell protoc to save the schema in a format that the Protobuf Java library can use:
protoc --descriptor_set_out=mymessages.desc mymessages.proto
Then load it into Java code:
FileInputStream fin = new FileInputStream("mymessages.desc"); Descriptors.FileDescriptorSet set = Descriptors.FileDescriptorSet.parseFrom(fin); Descriptors.Descriptor md = set.getFile(0).getMessageType(0);
Once you have a schema for the message ( Descriptor.Descriptor ), parsing the message is easy:
byte[] data = ...; DynamicMessage m = DynamicMessage.parseFrom(md, data);
DynamicMessage has a reflective API that allows you to view fields.
The unused part calls the protoc tool to convert the .proto file to a convenient format. The C ++ Protobuf library has the ability to directly upload the ".proto" files, but unfortunately there is no Protobuf Java library.
Kannan goundan
source share