ByteArrayInputStream constructor

ByteArrayInputStream(
  1. Uint8List _buffer
)

A byte-based input stream that reads from a Uint8List buffer.

ByteArrayInputStream provides a non-blocking, in-memory implementation of the InputStream interface over a Dart Uint8List. It's particularly useful for:

  • Testing input-based logic without relying on external files or sockets.
  • Wrapping encoded or serialized in-memory data for streaming processing.
  • Implementing low-latency input pipelines (e.g., compression, decryption).

Unlike traditional Streams in Dart, this class offers fine-grained control over the byte cursor, with support for methods like mark() and reset() if implemented in the extended InputStream class.


🔧 Example:

final bytes = Uint8List.fromList([104, 101, 108, 108, 111]); // "hello"
final inputStream = ByteArrayInputStream(bytes);

final char1 = inputStream.read(); // 104 (h)
final char2 = inputStream.read(); // 101 (e)

✅ Use Cases:

  • Parsing binary data from a known byte array.
  • Emulating I/O in memory-constrained or test environments.
  • Providing a readable wrapper for encoded API payloads.

Creates a ByteArrayInputStream that reads from the entire buffer.

The internal read cursor is set to the beginning of the buffer.

Implementation

ByteArrayInputStream(this._buffer);