chunked_stream 1.0.1

  • Readme
  • Changelog
  • Example
  • Installing
  • 79

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 reader.read(1);  // read one byte
  if (data.length < 0) {
    print('End of file reached');
    break;
  }
  print('next byte: ${data[0]}');
}

v1.0.1 #

  • Fixed lints reported by pana.

v1.0.0 #

  • Initial release.

example/main.dart

// 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
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// 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 iterator.read(4);

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

    // 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));
    print('\n');
  }
}

Use this package as a library

1. Depend on it

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


dependencies:
  chunked_stream: ^1.0.1

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter 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';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
57
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
79
Learn more about scoring.

We analyzed this package on Oct 21, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.5.1
  • pana: 0.12.21

Platforms

Detected platforms: Flutter, web, other

No platform restriction found in primary library package:chunked_stream/chunked_stream.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0
Dev dependencies
pedantic ^1.4.0
test ^1.5.1