readBytesImmutable method

Future<Uint8List> readBytesImmutable(
  1. int num
)

Returns a potentially immutable Uint8List containing the desired number of bytes.

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

Implementation

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