Package io.bytestreams.codec.core
Class VariableItemLengthCodec<V>
java.lang.Object
io.bytestreams.codec.core.VariableItemLengthCodec<V>
- Type Parameters:
V- the type of value this codec handles
- All Implemented Interfaces:
Codec<V>,Inspectable<V>
A codec for variable-length values where the item count is encoded as a prefix.
The lengthCodec encodes and decodes the item count. The codecFactory creates a
codec for the given item count, handling both encoding and decoding. Unlike VariableByteLengthCodec, this codec does not buffer the encoded value; it writes directly to the
output stream.
Example usage:
// String with code point count prefix
Codec<String> string = Codecs.utf8(Codecs.uint8());
// List with item count prefix
Codec<List<Foo>> list = Codecs.prefixed(Codecs.uint16(),
List::size,
length -> Codecs.listOf(length, fooCodec));
-
Method Summary
Modifier and TypeMethodDescriptiondecode(InputStream input) Decodes a value from the input stream.encode(V value, OutputStream output) Encodes the given value and writes it to the output stream.Returns a structured representation of the given value.
-
Method Details
-
encode
Encodes the given value and writes it to the output stream.Computes the item count using
lengthOf, writes the count prefix, then encodes the value directly using a codec created bycodecFactory.- Specified by:
encodein interfaceCodec<V>- Parameters:
value- 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 the item count prefix, then decodes the value using a codec created by
codecFactory.- 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 the length or value is fully 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:
value- the decoded value to inspect- Returns:
- a structured representation (Map, List, or scalar)
-