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 4 byte binary length (network byte order, not included in resulting byte[]). Writes a byte[] to an OutputStream after a 4 byte binary length. The length field contains the length of data following the length field. (network byte order).

Since:
2.0

Field Summary
 
Fields inherited from class org.springframework.integration.ip.tcp.serializer.AbstractByteArraySerializer
maxMessageSize
 
Constructor Summary
ByteArrayLengthHeaderSerializer()
           
 
Method Summary
 byte[] deserialize(java.io.InputStream inputStream)
          Reads a 4 byte length from the stream and then reads that 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.
 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).
 
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
 

Constructor Detail

ByteArrayLengthHeaderSerializer

public ByteArrayLengthHeaderSerializer()
Method Detail

deserialize

public byte[] deserialize(java.io.InputStream inputStream)
                   throws java.io.IOException
Reads a 4 byte length from the stream and then reads that 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