Class MessageCodec
Wire format (16-byte header + variable body):
Offset Size Field ────── ──── ──────────────────────────── 0 1 Magic byte (0xDC) 1 1 Opcode (MessageType.code) 2 4 Correlation ID (big-endian int) 6 4 Body length (big-endian int) 10 1 Flags 11 2 Key length (big-endian short) 13 1 Status code 14 1 Map name length (0 = no map name, max 255 — single unsigned byte) 15 1 Protocol version (must match PROTOCOL_VERSION) ────── ──── ──────────────────────────── 16 N Body = [mapName bytes][key bytes][value bytes]
Key length is embedded in the header so we can split key and value from the body without an additional delimiter. Map name length allows the server to extract the target data structure name from client requests.
Backward compatible: old messages have mapNameLen=0 (was reserved/zero).
All multi-byte integers are BIG-ENDIAN (network byte order).
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic @Nullable Messagedecode(InputStream in) Decode a Message from an InputStream.static @Nullable Messagedecode(InputStream in, WireCompression negotiatedCompression) Decode a Message from an InputStream using the negotiated compression mode.static @Nullable Messagedecode(ByteBuffer buf) Decode from a ByteBuffer (for NIO channel reads).static @Nullable Messagedecode(ByteBuffer buf, WireCompression negotiatedCompression) Decode from a ByteBuffer using the negotiated compression mode.static ByteBufferEncode a Message into a ByteBuffer (for NIO channel writes).static ByteBufferencode(Message msg, WireCompression compression) Encode a Message into a ByteBuffer using the negotiated compression mode.static voidencode(Message msg, OutputStream out) Encode a Message to an OutputStream (for traditional I/O).static voidencode(Message msg, OutputStream out, WireCompression compression) Encode a Message to an OutputStream using the negotiated compression mode.
-
Constructor Details
-
MessageCodec
public MessageCodec()
-
-
Method Details
-
encode
Encode a Message into a ByteBuffer (for NIO channel writes). Returns a flipped buffer ready for writing. -
encode
Encode a Message into a ByteBuffer using the negotiated compression mode. -
encode
Encode a Message to an OutputStream (for traditional I/O).The entire message is encoded into a single byte array before writing, so either the complete message is written or nothing is (no partial header). The caller is responsible for ensuring the stream is not shared concurrently (e.g., via a write lock per connection).
- Throws:
IOException- if the write or flush fails
-
encode
public static void encode(Message msg, OutputStream out, WireCompression compression) throws IOException Encode a Message to an OutputStream using the negotiated compression mode.- Throws:
IOException- if the write or flush fails
-
decode
Decode a Message from an InputStream. Blocks until the full message is read. The caller must ensure each underlying read is bounded; for socket-backed streams, configure SO_TIMEOUT before calling this method. Returns null if stream is closed (EOF on header read).- Throws:
IOException
-
decode
public static @Nullable Message decode(InputStream in, WireCompression negotiatedCompression) throws IOException Decode a Message from an InputStream using the negotiated compression mode.- Throws:
IOException
-
decode
Decode from a ByteBuffer (for NIO channel reads). Buffer must contain at least one complete message. -
decode
Decode from a ByteBuffer using the negotiated compression mode.
-