org.springframework.integration.ip.tcp.serializer
Class ByteArrayLengthHeaderSerializer

java.lang.Object
  extended by org.springframework.integration.ip.tcp.serializer.AbstractByteArraySerializer
      extended by org.springframework.integration.ip.tcp.serializer.ByteArrayLengthHeaderSerializer
All Implemented Interfaces:
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

Field Summary
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
maxMessageSize
 
Constructor Summary
ByteArrayLengthHeaderSerializer()
          Constructs the serializer using HEADER_SIZE_INT
ByteArrayLengthHeaderSerializer(int headerSize)
          Constructs the serializer using the supplied header size.
 
Method Summary
 byte[] deserialize(java.io.InputStream inputStream)
          Reads the header from the stream and then reads the provided length from the stream and returns the data in a byte[].
protected  int read(java.io.InputStream inputStream, byte[] buffer, boolean header)
          Reads data from the socket and puts the data in buffer.
protected  int readHeader(java.io.InputStream inputStream)
          Reads the header and returns the length of the data part.
 void serialize(byte[] bytes, java.io.OutputStream outputStream)
          Writes the byte[] to the output stream, preceded by a 4 byte length in network byte order (big endian).
protected  void writeHeader(java.io.OutputStream outputStream, int length)
          Writes the header, according to the header format.
 
Methods inherited from class org.springframework.integration.ip.tcp.serializer.AbstractByteArraySerializer
checkClosure, getMaxMessageSize, setMaxMessageSize
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

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:
Constant Field Values

HEADER_SIZE_UNSIGNED_BYTE

public static final int HEADER_SIZE_UNSIGNED_BYTE
A single unsigned byte, for data up to 255 bytes.

See Also:
Constant Field Values

HEADER_SIZE_UNSIGNED_SHORT

public static final int HEADER_SIZE_UNSIGNED_SHORT
An unsigned short, for data up to 2**16 bytes.

See Also:
Constant Field Values
Constructor Detail

ByteArrayLengthHeaderSerializer

public ByteArrayLengthHeaderSerializer()
Constructs the serializer using HEADER_SIZE_INT


ByteArrayLengthHeaderSerializer

public ByteArrayLengthHeaderSerializer(int headerSize)
Constructs 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 -
Method Detail

deserialize

public byte[] deserialize(java.io.InputStream inputStream)
                   throws java.io.IOException
Reads 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.

Throws:
java.io.IOException

serialize

public void serialize(byte[] bytes,
                      java.io.OutputStream outputStream)
               throws java.io.IOException
Writes the byte[] to the output stream, preceded by a 4 byte length in network byte order (big endian).

Throws:
java.io.IOException

read

protected int read(java.io.InputStream inputStream,
                   byte[] buffer,
                   boolean header)
            throws java.io.IOException
Reads data from the socket and puts the data in buffer. Blocks until buffer is full or a socket timeout occurs.

Parameters:
buffer -
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

writeHeader

protected void writeHeader(java.io.OutputStream outputStream,
                           int length)
                    throws java.io.IOException
Writes the header, according to the header format.

Parameters:
outputStream -
length -
Throws:
java.io.IOException

readHeader

protected int readHeader(java.io.InputStream inputStream)
                  throws java.io.IOException
Reads the header and returns the length of the data part.

Parameters:
inputStream -
Returns:
The length of the data part
Throws:
IOException, - SoftEndOfStreamException if socket closes before any length data read.
java.io.IOException