readExactly method

Future<Uint8List> readExactly(
  1. int count
)

Implementation

Future<Uint8List> readExactly(int count) async {
  while (_buffer.length < count) {
    if (!await iterator.moveNext()) {
      throw const SocketException(
        'SOCKS5 proxy closed the connection during the handshake',
      );
    }
    final chunk = iterator.current;
    final merged = Uint8List(_buffer.length + chunk.length)
      ..setRange(0, _buffer.length, _buffer)
      ..setRange(_buffer.length, _buffer.length + chunk.length, chunk);
    _buffer = merged;
  }
  final out = Uint8List.sublistView(_buffer, 0, count);
  _buffer = Uint8List.sublistView(_buffer, count);
  return out;
}