Package io.bytestreams.codec.core
Class FixedListCodec<V>
java.lang.Object
io.bytestreams.codec.core.FixedListCodec<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 fixed-length lists of values.
This codec encodes and decodes exactly length items. Unlike StreamListCodec,
which reads items until EOF, this codec reads a fixed number of items from the stream.
Example usage:
// Decode exactly 3 items
Codec<List<String>> codec = Codecs.listOf(3, Codecs.ascii(5));
codec.encode(List.of("hello", "world", "abcde"), output);
List<String> values = codec.decode(input);
-
Constructor Summary
ConstructorsConstructorDescriptionFixedListCodec(Codec<V> itemCodec, int length) Creates a new fixed list codec that usesArrayListfor decoded lists.Creates a new fixed 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
-
FixedListCodec
Creates a new fixed list codec that usesArrayListfor decoded lists.- Parameters:
itemCodec- the codec for encoding/decoding individual list itemslength- the exact number of items to encode/decode (must be non-negative)- Throws:
NullPointerException- if itemCodec is nullIllegalArgumentException- if length is negative
-
FixedListCodec
Creates a new fixed list codec with a custom list factory.- Parameters:
itemCodec- the codec for encoding/decoding individual list itemslength- the exact number of items to encode/decode (must be non-negative)listFactory- a factory that creates new list instances for decoding- Throws:
NullPointerException- if any argument is nullIllegalArgumentException- if length is negative
-
-
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:
IllegalArgumentException- if the list size does not equallengthIOException- if an I/O error occurs during encoding
-
decode
Decodes a value from the input stream.Decodes exactly
lengthitems from the stream.- 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 before all items are readIOException- 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)
-