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>>

public class FixedListCodec<V> extends Object implements 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 Details

    • FixedListCodec

      public FixedListCodec(Codec<V> itemCodec, int length)
      Creates a new fixed list codec that uses ArrayList for decoded lists.
      Parameters:
      itemCodec - the codec for encoding/decoding individual list items
      length - the exact number of items to encode/decode (must be non-negative)
      Throws:
      NullPointerException - if itemCodec is null
      IllegalArgumentException - if length is negative
    • FixedListCodec

      public FixedListCodec(Codec<V> itemCodec, int length, Supplier<List<V>> listFactory)
      Creates a new fixed list codec with a custom list factory.
      Parameters:
      itemCodec - the codec for encoding/decoding individual list items
      length - 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 null
      IllegalArgumentException - if length is negative
  • Method Details

    • encode

      public EncodeResult encode(List<V> values, OutputStream output) throws IOException
      Encodes the given value and writes it to the output stream.

      Encodes each item in the list sequentially using the item codec.

      Specified by:
      encode in interface Codec<V>
      Parameters:
      values - the value to encode
      output - 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 equal length
      IOException - if an I/O error occurs during encoding
    • decode

      public List<V> decode(InputStream input) throws IOException
      Decodes a value from the input stream.

      Decodes exactly length items from the stream.

      Specified by:
      decode in interface Codec<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 read
      IOException - if an I/O error occurs during decoding
    • inspect

      public Object inspect(List<V> values)
      Description copied from interface: Inspectable
      Returns a structured representation of the given value.
      Specified by:
      inspect in interface Inspectable<V>
      Parameters:
      values - the decoded value to inspect
      Returns:
      a structured representation (Map, List, or scalar)