Class LazyCodec<V>

java.lang.Object
io.bytestreams.codec.core.LazyCodec<V>
Type Parameters:
V - the type of value this codec handles
All Implemented Interfaces:
Codec<V>, Inspectable<V>

public class LazyCodec<V> extends Object implements Codec<V>, Inspectable<V>
A codec that defers resolution to first use, enabling recursive codec definitions.

The supplier is invoked at most once and the result is cached. This codec is thread-safe — multiple threads can safely share the same instance.

Example usage:


 Codec<TreeNode>[] holder = new Codec[1];
 holder[0] = Codecs.pair(
     Codecs.utf8(10),
     Codecs.prefixed(Codecs.uint8(), Codecs.listOf(Codecs.lazy(() -> holder[0])))
 ).as(TreeNode::new, n -> n.name, n -> n.children);
 
  • Method Details

    • encode

      public EncodeResult encode(V 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<V>
      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 V decode(InputStream input) throws IOException
      Description copied from interface: Codec
      Decodes a value from the input stream.
      Specified by:
      decode in interface Codec<V>
      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(V value)
      Description copied from interface: Inspectable
      Returns a structured representation of the given value.
      Specified by:
      inspect in interface Inspectable<V>
      Parameters:
      value - the decoded value to inspect
      Returns:
      a structured representation (Map, List, or scalar)