chunked_stream 1.0.1

  • Readme
  • Changelog
  • Example
  • Installing
  • --

Chunked Stream Utilities #

Utilities for working with chunked streams, such as Stream<List<int>>.

Disclaimer: This is not an officially supported Google product.

A chunked stream is a stream where the data arrives in chunks. The most common example is a byte stream, which conventionally has the type Stream<List<int>>. We say a byte stream in chunked because bytes arrives in chunks, rather than individiually.

A byte stream could technically have the type Stream<int>, however, this would be very inefficient, as each byte would be passed as an individual event. Instead bytes arrives in chunks (List<int>) and the type of a byte stream is Stream<List<int>>.

To make it easy to work with the chunk streams, such as Stream<List<int>>, this package provides ChunkedStreamIterator which allows you to specify how many elements you want, and buffer unconsumed elements, making it easy to work with chunked streams one element at the time.

Example #

final reader = ChunkedStreamIterator(File('my-file.txt').openRead());
// While the reader has a next byte
while (true) {
  var data = await;  // read one byte
  if (data.length < 0) {
    print('End of file reached');
  print('next byte: ${data[0]}');

v1.0.1 #

  • Fixed lints reported by pana.

v1.0.0 #

  • Initial release.


// Copyright 2019 Google LLC
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.

import 'dart:io' show stdout;
import 'dart:convert' show utf8;
import 'dart:typed_data' show Uint8List;
import 'package:chunked_stream/chunked_stream.dart';

void main() async {
  // Input consisting of: *([uint32 length] [blob of size length])
  // This is a series of blocks consisting of an uint32 length followed by
  // length number of bytes.
  // Many format including tar, msgpack, protobuf, etc. have formats binary
  // encodings that consist of an integer indicating length of a blob of bytes.
  // ChunkedStreamIterator can be useful when decoding such formats.
  final inputStream = () async* {
    // Yield blob1 from stream
    final blob1 = utf8.encode('hello world');
    yield [blob1.length, 0, 0, 0]; // uint32 encoding of length
    yield blob1;

    // Yield blob2 from stream
    final blob2 = utf8.encode('small blob');
    yield [blob2.length, 0, 0, 0];
    yield blob2;

  // To ensure efficient reading, we buffer the stream upto 4096 bytes, for I/O
  // buffering can improve performance (in some cases).
  final bufferedStream = bufferChunkedStream(inputStream, bufferSize: 4096);

  // Create a chunk stream iterator over the buffered stream.
  final iterator = ChunkedStreamIterator(bufferedStream);

  while (true) {
    // Read the first 4 bytes
    final lengthBytes = await;

    // We have EOF if there is no more bytes
    if (lengthBytes.length == 0) {

    // Read those 4 bytes as Uint32
    final length = Uint8List.fromList(lengthBytes).buffer.asUint32List()[0];

    // Read the next [length] bytes, and write them to stdout
    print('Blob of $length bytes:');
    await stdout.addStream(iterator.substream(length));

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:

  chunked_stream: ^1.0.1

2. Install it

You can install packages from the command line:

with pub:

$ pub get

Alternatively, your editor might support pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:

import 'package:chunked_stream/chunked_stream.dart';
Awaiting analysis to complete.