ChunkedStreamReader<T> class Null safety

Utility class for reading elements from a chunked stream.

A chunked stream is a stream where each event is a chunk of elements. Byte-streams with the type Stream<List<int>> is common of example of this. As illustrated in the example below, this utility class makes it easy to read a chunked stream using custom chunk sizes and sub-stream sizes, without managing partially read chunks.

final r = ChunkedStreamReader(File('myfile.txt').openRead());
try {
  // Read the first 4 bytes
  final firstBytes = await r.readChunk(4);
  if (firstBytes.length < 4) {
    throw Exception('myfile.txt has less than 4 bytes');

  // Read next 8 kilobytes as a substream
  Stream<List<int>> substream = r.readStream(8 * 1024);

} finally {
  // We always cancel the ChunkedStreamReader, this ensures the underlying
  // stream is cancelled.

The read-operations readChunk and readStream must not be invoked until the future from a previous call has completed.

Available Extensions


ChunkedStreamReader(Stream<List<T>> stream)


hashCode int
The hash code for this object. [...]
read-only, inherited
runtimeType Type
A representation of the runtime type of the object.
read-only, inherited


cancel() Future<void>
Cancel the underlying chunked stream. [...]
noSuchMethod(Invocation invocation) → dynamic
Invoked when a non-existent method or property is accessed. [...]
readChunk(int size) Future<List<T>>
Read next size elements from chunked stream, buffering to create a chunk with size elements. [...]
readStream(int size) Stream<List<T>>
Read next size elements from chunked stream as a sub-stream. [...]
toString() String
A string representation of this object. [...]


operator ==(Object other) bool
The equality operator. [...]