您的位置:首页 > Web前端

Google protocol-buffers java版--简单使用

2016-02-05 14:16 281 查看

Google protocol-buffers java版

         Model是用Model.proto生成的.需要配置proto环境
我这个Demo使用的是protobuf-java-2.6.1.jar
        cmd: protoc -I=./ --java_out=./ model.proto
        下载地址:
         https://developers.google.com/protocol-buffers/docs/downloads          可以参照文档:
         https://developers.google.com/protocol-buffers/docs/reference/java-generated 
       Protocol buffers are Google's language-neutral, platform-neutral, extensible mechanism for serializing structured data.

think XML, but smaller, faster, and simpler.据说是不分平台,比XML更小更快更简洁,具体的描述可以上https://developers.google.com/protocol-buffers/查看。

model.proto

package com.qzp.probuf;

message User {
required int32 id =1; // 主键,唯一(必填字段)
optional string username =11; // 帐号(可选)
optional string password =1000; // 密码(可选)
optional string email =4; // 邮箱(可选)
}

/*
* System Abbrev :
* system Name  :
* Component No  :
* Component Name:
* File name     :SaveReadData.java
* Author        :Peter.Qiu
* Date          :2016年2月5日
* Description   :  <description>
*/

/* Updation record 1:
* Updation date        :  2016年2月5日
* Updator          :  Peter.Qiu
* Trace No:  <Trace No>
* Updation No:  <Updation No>
* Updation Content:  <List all contents of updation and all methods updated.>
*/
package com.qzp.probuf;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;

import com.qzp.probuf.Model.User;
import com.qzp.probuf.Model.User.Builder;

/**
* <Description functions in a word>
* Protocol buffers are Google's language-neutral, platform-neutral, extensible mechanism for <Br>
* serializing structured data – think XML, but smaller, faster, and simpler.<Br>
* 据说是不分平台,比XML更小更快更简洁,具体的描述可以上https://developers.google.com/protocol-buffers/查看 <Br>
* Model是用Model.proto生成的.需要配置proto环境<Br/>
* 我这个Demo使用的是protobuf-java-2.6.1.jar<Br/>
* cmd:>protoc -I=./ --java_out=./ model.proto<Br/>
* 下载地址:<Br/>
* https://developers.google.com/protocol-buffers/docs/downloads<Br/> * 可以参照文档:<Br/>
* https://developers.google.com/protocol-buffers/docs/reference/java-generated *
* <Detail description>
*
* @author Peter.Qiu
* @version [Version NO, 2016年2月5日]
* @see [Related classes/methods]
* @since [product/module version]
*/
public class SaveReadData {

/**
* <Description functions in a word>
*
* <Detail description>
*
* @author Peter.Qiu
* @param args
* @see [Related classes#Related methods#Related properties]
*/
public static void main(String[] args) {
try {
//构建数据
Builder builder = User.newBuilder();
//Id是必填字段
builder.setId(100);
//选填字段
builder.setUsername("qiuzhping");
builder.setEmail("qiuzhping@163.com");
builder.setPassword("pwd_qiuzhping@163.com");
User user = builder.build();
byte[] b = user.toByteArray();
//保存字节数组
FileOutputStream fos = new FileOutputStream("./User.pb");
fos.write(b);
fos.flush();
fos.close();
//解析字节文件
File file = new File("./User.pb");
InputStream is = new FileInputStream(file);
User msg = User.parseFrom(is);
System.out.println(msg.toString());
} catch (Exception e) {
e.printStackTrace();
}
}

}
  对应Demo:http://download.csdn.net/detail/qiu_11/9428133
  转摘请注明:http://blog.csdn.net/qiuzhping/article/details/50637876

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: