Class ConfluentWireFormat
java.lang.Object
io.krabka.streams.schema.ConfluentWireFormat
Encodes and decodes Confluent schema registry frames.
Every framed record starts with a five-byte header: the MAGIC byte
followed by the schema ID as a big-endian 32-bit integer. Protobuf records insert a
zigzag-varint message-index list between the header and the body; the common case of
the first top-level message is encoded as the single byte 0.
The serdes in this package frame records automatically. Use this class directly when interoperating with foreign framed bytes, for example when inspecting records in tooling or writing a custom serde.
Example
byte[] framed = ConfluentWireFormat.encode(42, body);
var frame = ConfluentWireFormat.decode(framed);
assert frame.schemaId() == 42;
assert Arrays.equals(frame.body(), body);
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final recordA decoded frame.static final recordA decoded Protobuf frame. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final byteThe first byte of every Confluent-framed record. -
Method Summary
Modifier and TypeMethodDescriptionstatic ConfluentWireFormat.Framedecode(byte[] bytes) Splits framed record bytes into the schema ID and body.decodeProtobuf(byte[] bytes) Splits framed Protobuf record bytes into schema ID, message indexes, and body.static byte[]encode(int schemaId, byte[] body) Frames body bytes with the magic byte and schema ID.static byte[]encodeProtobuf(int schemaId, List<Integer> messageIndexes, byte[] body) Frames Protobuf body bytes, including the message-index list.
-
Field Details
-
MAGIC
public static final byte MAGICThe first byte of every Confluent-framed record.- See Also:
-
-
Method Details
-
encode
public static byte[] encode(int schemaId, byte[] body) Frames body bytes with the magic byte and schema ID.- Parameters:
schemaId- the registry schema ID to record in the headerbody- the encoded record body- Returns:
- the framed record bytes
-
decode
Splits framed record bytes into the schema ID and body.- Parameters:
bytes- the framed record bytes- Returns:
- the decoded frame
- Throws:
org.apache.kafka.common.errors.SerializationException- if the input is shorter than five bytes or does not start withMAGIC
-
encodeProtobuf
Frames Protobuf body bytes, including the message-index list.The indexes identify the message within its
.protofile: each entry is the message's position at its nesting level, outermost first. The common[0](first top-level message) is written in its compact single-byte form.- Parameters:
schemaId- the registry schema ID to record in the headermessageIndexes- the non-empty message-index path, outermost firstbody- the serialized Protobuf message- Returns:
- the framed record bytes
- Throws:
IllegalArgumentException- ifmessageIndexesis empty
-
decodeProtobuf
Splits framed Protobuf record bytes into schema ID, message indexes, and body.- Parameters:
bytes- the framed record bytes- Returns:
- the decoded Protobuf frame
- Throws:
org.apache.kafka.common.errors.SerializationException- if the input is shorter than five bytes, does not start withMAGIC, or carries a malformed message-index list
-