ByteArrayInputStream.fromRange constructor
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 a slice of the buffer
starting at offset
and spanning length
bytes.
This is useful when you want to expose only a portion of a larger buffer as an input stream.
Throws a RangeError if the slice is out of bounds.
Implementation
ByteArrayInputStream.fromRange(Uint8List buffer, int offset, int length)
: _buffer = buffer.sublist(offset, offset + length);