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. protobuf(Protocol Buffers)java初体验

    因为项目须要所以简单的研究了下protobuf.我也是參照网上的博客,所以大部分内容我也就不反复造轮子了.首先protobuf介绍点击这里,使用介绍点击这里,使用demo看这里. 我个人的第一个样例也 ...

  2. java&Protocol Buffers

    ps: Protocol Buffers简称PB PB 安装配置 下载 PB: 在 PB 官网,下载最新版(或者其他版本)PB,这里为了与 Java 项目中的 PB Maven 依赖版本一致,使用 P ...

  3. Java使用Protocol Buffers入门四步骤

    Protocol Buffers(简称protobuf)是谷歌的一项技术.用于将结构化的数据序列化.反序列化.经经常使用于网络传输. 这货实际上相似于XML生成和解析.但protobuf的效率高于XM ...

  4. Xml,Json,Hessian,Protocol Buffers序列化对比

    简介 这篇博客主要对Xml,Json,Hessian,Protocol Buffers的序列化和反序列化性能进行对比,Xml和Json的基本概念就不说了. Hessian:Hessian是一个轻量级的 ...

  5. Protocol buffers 介绍

    Protocol buffers和mxl一样在序列化数据结构时很灵活.高效和智能,但是它的优势在于定义文件更小,读取速度更快,使用更加简单.目前protocol buffers支持C++.java和p ...

  6. protocol buffers的使用示例[z]

    [http://blog.csdn.net/zhu_xun/article/details/19397081] protocol buffers的使用示例 如果不了解protocol buffers, ...

  7. 理解netty对protocol buffers的编码解码

    一,netty+protocol buffers简要说明 Netty是业界最流行的NIO框架之一优点:1)API使用简单,开发门槛低:2)功能强大,预置了多种编解码功能,支持多种主流协议:3)定制能力 ...

  8. Protocol Buffers(Protobuf) 官方文档--Protobuf语言指南

    Protocol Buffers(Protobuf) 官方文档--Protobuf语言指南 约定:为方便书写,ProtocolBuffers在下文中将已Protobuf代替. 本指南将向您描述如何使用 ...

  9. Protocol Buffers(Protobuf)开发者指南---概览

    Protocol Buffers(Protobuf)开发者指南---概览 欢迎来到protocol buffers的开发者指南文档,protocol buffers是一个与编程语言无关‘.系统平台无关 ...

随机推荐

  1. textarea标签内容为(英文或数字不自动换行)的解决方法

    textarea 显示一串英文时不会发生换行. 以下是两种解决方法:1.限制textarea的大小 width 设置为 00px (不要设置为00%)cols  设置为 30+ (也有类似效果) 2. ...

  2. DataList中动态显示DIV

    <%# DataBinder.Eval(Container,  "DataItem.ProductName").ToString() == "股票矩阵" ...

  3. 【转】Java中的多线程学习大总结

    多线程作为Java中很重要的一个知识点,在此还是有必要总结一下的. 一.线程的生命周期及五种基本状态 关于Java中线程的生命周期,首先看一下下面这张较为经典的图: 上图中基本上囊括了Java中多线程 ...

  4. Oracle创建directory

    Oracle创建directory   一般创建directory都是为了用数据泵导入/导出数据用,其实directory还有很多别的用处,本文不做阐述   1.新建directory的语法 CREA ...

  5. node.js富文本编辑器

    摘要: 最近在搭建自己的博客,这一段时间可能没有时间来写博客了,但是有了好东西还是要分享给大家.博客网站必然要有编辑文章的编辑器,所以在网上查了些资料.大部分编辑器的后台是基于java.php.asp ...

  6. python内存泄漏,python垃圾手动回收,1

    部署的舆情系统,内存变大,找原因. 一个小例子. def func(): local_list = list(range(10000000)) func() time.sleep(200) 能够观察到 ...

  7. Android测试跑单个包脚本文件

    脚本: adb shell monkey -p 应用包名 --throttle 随机事件间隔 -v -v -v -s 1 --ignore-security-exceptions --kill-pro ...

  8. 分分钟学会GCD

    2014 什么是GCD Grand Central Dispatch (GCD)是异步运行任务的技术之中的一个.一般将应用程序中记述的线程管理用的代码在系统级中实现.因为线程管理是作为系统的一部分来实 ...

  9. Java -- 异常的捕获及处理 -- 目录

    7 异常的捕获及处理 7.1 异常的基本概念 7.1.1 为什么需要异常处理 7.1.2 在程序中使用异常处理 7.1.3 异常类的继承结构 7.1.4 Java的异常处理机制 7.2 throws与 ...

  10. Import VMware ESXi from VirtualBox

    VirtualBox can export appliance VMs to OVF format. And you can import the ovf format to VMware ESXi, ...