foundation模块下有一个文件serialization.dart,里面有两个类:WriteBuffer和ReadBuffer,是关于字节流和int类型互相转换的,目前我只是在蓝牙硬件交互里常用到。
特别注意的是一个WriteBuffer和ReadBuffer的指针(引用)只能用一次
内部实现的其实很常规的,和其他类型没什么区别,在这里需要特别补充的资料是:
整型范围
- Int8-[-128:127]
- Int16-[-32768:32767]
- Int32-[-2147483648:2147483647]
- Int64-[-9223372036854775808:9223372036854775807]
无符号整型范围
- UInt8-[0:255]
- UInt16-[0:65535]
- UInt32-[0:4294967295]
- UInt64-[0:18446744073709551615]
关于WriteBuffer和ReadBuffer使用,贴出flutter的测试用例,就很清晰了。
group('Write and read buffer round-trip', () {
test('of single byte', () {
final WriteBuffer write = WriteBuffer();
write.putUint8(201);
final ByteData written = write.done();
expect(written.lengthInBytes, equals(1));
final ReadBuffer read = ReadBuffer(written);
expect(read.getUint8(), equals(201));
});
test('of 32-bit integer', () {
final WriteBuffer write = WriteBuffer();
write.putInt32(-9);
final ByteData written = write.done();
expect(written.lengthInBytes, equals(4));
final ReadBuffer read = ReadBuffer(written);
expect(read.getInt32(), equals(-9));
});
test('of 32-bit integer in big endian', () {
final WriteBuffer write = WriteBuffer();
write.putInt32(-9, endian: Endian.big);
final ByteData written = write.done();
expect(written.lengthInBytes, equals(4));
final ReadBuffer read = ReadBuffer(written);
expect(read.getInt32(endian: Endian.big), equals(-9));
});
test('of 64-bit integer', () {
final WriteBuffer write = WriteBuffer();
write.putInt64(-9000000000000);
final ByteData written = write.done();
expect(written.lengthInBytes, equals(8));
final ReadBuffer read = ReadBuffer(written);
expect(read.getInt64(), equals(-9000000000000));
});
test('of 64-bit integer in big endian', () {
final WriteBuffer write = WriteBuffer();
write.putInt64(-9000000000000, endian: Endian.big);
final ByteData written = write.done();
expect(written.lengthInBytes, equals(8));
final ReadBuffer read = ReadBuffer(written);
expect(read.getInt64(endian: Endian.big), equals(-9000000000000));
});
test('of double', () {
final WriteBuffer write = WriteBuffer();
write.putFloat64(3.14);
final ByteData written = write.done();
expect(written.lengthInBytes, equals(8));
final ReadBuffer read = ReadBuffer(written);
expect(read.getFloat64(), equals(3.14));
});
test('of double in big endian', () {
final WriteBuffer write = WriteBuffer();
write.putFloat64(3.14, endian: Endian.big);
final ByteData written = write.done();
expect(written.lengthInBytes, equals(8));
final ReadBuffer read = ReadBuffer(written);
expect(read.getFloat64(endian: Endian.big), equals(3.14));
});
test('of 32-bit int list when unaligned', () {
final Int32List integers = Int32List.fromList([-99, 2, 99]);
final WriteBuffer write = WriteBuffer();
write.putUint8(9);
write.putInt32List(integers);
final ByteData written = write.done();
expect(written.lengthInBytes, equals(16));
final ReadBuffer read = ReadBuffer(written);
read.getUint8();
expect(read.getInt32List(3), equals(integers));
});
test('of 64-bit int list when unaligned', () {
final Int64List integers = Int64List.fromList([-99, 2, 99]);
final WriteBuffer write = WriteBuffer();
write.putUint8(9);
write.putInt64List(integers);
final ByteData written = write.done();
expect(written.lengthInBytes, equals(32));
final ReadBuffer read = ReadBuffer(written);
read.getUint8();
expect(read.getInt64List(3), equals(integers));
});
test('of double list when unaligned', () {
final Float64List doubles = Float64List.fromList([3.14, double.nan]);
final WriteBuffer write = WriteBuffer();
write.putUint8(9);
write.putFloat64List(doubles);
final ByteData written = write.done();
expect(written.lengthInBytes, equals(24));
final ReadBuffer read = ReadBuffer(written);
read.getUint8();
final Float64List readDoubles = read.getFloat64List(2);
expect(readDoubles[0], equals(3.14));
expect(readDoubles[1], isNaN);
});
});
这是flutter框架源码分析的其中一篇,因能力有限,有诸多不足之处,还请斧正。