write method

  1. @override
int write(
  1. List<int> bytes,
  2. [int offset = 0,
  3. int? length]
)
override

Writes the provided bytes to the sink, optionally with offsets.

Returns the number of bytes written, which is always length.

This method is non-blocking and may be buffered.

If the buffer is full, this method will flush the buffer and then write the provided bytes, otherwise it will append the bytes to the buffer until it is full and write to the underlying buffer.

Implementation

@override
int write(List<int> bytes, [int offset = 0, int? length]) {
  length ??= bytes.length - offset;

  var remaining = length;
  while (remaining > 0) {
    // Try writing to the buffer.
    final wrote = _write(bytes, offset, remaining);
    offset += wrote;
    remaining -= wrote;

    // If the buffer is full, flush it.
    if (_length == _buffer.length) {
      _softFlush();
    }
  }

  return length;
}