Class RecordingCodec<T>

java.lang.Object
io.bytestreams.codec.core.RecordingCodec<T>
Type Parameters:
T - the type of the decoded value
All Implemented Interfaces:
Codec<Recorded<T>>, Inspectable<Recorded<T>>

public class RecordingCodec<T> extends Object implements Codec<Recorded<T>>, Inspectable<Recorded<T>>
A codec that captures raw bytes during decoding, returning a Recorded value containing both the decoded value and the original wire bytes.

Designed for use as the outermost codec — wrapping the top-level message codec. On decode, the input stream is tee'd so all bytes read by the delegate codec are captured. On encode, the delegate codec encodes Recorded.value() directly.

Example usage:


 Codec<Message> messageCodec = Codecs.<Message>sequential(Message::new)
     .field("id", Codecs.uint16(), Message::getId, Message::setId)
     .build();

 RecordingCodec<Message> recording = new RecordingCodec<>(messageCodec);
 Recorded<Message> result = recording.decode(inputStream);
 Message msg = result.value();
 byte[] rawBytes = result.rawBytes();
 
  • Constructor Details

    • RecordingCodec

      public RecordingCodec(Codec<T> delegate)
  • Method Details

    • of

      public static <T> RecordingCodec<T> of(Codec<T> delegate)
      Creates a new recording codec wrapping the given delegate.
      Type Parameters:
      T - the decoded value type
      Parameters:
      delegate - the codec to wrap
      Returns:
      a new recording codec
    • encode

      public EncodeResult encode(Recorded<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 Recorded<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(Recorded<T> value)
      Description copied from interface: Inspectable
      Returns a structured representation of the given value.
      Specified by:
      inspect in interface Inspectable<T>
      Parameters:
      value - the decoded value to inspect
      Returns:
      a structured representation (Map, List, or scalar)