Commit 4e6d649f authored by Sunbreak's avatar Sunbreak Committed by Flutter GitHub Bot

Add optional `endian` argument for WriteBuffer/ReadBuffer (#46661)

parent 945f206b
......@@ -30,33 +30,33 @@ class WriteBuffer {
}
/// Write a Uint16 into the buffer.
void putUint16(int value) {
_eightBytes.setUint16(0, value, Endian.host);
void putUint16(int value, {Endian endian}) {
_eightBytes.setUint16(0, value, endian ?? Endian.host);
_buffer.addAll(_eightBytesAsList, 0, 2);
}
/// Write a Uint32 into the buffer.
void putUint32(int value) {
_eightBytes.setUint32(0, value, Endian.host);
void putUint32(int value, {Endian endian}) {
_eightBytes.setUint32(0, value, endian ?? Endian.host);
_buffer.addAll(_eightBytesAsList, 0, 4);
}
/// Write an Int32 into the buffer.
void putInt32(int value) {
_eightBytes.setInt32(0, value, Endian.host);
void putInt32(int value, {Endian endian}) {
_eightBytes.setInt32(0, value, endian ?? Endian.host);
_buffer.addAll(_eightBytesAsList, 0, 4);
}
/// Write an Int64 into the buffer.
void putInt64(int value) {
_eightBytes.setInt64(0, value, Endian.host);
void putInt64(int value, {Endian endian}) {
_eightBytes.setInt64(0, value, endian ?? Endian.host);
_buffer.addAll(_eightBytesAsList, 0, 8);
}
/// Write an Float64 into the buffer.
void putFloat64(double value) {
void putFloat64(double value, {Endian endian}) {
_alignTo(8);
_eightBytes.setFloat64(0, value, Endian.host);
_eightBytes.setFloat64(0, value, endian ?? Endian.host);
_buffer.addAll(_eightBytesAsList);
}
......@@ -122,37 +122,37 @@ class ReadBuffer {
}
/// Reads a Uint16 from the buffer.
int getUint16() {
final int value = data.getUint16(_position, Endian.host);
int getUint16({Endian endian}) {
final int value = data.getUint16(_position, endian ?? Endian.host);
_position += 2;
return value;
}
/// Reads a Uint32 from the buffer.
int getUint32() {
final int value = data.getUint32(_position, Endian.host);
int getUint32({Endian endian}) {
final int value = data.getUint32(_position, endian ?? Endian.host);
_position += 4;
return value;
}
/// Reads an Int32 from the buffer.
int getInt32() {
final int value = data.getInt32(_position, Endian.host);
int getInt32({Endian endian}) {
final int value = data.getInt32(_position, endian ?? Endian.host);
_position += 4;
return value;
}
/// Reads an Int64 from the buffer.
int getInt64() {
final int value = data.getInt64(_position, Endian.host);
int getInt64({Endian endian}) {
final int value = data.getInt64(_position, endian ?? Endian.host);
_position += 8;
return value;
}
/// Reads a Float64 from the buffer.
double getFloat64() {
double getFloat64({Endian endian}) {
_alignTo(8);
final double value = data.getFloat64(_position, Endian.host);
final double value = data.getFloat64(_position, endian ?? Endian.host);
_position += 8;
return value;
}
......
......@@ -27,6 +27,14 @@ void main() {
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);
......@@ -35,6 +43,14 @@ void main() {
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);
......@@ -43,6 +59,14 @@ void main() {
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(<int>[-99, 2, 99]);
final WriteBuffer write = WriteBuffer();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment