ZetCode

Dart ReadPipe

last modified April 4, 2025

The ReadPipe class in Dart provides an efficient way to read data from streams. It's useful for processing continuous data sources like network sockets or files.

ReadPipe manages stream subscriptions and data buffering internally, simplifying stream consumption. It's part of Dart's asynchronous programming model for handling I/O operations.

Basic Definition

ReadPipe is a utility class for reading data from streams in chunks. It provides methods for both synchronous and asynchronous data reading patterns.

Key features include buffered reading, stream management, and flexible data consumption. It's particularly efficient for processing large streams of data.

Basic ReadPipe Usage

This example shows basic stream reading using ReadPipe.

main.dart
import 'dart:async';
import 'dart:io';

Future<void> main() async {
  var pipe = ReadPipe(Stream.fromIterable(['Hello', ' ', 'World']));
  
  var data = await pipe.read();
  print(data); // 'Hello World'
}

We create a ReadPipe from a simple stream and read all available data. The pipe handles stream subscription and data collection automatically.

$ dart main.dart
Hello World

Reading in Chunks

This example demonstrates reading stream data in fixed-size chunks.

main.dart
import 'dart:async';

Future<void> main() async {
  var stream = Stream.fromIterable(['Dart', ' ', 'is', ' ', 'awesome!']);
  var pipe = ReadPipe(stream);
  
  while (true) {
    var chunk = await pipe.readBytes(3);
    if (chunk.isEmpty) break;
    print('Chunk: $chunk');
  }
}

We read the stream in 3-byte chunks. The pipe buffers data between reads and handles partial chunks automatically. This is useful for fixed-size protocols.

$ dart main.dart
Chunk: Dar
Chunk: t i
Chunk: s a
Chunk: wes
Chunk: ome
Chunk: !

Handling Binary Data

This example shows reading binary data from a file stream.

main.dart
import 'dart:async';
import 'dart:io';

Future<void> main() async {
  var file = File('data.bin');
  var pipe = ReadPipe(file.openRead());
  
  var header = await pipe.readBytes(4);
  print('Header: $header');
  
  var body = await pipe.read();
  print('Body length: ${body.length} bytes');
}

We read a binary file with ReadPipe, first extracting a 4-byte header then reading the remaining content. The pipe handles the byte stream efficiently.

$ dart main.dart
Header: [137, 80, 78, 71]
Body length: 1024 bytes

Error Handling

This example demonstrates error handling with ReadPipe.

main.dart
import 'dart:async';

Future<void> main() async {
  var controller = StreamController();
  var pipe = ReadPipe(controller.stream);
  
  controller.add('Data');
  controller.addError('Stream error');
  
  try {
    await pipe.read();
  } catch (e) {
    print('Error: $e');
  } finally {
    await controller.close();
  }
}

We simulate a stream error and show how ReadPipe propagates stream errors to the reader. Proper error handling is essential for robust stream processing.

$ dart main.dart
Error: Stream error

Combining with Transformers

This example shows using ReadPipe with stream transformers.

main.dart
import 'dart:async';
import 'dart:convert';

Future<void> main() async {
  var stream = Stream.fromIterable(['{"name":', ' "John", "age": 30}']);
  var jsonStream = stream.transform(utf8.decoder).transform(json.decoder);
  var pipe = ReadPipe(jsonStream);
  
  var data = await pipe.read();
  print('Name: ${data['name']}');
  print('Age: ${data['age']}');
}

We use ReadPipe with JSON decoding transformers. The pipe works seamlessly with transformed streams, making complex processing pipelines easier.

$ dart main.dart
Name: John
Age: 30

Best Practices

Source

Dart ReadPipe Documentation

This tutorial covered Dart's ReadPipe class with practical examples showing basic usage, error handling, and integration with stream transformers.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all Dart tutorials.