Add optional endian
argument for WriteBuffer/ReadBuffer (#46661)
This commit is contained in:
parent
945f206b5e
commit
4e6d649f14
@ -30,33 +30,33 @@ class WriteBuffer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Write a Uint16 into the buffer.
|
/// Write a Uint16 into the buffer.
|
||||||
void putUint16(int value) {
|
void putUint16(int value, {Endian endian}) {
|
||||||
_eightBytes.setUint16(0, value, Endian.host);
|
_eightBytes.setUint16(0, value, endian ?? Endian.host);
|
||||||
_buffer.addAll(_eightBytesAsList, 0, 2);
|
_buffer.addAll(_eightBytesAsList, 0, 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Write a Uint32 into the buffer.
|
/// Write a Uint32 into the buffer.
|
||||||
void putUint32(int value) {
|
void putUint32(int value, {Endian endian}) {
|
||||||
_eightBytes.setUint32(0, value, Endian.host);
|
_eightBytes.setUint32(0, value, endian ?? Endian.host);
|
||||||
_buffer.addAll(_eightBytesAsList, 0, 4);
|
_buffer.addAll(_eightBytesAsList, 0, 4);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Write an Int32 into the buffer.
|
/// Write an Int32 into the buffer.
|
||||||
void putInt32(int value) {
|
void putInt32(int value, {Endian endian}) {
|
||||||
_eightBytes.setInt32(0, value, Endian.host);
|
_eightBytes.setInt32(0, value, endian ?? Endian.host);
|
||||||
_buffer.addAll(_eightBytesAsList, 0, 4);
|
_buffer.addAll(_eightBytesAsList, 0, 4);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Write an Int64 into the buffer.
|
/// Write an Int64 into the buffer.
|
||||||
void putInt64(int value) {
|
void putInt64(int value, {Endian endian}) {
|
||||||
_eightBytes.setInt64(0, value, Endian.host);
|
_eightBytes.setInt64(0, value, endian ?? Endian.host);
|
||||||
_buffer.addAll(_eightBytesAsList, 0, 8);
|
_buffer.addAll(_eightBytesAsList, 0, 8);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Write an Float64 into the buffer.
|
/// Write an Float64 into the buffer.
|
||||||
void putFloat64(double value) {
|
void putFloat64(double value, {Endian endian}) {
|
||||||
_alignTo(8);
|
_alignTo(8);
|
||||||
_eightBytes.setFloat64(0, value, Endian.host);
|
_eightBytes.setFloat64(0, value, endian ?? Endian.host);
|
||||||
_buffer.addAll(_eightBytesAsList);
|
_buffer.addAll(_eightBytesAsList);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -122,37 +122,37 @@ class ReadBuffer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Reads a Uint16 from the buffer.
|
/// Reads a Uint16 from the buffer.
|
||||||
int getUint16() {
|
int getUint16({Endian endian}) {
|
||||||
final int value = data.getUint16(_position, Endian.host);
|
final int value = data.getUint16(_position, endian ?? Endian.host);
|
||||||
_position += 2;
|
_position += 2;
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Reads a Uint32 from the buffer.
|
/// Reads a Uint32 from the buffer.
|
||||||
int getUint32() {
|
int getUint32({Endian endian}) {
|
||||||
final int value = data.getUint32(_position, Endian.host);
|
final int value = data.getUint32(_position, endian ?? Endian.host);
|
||||||
_position += 4;
|
_position += 4;
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Reads an Int32 from the buffer.
|
/// Reads an Int32 from the buffer.
|
||||||
int getInt32() {
|
int getInt32({Endian endian}) {
|
||||||
final int value = data.getInt32(_position, Endian.host);
|
final int value = data.getInt32(_position, endian ?? Endian.host);
|
||||||
_position += 4;
|
_position += 4;
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Reads an Int64 from the buffer.
|
/// Reads an Int64 from the buffer.
|
||||||
int getInt64() {
|
int getInt64({Endian endian}) {
|
||||||
final int value = data.getInt64(_position, Endian.host);
|
final int value = data.getInt64(_position, endian ?? Endian.host);
|
||||||
_position += 8;
|
_position += 8;
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Reads a Float64 from the buffer.
|
/// Reads a Float64 from the buffer.
|
||||||
double getFloat64() {
|
double getFloat64({Endian endian}) {
|
||||||
_alignTo(8);
|
_alignTo(8);
|
||||||
final double value = data.getFloat64(_position, Endian.host);
|
final double value = data.getFloat64(_position, endian ?? Endian.host);
|
||||||
_position += 8;
|
_position += 8;
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
@ -27,6 +27,14 @@ void main() {
|
|||||||
final ReadBuffer read = ReadBuffer(written);
|
final ReadBuffer read = ReadBuffer(written);
|
||||||
expect(read.getInt32(), equals(-9));
|
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', () {
|
test('of 64-bit integer', () {
|
||||||
final WriteBuffer write = WriteBuffer();
|
final WriteBuffer write = WriteBuffer();
|
||||||
write.putInt64(-9000000000000);
|
write.putInt64(-9000000000000);
|
||||||
@ -35,6 +43,14 @@ void main() {
|
|||||||
final ReadBuffer read = ReadBuffer(written);
|
final ReadBuffer read = ReadBuffer(written);
|
||||||
expect(read.getInt64(), equals(-9000000000000));
|
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', () {
|
test('of double', () {
|
||||||
final WriteBuffer write = WriteBuffer();
|
final WriteBuffer write = WriteBuffer();
|
||||||
write.putFloat64(3.14);
|
write.putFloat64(3.14);
|
||||||
@ -43,6 +59,14 @@ void main() {
|
|||||||
final ReadBuffer read = ReadBuffer(written);
|
final ReadBuffer read = ReadBuffer(written);
|
||||||
expect(read.getFloat64(), equals(3.14));
|
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', () {
|
test('of 32-bit int list when unaligned', () {
|
||||||
final Int32List integers = Int32List.fromList(<int>[-99, 2, 99]);
|
final Int32List integers = Int32List.fromList(<int>[-99, 2, 99]);
|
||||||
final WriteBuffer write = WriteBuffer();
|
final WriteBuffer write = WriteBuffer();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user