ByteStream class
A stream of bytes similar to Java's InputStream/OutputStream.
This class provides a stream-based interface for reading and writing bytes,
wrapping Dart's Stream<List<int>>
with Java-like methods.
Example usage:
ByteStream stream = ByteStream.fromList([72, 101, 108, 108, 111]);
List<int> data = await stream.readAll();
print(String.fromCharCodes(data)); // "Hello"
Constructors
- ByteStream.empty()
-
Creates an empty ByteStream that can be written to.
factory
-
ByteStream.fromBroadcast(List<
int> bytes) -
Creates a ByteStream from a list of bytes that can be broadcasted.
factory
-
ByteStream.fromList(List<
int> bytes) -
Creates a ByteStream from a list of bytes.
factory
-
ByteStream.fromStream(Stream<
List< stream)int> > -
Creates a ByteStream from a stream of bytes.
factory
- ByteStream.fromString(String str)
-
Creates a ByteStream from a string.
factory
- ByteStream.fromUint8List(Uint8List data)
-
Creates a ByteStream from a Uint8List.
factory
-
ByteStream.fromUint8ListStream(Stream<
Uint8List> stream) -
Creates a ByteStream from a Stream of Uint8List.
factory
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
-
length
→ Future<
int> -
Returns the length of the stream (consumes the stream).
no setter
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
-
stream
→ Stream<
List< int> > -
Returns the underlying stream.
no setter
Methods
-
bytesToString(
[Encoding encoding = utf8]) → Future< String> -
Converts the byte stream to a string using UTF-8 or the given
encoding
. -
close(
) → void - Closes the stream for writing.
-
listen(
void onData(List< int> )?, {Function? onError, void onDone()?, bool? cancelOnError}) → StreamSubscription<List< int> > - Listens to the stream with the provided callback.
-
map(
List< int> mapper(List<int> )) → ByteStream - Maps each chunk of bytes using the provided function.
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
read(
int count) → Future< List< int> > - Reads a specific number of bytes from the stream.
-
readAll(
) → Future< List< int> > - Reads all bytes from the stream.
-
readAllAsString(
) → Future< String> - Reads all bytes and converts them to a string.
-
readAllAsUint8List(
) → Future< Uint8List> - Reads all bytes and returns them as a Uint8List.
-
skip(
int count) → ByteStream -
Skips the first
count
chunks. -
take(
int count) → ByteStream -
Takes only the first
count
chunks. -
toBytes(
) → Future< Uint8List> - Collects all emitted chunks into a single byte array.
-
toList(
) → Future< List< int> > -
Converts the stream to a single
Future<List<int>>
. -
toString(
) → String -
Returns a string representation of this ByteStream.
override
-
transform<
T> (StreamTransformer< List< transformer) → ByteStreamint> , T> - Transforms this ByteStream using the provided transformer.
-
where(
bool predicate(List< int> )) → ByteStream - Filters chunks of bytes using the provided predicate.
-
write(
List< int> bytes) → void - Writes bytes to the stream (only if created with empty constructor).
-
writeByte(
int byte) → void - Writes a single byte to the stream.
-
writeString(
String str) → void - Writes a string to the stream as bytes.
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited
Static Methods
-
concat(
List< ByteStream> streams) → ByteStream - Static utility methods Concatenates multiple ByteStreams into one.
-
fromArrays(
List< List< byteArrays) → ByteStreamint> > - Creates a ByteStream from multiple byte arrays.
-
repeat(
List< int> bytes, [int? count]) → ByteStream - Creates a ByteStream that repeats the given bytes.