1.编写一个.proto文件命名为:addressbook.proto,该文件内容来自protocal-buffers官网
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
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 phone = 4;
}
message AddressBook {
repeated Person person = 1;
}
|
2.使用protoc-2.6.0-win32.zip解压后的protoc.exe生成Java类
查询protoc.exe帮助:
1
|
D:\__dev\jar\protobuff>protoc.exe -h
|
生成Java类:
1
2
|
D:\__dev\jar\protobuff>protoc.exe --proto_path=F:\__eclipse\test\proto
--java_out=F:\__eclipse\test\src F:\__eclipse\test\proto\addressbook.proto
|
在Eclipse中的项目目录结构图如下:
说明:
上图中:addressbook.proto数据格式文件,AddressBookProtos.java是生成的java类,protobuf-java-2.5.0.jar是Java运行时类库。
3.使用AddressBookProtos类来实现对象的序列号和反序列化,了解Protocol-buffers jar的相关API
代码实例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
package
com.example.test;
import
java.util.Arrays;
import
com.example.tutorial.AddressBookProtos.AddressBook;
import
com.example.tutorial.AddressBookProtos.Person;
import
com.google.protobuf.InvalidProtocolBufferException;
public
class
AddressBookProtoUse {
public
static
void
main(String[] args) {
//构建一个Person对象
Person person = Person
.newBuilder()
.setEmail(
"zhangsan@163.com"
)
.setId(
10086
)
.setName(
"zhangsan"
)
.addPhone(
Person.PhoneNumber.newBuilder().setNumber(
"186"
)
.setType(Person.PhoneType.HOME).build())
.build();
System.out.println(
"打印输出Person对象信息:"
);
System.out.println(person);
System.out.println(
"Person对象调用toString()方法:"
);
System.out.println(person.toString());
System.out.println(
"Person对象字段是否初始化:"
+ person.isInitialized());
// 序列号
System.out.println(
"Person对象调用toByteString()方法:"
);
System.out.println(person.toByteString());
System.out.println(
"Person对象调用toByteArray()方法:"
);
System.out.println(Arrays.toString(person.toByteArray()));
try
{
System.out.println(
"反序列化后的对象信息:"
);
// 反序列化
Person newPerson = Person.parseFrom(person.toByteArray());
System.out.println(newPerson);
newPerson = Person.parseFrom(person.toByteString());
System.out.println(newPerson);
}
catch
(InvalidProtocolBufferException e) {
e.printStackTrace();
}
// 向地址簿添加两条Person信息
AddressBook.Builder books = AddressBook.newBuilder();
books.addPerson(person);
books.addPerson(Person.newBuilder(person).setEmail(
"tom@163.com"
)
.build());
System.out.println(
"AddressBook对象信息:"
);
System.out.println(books.build());
}
}
|
运行结果:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
打印输出Person对象信息:
name: "zhangsan"
id: 10086
email: "zhangsan@163.com"
phone {
number: "186"
type: HOME
}
Person对象调用toString()方法:
name: "zhangsan"
id: 10086
email: "zhangsan@163.com"
phone {
number: "186"
type: HOME
}
Person对象字段是否初始化:true
Person对象调用toByteString()方法:
<ByteString@89ec59 size=40>
Person对象调用toByteArray()方法:
[10, 8, 122, 104, 97, 110, 103, 115, 97, 110, 16, -26, 78, 26, 16, 122, 104, 97, 110, 103, 115, 97, 110, 64, 49, 54, 51, 46, 99, 111, 109, 34, 7, 10, 3, 49, 56, 54, 16, 1]
反序列化后的对象信息:
name: "zhangsan"
id: 10086
email: "zhangsan@163.com"
phone {
number: "186"
type: HOME
}
name: "zhangsan"
id: 10086
email: "zhangsan@163.com"
phone {
number: "186"
type: HOME
}
AddressBook对象信息:
person {
name: "zhangsan"
id: 10086
email: "zhangsan@163.com"
phone {
number: "186"
type: HOME
}
}
person {
name: "zhangsan"
id: 10086
email: "tom@163.com"
phone {
number: "186"
type: HOME
}
}
|
探究AddressBookProtos类:
a.构造Person对象,Person类继承自com.google.protobuf.GeneratedMessage类,而GeneratedMessage类继承自AbstractMessage类,并且实现了序列化接口Serializable。在AbstractMessage类中重写了toString()方法,具体内容如下:
1
2
3
4
|
@Override
public
final
String toString() {
return
TextFormat.printToString(
this
);
}
|
于是有了Person对象调用toString()方法后直接输出Person对象的文本内容。
b.toByteString()返回的ByteString是一个不可变的byte序列,由AbstractMessage类实现。toByteArray()返回byte[]。这两个方法都是对象进行序列化的方法。
c.isInitialized()判断对象的字段是否初始化,该方法与Person类的initFields()方法相关。
initFields()源代码:
1
2
3
4
5
6
|
private
void
initFields() {
name_ =
""
;
id_ =
0
;
email_ =
""
;
phone_ = java.util.Collections.emptyList();
}
|
initFields()方法的调用是在Person类的默认实力对象初始化之后调用的,在Person类的静态代码块中可以看到:
1
2
3
4
|
static
{
defaultInstance =
new
Person(
true
);
defaultInstance.initFields();
}
|
d.Person类提供了一系列的反序列化的重载方法用来讲数据反序列化为Person对象。
e.关于Person对象的Build
Person类中有一个Builder的内部类,该类用来构建Person对象,并且为Person对象添加数据。
1
2
3
|
public
static
final
class
Builder
extends
com.google.protobuf.GeneratedMessage.Builder<Builder>
implements
com.example.tutorial.AddressBookProtos.PersonOrBuilder
|
4.通过学习官网的实例,手绘一张Java使用protobuf的基本流程图
本文转自 secondriver 51CTO博客,原文链接:http://blog.51cto.com/aiilive/1563449,如需转载请自行联系原作者