Interface Codec<T>
- Type Parameters:
T- the type this codec converts
- All Known Implementing Classes:
Base32Codec, Base64Codec, ChecksumCodec, CompressionStreamCodec, DeflateCodec, EnvelopeCodec, GzipCodec, HexCodec, Lz4Codec, StringCodec, ZstdCodec
T to and from byte[].
Implementations must be symmetric: decode(encode(value)) yields a value equal to the
original. Codecs are expected to be thread-safe.
Failures. Every failure encode or decode reports is a CodecException for the codecs this library provides; a codec derived with xmap(Function, Function) also
propagates whatever the caller's conversion functions throw. More precisely, a
CodecException is one of four families keyed to what the caller does next: InvalidValueException (fix the value), InvalidPayloadException (quarantine the payload),
UnsupportedFormatException (hold it for a newer reader), TransientCodecException
(retry). Implementations wrap their underlying library's exception as the cause rather than
letting it escape. A null argument is a programmer error and throws NullPointerException; see nullSafe() for the explicit pass-through.
-
Method Summary
Modifier and TypeMethodDescriptionLayers a byte-level transform (compression, encryption, etc.) onto this codec.decode(byte[] bytes) Decodes bytes back into a value.byte[]Encodes a value to bytes.nullSafe()Wraps this codec so thatnullpasses straight through in both directions:encode(null)returnsnullanddecode(null)returnsnull, without consulting this codec.default <U> Codec<U> Derives a codec for another type from this one, given a conversion in each direction.
-
Method Details
-
encode
Encodes a value to bytes.- Parameters:
value- the value to encode- Returns:
- the encoded bytes
- Throws:
InvalidValueException- if the value cannot be encoded by this codecTransientCodecException- if something the codec depends on failedNullPointerException- ifvalueis null and this codec does not accept null
-
decode
Decodes bytes back into a value.- Parameters:
bytes- the bytes to decode- Returns:
- the decoded value
- Throws:
InvalidPayloadException- if the bytes are malformed, corrupt, or forgedUnsupportedFormatException- if the bytes are well-formed but in a format this codec cannot readTransientCodecException- if something the codec depends on failedNullPointerException- ifbytesis null and this codec does not accept null
-
andThen
Layers a byte-level transform (compression, encryption, etc.) onto this codec.The returned codec applies
transform.encodeafter this codec'sencode, andtransform.decodebefore this codec'sdecode. Chained transforms compose outward on encode and unwind automatically in reverse order on decode:EnvelopeCodec envelope = EnvelopeCodec.builder(provider).build(); Codec<Person> codec = factory.create(Person.class).andThen(new GzipCodec()).andThen(envelope);- Parameters:
transform- the byte transform to apply after encoding (and invert before decoding)- Returns:
- a codec of the same type with the transform applied
- Throws:
NullPointerException- iftransformis null
-
nullSafe
Wraps this codec so thatnullpasses straight through in both directions:encode(null)returnsnullanddecode(null)returnsnull, without consulting this codec. Every other value is delegated unchanged.Whether a bare codec accepts
nullis up to its implementation — a JSON backend encodes it as the literalnull, the transforms reject it — so this is the explicit form for integrations whose contract treatsnullas "absent", such as cache serializers that are handednullon a miss.- Returns:
- a codec that maps
nulltonulland otherwise behaves as this one
-
xmap
default <U> Codec<U> xmap(Function<? super T, ? extends U> forward, Function<? super U, ? extends T> backward) Derives a codec for another type from this one, given a conversion in each direction.Where
andThen(Codec)wraps the bytes side of a codec, this wraps the value side: the returned codec encodes aUby converting it to aTwithbackwardand then encoding it here, and decodes by decoding aThere and converting it withforward. It is the tool for the domain-type-versus-wire-type split — a backend that only serializes generated or registered classes, wrapped as the type the application actually uses:Codec<Person> codec = factory.create(PersonProto.class).xmap(Person::fromProto, Person::toProto);Exceptions thrown by either function propagate unchanged.
- Type Parameters:
U- the derived codec's type- Parameters:
forward- converts a decodedTto aUbackward- converts aUto theTthis codec encodes- Returns:
- a codec for
U - Throws:
NullPointerException- if either function is null
-