Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 17 additions & 14 deletions lib/utilities.dart
Original file line number Diff line number Diff line change
Expand Up @@ -75,24 +75,21 @@ class ByteUtils {
static Uint8List bigIntToBytes(
BigInt? value,
int length, {
endianness = Endianness.Big,
Endianness endianness = Endianness.Big,
}) {
Uint8List? list = List<int?>.filled(0, null, growable: false) as Uint8List;
BigInt? v = value;
var list = <int>[];
BigInt v = value!;
Comment on lines +80 to +81
for (int i = 0; i < length; i++) {
list.add((v! % (BigInt.from(256))).toInt());
v ~/= (BigInt.from(256));
list.add((v % BigInt.from(256)).toInt());
v ~/= BigInt.from(256);
Comment on lines 82 to +84
}

/// unrelated_type_equality_check checked!
BigInt zero = 0 as BigInt;
if (v != zero) {
if (v != BigInt.zero) {
throw ArgumentError("Value $value overflows $length bytes");
}
Comment on lines +86 to 88
if (endianness == Endianness.Big) {
list = list.reversed as Uint8List?;
list = list.reversed.toList();
}
return Uint8List.fromList(list!);
return Uint8List.fromList(list);
Comment on lines 75 to +92
}

/// Converts a single byte [value] to a 2-character hex string.
Expand All @@ -119,7 +116,9 @@ class ByteUtils {
},
);
if (hex.length % 2 != 0) {
throw FormatException("Hex string length must be even, got ${hex.length}");
throw FormatException(
"Hex string length must be even, got ${hex.length}",
);
}
var result = <int>[];
for (int i = 0; i < hex.length; i += 2) {
Expand Down Expand Up @@ -318,7 +317,9 @@ class ByteStream {
/// Throws an error if not enough bytes are available.
void checkBytesAvailable(int number) {
if (number > unreadLength) {
throw RangeError("Not enough bytes in stream: need $number, have $unreadLength");
throw RangeError(
"Not enough bytes in stream: need $number, have $unreadLength",
);
}
}

Expand All @@ -327,7 +328,9 @@ class ByteStream {
/// Throws an error if there are unread bytes.
void checkEmpty() {
if (unreadLength != 0) {
throw FormatException("Stream has $unreadLength unexpected bytes after decode");
throw FormatException(
"Stream has $unreadLength unexpected bytes after decode",
);
}
}
}
Expand Down
Loading