Push unsigned read methods
This commit is contained in:
parent
d4cf453005
commit
f8918b9fcf
2 changed files with 41 additions and 12 deletions
|
@ -86,12 +86,24 @@ class ByteLayer {
|
||||||
_position++;
|
_position++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void writeUnsignedByte(int value) {
|
||||||
|
_ensureCapacity(1);
|
||||||
|
_byteBuffer.buffer.asByteData().setUint8(_position, value);
|
||||||
|
_position++;
|
||||||
|
}
|
||||||
|
|
||||||
int readByte() {
|
int readByte() {
|
||||||
final value = _byteBuffer[_position];
|
final value = _byteBuffer[_position];
|
||||||
_position++;
|
_position++;
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int readUnsignedByte() {
|
||||||
|
final value = _byteBuffer.buffer.asByteData().getUint8(_position);
|
||||||
|
_position++;
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
void writeVarInt(int value) {
|
void writeVarInt(int value) {
|
||||||
while ((value & ~0x7F) != 0) {
|
while ((value & ~0x7F) != 0) {
|
||||||
writeByte((value & 0x7F) | 0x80);
|
writeByte((value & 0x7F) | 0x80);
|
||||||
|
@ -239,6 +251,13 @@ class ByteLayer {
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int readUnsignedLong() {
|
||||||
|
final value =
|
||||||
|
_byteBuffer.buffer.asByteData().getUint64(_position, Endian.big);
|
||||||
|
_position += 8;
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
void writeVarLongNoZigZag(int value) {
|
void writeVarLongNoZigZag(int value) {
|
||||||
while (true) {
|
while (true) {
|
||||||
if ((value & ~0x7F) == 0) {
|
if ((value & ~0x7F) == 0) {
|
||||||
|
@ -286,14 +305,14 @@ class ByteLayer {
|
||||||
|
|
||||||
void writeBytes(Iterable<int> bytes) {
|
void writeBytes(Iterable<int> bytes) {
|
||||||
for (int byte in bytes) {
|
for (int byte in bytes) {
|
||||||
writeByte(byte);
|
writeUnsignedByte(byte);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
List<int> readBytes(int num) {
|
List<int> readBytes(int num) {
|
||||||
List<int> lst = [];
|
List<int> lst = [];
|
||||||
for (int i = 0; i < num; i++) {
|
for (int i = 0; i < num; i++) {
|
||||||
lst.add(readByte());
|
lst.add(readUnsignedByte());
|
||||||
}
|
}
|
||||||
|
|
||||||
return lst;
|
return lst;
|
||||||
|
@ -303,10 +322,10 @@ class ByteLayer {
|
||||||
if (position < _byteBuffer.length) {
|
if (position < _byteBuffer.length) {
|
||||||
// Set the value now
|
// Set the value now
|
||||||
seek(position);
|
seek(position);
|
||||||
int current = readByte();
|
int current = readUnsignedByte();
|
||||||
seek(position);
|
seek(position);
|
||||||
current |= maskToSet;
|
current |= maskToSet;
|
||||||
writeByte(current);
|
writeUnsignedByte(current);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -314,17 +333,17 @@ class ByteLayer {
|
||||||
if (position < _byteBuffer.length) {
|
if (position < _byteBuffer.length) {
|
||||||
// Lets clear the bit
|
// Lets clear the bit
|
||||||
seek(position);
|
seek(position);
|
||||||
int current = readByte();
|
int current = readUnsignedByte();
|
||||||
current &= maskToClear;
|
current &= maskToClear;
|
||||||
seek(position);
|
seek(position);
|
||||||
writeByte(current);
|
writeUnsignedByte(current);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool checkBit(int position, int mask) {
|
bool checkBit(int position, int mask) {
|
||||||
if (position < _byteBuffer.length) {
|
if (position < _byteBuffer.length) {
|
||||||
seek(position);
|
seek(position);
|
||||||
int current = readByte();
|
int current = readUnsignedByte();
|
||||||
return (current & mask) == mask;
|
return (current & mask) == mask;
|
||||||
} else
|
} else
|
||||||
return false;
|
return false;
|
||||||
|
@ -333,9 +352,10 @@ class ByteLayer {
|
||||||
int getBit(int position) {
|
int getBit(int position) {
|
||||||
if (position < _byteBuffer.length) {
|
if (position < _byteBuffer.length) {
|
||||||
seek(position);
|
seek(position);
|
||||||
return readByte();
|
return readUnsignedByte();
|
||||||
} else
|
} else {
|
||||||
return 0;
|
return 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void seek(int position) {
|
void seek(int position) {
|
||||||
|
|
|
@ -94,11 +94,20 @@ class UUID {
|
||||||
layer.writeBytes(bytes);
|
layer.writeBytes(bytes);
|
||||||
|
|
||||||
layer.unsetSetBit(6, 0x0F, 0x30);
|
layer.unsetSetBit(6, 0x0F, 0x30);
|
||||||
|
print(
|
||||||
|
"Existing bit at position 8: ${layer.getBit(8).toRadixString(2)}:${layer.getBit(8)}");
|
||||||
layer.unsetSetBit(8, 0x3F, 0x80);
|
layer.unsetSetBit(8, 0x3F, 0x80);
|
||||||
|
print(
|
||||||
|
"New bit at position 8: ${layer.getBit(8).toRadixString(2)}:${layer.getBit(8)}");
|
||||||
|
|
||||||
layer.resetPosition();
|
layer.resetPosition();
|
||||||
|
|
||||||
return UUID(layer.readLong(), layer.readLong());
|
var msb = layer.readUnsignedLong();
|
||||||
|
var lsb = layer.readUnsignedLong();
|
||||||
|
|
||||||
|
if (msb < 0) print("Most significant bit is negative!");
|
||||||
|
if (lsb < 0) print("Least significant bit is negative!");
|
||||||
|
return UUID(msb, lsb);
|
||||||
}
|
}
|
||||||
case 4:
|
case 4:
|
||||||
{
|
{
|
||||||
|
@ -115,7 +124,7 @@ class UUID {
|
||||||
|
|
||||||
layer.resetPosition();
|
layer.resetPosition();
|
||||||
|
|
||||||
return UUID(layer.readLong(), layer.readLong());
|
return UUID(layer.readUnsignedLong(), layer.readUnsignedLong());
|
||||||
}
|
}
|
||||||
case 5:
|
case 5:
|
||||||
{
|
{
|
||||||
|
@ -145,7 +154,7 @@ class UUID {
|
||||||
|
|
||||||
layer.resetPosition();
|
layer.resetPosition();
|
||||||
|
|
||||||
return UUID(layer.readLong(), layer.readLong());
|
return UUID(layer.readUnsignedLong(), layer.readUnsignedLong());
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
throw ArgumentError('Unsupported UUID version: $version');
|
throw ArgumentError('Unsupported UUID version: $version');
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue