org.khelekore.rnio.impl
Class SimpleBlockReader

java.lang.Object
  extended by org.khelekore.rnio.impl.SocketHandlerBase<SocketChannel>
      extended by org.khelekore.rnio.impl.SimpleBlockReader
All Implemented Interfaces:
ReadHandler, SocketChannelHandler

public abstract class SimpleBlockReader
extends SocketHandlerBase<SocketChannel>
implements ReadHandler

A reader of data. Will wait until a channel is read-ready and then read a block of data from it.

Author:
Robert Olofsson

Field Summary
 
Fields inherited from class org.khelekore.rnio.impl.SocketHandlerBase
nioHandler, sc, timeout
 
Constructor Summary
SimpleBlockReader(SocketChannel sc, NioHandler nioHandler, Long timeout)
          Create a new block reader.
 
Method Summary
abstract  void channelClosed()
          Do any cleanup that needs to be done when the channel we tried to read from was closed.
 ByteBuffer getByteBuffer()
          Called before a read attempt is made.
abstract  void handleBufferRead(ByteBuffer buf)
          Handle the buffer content.
 void handleIOException(IOException e)
          Handle the exception, default is to log it and to close the channel.
 void putByteBuffer(ByteBuffer buf)
          Return the ByteBuffer, this method will be called when read gets EOF or no data.
 void read()
          Try to read data from the channel.
 void register()
          Wait for the channel to become read ready.
 
Methods inherited from class org.khelekore.rnio.impl.SocketHandlerBase
closed, getDescription, getTimeout, timeout, useSeparateThread
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.khelekore.rnio.SocketChannelHandler
closed, getDescription, getTimeout, timeout, useSeparateThread
 

Constructor Detail

SimpleBlockReader

public SimpleBlockReader(SocketChannel sc,
                         NioHandler nioHandler,
                         Long timeout)
Create a new block reader.

Parameters:
sc - the channel to read from
nioHandler - the NioHandler to use for waiting on data
timeout - the timeout time, may be null if not timeout is set
Method Detail

read

public void read()
Try to read data from the channel.

Specified by:
read in interface ReadHandler

getByteBuffer

public ByteBuffer getByteBuffer()
Called before a read attempt is made. The default is to create a new 1kB big ByteBuffer and return it.


putByteBuffer

public void putByteBuffer(ByteBuffer buf)
Return the ByteBuffer, this method will be called when read gets EOF or no data. The default is to do nothing.


handleIOException

public void handleIOException(IOException e)
Handle the exception, default is to log it and to close the channel.


channelClosed

public abstract void channelClosed()
Do any cleanup that needs to be done when the channel we tried to read from was closed.


handleBufferRead

public abstract void handleBufferRead(ByteBuffer buf)
                               throws IOException
Handle the buffer content.

Throws:
IOException

register

public void register()
Wait for the channel to become read ready.