现在的位置: 首页 > 综合 > 正文

java中使用protobuf序列化(反序列化)

2013年10月23日 ⁄ 综合 ⁄ 共 1031字 ⁄ 字号 评论关闭

调研环境:windows

1.http://code.google.com/p/protobuf/downloads/list ,选择其中的win版本下载

2.下载一个protobuf-java-2.4.1.jar文件(注意,要与你刚才下的proto.exe版本相同,否则可能出现编译通不过现象)

3.在proto.exe同级目录,编写一个msg.proto文件:

package Feinno.Practice.Learn;

option java_package = "Feinno.Practice.Learn";
option java_outer_classname = "ProtoBufferPractice";

message msgInfo  {
  required int32 ID = 1;
  required int64 GoodID = 2;       
  required string Url = 3;
  required string Guid = 4;
  required string Type = 5;
  required int32 Order = 6;
}

4.使用如下命令编译这个文件:

5.将生成的ProtoBufferPractice.java文件引入eclipse

6.把下载的protobuf-java-2.4.1.jar也引入工程

7.使用方法(序列化):

ProtoBufferPractice.msgInfo.Builder builder=ProtoBufferPractice.msgInfo.newBuilder();
    builder.setGoodID(100);
    builder.setGuid("11111-23222-3333-444");
    builder.setOrder(0);
    builder.setType("ITEM");
    builder.setID(10);
    builder.setUrl("http://xxx.jpg");
    ProtoBufferPractice.msgInfo info=builder.build();

    byte[] result=info.toByteArray() ;

8.反序列化:

try{
        ProtoBufferPractice.msgInfo msg = ProtoBufferPractice.msgInfo.parseFrom(result);
        System.out.println(msg);
    }
    catch(Exception ex){
        System.out.println(ex.getMessage());
    }

抱歉!评论已关闭.