writeInputStream method

  1. @override
void writeInputStream(
  1. InputStreamBase stream
)
override

Write an InputStream to the output stream.

Implementation

@override
void writeInputStream(InputStreamBase stream) {
  if (stream is InputStream) {
    final len = stream.length;

    if (_bufferPosition + len >= _buffer.length) {
      flush();

      if (_bufferPosition + len < _buffer.length) {
        for (int i = 0, j = _bufferPosition, k = stream.offset;
            i < len;
            ++i, ++j, ++k) {
          _buffer[j] = stream.buffer[k];
        }
        _bufferPosition += len;
        _length += len;
        return;
      }
    }

    if (_bufferPosition > 0) {
      flush();
    }
    _fileHandle.writeFromSync(
        stream.buffer, stream.offset, stream.offset + stream.length);
    _length += stream.length;
  } else {
    var size = stream.length;
    const chunkSize = 1024 * 1024;
    Uint8List? bytes;
    while (size > chunkSize) {
      bytes = stream.readBytes(chunkSize).toUint8List(bytes);
      writeBytes(bytes);
      size -= chunkSize;
    }
    if (size > 0) {
      bytes = stream.readBytes(size).toUint8List(bytes);
      writeBytes(bytes);
    }
  }
}