ChunkedStreamReader<T> class

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.
  r.cancel();
}

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

Available Extensions

Constructors

ChunkedStreamReader(Stream<List<T>> stream)
factory

Properties

hashCode int
The hash code for this object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

cancel() Future<void>
Cancel the underlying chunked stream.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
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.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited