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

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

    • StreamListCodec

      public StreamListCodec(Codec<V> itemCodec)
      Creates a new stream list codec that uses ArrayList for decoded lists.
      Parameters:
      itemCodec - the codec for encoding/decoding individual list items
      Throws:
      NullPointerException - if itemCodec is null
    • StreamListCodec

      public StreamListCodec(Codec<V> itemCodec, Supplier<List<V>> listFactory)
      Creates a new stream list codec with a custom list factory.
      Parameters:
      itemCodec - the codec for encoding/decoding individual list items
      listFactory - a factory that creates new list instances for decoding
      Throws:
      NullPointerException - if any argument is null
  • 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:
      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 items from the stream until EOF is reached.

      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 mid-item
      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)