ByteArrayInputStream class
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.
- Inheritance
-
- Object
- InputStream
- ByteArrayInputStream
Constructors
- ByteArrayInputStream(Uint8List _buffer)
- A byte-based input stream that reads from a Uint8List buffer.
- ByteArrayInputStream.fromRange(Uint8List buffer, int offset, int length)
- A byte-based input stream that reads from a Uint8List buffer.
Properties
- hashCode β int
-
The hash code for this object.
no setterinherited
- isAtEnd β bool
-
Returns true if all data has been read.
no setter
- isClosed β bool
-
Returns
true
if this stream has been closed.no setterinherited - position β int
-
Returns the current position in the buffer.
no setter
- runtimeType β Type
-
A representation of the runtime type of the object.
no setterinherited
- size β int
-
Returns the size of the buffer.
no setter
Methods
-
available(
) β Future< int> -
Returns an estimate of the number of bytes that can be read (or skipped over)
from this input stream without blocking by the next invocation of a method
for this input stream.
override
-
checkClosed(
) β void -
Checks if the stream is closed and throws an exception if it is.
inherited
-
close(
) β Future< void> -
Closes this input stream and releases any system resources associated
with the stream.
inherited
-
mark(
int readLimit) β void -
Marks the current position in this input stream.
override
-
markSupported(
) β bool -
Tests if this input stream supports the mark and reset methods.
override
-
noSuchMethod(
Invocation invocation) β dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
read(
List< int> b, [int offset = 0, int? length]) β Future<int> -
Reads some number of bytes from the input stream and stores them into
the buffer array
b
.override -
readAll(
) β Future< Uint8List> -
Reads all remaining bytes from the input stream.
inherited
-
readByte(
) β Future< int> -
Reads the next byte of data from the input stream.
override
-
readFully(
int length) β Future< Uint8List> -
Reads exactly
length
bytes from the input stream.inherited -
reset(
) β Future< void> -
Repositions this stream to the position at the time the mark method
was last called on this input stream.
override
-
skip(
int n) β Future< int> -
Skips over and discards
n
bytes of data from this input stream.override -
toString(
) β String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) β bool -
The equality operator.
inherited