readBytes method Null safety

  1. @override
InputStream readBytes(
  1. int length
)
override

Read count bytes from the stream.

Implementation

@override
InputStream readBytes(int length) {
  if (isEOS) {
    return InputStream(<int>[]);
  }

  if (_bufferPosition == _bufferSize) {
    _readBuffer();
  }

  if (_remainingBufferSize >= length) {
    final bytes = _buffer.sublist(_bufferPosition, _bufferPosition + length);
    _bufferPosition += length;
    return InputStream(bytes);
  }

  var total_remaining = fileRemaining + _remainingBufferSize;
  if (length > total_remaining) {
    length = total_remaining;
  }

  final bytes = Uint8List(length);

  var offset = 0;
  while (length > 0) {
    var remaining = _bufferSize - _bufferPosition;
    var end = (length > remaining) ? _bufferSize : (_bufferPosition + length);
    final l = _buffer.sublist(_bufferPosition, end);
    // TODO probably better to use bytes.setRange here.
    for (var i = 0; i < l.length; ++i) {
      bytes[offset + i] = l[i];
    }
    offset += l.length;
    length -= l.length;
    _bufferPosition = end;
    if (length > 0 && _bufferPosition == _bufferSize) {
      _readBuffer();
      if (_bufferSize == 0) {
        break;
      }
    }
  }

  return InputStream(bytes);
}