Package io.bytestreams.codec.core
Class StreamListCodec<V>
java.lang.Object
io.bytestreams.codec.core.StreamListCodec<V>
- Type Parameters:
V- the type of values in the list
- All Implemented Interfaces:
Codec<List<V>>,Inspectable<List<V>>
A codec for encoding and decoding lists of values by reading items until EOF.
This codec encodes each item in the list sequentially using the item codec. When decoding, it reads items from the stream until EOF is reached.
Unlike FixedListCodec, which decodes a fixed number of items, this codec reads all
available items. This makes it suitable for use as a value codec inside VariableByteLengthCodec, where the stream is bounded by the length prefix.
Example usage:
// Using default ArrayList
Codec<List<String>> codec = Codecs.listOf(Codecs.ascii(5));
codec.encode(List.of("hello", "world"), output);
List<String> values = codec.decode(input);
-
Constructor Summary
ConstructorsConstructorDescriptionStreamListCodec(Codec<V> itemCodec) Creates a new stream list codec that usesArrayListfor decoded lists.Creates a new stream list codec with a custom list factory. -
Method Summary
Modifier and TypeMethodDescriptiondecode(InputStream input) Decodes a value from the input stream.encode(List<V> values, OutputStream output) Encodes the given value and writes it to the output stream.Returns a structured representation of the given value.
-
Constructor Details
-
StreamListCodec
Creates a new stream list codec that usesArrayListfor decoded lists.- Parameters:
itemCodec- the codec for encoding/decoding individual list items- Throws:
NullPointerException- if itemCodec is null
-
StreamListCodec
Creates a new stream list codec with a custom list factory.- Parameters:
itemCodec- the codec for encoding/decoding individual list itemslistFactory- a factory that creates new list instances for decoding- Throws:
NullPointerException- if any argument is null
-
-
Method Details
-
encode
Encodes the given value and writes it to the output stream.Encodes each item in the list sequentially using the item codec.
- Specified by:
encodein interfaceCodec<V>- Parameters:
values- the value to encodeoutput- the output stream to write the encoded bytes to- Returns:
- the encode result containing logical count and bytes written
- Throws:
IOException- if an I/O error occurs during encoding
-
decode
Decodes a value from the input stream.Decodes items from the stream until EOF is reached.
- Specified by:
decodein interfaceCodec<V>- Parameters:
input- the input stream to read the encoded bytes from- Returns:
- the decoded value
- Throws:
EOFException- if the stream ends mid-itemIOException- if an I/O error occurs during decoding
-
inspect
Description copied from interface:InspectableReturns a structured representation of the given value.- Specified by:
inspectin interfaceInspectable<V>- Parameters:
values- the decoded value to inspect- Returns:
- a structured representation (Map, List, or scalar)
-