readBuffer method

Future<ReadBuffer> readBuffer(
  1. ReadBuffer buffer
)

Reads into buffer from the socket, and returns the same buffer in a Future which completes when enough bytes have been read to fill the buffer.

This must not be called while there is still a read ongoing, but may be called before onDataReady is called, in which case onDataReady will not be called when data arrives, and the read will start instead.

Implementation

Future<ReadBuffer> readBuffer(ReadBuffer buffer) {
  if (_closed) throw StateError("Cannot read from a closed socket!");

  if (_readingBuffer != null) throw StateError("Cannot read from socket, already reading!");

  _readingBuffer = buffer;
  _readOffset = 0;
  _readCompleter = Completer<ReadBuffer>();

  if (_socket.available() > 0) _readBuffer();

  return _readCompleter.future;
}