read method
Reads up to size bytes from the stream.
The returned data may be less than size if the end of the stream is
reached or if chunk boundaries are encountered.
Returns null if the end of the stream is reached.
Implementation
Future<T?> read(int size) async {
if (_currentChunk == null) {
if (!await _stream.moveNext()) {
return null;
}
_currentChunk = _stream.current;
_currentOffset = 0;
}
/// A fast path for the case where [_currentChunk] can be directly returned
/// without creating a sublist view.
if (_currentOffset == 0 && size >= getLength(_currentChunk as T)) {
final result = _currentChunk;
_currentChunk = null;
return result;
}
final effectSize = min(
size,
getLength(_currentChunk as T) - _currentOffset,
);
final result = getSublistView(
_currentChunk as T,
_currentOffset,
_currentOffset + effectSize,
);
_currentOffset += effectSize;
if (_currentOffset >= getLength(_currentChunk as T)) {
_currentChunk = null;
}
return result;
}