Generic Parsing of PB in java
来源:互联网 发布:电脑为什么要优化系统 编辑:程序博客网 时间:2024/05/17 07:48
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 byprotoc
. However, you can also tellprotoc
to 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);
ynamicMessage
has a reflective API that lets you look through the fields.The messy part is calling out to the protoc
tool 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.
- Generic Parsing of PB in java
- Parsing Twitter Timestamp in Java
- GENERIC FRAMEWORK MODEL OF JAVA PLATFORM
- Generic Method in Java with Map
- [Java Web]Error parsing HTTP request header Note: further occurrences of HTTP header parsing errors
- [Java Web]Error parsing HTTP request header Note: further occurrences of HTTP header parsing errors
- [Java Web]Error parsing HTTP request header Note: further occurrences of HTTP header parsing errors
- parsing XML using DOM, SAX and StAX Parser in Java
- Parsing of JSP File
- Java generic
- Java Generic
- java Generic
- Java Generic
- Parsing XML in J2ME
- Parsing HTML in PHP
- Generic Programming in C++
- Parsing XML in Python&Assignment
- Hive: SemanticException Error in parsing
- c++ 类型转换
- Git相关操作
- SharePreferences 存储复杂类型数据
- 企业经营流程重组与Synchro Workflow
- 使用AXD调试S3C6410 u-boot .
- Generic Parsing of PB in java
- dwz + ssh 新增,删除的时候不跳转或者刷新页面的问题
- linux驱动
- oracle 修改数据库字符集
- flashbuilder4配置新版SVN插件
- DTD及Schema的使用中一些小的问题及解决方案
- Xcode代码颜色高亮及代码智能补全失效的解决方法
- 计算器
- 银行业务调度系统