Class MapReplaceCasCodec

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

public final class MapReplaceCasCodec extends Object
Wire-format codec for the MessageType.MAP_REPLACE_CAS value field.

Prior implementations encoded "oldValue|newValue" as a pipe-delimited UTF-8 string and split on "|" on the server, which made any value containing a '|' character unrepresentable or ambiguous. This codec replaces that with a length-prefixed binary format that is safe for arbitrary UTF-8 payloads.

Wire format (both lengths are 4-byte signed big-endian integers):

  [4 bytes oldValueLen][oldValueLen bytes — UTF-8 oldValue]
  [4 bytes newValueLen][newValueLen bytes — UTF-8 newValue]

The codec is deliberately self-contained so both the server handler and server-side test fixtures can share a single canonical format. Adding a client API for CAS in the future will reuse this codec as the wire contract.

Since:
1.4
  • Field Details

    • HEADER_BYTES

      public static final int HEADER_BYTES
      Fixed header overhead: 4 (oldLen) + 4 (newLen) = 8 bytes.
      See Also:
  • Method Details

    • encode

      public static byte[] encode(String oldValue, String newValue)
      Encode (oldValue, newValue) into the binary wire format.
      Parameters:
      oldValue - the expected current value (non-null; use "" for empty)
      newValue - the replacement value (non-null; use "" for empty)
      Returns:
      the encoded byte array
      Throws:
      NullPointerException - if either argument is null
    • decode

      public static @Nullable MapReplaceCasCodec.CasPayload decode(byte[] data)
      Decode a previously-encoded payload back into MapReplaceCasCodec.CasPayload.
      Parameters:
      data - the encoded bytes (non-null)
      Returns:
      the decoded payload, or null if data is malformed