Class CompositeCodec

java.lang.Object
org.springframework.integration.codec.CompositeCodec
All Implemented Interfaces:
Codec

public class CompositeCodec extends Object implements Codec
An implementation of Codec that combines multiple codecs into a single codec, delegating encoding and decoding operations to the appropriate type-specific codec. This implementation associates object types with their appropriate codecs while providing a fallback default codec for unregistered types. This class uses ClassUtils.findClosestMatch to select the appropriate codec for a given object type. When multiple codecs match an object type, ClassUtils.findClosestMatch offers the failOnTie option. If failOnTie is false, it will return any one of the matching codecs. If failOnTie is true and multiple codecs match, it will throw an IllegalStateException. CompositeCodec sets failOnTie to true, so if multiple codecs match, an IllegalStateException is thrown.
Since:
4.2
Author:
David Turanski, Glenn Renfro
  • Constructor Details

    • CompositeCodec

      public CompositeCodec(Map<Class<?>,Codec> delegates, Codec defaultCodec)
  • Method Details

    • encode

      public void encode(Object object, OutputStream outputStream) throws IOException
      Description copied from interface: Codec
      Encode (encode) an object to an OutputStream.
      Specified by:
      encode in interface Codec
      Parameters:
      object - the object to encode
      outputStream - the OutputStream
      Throws:
      IOException - if the operation fails
    • encode

      public byte[] encode(Object object) throws IOException
      Description copied from interface: Codec
      Encode an object to a byte array.
      Specified by:
      encode in interface Codec
      Parameters:
      object - the object to encode
      Returns:
      the bytes
      Throws:
      IOException - if the operation fails
    • decode

      public <T> T decode(InputStream inputStream, Class<T> type) throws IOException
      Description copied from interface: Codec
      Decode an object of a given type.
      Specified by:
      decode in interface Codec
      Type Parameters:
      T - the object's type
      Parameters:
      inputStream - the input stream containing the encoded object
      type - the object's class
      Returns:
      the object
      Throws:
      IOException - if the operation fails
    • decode

      public <T> T decode(byte[] bytes, Class<T> type) throws IOException
      Description copied from interface: Codec
      Decode an object of a given type.
      Specified by:
      decode in interface Codec
      Type Parameters:
      T - the object's type
      Parameters:
      bytes - the byte array containing the encoded object
      type - the object's class
      Returns:
      the object
      Throws:
      IOException - if the operation fails