org.springframework.integration.ip.tcp
Class AbstractSocketReader

java.lang.Object
  extended by org.springframework.integration.ip.tcp.AbstractSocketReader
All Implemented Interfaces:
MessageFormats, SocketReader
Direct Known Subclasses:
NetSocketReader, NioSocketReader

public abstract class AbstractSocketReader
extends java.lang.Object
implements SocketReader, MessageFormats

Abstract SocketReader that handles data in 3 standard, and one custom format. The default format is MessageFormats.FORMAT_LENGTH_HEADER in which the message consists of a 4 byte integer (in network byte order) containing the length of data that follows. MessageFormats.FORMAT_STX_ETX indicates a message where the data begins with STX (0x02) and ends with ETX (0x03); the STX and ETX are not part of the data. MessageFormats.FORMAT_CRLF indicates a message followed by carriage return and line feed '\r\n'. FORMAT_LENGTH_HEADER can be used for Socket and SocketChannel implementations are provided for the standard formats. Users requiring other formats should subclass the appropriate implementation, and provide an implementation for assembleDataCustomFormat() which is invoked by assembleData() when the format is MessageFormats.FORMAT_CUSTOM.

Author:
Gary Russell

Field Summary
protected  byte[] assembledData
          The assembled data; must contain a reference when assembleData() returns true; will be set to null when getAssembledData() is called.
protected  int maxMessageSize
           
protected  int messageFormat
           
 
Fields inherited from interface org.springframework.integration.ip.tcp.MessageFormats
ETX, FORMAT_CRLF, FORMAT_CUSTOM, FORMAT_LENGTH_HEADER, FORMAT_STX_ETX, STX
 
Constructor Summary
AbstractSocketReader()
           
 
Method Summary
 boolean assembleData()
          Reads the data the socket and assembles packets of data into a complete message, depending on the format of that data.
protected abstract  boolean assembleDataCrLfFormat()
          Assembles data in format MessageFormats.FORMAT_CRLF.
protected abstract  boolean assembleDataCustomFormat()
          Assembles data in format MessageFormats.FORMAT_CUSTOM.
protected abstract  boolean assembleDataLengthFormat()
          Assembles data in format MessageFormats.FORMAT_LENGTH_HEADER.
protected abstract  boolean assembleDataStxEtxFormat()
          Assembles data in format MessageFormats.FORMAT_STX_ETX.
protected abstract  void doClose()
          Called after an exception; close the transport.
 void setMaxMessageSize(int maxMessageSize)
           
 void setMessageFormat(int messageFormat)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.springframework.integration.ip.tcp.SocketReader
getAddress, getAssembledData, getSocket
 

Field Detail

messageFormat

protected int messageFormat

assembledData

protected byte[] assembledData
The assembled data; must contain a reference when assembleData() returns true; will be set to null when getAssembledData() is called.


maxMessageSize

protected int maxMessageSize
Constructor Detail

AbstractSocketReader

public AbstractSocketReader()
Method Detail

assembleDataLengthFormat

protected abstract boolean assembleDataLengthFormat()
                                             throws java.io.IOException
Assembles data in format MessageFormats.FORMAT_LENGTH_HEADER.

Returns:
True when a message is completely assembled.
Throws:
java.io.IOException

assembleDataStxEtxFormat

protected abstract boolean assembleDataStxEtxFormat()
                                             throws java.io.IOException
Assembles data in format MessageFormats.FORMAT_STX_ETX.

Returns:
True when a message is completely assembled.
Throws:
java.io.IOException

assembleDataCrLfFormat

protected abstract boolean assembleDataCrLfFormat()
                                           throws java.io.IOException
Assembles data in format MessageFormats.FORMAT_CRLF.

Returns:
True when a message is completely assembled.
Throws:
java.io.IOException

assembleDataCustomFormat

protected abstract boolean assembleDataCustomFormat()
                                             throws java.io.IOException
Assembles data in format MessageFormats.FORMAT_CUSTOM. Implementations must return false until the message is completely assembled, at which time the implementation must update assembledData to reference the assembled message.

Returns:
True when a message is completely assembled.
Throws:
java.io.IOException

assembleData

public boolean assembleData()
                     throws java.io.IOException
Description copied from interface: SocketReader
Reads the data the socket and assembles packets of data into a complete message, depending on the format of that data.

Specified by:
assembleData in interface SocketReader
Returns:
true when the message is assembled.
Throws:
java.io.IOException

doClose

protected abstract void doClose()
Called after an exception; close the transport.


setMessageFormat

public void setMessageFormat(int messageFormat)
Parameters:
messageFormat - the messageFormat to set,

setMaxMessageSize

public void setMaxMessageSize(int maxMessageSize)