protobuf 编码实现解析(java)
一:protobuf编码基本数据类型
public enum FieldType { DOUBLE (JavaType.DOUBLE , WIRETYPE_FIXED64 ), FLOAT (JavaType.FLOAT , WIRETYPE_FIXED32 ), INT64 (JavaType.LONG , WIRETYPE_VARINT ), UINT64 (JavaType.LONG , WIRETYPE_VARINT ), INT32 (JavaType.INT , WIRETYPE_VARINT ), FIXED64 (JavaType.LONG , WIRETYPE_FIXED64 ), FIXED32 (JavaType.INT , WIRETYPE_FIXED32 ), BOOL (JavaType.BOOLEAN , WIRETYPE_VARINT ), STRING (JavaType.STRING , WIRETYPE_LENGTH_DELIMITED) { public boolean isPackable() { return false; } }, GROUP (JavaType.MESSAGE , WIRETYPE_START_GROUP ) { public boolean isPackable() { return false; } }, MESSAGE (JavaType.MESSAGE , WIRETYPE_LENGTH_DELIMITED) { public boolean isPackable() { return false; } }, BYTES (JavaType.BYTE_STRING, WIRETYPE_LENGTH_DELIMITED) { public boolean isPackable() { return false; } }, UINT32 (JavaType.INT , WIRETYPE_VARINT ), ENUM (JavaType.ENUM , WIRETYPE_VARINT ), SFIXED32(JavaType.INT , WIRETYPE_FIXED32 ), SFIXED64(JavaType.LONG , WIRETYPE_FIXED64 ), SINT32 (JavaType.INT , WIRETYPE_VARINT ), SINT64 (JavaType.LONG , WIRETYPE_VARINT );
附图:
static Object readPrimitiveField( CodedInputStream input, FieldType type, Utf8Validation utf8Validation) throws IOException { switch (type) { case DOUBLE : return input.readDouble (); case FLOAT : return input.readFloat (); case INT64 : return input.readInt64 (); case UINT64 : return input.readUInt64 (); case INT32 : return input.readInt32 (); case FIXED64 : return input.readFixed64 (); case FIXED32 : return input.readFixed32 (); case BOOL : return input.readBool (); case BYTES : return input.readBytes (); case UINT32 : return input.readUInt32 (); case SFIXED32: return input.readSFixed32(); case SFIXED64: return input.readSFixed64(); case SINT32 : return input.readSInt32 (); case SINT64 : return input.readSInt64 ();
MessageLite对应的java类型默认值:
public enum JavaType { INT(0), LONG(0L), FLOAT(0F), DOUBLE(0D), BOOLEAN(false), STRING(""), BYTE_STRING(ByteString.EMPTY), ENUM(null), MESSAGE(null);
在Java种对不同类型的选择,其他的类型区别很明显,主要在与int32、uint32、sint32、fixed32中以及对应的64位版本的选择,因为在Java中这些类型都用int(long)来表达,但是protobuf内部使用ZigZag编码方式来处理多余的符号问题,但是在编译生成的代码中并没有验证逻辑,比如uint的字段不能传入负数之类的。而从编码效率上,对fixed32类型,如果字段值大于2^28,它的编码效率比int32更加有效;而在负数编码上sint32的效率比int32要高;uint32则用于字段值永远是正整数的情况。
在实现上,protobuf使用CodedOutputStream实现序列化逻辑、CodedInputStream实现反序列化逻辑,他们都包含write/read基本类型和Message类型的方法,write方法中同时包含fieldNumber和value参数,在写入时先写入由fieldNumber和WireType组成的tag值(添加这个WireType类型信息是为了在对无法识别的字段编码时可以通过这个类型信息判断使用那种方式解析这个未知字段,所以这几种类型值即可),这个tag值是一个可变长int类型,所谓的可变长类型就是一个字节的最高位(msb,most significant bit)用1表示后一个字节属于当前字段,而最高位0表示当前字段编码结束。
varint32如下:
/** * Compute the number of bytes that would be needed to encode a varint. * {@code value} is treated as unsigned, so it won't be sign-extended if * negative. */ public static int computeRawVarint32Size(final int value) { if ((value & (0xffffffff << 7)) == 0) return 1; if ((value & (0xffffffff << 14)) == 0) return 2; if ((value & (0xffffffff << 21)) == 0) return 3; if ((value & (0xffffffff << 28)) == 0) return 4; return 5; }
wireType类型如下:
public static final int WIRETYPE_VARINT = 0; public static final int WIRETYPE_FIXED64 = 1; public static final int WIRETYPE_LENGTH_DELIMITED = 2; public static final int WIRETYPE_START_GROUP = 3; public static final int WIRETYPE_END_GROUP = 4; public static final int WIRETYPE_FIXED32 = 5;
3bits表示;
static final int TAG_TYPE_BITS = 3; static final int TAG_TYPE_MASK = (1 << TAG_TYPE_BITS) - 1; /** Given a tag value, determines the wire type (the lower 3 bits). */ static int getTagWireType(final int tag) { return tag & TAG_TYPE_MASK; } /** Given a tag value, determines the field number (the upper 29 bits). */ public static int getTagFieldNumber(final int tag) { return tag >>> TAG_TYPE_BITS; } /** Makes a tag value given a field number and wire type. */ static int makeTag(final int fieldNumber, final int wireType) { return (fieldNumber << TAG_TYPE_BITS) | wireType; }
/** Write a {@code double} field, including tag, to the stream. */ public void writeDouble(final int fieldNumber, final double value) throws IOException { writeTag(fieldNumber, WireFormat.WIRETYPE_FIXED64); writeDoubleNoTag(value); } /** Write a {@code float} field, including tag, to the stream. */ public void writeFloat(final int fieldNumber, final float value) throws IOException { writeTag(fieldNumber, WireFormat.WIRETYPE_FIXED32); writeFloatNoTag(value); } /** Write a {@code uint64} field, including tag, to the stream. */ public void writeUInt64(final int fieldNumber, final long value) throws IOException { writeTag(fieldNumber, WireFormat.WIRETYPE_VARINT); writeUInt64NoTag(value); } /** Write an {@code int64} field, including tag, to the stream. */ public void writeInt64(final int fieldNumber, final long value) throws IOException { writeTag(fieldNumber, WireFormat.WIRETYPE_VARINT); writeInt64NoTag(value); } /** Write an {@code int32} field, including tag, to the stream. */ public void writeInt32(final int fieldNumber, final int value) throws IOException { writeTag(fieldNumber, WireFormat.WIRETYPE_VARINT); writeInt32NoTag(value); } /** Write a {@code fixed64} field, including tag, to the stream. */ public void writeFixed64(final int fieldNumber, final long value) throws IOException { writeTag(fieldNumber, WireFormat.WIRETYPE_FIXED64); writeFixed64NoTag(value); } /** Write a {@code fixed32} field, including tag, to the stream. */ public void writeFixed32(final int fieldNumber, final int value) throws IOException { writeTag(fieldNumber, WireFormat.WIRETYPE_FIXED32); writeFixed32NoTag(value); } /** Write a {@code bool} field, including tag, to the stream. */ public void writeBool(final int fieldNumber, final boolean value) throws IOException { writeTag(fieldNumber, WireFormat.WIRETYPE_VARINT); writeBoolNoTag(value); } /** Write a {@code string} field, including tag, to the stream. */ public void writeString(final int fieldNumber, final String value) throws IOException { writeTag(fieldNumber, WireFormat.WIRETYPE_LENGTH_DELIMITED); writeStringNoTag(value); }
在写入tag值后,再写入字段值value,对不同的字段类型采用不同的编码方式:
1. 对int32/int64类型,如果值大于等于0,直接采用可变长编码,否则,采用64位的可变长编码,因而其编码结果永远是10个字节,所有说它int32/int64类型在编码负数效率很低(varint32,最高5bytes, 去除5个bits标志位也够啊!为什么sign-extend到64,并且为10bytes?,本人除了在codeInputStream中看出解析时方便,别的看不到任何原因,求解释??).。
/** Write an {@code int32} field to the stream. */ public void writeInt32NoTag(final int value) throws IOException { if (value >= 0) { writeRawVarint32(value); } else { // Must sign-extend. writeRawVarint64(value); } }
sign-extend 64:
public void writeRawVarint64(long value) throws IOException { while (true) { if ((value & ~0x7FL) == 0) { writeRawByte((int)value); return; } else { writeRawByte(((int)value & 0x7F) | 0x80); value >>>= 7; } } }
10bytes:
public static int computeInt32SizeNoTag(final int value) { if (value >= 0) { return computeRawVarint32Size(value); } else { // Must sign-extend. return 10; } }
2. 对uint32/uint64类型,也采用变长编码,不对负数做验证。
public void writeUInt32NoTag(final int value) throws IOException { writeRawVarint32(value); }
只是简单的调用varint32变长write(不对value值有任何判断非负);
3. 对sint32/sint64类型,首先对该值做ZigZag编码,以保留,然后将编码后的值采用变长编码。所谓ZigZag编码即将负数转换成正数,而所有正数都乘2,如0编码成0,-1编码成1,1编码成2,-2编码成3,以此类推,因而它对负数的编码依然保持比较高的效率。
public void writeSInt32NoTag(final int value) throws IOException { writeRawVarint32(encodeZigZag32(value)); }
顺带32,64,zigzag:
/** * Encode a ZigZag-encoded 32-bit value. ZigZag encodes signed integers * into values that can be efficiently encoded with varint. (Otherwise, * negative values must be sign-extended to 64 bits to be varint encoded, * thus always taking 10 bytes on the wire.) * * @param n A signed 32-bit integer. * @return An unsigned 32-bit integer, stored in a signed int because * Java has no explicit unsigned support. */ public static int encodeZigZag32(final int n) { // Note: the right-shift must be arithmetic return (n << 1) ^ (n >> 31); } /** * Encode a ZigZag-encoded 64-bit value. ZigZag encodes signed integers * into values that can be efficiently encoded with varint. (Otherwise, * negative values must be sign-extended to 64 bits to be varint encoded, * thus always taking 10 bytes on the wire.) * * @param n A signed 64-bit integer. * @return An unsigned 64-bit integer, stored in a signed int because * Java has no explicit unsigned support. */ public static long encodeZigZag64(final long n) { // Note: the right-shift must be arithmetic return (n << 1) ^ (n >> 63); } }
4. 对fixed32/sfixed32/fixed64/sfixed64类型,直接将该值以小端模式的固定长度编码。
以fixed32为例:
/** Write a {@code fixed32} field to the stream. */ public void writeFixed32NoTag(final int value) throws IOException { writeRawLittleEndian32(value); }
public void writeRawLittleEndian32(final int value) throws IOException { writeRawByte((value ) & 0xFF); writeRawByte((value >> 8) & 0xFF); writeRawByte((value >> 16) & 0xFF); writeRawByte((value >> 24) & 0xFF); }
其他类似。
5. 对double类型,先将double转换成long类型,然后以8个字节固定长度小端模式写入。
6. 对float类型,先将float类型转换成int类型,然后以4个字节固定长度小端模式写入。
public void writeDouble(final int fieldNumber, final double value) throws IOException { writeTag(fieldNumber, WireFormat.WIRETYPE_FIXED64); writeDoubleNoTag(value); } /** Write a {@code float} field, including tag, to the stream. */ public void writeFloat(final int fieldNumber, final float value) throws IOException { writeTag(fieldNumber, WireFormat.WIRETYPE_FIXED32); writeFloatNoTag(value); }
/** Write a {@code double} field to the stream. */ public void writeDoubleNoTag(final double value) throws IOException { writeRawLittleEndian64(Double.doubleToRawLongBits(value)); } /** Write a {@code float} field to the stream. */ public void writeFloatNoTag(final float value) throws IOException { writeRawLittleEndian32(Float.floatToRawIntBits(value)); }
7. 对bool类型,写0或1的一个字节。
public void writeBool(final int fieldNumber, final boolean value) throws IOException { writeTag(fieldNumber, WireFormat.WIRETYPE_VARINT); writeBoolNoTag(value); }
public void writeBoolNoTag(final boolean value) throws IOException { writeRawByte(value ? 1 : 0); }
8. 对string类型,使用UTF-8编码获取字节数组,然后先用变长编码写入字节数组长度,然后写入所有的字节数组。
Tag |
msgByteSize |
msgByte |
public void writeStringNoTag(final String value) throws IOException { // Unfortunately there does not appear to be any way to tell Java to encode // UTF-8 directly into our buffer, so we have to let it create its own byte // array and then copy. final byte[] bytes = value.getBytes(Internal.UTF_8); writeRawVarint32(bytes.length); writeRawBytes(bytes); }
9. 对bytes类型(ByteString),先用变长编码写入长度,然后写入整个字节数组。
Tag |
msgByteSize |
msgByte |
public void writeBytesNoTag(final ByteString value) throws IOException { writeRawVarint32(value.size()); writeRawBytes(value); }
10. 对枚举类型(类型值WIRETYPE_VARINT),用int32编码方式写入定义枚举项时给定的值(因而在给枚举类型项赋值时不推荐使用负数,因为int32编码方式对负数编码效率太低)。
/** * Write an enum field, including tag, to the stream. Caller is responsible * for converting the enum value to its numeric value. */ public void writeEnum(final int fieldNumber, final int value) throws IOException { writeTag(fieldNumber, WireFormat.WIRETYPE_VARINT); writeEnumNoTag(value); }
public void writeEnumNoTag(final int value) throws IOException { writeInt32NoTag(value); }
11. 对内嵌Message类型(类型值WIRETYPE_LENGTH_DELIMITED),先写入整个Message序列化后字节长度,然后写入整个Message。
Tag |
msgByteSize |
msgByte
|
public void writeMessageNoTag(final MessageLite value) throws IOException { writeRawVarint32(value.getSerializedSize()); value.writeTo(this); }
repeated字段编码
对于repeated字段,一般有两种编码方式:
1. 每个项都先写入tag,然后写入具体数据。如对基本类型:
Tag |
Data |
Tag |
Data |
… |
而对message类型:
Tag |
Length |
Data |
Tag |
Length |
Data |
… |
2. 先写入tag,后count,再写入count个项,每个项包含length|data数据。即:
Tag |
Count |
Length |
Data |
Length |
Data |
… |
从编码效率的角度来看,个人感觉第二中情况更加有效,然而不知道处于什么原因考虑,protobuf采用了第一种方式来编码,个人能想到的一个理由是第一种情况下,每个消息项都是相对独立的,因而在传输过程中接收端每接收到一个消息项就可以进行解析,而不需要等待整个repeated字段的消息包。对于基本类型,protobuf也采用了第一种编码方式,后来发现这种编码方式效率太低,因而可以添加[packed = true]的描述将其转换成第三种编码方式(第二种方式的变种,对基本数据类型,比第二种方式更加有效):
3. 先写入tag,后写入字段的总字节数,再写入每个项数据。即:
Tag |
dataByteSize |
Data |
Data |
… |
目前protobuf只支持基本类型的packed修饰,因而如果将packed添加到非repeated字段或非基本类型的repeated字段,编译器在编译.proto文件时会报错。
未识别字段编码
在protobuf中,将所有未识别字段保存在UnknownFieldSet中,并且在每个由protobuf编译生成的Message类以及GeneratedMessage.Builder中保存了UnknownFieldSet字段unknownFields;该字段可以从CodedInputStream中初始化(调用UnknownFieldSet.Builder的mergeFieldFrom()方法)或从用户自己通过Builder设置;在序列化时,调用UnknownFieldSet的writeTo()方法将自身内容序列化到CodedOutputStream中。
UnknownFieldSet顾名思义是未知字段的集合,其内部数据结构是一个FieldNumber到Field的Map,而一个Field用于表达一个未知字段,它可以是任何值,因而它包含了所有5中类型的List字段,这里并没有对一个Field验证,因而允许多个相同FieldNumber的未知字段,并且他们可以是任意类型值。UnknownFieldSet采用MessageLite编程模式,因而它实现了MessageLite接口,并且定义了一个Builder类实现MessageLite.Builder接口用于手动或从CodedInputStream中构建UnknownFieldSet。虽然Field本身没有实现MessageLite接口,它依然实现了该接口的部分方法,如writeTo()、getSerializedSize()用于实现向CodedOutputStream中序列化自身,并且定义了Field.Builder类用于构建Field实例。
在一个Message序列化时(writeTo()方法实现),在写完所有可识别的字段以及扩展字段,这个定义在Message中的UnknownFieldSet也会被写入CodedOutputStream中;而在从CodedInputStream中解析时,对任何未知字段也都会被写入这个UnknownFieldSet中。
扩展字段编码
在写框架代码时,经常由扩展性的需求,在Java中,只需要简单的定义一个父类或接口即可解决,如果框架本身还负责构建实例本身,可以使用反射或暴露Factory类也可以顺利实现,然而对序列化来说,就很难提供这种动态plugin机制了。然而protobuf还是提出来一个相对可以接受的机制(语法有点怪异,但是至少可以用):在一个message中定义它支持的可扩展字段值的范围,然后用户可以使用extend关键字扩展该message定义(具体参考相关章节)。在实现中,所有这些支持字段扩展的message类型继承自ExtendableMessage类(它本身继承自GeneratedMessage类)并实现ExtendableMessageOrBuilder接口,而它们的Builder类则继承自ExtendableBuilder类并且同时也实现了ExtendableMessageOrBuilder接口。
ExtendableMessage和ExtendableBuilder类都包含FieldSet<FieldDescriptor>类型的字段用于保存该message所有的扩展字段值。FieldSet中保存了FieldDescriptor到其Object值的Map,然而在ExtendableMessage和ExtendableBuilder中则使用GeneratedExtension来表识一个扩展字段,这是因为GeneratedExtension除了包含对一个扩展字段的描述信息FieldDescriptor外,还存储了该扩展字段的类型、默认值等信息,在protobuf消息定义编译器中会为每个扩展字段生成相应的GeneratedExtension实例以供用户使用