readBytes method

Future<Uint8List> readBytes(
  1. int num
)

Returns a new, mutable Uint8List containing the desired number of bytes.

Throws EOFException if EOF is reached before the needed bytes are read.

Implementation

Future<Uint8List> readBytes(int num) async {
  if (num == 0) {
    return Uint8List(0);
  }
  await _ensureNext();
  if (_pos == 0 && num == _curr!.length) {
    _pos = _curr!.length;
    return Uint8List.fromList(_curr!);
  } else if (_pos + num <= _curr!.length) {
    final result = Uint8List.fromList(_curr!.sublist(_pos, _pos + num));
    _pos += num;
    return result;
  } else {
    final len = _curr!.length - _pos;
    assert(len > 0 && len < num);
    final result = Uint8List(num);
    final buf = await readBytesImmutable(len);
    result.setRange(0, len, buf);
    final buf2 = await readBytesImmutable(num - len);
    result.setRange(len, num, buf2);
    return result;
  }
}