Generic Parsing of PB in java
2013-04-18 09:03
330 查看
Is it possible to parse PB in a generic fashion in java ? I looked into GeneratedMessage and could not find a way to parse any PB byte buffer into a GeneratedMessage. Essentially, i am trying to parse a PB byte buffer into GeneratedMessage and then i would
use reflection to detect fields inside it.Thanks in advance
First of all, you can't parse PB data without knowing the schema. The schema originally comes from a ".proto" file and is typically embedded in the code generated by
Then load it in your Java code:
Once you have the schema for a message (
The messy part is calling out to the
use reflection to detect fields inside it.Thanks in advance
First of all, you can't parse PB data without knowing the schema. The schema originally comes from a ".proto" file and is typically embedded in the code generated by
protoc. However, you can also tell
protocto store the schema in a format that's usable by the Java Protobuf library:
protoc --descriptor_set_out=mymessages.desc mymessages.proto
Then load it in your 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 the schema for a message (
Descriptor.Descriptor) parsing a message is easy:
byte[] data = ...; DynamicMessage m = DynamicMessage.parseFrom(md, data);
ynamicMessagehas a reflective API that lets you look through the fields.
The messy part is calling out to the
protoctool to convert the ".proto" file into a usable format. The C++ Protobuf library has a way to load ".proto" files directly, but unfortunately the Java Protobuf library does not.
相关文章推荐
- Exception in thread ""http-bio-80"exec-1" java.lang.OutOfMemoryError: PermGen s解决方案
- 解决办法:matlab Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap
- Class JavaLaunchHelper is implemented in both … libinstrument.dylib. One of the two will be used. Wh
- Exception in thread "ContainerBackgroundProcessor[StandardEngine[Catalina]]" java.lang.OutOfMemoryEr
- java.sql.SQLException: Unknown type '246 in column 0 of 1 in binary-encoded result set的解决办法
- Parsing Twitter Timestamp in Java
- how to compile source code of "Data Structures & Algorithm Analysis in Java" writen by Mark Allen Weiss
- 解决办法:matlab Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap
- Class JavaLaunchHelper is implemented in both. One of the two will be used. Which one is undefined
- Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
- The Difference of Class.forName()、Class.forName().newInstance() and New in Java
- Exception in thread "main" java.lang.OutOfMemoryError: GC overhead limit exceede
- java.lang.OutOfMemoryError thrown from the UncaughtExceptionHandler in thread "javamelody jenkins
- the usage of key word "static" in java language
- The Java™ Tutorials — Generics :Erasure of Generic Methods 泛型方法的类型擦除
- How to create different types of thread pool in Java
- Exception in thread "AWT-Shutdown" java.lang.OutOfMemoryError: PermGen space
- 处理教材:Reusing Classes of "Thinking in Java"
- Intellij出现错误Exception in thread "RMI TCP Connection(idle)" java.lang.OutOfMemoryError: PermGen space
- Web Tier to Go With Java EE 5: Summary of New Features in JavaServer Faces 1.2 Technology