u8aFixLength function

Uint8List u8aFixLength(
  1. Uint8List value, {
  2. int bitLength = -1,
  3. bool atStart = false,
})

Implementation

Uint8List u8aFixLength(
  Uint8List value, {
  int bitLength = -1,
  bool atStart = false,
}) {
  final byteLength = (bitLength / 8).ceil();

  if (bitLength == -1 || value.length == byteLength) {
    return value;
  } else if (value.length > byteLength) {
    return value.sublist(0, byteLength);
  }

  final result = Uint8List(byteLength);
  if (atStart) {
    result.setRange(0, value.length, value);
  } else {
    result.setRange(byteLength - value.length, byteLength, value);
  }
  return result;
}