Class Codecs
This facade provides factory methods for number, string, hex, binary, boolean, list, composition, and object codecs.
Example usage:
// Number codecs
Codec<Integer> u8 = Codecs.uint8();
Codec<Long> i64 = Codecs.int64();
// String codecs
Codec<String> ascii = Codecs.ascii(10);
Codec<String> utf8 = Codecs.utf8();
// Constant bytes
Codec<byte[]> magic = Codecs.constant(new byte[] {0x4D, 0x5A});
// Numeric string codecs
Codec<Integer> bcd = Codecs.bcdInt(4);
Codec<Integer> asciiNum = Codecs.asciiInt(4);
Codec<Long> ebcdicNum = Codecs.ebcdicLong(10);
// Composition
Codec<String> prefixed = Codecs.prefixed(Codecs.uint16(), Codecs.utf8());
Codec<List<Integer>> list = Codecs.listOf(5, Codecs.uint8());
// Object codecs
SequentialObjectCodec<Msg> codec = Codecs.<Msg>sequential(Msg::new)
.field("id", Codecs.int32(), Msg::getId, Msg::setId)
.build();
-
Method Summary
Modifier and TypeMethodDescriptionascii()Creates a variable-length US-ASCII string codec.ascii(int length) Creates a fixed-length US-ASCII string codec.Creates a variable-length US-ASCII string codec where the code point count is encoded as a prefix.asciiInt(int digits) Creates a fixed-length ASCII numeric codec that decodes toInteger.asciiLong(int digits) Creates a fixed-length ASCII numeric codec that decodes toLong.bcdInt(int digits) Creates a fixed-length BCD (Binary Coded Decimal) codec that decodes toInteger.bcdLong(int digits) Creates a fixed-length BCD (Binary Coded Decimal) codec that decodes toLong.static Codec<byte[]> binary()Creates a variable-length binary codec that reads all remaining bytes from the stream.static Codec<byte[]> binary(int length) Creates a fixed-length binary codec.static Codec<byte[]> Creates a variable-length binary codec where the byte count is encoded as a prefix.bool()Creates a boolean codec (single byte: 0x00 = false, 0x01 = true).static <V> ChoiceCodec.Builder<V> Creates a new builder for a choice codec that encodes discriminated unions.static Codec<byte[]> constant(byte[] expected) Creates a constant codec that always writes the expected bytes on encode (ignoring the input value) and validates that the bytes match on decode.ebcdic()Creates a variable-length EBCDIC (IBM1047) string codec.ebcdic(int length) Creates a fixed-length EBCDIC (IBM1047) string codec.Creates a variable-length EBCDIC (IBM1047) string codec where the code point count is encoded as a prefix.ebcdicInt(int digits) Creates a fixed-length EBCDIC numeric codec that decodes toInteger.ebcdicLong(int digits) Creates a fixed-length EBCDIC numeric codec that decodes toLong.float32()Creates a codec for float values (IEEE 754 single-precision, 4 bytes).float64()Creates a codec for double values (IEEE 754 double-precision, 8 bytes).hex()Creates a variable-length hex string codec.hex(int length) Creates a fixed-length hex string codec.Creates a variable-length hex string codec where the hex digit count is encoded as a prefix.int16()Creates a codec for signed short values (-32768 to 32767), encoded as 2-byte big-endian binary.int32()Creates a codec for signed integer values (-2147483648 to 2147483647), encoded as 4-byte big-endian binary.int64()Creates a codec for signed long values, encoded as 8-byte big-endian binary.latin1()Creates a variable-length ISO-8859-1 (Latin-1) string codec.latin1(int length) Creates a fixed-length ISO-8859-1 (Latin-1) string codec.Creates a variable-length ISO-8859-1 (Latin-1) string codec where the code point count is encoded as a prefix.static <V> Codec<V> Creates a lazy codec that defers resolution to first use, enabling recursive definitions.Creates a fixed-length list codec that encodes/decodes exactlylengthitems.Creates a variable-length list codec where the item count is encoded as a prefix.Creates a stream list codec that reads items until EOF.Creates a variable-length string codec with the specified charset.Creates a fixed-length string codec with the specified charset.Creates a variable-length string codec where the code point count is encoded as a prefix.static <A,B> PairCodec <A, B> Creates a pair codec that encodes and decodes two values sequentially.static <V> Codec<V> Creates a variable-length codec where the byte count is encoded as a prefix.static <V> Codec<V> prefixed(Codec<Integer> lengthCodec, ToIntFunction<V> lengthOf, IntFunction<Codec<V>> codecFactory) Creates a variable-length codec where the item count is encoded as a prefix.static <T> SequentialObjectCodec.Builder<T> sequential(Supplier<T> factory) Creates a new builder for a sequential object codec.static <K> TaggedObjectCodec.Builder<TaggedData<K>, K> Creates a new builder for a tagged object codec usingTaggedData.static <T extends Tagged<T,K>, K>
TaggedObjectCodec.Builder<T, K> Creates a new builder for a tagged object codec.static <A,B, C> TripleCodec <A, B, C> Creates a triple codec that encodes and decodes three values sequentially.uint16()Creates a codec for unsigned short values (0 to 65535), encoded as 2-byte big-endian binary.uint32()Creates a codec for unsigned integer values (0 to 4294967295), encoded as 4-byte big-endian binary.uint8()Creates a codec for unsigned byte values (0 to 255), encoded as 1-byte binary.utf8()Creates a variable-length UTF-8 string codec.utf8(int length) Creates a fixed-length UTF-8 string codec.Creates a variable-length UTF-8 string codec where the code point count is encoded as a prefix.
-
Method Details
-
uint8
Creates a codec for unsigned byte values (0 to 255), encoded as 1-byte binary.- Returns:
- a new unsigned byte codec
-
uint16
Creates a codec for unsigned short values (0 to 65535), encoded as 2-byte big-endian binary.- Returns:
- a new unsigned short codec
-
uint32
Creates a codec for unsigned integer values (0 to 4294967295), encoded as 4-byte big-endian binary.- Returns:
- a new unsigned integer codec
-
int16
Creates a codec for signed short values (-32768 to 32767), encoded as 2-byte big-endian binary.- Returns:
- a new signed short codec
-
int32
Creates a codec for signed integer values (-2147483648 to 2147483647), encoded as 4-byte big-endian binary.- Returns:
- a new signed integer codec
-
int64
Creates a codec for signed long values, encoded as 8-byte big-endian binary.- Returns:
- a new signed long codec
-
float32
Creates a codec for float values (IEEE 754 single-precision, 4 bytes).- Returns:
- a new float codec
-
float64
Creates a codec for double values (IEEE 754 double-precision, 8 bytes).- Returns:
- a new double codec
-
ascii
Creates a fixed-length US-ASCII string codec.- Parameters:
length- the number of code points- Returns:
- a new codec
-
ascii
Creates a variable-length US-ASCII string codec.- Returns:
- a new codec
-
ascii
Creates a variable-length US-ASCII string codec where the code point count is encoded as a prefix.- Parameters:
lengthCodec- the codec for the code point count prefix- Returns:
- a new codec
-
utf8
Creates a fixed-length UTF-8 string codec.- Parameters:
length- the number of code points- Returns:
- a new codec
-
utf8
Creates a variable-length UTF-8 string codec.- Returns:
- a new codec
-
utf8
Creates a variable-length UTF-8 string codec where the code point count is encoded as a prefix.- Parameters:
lengthCodec- the codec for the code point count prefix- Returns:
- a new codec
-
latin1
Creates a fixed-length ISO-8859-1 (Latin-1) string codec.- Parameters:
length- the number of code points- Returns:
- a new codec
-
latin1
Creates a variable-length ISO-8859-1 (Latin-1) string codec.- Returns:
- a new codec
-
latin1
Creates a variable-length ISO-8859-1 (Latin-1) string codec where the code point count is encoded as a prefix.- Parameters:
lengthCodec- the codec for the code point count prefix- Returns:
- a new codec
-
ebcdic
Creates a fixed-length EBCDIC (IBM1047) string codec.- Parameters:
length- the number of code points- Returns:
- a new codec
-
ebcdic
Creates a variable-length EBCDIC (IBM1047) string codec.- Returns:
- a new codec
-
ebcdic
Creates a variable-length EBCDIC (IBM1047) string codec where the code point count is encoded as a prefix.- Parameters:
lengthCodec- the codec for the code point count prefix- Returns:
- a new codec
-
ofCharset
Creates a fixed-length string codec with the specified charset.- Parameters:
charset- the charsetlength- the number of code points- Returns:
- a new codec
-
ofCharset
Creates a variable-length string codec with the specified charset.- Parameters:
charset- the charset- Returns:
- a new codec
-
ofCharset
Creates a variable-length string codec where the code point count is encoded as a prefix.For single-byte charsets, uses
String.length()for the count (O(1)). For multibyte charsets, usesStrings.codePointCount(java.lang.String)(O(n)).- Parameters:
charset- the charsetlengthCodec- the codec for the code point count prefix- Returns:
- a new codec
-
hex
Creates a fixed-length hex string codec. Odd-length values are left-padded with '0' to align to byte boundaries.- Parameters:
length- the number of hex digits- Returns:
- a new codec
-
hex
Creates a variable-length hex string codec. Odd-length values are left-padded with '0' to align to byte boundaries.- Returns:
- a new codec
-
hex
Creates a variable-length hex string codec where the hex digit count is encoded as a prefix. Odd-length values are left-padded with '0' to align to byte boundaries.- Parameters:
lengthCodec- the codec for the hex digit count prefix- Returns:
- a new codec
-
bcdInt
Creates a fixed-length BCD (Binary Coded Decimal) codec that decodes toInteger.Each byte holds two decimal digits (0–9) in its high and low nibbles. Odd-length digit counts are left-padded with a zero nibble.
Codec<Integer> codec = Codecs.bcdInt(4); codec.encode(42, out); // writes 0x00, 0x42 codec.decode(in); // reads 0x00, 0x42 → 42- Parameters:
digits- the number of BCD digits (1 to 9)- Returns:
- a new BCD integer codec
- Throws:
IllegalArgumentException- if digits is not between 1 and 9
-
bcdLong
Creates a fixed-length BCD (Binary Coded Decimal) codec that decodes toLong.Each byte holds two decimal digits (0–9) in its high and low nibbles. Odd-length digit counts are left-padded with a zero nibble.
Codec<Long> codec = Codecs.bcdLong(10); codec.encode(1234567890L, out); // writes 0x12, 0x34, 0x56, 0x78, 0x90 codec.decode(in); // reads 0x12, 0x34, 0x56, 0x78, 0x90 → 1234567890- Parameters:
digits- the number of BCD digits (1 to 18)- Returns:
- a new BCD long codec
- Throws:
IllegalArgumentException- if digits is not between 1 and 18
-
asciiInt
Creates a fixed-length ASCII numeric codec that decodes toInteger.The value is encoded as a zero-padded decimal string in US-ASCII.
Codec<Integer> codec = Codecs.asciiInt(4); codec.encode(42, out); // writes "0042" in ASCII codec.decode(in); // reads "0042" in ASCII → 42- Parameters:
digits- the number of digits (1 to 9)- Returns:
- a new ASCII integer codec
- Throws:
IllegalArgumentException- if digits is not between 1 and 9
-
asciiLong
Creates a fixed-length ASCII numeric codec that decodes toLong.The value is encoded as a zero-padded decimal string in US-ASCII.
Codec<Long> codec = Codecs.asciiLong(10); codec.encode(1234567890L, out); // writes "1234567890" in ASCII codec.decode(in); // reads "1234567890" in ASCII → 1234567890- Parameters:
digits- the number of digits (1 to 18)- Returns:
- a new ASCII long codec
- Throws:
IllegalArgumentException- if digits is not between 1 and 18
-
ebcdicInt
Creates a fixed-length EBCDIC numeric codec that decodes toInteger.The value is encoded as a zero-padded decimal string in EBCDIC (IBM1047).
Codec<Integer> codec = Codecs.ebcdicInt(4); codec.encode(42, out); // writes "0042" in EBCDIC codec.decode(in); // reads "0042" in EBCDIC → 42- Parameters:
digits- the number of digits (1 to 9)- Returns:
- a new EBCDIC integer codec
- Throws:
IllegalArgumentException- if digits is not between 1 and 9
-
ebcdicLong
Creates a fixed-length EBCDIC numeric codec that decodes toLong.The value is encoded as a zero-padded decimal string in EBCDIC (IBM1047).
Codec<Long> codec = Codecs.ebcdicLong(10); codec.encode(1234567890L, out); // writes "1234567890" in EBCDIC codec.decode(in); // reads "1234567890" in EBCDIC → 1234567890- Parameters:
digits- the number of digits (1 to 18)- Returns:
- a new EBCDIC long codec
- Throws:
IllegalArgumentException- if digits is not between 1 and 18
-
choice
Creates a new builder for a choice codec that encodes discriminated unions.The class tag codec determines which alternative to use. The tag-to-class mapping is handled externally via
xmap.- Type Parameters:
V- the base type of the discriminated union- Parameters:
classCodec- the codec for the class tag- Returns:
- a new choice codec builder
-
lazy
Creates a lazy codec that defers resolution to first use, enabling recursive definitions.- Type Parameters:
V- the value type- Parameters:
supplier- supplies the codec on first use- Returns:
- a new lazy codec
-
pair
Creates a pair codec that encodes and decodes two values sequentially.- Type Parameters:
A- the first value typeB- the second value type- Parameters:
first- the codec for the first valuesecond- the codec for the second value- Returns:
- a new pair codec
-
triple
Creates a triple codec that encodes and decodes three values sequentially.- Type Parameters:
A- the first value typeB- the second value typeC- the third value type- Parameters:
first- the codec for the first valuesecond- the codec for the second valuethird- the codec for the third value- Returns:
- a new triple codec
-
prefixed
Creates a variable-length codec where the byte count is encoded as a prefix.- Type Parameters:
V- the value type- Parameters:
lengthCodec- the codec for the byte count prefixvalueCodec- the codec for the value- Returns:
- a new variable byte-length codec
-
prefixed
public static <V> Codec<V> prefixed(Codec<Integer> lengthCodec, ToIntFunction<V> lengthOf, IntFunction<Codec<V>> codecFactory) Creates a variable-length codec where the item count is encoded as a prefix.- Type Parameters:
V- the value type- Parameters:
lengthCodec- the codec for the item count prefixlengthOf- a function that returns the item count for a given valuecodecFactory- a function that creates a codec for the given item count- Returns:
- a new variable item-length codec
-
listOf
Creates a fixed-length list codec that encodes/decodes exactlylengthitems.- Type Parameters:
V- the item type- Parameters:
length- the exact number of itemsitemCodec- the codec for individual list items- Returns:
- a new fixed list codec
-
listOf
Creates a stream list codec that reads items until EOF.- Type Parameters:
V- the item type- Parameters:
itemCodec- the codec for individual list items- Returns:
- a new stream list codec
-
listOf
Creates a variable-length list codec where the item count is encoded as a prefix.- Type Parameters:
V- the item type- Parameters:
lengthCodec- the codec for the item count prefixitemCodec- the codec for individual list items- Returns:
- a new codec
-
sequential
Creates a new builder for a sequential object codec.- Type Parameters:
T- the type of object to encode/decode- Parameters:
factory- factory that creates new instances during decoding- Returns:
- a new sequential object codec builder
-
tagged
public static <T extends Tagged<T,K>, TaggedObjectCodec.Builder<T,K> K> tagged(Supplier<T> factory, Codec<K> tagCodec) Creates a new builder for a tagged object codec.- Type Parameters:
T- the type of object to encode/decodeK- the tag key type- Parameters:
factory- factory that creates new instances during decodingtagCodec- the codec used to read and write tags- Returns:
- a new tagged object codec builder
-
tagged
Creates a new builder for a tagged object codec usingTaggedData.- Type Parameters:
K- the tag key type- Parameters:
tagCodec- the codec used to read and write tags- Returns:
- a new tagged object codec builder
-
binary
Creates a fixed-length binary codec.- Parameters:
length- the number of bytes- Returns:
- a new binary codec
-
binary
Creates a variable-length binary codec that reads all remaining bytes from the stream.- Returns:
- a new codec
-
binary
Creates a variable-length binary codec where the byte count is encoded as a prefix.- Parameters:
lengthCodec- the codec for the byte count prefix- Returns:
- a new codec
-
constant
Creates a constant codec that always writes the expected bytes on encode (ignoring the input value) and validates that the bytes match on decode.Useful for magic numbers, version bytes, and protocol signatures. The value passed to
encodeis ignored;nullis acceptable.Codec<byte[]> magic = Codecs.constant(new byte[] {0x4D, 0x5A});- Parameters:
expected- the expected byte sequence (must be non-null and non-empty)- Returns:
- a new constant codec
- Throws:
NullPointerException- if expected is nullIllegalArgumentException- if expected is empty
-
bool
Creates a boolean codec (single byte: 0x00 = false, 0x01 = true).- Returns:
- a new boolean codec
-