Protocol Buffers

https://developers.google.cn/protocol-buffers/

一. 例

addressbook.proto.syntax = "proto2";package tutorial;option java_package = "com.example.tutorial";
option java_outer_classname = "AddressBookProtos";message Person {required string name = 1;required int32 id = 2;optional string email = 3;enum PhoneType {MOBILE = 0;HOME = 1;WORK = 2;}message PhoneNumber {required string number = 1;optional PhoneType type = 2 [default = HOME];}repeated PhoneNumber phones = 4;
}message AddressBook {repeated Person people = 1;
}

二.编译

protoc -I=$SRC_DIR --java_out=$DST_DIR $SRC_DIR/addressbook.proto

三. 生成文件的常用接口

Person class (implementations omitted for brevity):// required string name = 1;
public boolean hasName();
public String getName();// required int32 id = 2;
public boolean hasId();
public int getId();// optional string email = 3;
public boolean hasEmail();
public String getEmail();// repeated .tutorial.Person.PhoneNumber phones = 4;
public List<PhoneNumber> getPhonesList();
public int getPhonesCount();
public PhoneNumber getPhones(int index);

四. Builders vs. Parsing

Here's an example of how you would create an instance of Person:

Person john =Person.newBuilder().setId(1234).setName("John Doe").setEmail("jdoe@example.com").addPhones(Person.PhoneNumber.newBuilder().setNumber("555-4321").setType(Person.PhoneType.HOME)).build();

Parsing

  Person john;
fstream input(argv[1],ios::in | ios::binary);
john.ParseFromIstream(&input);
id = john.id();
name = john.name();
email = john.email();

五.Standard Message Methods

Each message and builder class also contains a number of other methods that let you check or manipulate the entire message, including:

isInitialized(): checks if all the required fields have been set.
toString(): returns a human-readable representation of the message, particularly useful for debugging.
mergeFrom(Message other): (builder only) merges the contents of other into this message, overwriting singular scalar fields, merging composite fields, and concatenating repeated fields.
clear(): (builder only) clears all the fields back to the empty state.
These methods implement the Message and Message.Builder interfaces shared by all Java messages and builders. For more information, see the complete API documentation for Message.

六.Parsing and Serialization

Finally, each protocol buffer class has methods for writing and reading messages of your chosen type using the protocol buffer binary format. These include:

byte[] toByteArray();: serializes the message and returns a byte array containing its raw bytes.
static Person parseFrom(byte[] data);: parses a message from the given byte array.
void writeTo(OutputStream output);: serializes the message and writes it to an OutputStream.
static Person parseFrom(InputStream input);: reads and parses a message from an InputStream.
These are just a couple of the options provided for parsing and serialization. Again, see the Message API reference for a complete list.

Protocol Buffers java相关推荐

  1. 在Java中使用Protocol Buffers

    这份教程为Java开发者提供了使用 Protocol Buffer 的基本介绍.通过创建一个简单的示例应用,它展示了 在 .proto 文件中定义消息格式. 使用 Protocol Buffer 编译 ...

  2. Google Protocol Buffers 2.3.0 for java 快速开始

    Google Protocol Buffers 2.3.0 for java 快速开始 博客分类: Java JavaGoogleUbuntuLinux数据结构 Protocol Buffers是一个 ...

  3. 是时候该了解一波Protocol Buffers了[Java]

    前言 Protocol Buffers,是Google公司开发的一种数据描述语言,类似于XML能够将结构化数据序列化,可用于数据存储.通信协议等方面. 它不依赖于语言和平台并且可扩展性极强.现阶段官方 ...

  4. riak教程 java_Riak学习(2):java连接Riak服务,使用Protocol Buffers连接

    1,介绍 Riak服务搭建,可以参考: http://blog.csdn.net/freewebsys/article/details/12609995 Riak的接口访问有两种方式: HTTP Pr ...

  5. Riak学习(2):java连接Riak服务,使用Protocol Buffers连接

    1,介绍 Riak服务搭建,可以参考: http://blog.csdn.net/freewebsys/article/details/12609995 Riak的接口访问有两种方式: HTTP Pr ...

  6. Protocol Buffers简明教程

    随着微服务架构的流行,RPC框架渐渐地成为服务框架的一个重要部分.在很多RPC的设计中,都采用了高性能的编解码技术,Protocol Buffers就属于其中的佼佼者.Protocol Buffers ...

  7. Google Protocol Buffers介绍

    Google Protocol Buffers(简称Protobuf),是Google的一个开源项目,它是一种结构化数据存储格式,是Google公司内部的混合语言数据标准,是一个用来序列化(将对象的状 ...

  8. 序列化--反序列化:Schema evolution in Avro, Protocol Buffers and Thrift

    当想要数据, 比如对象或其他类型的, 存到文件或是通过网络传输, 需要面对的问题是序列化问题 对于序列化, 当然各个语言都提供相应的包, 比如, Java serialization, Ruby's ...

  9. Go 支持Protocol Buffers的配置

    Go 支持Protocol Buffers的配置 安装 protoc (The protocol compiler)是由C++写的,支持的 C++.Java.Python.Objective-C.C# ...

最新文章

  1. Linux必知必会的目录与启动过程
  2. Linux多线程编程----IO【select、poll、epoll】
  3. 图数据库Neo4j简介
  4. unity fixedupdate_3D俯视角射击——用Unity还原东方弹幕(上)
  5. Mbp,一个用于学习.net core的开发框架
  6. 高手教您编写简单的JSON解析器
  7. 初始化请求例子_当一个http请求来临时,SpringMVC究竟偷偷帮你做了什么?
  8. 掌握好这几点方法学习Linux,一定比别人更快入门运维!
  9. 【好书推荐】写Web必须知道的安全知识 | 《白帽子讲Web安全》
  10. python统计(二)假设检验
  11. IDEA怎样自定义 Touch Bar
  12. element el-upload 一次上传单张/多张图片(多选)
  13. >> 读书记录_2021
  14. linux怎么查看是不是centos版本
  15. [UE4教程] UE4蓝图总结(入门)
  16. win10用户账户控制怎么取消或打开
  17. mysql链接liunx远程服务器
  18. 提取文件名-Java
  19. android版本5.1.1更新包,华为EMUI3.1(安卓5.0/5.1)升级包
  20. keil如何添加h文件_ECBM系列教程1:如何搭建ECBM库环境。

热门文章

  1. table 在网页无法顶到头部问题
  2. 常用的上网 发帖技巧
  3. zoj 3329 概率dp
  4. 089-袁佳鹏-实验报告1
  5. U3D 飞机大战(MVC模式)解析--面向对象编程思想
  6. linux下安装MySQL出错file /usr/share/mysql/charsets/latin2.xml from install of MySQL-......
  7. adobe photoshop cc 复制css链接
  8. Postman for Linux(x86)
  9. ADAS在车载导航设备上的应用
  10. 图像处理与计算机视觉资源汇总——论文+代码+教材+视频等等