readExact method

Future<Uint8List> readExact(
  1. int length
)

Read exactly length bytes from the stream. Throws if EOF is reached before reading length bytes.

Implementation

Future<Uint8List> readExact(int length) async {
  if (_closed) {
    throw StateError('BufferedP2PStreamReader is closed');
  }

  if (length == 0) {
    return Uint8List(0);
  }

  // Fill buffer until we have enough bytes
  while (_buffer.remainingLength < length && !_eof) {
    await _fillBuffer(length - _buffer.remainingLength);
  }

  if (_buffer.remainingLength < length) {
    throw Exception(
        'Unexpected EOF: requested $length bytes, got ${_buffer.remainingLength}');
  }

  return _buffer.read(length);
}