Class SequentialObjectCodec<T>

java.lang.Object
io.bytestreams.codec.core.SequentialObjectCodec<T>
Type Parameters:
T - the type of object to encode/decode
All Implemented Interfaces:
Codec<T>, Inspectable<T>

public class SequentialObjectCodec<T> extends Object implements Codec<T>, Inspectable<T>
A codec for objects with sequential fields.

Each field is encoded/decoded in the order it was added to the builder. Optional fields use a predicate to determine presence - if the predicate returns false, the field is skipped during both encoding and decoding.

Example usage:

SequentialObjectCodec<Message> codec = Codecs.<Message>sequential(Message::new)
    .field("id", idCodec, Message::getId, Message::setId)
    .field("content", contentCodec, Message::getContent, Message::setContent)
    .field("tag", tagCodec, Message::getTag, Message::setTag,
           msg -> msg.getId() > 0)  // optional, based on earlier field
    .build();

Fields are written straight to the output as they are encoded, so a failure partway through leaves the fields already written on the stream. Codecs that append something derived from the value — terminated, checked and prefixed — buffer instead, since they cannot know what to append until the value is complete. Buffering every object would double peak memory, so wrap this codec in one of those, or write to a buffer of your own, where a partly written record would matter.

  • Method Details

    • builder

      public static <T> SequentialObjectCodec.Builder<T> builder(Supplier<T> factory)
      Creates a new builder for constructing a SequentialObjectCodec.
      Type Parameters:
      T - the type of object to encode/decode
      Parameters:
      factory - factory that creates new instances during decoding
      Returns:
      a new builder
    • encode

      public EncodeResult encode(T value, OutputStream output) throws IOException
      Description copied from interface: Codec
      Encodes the given value and writes it to the output stream.
      Specified by:
      encode in interface Codec<T>
      Parameters:
      value - 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 T decode(InputStream input) throws IOException
      Description copied from interface: Codec
      Decodes a value from the input stream.
      Specified by:
      decode in interface Codec<T>
      Parameters:
      input - the input stream to read the encoded bytes from
      Returns:
      the decoded value
      Throws:
      IOException - if an I/O error occurs during decoding
    • inspect

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