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>
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.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classBuilder for constructing a SequentialObjectCodec. -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> SequentialObjectCodec.Builder<T> Creates a new builder for constructing a SequentialObjectCodec.decode(InputStream input) Decodes a value from the input stream.encode(T value, OutputStream output) Encodes the given value and writes it to the output stream.Returns a structured representation of the given value.
-
Method Details
-
builder
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
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:
object- the decoded value to inspect- Returns:
- a structured representation (Map, List, or scalar)
-