Package io.bytestreams.codec.core
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptiondecode(InputStream input) Decodes a value from the input stream.encode(Recorded<T> value, OutputStream output) Encodes the given value and writes it to the output stream.Returns a structured representation of the given value.static <T> RecordingCodec<T> Creates a new recording codec wrapping the given delegate.
-
Constructor Details
-
RecordingCodec
-
-
Method Details
-
of
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
Description copied from interface:CodecEncodes the given value and writes it to the output stream.- Specified by:
encodein interfaceCodec<T>- Parameters:
value- the value to encodeoutput- 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
Description copied from interface:CodecDecodes a value from the input stream.- Specified by:
decodein interfaceCodec<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
Description copied from interface:InspectableReturns a structured representation of the given value.- Specified by:
inspectin interfaceInspectable<T>- Parameters:
value- the decoded value to inspect- Returns:
- a structured representation (Map, List, or scalar)
-