在之前的文章中,我们只是定义了一些非常简单的模型,其中只包含了string、int和一个Name对象,所以我们首先先定义一个更复杂的模型
.proto文件如下
复制代码
syntax = “proto3”;
option java_package = “cn.tera.protobuf.model”;
option java_outer_classname = “ProtobufStudent”;
message Student{
int32 age = 1;
int64 hairCount = 2;
bool isMale = 3;
string name = 4;
double height = 5;
float weight = 6;
Parent father = 7;
Parent mother = 8;
repeated string friends = 9;
repeated Hobby hobbies = 10;
Color hairColor = 11;
bytes scores = 12;
uint32 uage = 13;
sint32 sage = 14;
}
message Parent {
string name = 1;
int32 age = 2;
}
message Hobby {
string name = 1;
int32 cost = 2;
}
enum Color {
BLACK = 0;
RED = 1;
YELLOW = 2;
}
复制代码
相比之前定义的模型,这里新增了int64,bool,double,float,repeated,enum,uint,sint类型
repeated类型对应的是java中的list
protobuf将这些具体的类型分为了几个大类,如下面这个表格所示
Type Meaning Used For
0 Varint int32, int64, uint32, uint64, sint32, sint64, bool, enum
1 64-bit fixed64, sfixed64, double
2 Length-delimited string, bytes, embedded messages, packed repeated fields
5 32-bit fixed32, sfixed32, float
接着我们就通过实例来看下每种数据结构的编码方式
1.Varint
这种类型的数据,在序号字节中的类型部分表示为000,即表格中的Type字段0
首先我们看最简单的4种类型,protobuf类型为int32、int64、bool、enum,模型中对应这种类型的字段是age、hairCount、isMale、hairColor,因此我们分别给这4个字段赋值
age测试代码
复制代码
/**
- protobuf基础编码,varint类型
*/
@Test
void protobufBaseEncodeTest() {
ProtobufStudent.Student student = ProtobufStudent.Student.newBuilder()
.setAge(15)
Utility.printByte(student.toByteArray());
}
复制代码
输出结果
8 15
00001000 00001111
这里复习一下上一篇文章中关于protobuf的编码基础
第一个字节表示字段的序号和类型
黄色底000,表示该数据类型是varint
蓝色0001,表示序号为1
红色0,表示序号解析到了最后一个字节
第二个字节表示数字的值15
通过varint解码后,即是15
hairCount测试代码
复制代码
@Test
void protobufBaseEncodeTest() {
ProtobufStudent.Student student = ProtobufStudent.Student.newBuilder()
.setHairCount(239281373231123L)
.build();
Utility.printByte(student.toByteArray());
}
复制代码
输出结果
16 -109 -16 -126 -54 -128 -76 54
00010000 10010011 11110000 10000010 11001010 10000000 10110100 00110110
第一个字节表示字段的序号和类型
黄色底000,表示该数据类型是varint
蓝色0010,表示序号为2
红色0,表示序号解析到了最后一个字节
后面7个字节,通过varint解码后,即是239281373231123L
isMale测试代码
复制代码
@Test
void protobufBaseEncodeTest() {
ProtobufStudent.Student student = ProtobufStudent.Student.newBuilder()
.setIsMale(true)
.build();
Utility.printByte(student.toByteArray());
}
复制代码
输出结果
24 1
00011000 00000001
序号字节结构和之前一样
这里因为赋值的是true,所以值是1,如果赋值是false的话,那么该字段就不会被编码了(因为bool类型默认就是false)
hairColor测试代码
复制代码
@Test
void protobufBaseEncodeTest() {
ProtobufStudent.Student student = ProtobufStudent.Student.newBuilder()
.setHairColor(ProtobufStudent.Color.RED)
.build();
Utility.printByte(student.toByteArray());
}
复制代码
输出结果
88 1
01011000 00000001
序号字节结构和之前一样,这里因为赋值的是Color.RED,我们查看枚举值表即为1,如果赋值的是Color.BLACK,则该字段将不会被编码(因为int类型默认值就是0)
上面4个例子是可以通过正数就可以表达的类型,接着我们看对于有符号的正数,protobuf是如何表达的
protobuf类型为int32、uint32、sint32,对应模型中的age、uage、sage(这里注意,虽然在.proto文件中我们分了3个类型进行定义,但最终映射到java的类型都是int)
负数age测试代码
复制代码
/**
- protobuf基础编码,有符号的整数
*/
@Test
void negativeIntTest() {
ProtobufStudent.Student student = ProtobufStudent.Student.newBuilder()
.setAge(-7)
.build();
Utility.printByte(student.toByteArray());
}
复制代码
输出结果
8 -1 -1 -1 -1 -1 -1 -1 -1 -1 1
00001000 11111001 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 00000001
可以看到数据体占用了10个字节,通过varint解码后就可以得到-7
因为一般负数的二进制结果都是采用正数补码的形式存储,所以protobuf使用了一个长度固定为10个字节的空间对负数进行编码,即使是-7也需要10个字节进行存储,其实是十分不合理的,因此我们看下uint和sint的表现
uage测试代码
复制代码
/**
- protobuf基础编码,有符号的整数
*/
@Test
void negativeIntTest() {
ProtobufStudent.Student student = ProtobufStudent.Student.newBuilder()
.setUage(-7)
.build();
Utility.printByte(student.toByteArray());
}
复制代码
输出结果
104 -1 -1 -1 -1 15
01101000 11111001 11111111 11111111 11111111 00001111
如果定义为uint32的话,那么固定的数据存储空间则会缩减为5个字节
sage测试代码
复制代码
/**
- protobuf基础编码,有符号的整数
*/
@Test
void negativeIntTest() {
ProtobufStudent.Student student = ProtobufStudent.Student.newBuilder()
.setSage(-7)
.build();
Utility.printByte(student.toByteArray());
}
复制代码
输出结果
112 13
01110000 00001101
龙华大道1号http://www.kinghill.cn/LongHuaDaDao1Hao/index.html