peekBytes method Null safety

  1. @override
InputStream peekBytes(
  1. int count,
  2. [int offset = 0]
)
override

Read count bytes from an offset of the current read position, without moving the read position.

Implementation

@override
InputStream peekBytes(int count, [int offset = 0]) {
  var end = _bufferPosition + offset + count;
  if (end > 0 && end < _bufferSize) {
    final bytes = _buffer.sublist(_bufferPosition + offset, end);
    return InputStream(bytes);
  }

  final bytes = Uint8List(count);

  var remaining = _bufferSize - (_bufferPosition + offset);
  if (remaining > 0) {
    final bytes1 = _buffer.sublist(_bufferPosition + offset, _bufferSize);
    bytes.setRange(0, remaining, bytes1);
  }

  _file.readIntoSync(bytes, remaining, count);
  _file.setPositionSync(_filePosition);

  return InputStream(bytes);
}