Class ByteArrayLengthHeaderSerializer

java.lang.Object
org.springframework.integration.ip.tcp.serializer.AbstractByteArraySerializer
org.springframework.integration.ip.tcp.serializer.ByteArrayLengthHeaderSerializer
All Implemented Interfaces:
org.springframework.beans.factory.Aware, org.springframework.context.ApplicationEventPublisherAware, org.springframework.core.serializer.Deserializer<byte[]>, org.springframework.core.serializer.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
  • Field Summary

    Fields 
    Modifier and Type Field Description
    static int HEADER_SIZE_INT
    Default length-header field, allows for data up to 2**31-1 bytes.
    static int HEADER_SIZE_UNSIGNED_BYTE
    A single unsigned byte, for data up to 255 bytes.
    static int HEADER_SIZE_UNSIGNED_SHORT
    An unsigned short, for data up to 2**16 bytes.

    Fields inherited from class org.springframework.integration.ip.tcp.serializer.AbstractByteArraySerializer

    DEFAULT_MAX_MESSAGE_SIZE, logger
  • Constructor Summary

    Constructors 
    Constructor Description
    ByteArrayLengthHeaderSerializer()
    Construct the serializer using HEADER_SIZE_INT
    ByteArrayLengthHeaderSerializer​(int headerSize)
    Construct the serializer using the supplied header size.
  • Method Summary

    Modifier and Type Method Description
    byte[] deserialize​(java.io.InputStream inputStream)
    Read the header from the stream and then reads the provided length from the stream and returns the data in a byte[].
    ByteArrayLengthHeaderSerializer inclusive()
    Include the length of the header in addition to the payload.
    protected boolean isInclusive()
    Return true if the length header value includes its own length.
    protected int read​(java.io.InputStream inputStream, byte[] buffer, boolean header)
    Read data from the socket and puts the data in buffer.
    protected int readHeader​(java.io.InputStream inputStream)
    Read the header and returns the length of the data part.
    void serialize​(byte[] bytes, java.io.OutputStream outputStream)
    Write the byte[] to the output stream, preceded by a 4 byte length in network byte order (big endian).
    void setInclusive​(boolean inclusive)
    Set to true to set the length header to include the length of the header in addition to the payload.
    protected void writeHeader​(java.io.OutputStream outputStream, int length)
    Write the header, according to the header format.

    Methods inherited from class org.springframework.integration.ip.tcp.serializer.AbstractByteArraySerializer

    checkClosure, getMaxMessageSize, publishEvent, setApplicationEventPublisher, setMaxMessageSize

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface org.springframework.core.serializer.Deserializer

    deserializeFromByteArray

    Methods inherited from interface org.springframework.core.serializer.Serializer

    serializeToByteArray
  • Field Details

  • Constructor Details

  • 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()
    • 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:
      setInclusive(boolean)
    • deserialize

      public byte[] deserialize​(java.io.InputStream inputStream) throws java.io.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:
      java.io.IOException - Any IOException.
    • serialize

      public void serialize​(byte[] bytes, java.io.OutputStream outputStream) throws java.io.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:
      java.io.IOException
    • read

      protected int read​(java.io.InputStream inputStream, byte[] buffer, boolean header) throws java.io.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:
      java.io.IOException - Any IOException.
    • writeHeader

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

      protected int readHeader​(java.io.InputStream inputStream) throws java.io.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:
      java.io.IOException - Any IOException.
      SoftEndOfStreamException - if socket closes before any length data read.