Class MessageCodec

java.lang.Object
com.loomcache.common.protocol.MessageCodec

public final class MessageCodec extends Object
Encodes/decodes Messages to/from the LoomCache binary wire protocol.

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 Details

    • MessageCodec

      public MessageCodec()
  • Method Details

    • encode

      public static ByteBuffer encode(Message msg)
      Encode a Message into a ByteBuffer (for NIO channel writes). Returns a flipped buffer ready for writing.
    • encode

      public static ByteBuffer encode(Message msg, WireCompression compression)
      Encode a Message into a ByteBuffer using the negotiated compression mode.
    • encode

      public static void encode(Message msg, OutputStream out) throws IOException
      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

      public static @Nullable Message decode(InputStream in) throws IOException
      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

      public static @Nullable Message decode(ByteBuffer buf)
      Decode from a ByteBuffer (for NIO channel reads). Buffer must contain at least one complete message.
    • decode

      public static @Nullable Message decode(ByteBuffer buf, WireCompression negotiatedCompression)
      Decode from a ByteBuffer using the negotiated compression mode.