1.
With protocol buffers, you
write a .proto description of the data structure you wish to store. From that,
the protocol buffer compiler creates a class that implements automatic encoding
and parsing of the protocol buffer data with an efficient binary format. The
generated class provides getters and setters for the fields that make up a
protocol buffer and takes care of the details of reading and writing the
protocol buffer as a unit. Importantly, the protocol buffer format supports the
idea of extending the format over time in such a way that the code can still
read data encoded with the old format.
2. T
he definitions in a .proto
file are simple: you add a message for each data structure you want to
serialize, then specify a name and a type for each field in the message. . A
message is just an aggregate containing a set of typed fields. Many standard
simple data types are available as field types, including bool
, int32
, float
, double
,
and string
. You can also add further structure to your messages by using other
message types as field types
3.
A value for a required field
must be provided, otherwise the message will be considered
"uninitialized". Trying to build an uninitialized message will throw
a RuntimeException
. Parsing an uninitialized message will throw an IOException
.
4.
For embedded messages, the
default value is always the "default instance" or
"prototype" of the message, which has none of its fields set.
5.
Each message class has its own Builder
class that you use to create instances of that class. Both messages and
builders have auto-generated accessor methods for each field of the message;
messages have only getters while builders have both getters and setters. There
are simple JavaBeans-style getters and setters for each field. There are also has
getters for each singular field which return true if that field has been set.
Finally, each field has a clear method that un-sets the field back to its empty
state.
6.
Even though the .proto
file
uses lowercase-with-underscores, the generated classes match standard Java
style conventions. You should always use lowercase-with-underscores for field
names in your .proto
files; this ensures good naming practice in all the
generated languages.
7. T
he message classes generated
by the protocol buffer compiler are all immutable. Once a message object is
constructed, it cannot be modified. To construct a message, you must first
construct a builder, set any fields you want to set to your chosen values, then
call the builder's build()
method.
8
.
Each method of the builder
which modifies the message returns another builder. The returned object is
actually the same builder on which you called the method. It is returned for
convenience so that you can string several setters together on a single line of
code.
9
.
Each message and builder class
also contains a number of other methods that let you check or manipulate the
entire message, including:
a)
isInitialized()
: checks if all
the required fields have been set.
b)
toString()
: returns a
human-readable representation of the message.
c)
mergeFrom(Message other)
:
(builder only) merges the contents of other into this message, overwriting
singular fields and concatenating repeated ones.
d)
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.
10.
Each protocol buffer class has
methods for writing and reading messages of your chosen type using the protocol
buffer binary format
.
These include:
a)
byte[] toByteArray()
:
serializes the message and returns a byte array containing its raw bytes.
b)
static Person parseFrom(byte[]
data)
: parses a message from the given byte array.
c)
void writeTo(OutputStream
output)
: serializes the message and writes it to an OutputStream
.
d)
static Person
parseFrom(InputStream input)
: reads and parses a message from an InputStream
.
11. I
f you want your new buffers to
be backwards-compatible, and your old buffers to be forward-compatible – and
you almost certainly do want this – then there are some rules you need to
follow. In the new version of the protocol buffer:
a)
you must not change the tag
numbers of any existing fields.
b)
you must not add or delete any
required fields.
c)
you may delete optional or
repeated fields.
d)
you may add new optional or
repeated fields but you must use fresh tag numbers (i.e. tag numbers that were
never used in this protocol buffer, not even by deleted fields).
If you follow these rules, old code will happily read new
messages and simply ignore any new fields. To the old code, optional fields
that were deleted will simply have their default value, and deleted repeated
fields will be empty. New code will also transparently read old messages.
12.
One key feature provided by
protocol message classes is reflection
. You
can iterate over the fields of a message and manipulate their values without
writing your code against any specific message type. One very useful way to use
reflection is for converting protocol messages to and from other encodings,
such as XML or JSON. A more advanced use of reflection might be to find
differences between two messages of the same type, or to develop a sort of
"regular expressions for protocol messages" in which you can write
expressions that match certain message contents.
分享到:
相关推荐
JavaBasics: Java基础 Java是一种广泛使用的面向对象的编程语言,由Sun Microsystems(后被Oracle收购)于1995年推出。它的设计目标是具有简单性、面向对象、健壮性、安全性、可移植性等特性,使得Java成为跨平台...
在Java编程语言的世界里,"javabasics:javabasics"是一个主题,它涵盖了初学者需要掌握的基本概念和技术。Java是一种面向对象的、跨平台的编程语言,由Sun Microsystems(现已被Oracle收购)于1995年推出。它的设计...
#### Protocol_Buffer Basics: Java - **定义proto文件**:首先定义消息类型。 - **编译Protocol_Buffer文件**:使用特定语言环境下的编译器生成对应的代码。 - **Protocol_Buffer API使用**:学习如何使用生成的...
在"Java-basics-master"这个资源中,可能包含了上述知识的教程、示例代码和练习题,通过深入学习和实践,你可以建立起坚实的Java基础。无论你是初学者还是有经验的开发者,持续掌握和提升这些Java基础知识都是至关...
"Java-Basics:Java基本代码"这个标题表明,这个压缩包包含了学习Java编程基础的一些示例代码。这些代码可能涵盖变量声明、数据类型、运算符、流程控制、函数、类和对象等核心概念。 首先,让我们来探讨一下Java的...
JavaBasics是一个面向初学者的学习资源,主要涵盖了Java编程语言的基础知识。这个压缩包很可能包含了一系列的代码示例,帮助用户逐步理解并掌握Java编程的核心概念。以下是对Java基础知识的详细解释: 1. **Java...
"Java-Basics"这个主题涵盖了Java编程的基础概念和关键知识点。在这个压缩包中,我们很可能会找到一系列的Java基础示例代码,帮助初学者或开发者巩固和理解Java的基本用法。 1. **基本语法**: Java是一种静态类型的...
Python Basics: A Self-Teaching Introduction By 作者: H. Bhasin ISBN-10 书号: 1683923537 ISBN-13 书号: 9781683923534 出版日期: 2018-12-17 pages 页数: (604) Python has become the language of choice ...
Java基础 Java基础知识Java 8概念数据结构和问题解决 Java基础知识-核心 大批 基本 收藏 仿制药 一成不变的 对象类 力量 Java 8概念 基本的 收藏 拉姆达 流 数据结构与问题解决 大批 链表 数字 递归 细绳
JavaBasics是一个针对Java编程语言基础知识的练习项目,旨在帮助初学者和有一定经验的开发者巩固和提升Java编程技能。在本项目中,你将找到一系列关于Java基础的实践练习,涵盖了从基本语法到面向对象编程的各个核心...
"java-basics"这个模块显然专注于Java的基础知识,这包括了类、对象、数据类型、控制结构、异常处理、字符串处理等多个核心概念。下面将详细讨论这些知识点。 1. **类与对象**:在Java中,一切皆为对象。类是创建...
"baker-learning-javabasics"这个项目显然旨在帮助开发者深化对Java基础知识的理解,并探索一些高级特性。下面我们将深入探讨其中涉及的关键知识点。 1. **泛型**:泛型是Java 5引入的一个重要特性,它允许在类、...
这个“java-basics”项目涵盖了Java语言的基础知识,对于初学者或希望巩固基础知识的开发者来说,这是一个很好的资源。 首先,我们需要了解Java的基本语法。Java语言是C++的简化版,但具有更严格的类型检查和垃圾...
Java基础是编程学习的重要部分,尤其对于初学者来说,掌握Java的基本概念、语法和特性是成为熟练Java开发者的基石。高琪的Java300集系列涵盖了广泛的Java知识,旨在帮助学习者系统地理解和复习Java编程。以下是这个...
Blockchain Basics: A Non-Technical Introduction in 25 Steps By 作者: Daniel Drescher ISBN-10 书号: 1484226038 ISBN-13 书号: 9781484226032 Edition 版本: 1st ed. 出版日期: 2017-03-16 pages 页数: (276 ) ...
JavaBasics: 学习Java的关键概念 Java是一种广泛使用的高级编程语言,以其“一次编写,到处运行”的特性而闻名。这个"JavaBasics"学习资料涵盖了Java编程的基础,是初学者入门的理想选择。让我们深入探讨一下Java的...
JavaBasics是一个针对Java编程初学者的资源集合,包含了基础的Java代码示例。这个压缩包文件名为"JavaBasics-main",很可能包含了用于教学或实践的Java源代码文件。在学习Java基础知识时,理解并掌握这些核心概念是...
Blockchain Basics A Non-Technical Introduction in 25 Steps 英文epub 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除
JavaBasics:这是针对初学者和有一定经验的程序员的一个学习资源集合,专注于Java编程语言的基础概念。这个压缩包可能包含了从最基础的语法讲解到核心编程理念的详细教程,旨在帮助用户建立坚实的Java编程基础。 ...