Class BytesCodec

java.lang.Object
org.acemq.amqp.core.BytesCodec
All Implemented Interfaces:
Codec

public final class BytesCodec extends Object implements Codec
Passes bytes through untouched.

For payloads that are already encoded by something else: an image, a file, a message serialised by a library the application would rather call itself. Encoding twice is a common and quiet mistake, and this is the way to say that it has been done once already.

Never offered to a message that did not ask for it. It would accept any bytes at all and hand a consumer a byte[] where an object was expected, so it sits at the very back of the order and only claims application/octet-stream.

  • Constructor Details

    • BytesCodec

      public BytesCodec()
  • Method Details

    • contentType

      public String contentType()
      Specified by:
      contentType in interface Codec
      Returns:
      the content type this codec produces, for example application/json
    • encode

      public byte[] encode(Object payload)
      Specified by:
      encode in interface Codec
      Parameters:
      payload - object to encode; never null
      Returns:
      encoded bytes
    • decode

      public <T> T decode(byte[] body, Class<T> target)
      Specified by:
      decode in interface Codec
      Type Parameters:
      T - target type
      Parameters:
      body - encoded bytes
      target - type to produce
      Returns:
      the decoded payload
    • canDecode

      public boolean canDecode(@Nullable String contentType)
      Specified by:
      canDecode in interface Codec
      Parameters:
      contentType - content type of a received message, or null when the sender did not set one
      Returns:
      whether this codec can decode it
    • toString

      public String toString()
      Overrides:
      toString in class Object