Class ByteArrayLengthHeaderSerializer

java.lang.Object
org.springframework.integration.ip.tcp.serializer.AbstractByteArraySerializer
org.springframework.integration.ip.tcp.serializer.ByteArrayLengthHeaderSerializer
All Implemented Interfaces:
Aware, ApplicationEventPublisherAware, Deserializer<byte[]>, Serializer<byte[]>

public class ByteArrayLengthHeaderSerializer extends AbstractByteArraySerializer
Reads data in an InputStream to a byte[]; data must be preceded by a binary length (network byte order, not included in resulting byte[]).

Writes a byte[] to an OutputStream after a binary length. The length field contains the length of data following the length field. (network byte order).

The default length field is a 4 byte signed integer. During deserialization, negative values will be rejected. Other options are an unsigned byte, and unsigned short.

For other header formats, override readHeader(InputStream) and writeHeader(OutputStream, int).

Since:
2.0
Author:
Gary Russell, Artem Bilan
  • Field Details

    • HEADER_SIZE_INT

      public static final int HEADER_SIZE_INT
      Default length-header field, allows for data up to 2**31-1 bytes.
      See Also:
    • HEADER_SIZE_UNSIGNED_SHORT

      public static final int HEADER_SIZE_UNSIGNED_SHORT
      An unsigned short, for data up to 2**16 bytes.
      See Also:
    • HEADER_SIZE_UNSIGNED_BYTE

      public static final int HEADER_SIZE_UNSIGNED_BYTE
      A single unsigned byte, for data up to 255 bytes.
      See Also:
  • Constructor Details

    • ByteArrayLengthHeaderSerializer

      public ByteArrayLengthHeaderSerializer()
      Construct the serializer using HEADER_SIZE_INT.
    • ByteArrayLengthHeaderSerializer

      public ByteArrayLengthHeaderSerializer(int headerSize)
      Construct the serializer using the supplied header size. Valid header sizes are HEADER_SIZE_INT (default), HEADER_SIZE_UNSIGNED_BYTE and HEADER_SIZE_UNSIGNED_SHORT
      Parameters:
      headerSize - The header size.
  • Method Details

    • isInclusive

      protected boolean isInclusive()
      Return true if the length header value includes its own length.
      Returns:
      true if the length includes the header length.
      Since:
      5.2
    • setInclusive

      public void setInclusive(boolean inclusive)
      Set to true to set the length header to include the length of the header in addition to the payload. Valid header sizes are HEADER_SIZE_INT (default), HEADER_SIZE_UNSIGNED_BYTE and HEADER_SIZE_UNSIGNED_SHORT and 4, 1 and 2 will be added to the payload length respectively.
      Parameters:
      inclusive - true to include the header length.
      Since:
      5.2
      See Also:
    • inclusive

      public ByteArrayLengthHeaderSerializer inclusive()
      Include the length of the header in addition to the payload. Valid header sizes are HEADER_SIZE_INT (default), HEADER_SIZE_UNSIGNED_BYTE and HEADER_SIZE_UNSIGNED_SHORT and 4, 1 and 2 will be added to the payload length respectively. Fluent API form of setInclusive(boolean).
      Returns:
      the serializer.
      Since:
      5.2
      See Also:
    • deserialize

      public byte[] deserialize(InputStream inputStream) throws IOException
      Read the header from the stream and then reads the provided length from the stream and returns the data in a byte[]. Throws an IOException if the length field exceeds the maxMessageSize. Throws a SoftEndOfStreamException if the stream is closed between messages.
      Parameters:
      inputStream - The input stream.
      Throws:
      IOException - Any IOException.
    • serialize

      public void serialize(byte[] bytes, OutputStream outputStream) throws IOException
      Write the byte[] to the output stream, preceded by a 4 byte length in network byte order (big endian).
      Parameters:
      bytes - The bytes.
      outputStream - The output stream.
      Throws:
      IOException
    • read

      protected int read(InputStream inputStream, byte[] buffer, boolean header) throws IOException
      Read data from the socket and puts the data in buffer. Blocks until buffer is full or a socket timeout occurs.
      Parameters:
      inputStream - The input stream.
      buffer - the buffer into which the data should be read
      header - true if we are reading the header
      Returns:
      < 0 if socket closed and not in the middle of a message
      Throws:
      IOException - Any IOException.
    • writeHeader

      protected void writeHeader(OutputStream outputStream, int length) throws IOException
      Write the header, according to the header format.
      Parameters:
      outputStream - The output stream.
      length - The length.
      Throws:
      IOException - Any IOException.
    • readHeader

      protected int readHeader(InputStream inputStream) throws IOException
      Read the header and returns the length of the data part.
      Parameters:
      inputStream - The input stream.
      Returns:
      The length of the data part.
      Throws:
      IOException - Any IOException.
      SoftEndOfStreamException - if socket closes before any length data read.