org.springframework.integration.ip.tcp
Class NioSocketReader

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

public class NioSocketReader
extends AbstractSocketReader

A non-blocking SocketReader that reads from a SocketChannel.

Author:
Gary Russell

Field Summary
protected  java.nio.ByteBuffer buildBuffer
           
protected  boolean building
           
protected  java.nio.channels.SocketChannel channel
           
protected  java.nio.ByteBuffer dataPart
           
protected  java.nio.ByteBuffer lengthPart
           
protected  org.apache.commons.logging.Log logger
           
protected  java.nio.ByteBuffer rawBuffer
           
protected  int receiveBufferSize
           
protected  boolean usingDirectBuffers
           
 
Fields inherited from class org.springframework.integration.ip.tcp.AbstractSocketReader
assembledData, 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
NioSocketReader(java.nio.channels.SocketChannel channel)
          Constructs an NioSocketReader which reads from the SocketChannel.
 
Method Summary
protected  java.nio.ByteBuffer allocate(int length)
          Allocates a ByteBuffer of the requested length using normal or direct buffers, depending on the usingDirectBuffers field.
protected  boolean assembleDataCrLfFormat()
          Assembles data in format MessageFormats.FORMAT_CRLF.
protected  boolean assembleDataCustomFormat()
          Throws UnsupportedOperationException; custom implementations can subclass this class and provide an implementation.
 boolean assembleDataLengthFormat()
          Assembles data in format MessageFormats.FORMAT_LENGTH_HEADER.
protected  boolean assembleDataStxEtxFormat()
          Assembles data in format MessageFormats.FORMAT_STX_ETX.
protected  void doClose()
          Called after an exception; close the transport.
 java.net.InetAddress getAddress()
          Returns the InetAddress of the underlying socket.
 byte[] getAssembledData()
          Retrieves the assembled tcp data or null if the data is not yet assembled.
 boolean isUsingDirectBuffers()
           
protected  void readChannel(java.nio.ByteBuffer buffer)
          Reads from the channel into the buffer.
protected  boolean readChannelNonDeterministic()
          Reads data into the rawBuffer for non-deterministic algorithms.
 void setUsingDirectBuffers(boolean usingDirectBuffers)
           
 
Methods inherited from class org.springframework.integration.ip.tcp.AbstractSocketReader
assembleData, setMessageFormat
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

logger

protected final org.apache.commons.logging.Log logger

channel

protected java.nio.channels.SocketChannel channel

usingDirectBuffers

protected boolean usingDirectBuffers

lengthPart

protected java.nio.ByteBuffer lengthPart

dataPart

protected java.nio.ByteBuffer dataPart

rawBuffer

protected java.nio.ByteBuffer rawBuffer

buildBuffer

protected java.nio.ByteBuffer buildBuffer

receiveBufferSize

protected int receiveBufferSize

building

protected boolean building
Constructor Detail

NioSocketReader

public NioSocketReader(java.nio.channels.SocketChannel channel)
Constructs an NioSocketReader which reads from the SocketChannel.

Parameters:
channel - The channel.
Method Detail

getAssembledData

public byte[] getAssembledData()
Description copied from interface: SocketReader
Retrieves the assembled tcp data or null if the data is not yet assembled. Once this method is called, the assembled data is again null until a new assembly is completed.

Returns:
The assembled data or null.

assembleDataLengthFormat

public boolean assembleDataLengthFormat()
                                 throws java.io.IOException
Description copied from class: AbstractSocketReader
Assembles data in format MessageFormats.FORMAT_LENGTH_HEADER.

Specified by:
assembleDataLengthFormat in class AbstractSocketReader
Returns:
True when a message is completely assembled.
Throws:
java.io.IOException

assembleDataStxEtxFormat

protected boolean assembleDataStxEtxFormat()
                                    throws java.io.IOException
Description copied from class: AbstractSocketReader
Assembles data in format MessageFormats.FORMAT_STX_ETX.

Specified by:
assembleDataStxEtxFormat in class AbstractSocketReader
Returns:
True when a message is completely assembled.
Throws:
java.io.IOException

assembleDataCrLfFormat

protected boolean assembleDataCrLfFormat()
                                  throws java.io.IOException
Description copied from class: AbstractSocketReader
Assembles data in format MessageFormats.FORMAT_CRLF.

Specified by:
assembleDataCrLfFormat in class AbstractSocketReader
Returns:
True when a message is completely assembled.
Throws:
java.io.IOException

assembleDataCustomFormat

protected boolean assembleDataCustomFormat()
                                    throws java.io.IOException
Throws UnsupportedOperationException; custom implementations can subclass this class and provide an implementation.

Specified by:
assembleDataCustomFormat in class AbstractSocketReader
Returns:
True when a message is completely assembled.
Throws:
java.io.IOException
See Also:
AbstractSocketReader.assembleDataCustomFormat().

readChannel

protected void readChannel(java.nio.ByteBuffer buffer)
                    throws java.io.IOException
Reads from the channel into the buffer. Reads as much data as is currently available in the channel.

Parameters:
buffer -
Throws:
java.io.IOException

readChannelNonDeterministic

protected boolean readChannelNonDeterministic()
                                       throws java.io.IOException
Reads data into the rawBuffer for non-deterministic algorithms.

Returns:
true If data is available.
Throws:
java.io.IOException

allocate

protected java.nio.ByteBuffer allocate(int length)
Allocates a ByteBuffer of the requested length using normal or direct buffers, depending on the usingDirectBuffers field.

Parameters:
length -
Returns:

doClose

protected void doClose()
Description copied from class: AbstractSocketReader
Called after an exception; close the transport.

Specified by:
doClose in class AbstractSocketReader

getAddress

public java.net.InetAddress getAddress()
Description copied from interface: SocketReader
Returns the InetAddress of the underlying socket.

Returns:
The InetAddress.

isUsingDirectBuffers

public boolean isUsingDirectBuffers()
Returns:
the usingeDirectBuffers

setUsingDirectBuffers

public void setUsingDirectBuffers(boolean usingDirectBuffers)
Parameters:
usingDirectBuffers - the usingDirectBuffers to set