readBuffer method

Future<Buffer> readBuffer(
  1. Buffer 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<Buffer> readBuffer(Buffer buffer) {
  log.fine('readBuffer, length=${buffer.length}');
  if (_closed) {
    throw StateError('Cannot read from socket, it is closed');
  }
  if (_readingBuffer != null) {
    throw StateError('Cannot read from socket, already reading');
  }
  _readingBuffer = buffer;
  _readOffset = 0;
  _readCompleter = Completer<Buffer>();

  if (_socket.available() > 0) {
    log.fine('readBuffer, data already ready');
    _readBuffer();
  }

  return _readCompleter.future;
}