org.xlightweb
Class NonBlockingBodyDataSource

Package class diagram package NonBlockingBodyDataSource
java.lang.Object
  extended by org.xlightweb.NonBlockingBodyDataSource
All Implemented Interfaces:
Closeable, Channel, ReadableByteChannel, IDataSource

public abstract class NonBlockingBodyDataSource
extends Object
implements IDataSource, ReadableByteChannel, Closeable

data source base implementation


Method Summary
 void addCompleteListener(IBodyCompleteListener listener)
          adds a complete listener
 void addDestroyListener(IBodyDestroyListener destroyListener)
          add a destroy listener
 int available()
          returns the available bytes
protected  void callCloseListener()
           
protected  void callDestroyListener()
           
 void close()
          closes the body data source
 void closeQuitly()
          closes this connection by swallowing io exceptions
 void destroy()
          destroys the data source
 long getBodyDataReceiveTimeoutMillis()
          returns body data receive timeout
 IBodyDataHandler getDataHandler()
          returns the body data handler or null if no data handler is assigned
 int getReadBufferVersion()
          get the version of read buffer.
 int indexOf(String str)
          Returns the index of the first occurrence of the given string.
 int indexOf(String str, String encoding)
          Returns the index of the first occurrence of the given string.
 boolean isMultipart()
          return true if the body is a mulipart
protected abstract  boolean isNetworkendpoint()
           
 boolean isOpen()
          returns true, if the body data source is open
 void markReadPosition()
          Marks the read position in the connection.
protected  void onRead()
           
 int read(ByteBuffer buffer)
          see ReadableByteChannel.read(ByteBuffer)
 byte readByte()
          read a byte
 ByteBuffer[] readByteBufferByDelimiter(String delimiter)
          read a ByteBuffer by using a delimiter.
 ByteBuffer[] readByteBufferByDelimiter(String delimiter, int maxLength)
          read a ByteBuffer by using a delimiter For performance reasons, the ByteBuffer readByteBuffer method is generally preferable to get bytes
 ByteBuffer[] readByteBufferByLength(int length)
          read a ByteBuffer
 byte[] readBytesByDelimiter(String delimiter)
          read a byte array by using a delimiter For performance reasons, the ByteBuffer readByteBuffer method is generally preferable to get bytes
 byte[] readBytesByDelimiter(String delimiter, int maxLength)
          read a byte array by using a delimiter For performance reasons, the ByteBuffer readByteBuffer method is generally preferable to get bytes
 byte[] readBytesByLength(int length)
          read bytes by using a length definition
 double readDouble()
          read a double
 int readInt()
          read an int
 long readLong()
          read a long
 IPart readPart()
          read the part of the multipart body.
 short readShort()
          read a short value
 String readStringByDelimiter(String delimiter)
          read a string by using a delimiter
 String readStringByDelimiter(String delimiter, int maxLength)
          read a string by using a delimiter
 String readStringByDelimiter(String delimiter, String encoding)
          read a string by using a delimiter
 String readStringByDelimiter(String delimiter, String encoding, int maxLength)
          read a string by using a delimiter
 String readStringByLength(int length)
          read a string by using a length definition
 String readStringByLength(int length, String encoding)
          read a string by using a length definition
 void removeReadMark()
          remove the read mark
 boolean resetToReadMark()
          Resets to the marked read position.
 void setBodyDataReceiveTimeoutMillis(long bodyDataReceiveTimeoutMillis)
          set the body data receive timeout
 void setBodyPartHandler(IPartHandler partHandler)
          set the part handler.
 void setDataHandler(IBodyDataHandler bodyDataHandler)
          set the body handler
 String toString()
          
 long transferTo(BodyDataSink dataSink)
          transfer the available data of the this source channel to the given data sink
 long transferTo(BodyDataSink dataSink, int length)
          transfer the data of the this source channel to the given data sink
 void transferTo(File file, org.xlightweb.NonBlockingBodyDataSource.ITransferResultHandler resultHandler)
          transfer the data of the this source channel to the given file
 long transferTo(WritableByteChannel target, int length)
          transfer the data of the this source channel to the given data sink
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Method Detail

addDestroyListener

public final void addDestroyListener(IBodyDestroyListener destroyListener)
add a destroy listener

Parameters:
destroyListener - the destroy listener to add

callCloseListener

protected final void callCloseListener()

callDestroyListener

protected final void callDestroyListener()

isMultipart

public final boolean isMultipart()
return true if the body is a mulipart

Returns:
true, if the body is a mulipart

destroy

public void destroy()
destroys the data source


addCompleteListener

public void addCompleteListener(IBodyCompleteListener listener)
adds a complete listener

Parameters:
listener - the complete listener

setDataHandler

public final void setDataHandler(IBodyDataHandler bodyDataHandler)
set the body handler

Parameters:
bodyDataHandler - the body handler

readPart

public IPart readPart()
               throws NoMultipartTypeException,
                      IOException,
                      BufferUnderflowException
read the part of the multipart body. BodyDataSource.isMultipart() can be used to verify if the body is a multipart one

Returns:
the part
Throws:
NoMultipartTypeException - if the body is not a multipart body
IOException - if an exception occurs
BufferUnderflowException - if not enough data is available

setBodyPartHandler

public void setBodyPartHandler(IPartHandler partHandler)
                        throws NoMultipartTypeException,
                               IOException
set the part handler. BodyDataSource.isMultipart() can be used to verify if the body is a multipart one

Parameters:
bodyDataHandler - the body handler
Throws:
IOException - if an exception occurs
NoMultipartTypeException - if the body is not a multipart body

getDataHandler

public IBodyDataHandler getDataHandler()
returns the body data handler or null if no data handler is assigned

Returns:
the body data handler or null if no data handler is assigned

setBodyDataReceiveTimeoutMillis

public final void setBodyDataReceiveTimeoutMillis(long bodyDataReceiveTimeoutMillis)
set the body data receive timeout

Parameters:
bodyDataReceiveTimeoutMillis - the timeout

available

public final int available()
                    throws ProtocolException,
                           IOException
returns the available bytes

Returns:
the number of available bytes, possibly zero, or -1 if the channel has reached end-of-stream
Throws:
ProtocolException - if a protocol error occurs
IOException - if some other exception occurs

close

public final void close()
                 throws IOException
closes the body data source

Specified by:
close in interface Closeable
Specified by:
close in interface Channel
Throws:
IOException - if an exception occurs

closeQuitly

public final void closeQuitly()
closes this connection by swallowing io exceptions


isOpen

public final boolean isOpen()
returns true, if the body data source is open

Specified by:
isOpen in interface Channel
Returns:
true, if the body data source is open

read

public final int read(ByteBuffer buffer)
               throws IOException
see ReadableByteChannel.read(ByteBuffer)

Specified by:
read in interface ReadableByteChannel
Specified by:
read in interface IDataSource
Throws:
IOException

transferTo

public long transferTo(BodyDataSink dataSink)
                throws ProtocolException,
                       IOException,
                       ClosedChannelException
transfer the available data of the this source channel to the given data sink

Parameters:
dataSink - the data sink
Returns:
the number of transfered bytes
Throws:
ClosedChannelException - If either this channel or the target channel is closed
IOException - If some other I/O error occurs
ProtocolException

transferTo

public long transferTo(BodyDataSink dataSink,
                       int length)
                throws ProtocolException,
                       IOException,
                       ClosedChannelException
transfer the data of the this source channel to the given data sink

Parameters:
dataSink - the data sink
length - the size to transfer
Returns:
the number of transfered bytes
Throws:
ClosedChannelException - If either this channel or the target channel is closed
IOException - If some other I/O error occurs
ProtocolException

transferTo

public final long transferTo(WritableByteChannel target,
                             int length)
                      throws IOException,
                             ClosedChannelException
transfer the data of the this source channel to the given data sink

Specified by:
transferTo in interface IDataSource
Parameters:
dataSink - the data sink
length - the size to transfer
Returns:
the number of transfered bytes
Throws:
ClosedChannelException - If either this channel or the target channel is closed
IOException - If some other I/O error occurs

transferTo

public final void transferTo(File file,
                             org.xlightweb.NonBlockingBodyDataSource.ITransferResultHandler resultHandler)
                      throws IOException,
                             FileNotFoundException,
                             ClosedChannelException
transfer the data of the this source channel to the given file

Parameters:
file - the file
resultHandler - the transferResultHandler
Throws:
FileNotFoundException - If the file does not exist
ClosedChannelException - If either this channel or the target channel is closed
IOException - If some other I/O error occurs

readByteBufferByDelimiter

public final ByteBuffer[] readByteBufferByDelimiter(String delimiter,
                                                    int maxLength)
                                             throws IOException,
                                                    MaxReadSizeExceededException
read a ByteBuffer by using a delimiter For performance reasons, the ByteBuffer readByteBuffer method is generally preferable to get bytes

Specified by:
readByteBufferByDelimiter in interface IDataSource
Parameters:
delimiter - the delimiter
maxLength - the max length of bytes that should be read. If the limit is exceeded a MaxReadSizeExceededException will been thrown
Returns:
the ByteBuffer
Throws:
MaxReadSizeExceededException - If the max read length has been exceeded and the delimiter hasn't been found
IOException - If some other I/O error occurs
BufferUnderflowException - if not enough data is available

readByteBufferByLength

public final ByteBuffer[] readByteBufferByLength(int length)
                                          throws IOException,
                                                 BufferUnderflowException
read a ByteBuffer

Specified by:
readByteBufferByLength in interface IDataSource
Parameters:
length - the length could be negative, in this case a empty array will be returned
Returns:
the ByteBuffer
Throws:
IOException - If some other I/O error occurs
BufferUnderflowException - if not enough data is available

onRead

protected void onRead()
               throws IOException
Throws:
IOException

isNetworkendpoint

protected abstract boolean isNetworkendpoint()

readByteBufferByDelimiter

public final ByteBuffer[] readByteBufferByDelimiter(String delimiter)
                                             throws IOException
read a ByteBuffer by using a delimiter. The default encoding will be used to decode the delimiter To avoid memory leaks the IReadWriteableConnection#readByteBufferByDelimiter(String, int) method is generally preferable
For performance reasons, the ByteBuffer readByteBuffer method is generally preferable to get bytes

Specified by:
readByteBufferByDelimiter in interface IDataSource
Parameters:
delimiter - the delimiter
Returns:
the ByteBuffer
Throws:
IOException - If some other I/O error occurs
BufferUnderflowException - if not enough data is available

readBytesByDelimiter

public final byte[] readBytesByDelimiter(String delimiter)
                                  throws IOException
read a byte array by using a delimiter For performance reasons, the ByteBuffer readByteBuffer method is generally preferable to get bytes

Specified by:
readBytesByDelimiter in interface IDataSource
Parameters:
delimiter - the delimiter
Returns:
the read bytes
Throws:
IOException - If some other I/O error occurs
BufferUnderflowException - if not enough data is available

readBytesByDelimiter

public final byte[] readBytesByDelimiter(String delimiter,
                                         int maxLength)
                                  throws IOException,
                                         MaxReadSizeExceededException
read a byte array by using a delimiter For performance reasons, the ByteBuffer readByteBuffer method is generally preferable to get bytes

Specified by:
readBytesByDelimiter in interface IDataSource
Parameters:
delimiter - the delimiter
maxLength - the max length of bytes that should be read. If the limit is exceeded a MaxReadSizeExceededException will been thrown
Returns:
the read bytes
Throws:
MaxReadSizeExceededException - If the max read length has been exceeded and the delimiter hasn�t been found
IOException - If some other I/O error occurs
BufferUnderflowException - if not enough data is available

readBytesByLength

public final byte[] readBytesByLength(int length)
                               throws IOException
read bytes by using a length definition

Specified by:
readBytesByLength in interface IDataSource
Parameters:
length - the amount of bytes to read
Returns:
the read bytes
Throws:
IOException - If some other I/O error occurs
IllegalArgumentException, - if the length parameter is negative
BufferUnderflowException - if not enough data is available

readStringByDelimiter

public final String readStringByDelimiter(String delimiter)
                                   throws IOException,
                                          UnsupportedEncodingException
read a string by using a delimiter

Specified by:
readStringByDelimiter in interface IDataSource
Parameters:
delimiter - the delimiter
Returns:
the string
Throws:
IOException - If some other I/O error occurs
UnsupportedEncodingException - if the default encoding is not supported
BufferUnderflowException - if not enough data is available

readStringByDelimiter

public final String readStringByDelimiter(String delimiter,
                                          String encoding)
                                   throws IOException,
                                          UnsupportedEncodingException
read a string by using a delimiter

Parameters:
delimiter - the delimiter
encoding - encoding
Returns:
the string
Throws:
IOException - If some other I/O error occurs
UnsupportedEncodingException - if the default encoding is not supported
BufferUnderflowException - if not enough data is available

readStringByDelimiter

public final String readStringByDelimiter(String delimiter,
                                          int maxLength)
                                   throws IOException,
                                          UnsupportedEncodingException,
                                          MaxReadSizeExceededException
read a string by using a delimiter

Specified by:
readStringByDelimiter in interface IDataSource
Parameters:
delimiter - the delimiter
maxLength - the max length of bytes that should be read. If the limit is exceeded a MaxReadSizeExceededException will been thrown
Returns:
the string
Throws:
MaxReadSizeExceededException - If the max read length has been exceeded and the delimiter hasn�t been found
IOException - If some other I/O error occurs
UnsupportedEncodingException - If the given encoding is not supported
BufferUnderflowException - if not enough data is available

readStringByDelimiter

public final String readStringByDelimiter(String delimiter,
                                          String encoding,
                                          int maxLength)
                                   throws IOException,
                                          UnsupportedEncodingException,
                                          MaxReadSizeExceededException
read a string by using a delimiter

Parameters:
delimiter - the delimiter
maxLength - the max length of bytes that should be read. If the limit is exceeded a MaxReadSizeExceededException will been thrown
encoding - the encoding
Returns:
the string
Throws:
MaxReadSizeExceededException - If the max read length has been exceeded and the delimiter hasn�t been found
IOException - If some other I/O error occurs
UnsupportedEncodingException - If the given encoding is not supported
BufferUnderflowException - if not enough data is available

readStringByLength

public final String readStringByLength(int length)
                                throws IOException,
                                       BufferUnderflowException
read a string by using a length definition

Specified by:
readStringByLength in interface IDataSource
Parameters:
length - the amount of bytes to read
Returns:
the string
Throws:
IOException - If some other I/O error occurs
UnsupportedEncodingException - if the given encoding is not supported
IllegalArgumentException, - if the length parameter is negative
BufferUnderflowException - if not enough data is available

readStringByLength

public final String readStringByLength(int length,
                                       String encoding)
                                throws IOException,
                                       BufferUnderflowException
read a string by using a length definition

Parameters:
length - the amount of bytes to read
encoding - the encoding
Returns:
the string
Throws:
IOException - If some other I/O error occurs
UnsupportedEncodingException - if the given encoding is not supported
IllegalArgumentException, - if the length parameter is negative
BufferUnderflowException - if not enough data is available

readDouble

public final double readDouble()
                        throws IOException
read a double

Specified by:
readDouble in interface IDataSource
Returns:
the double value
Throws:
IOException - If some other I/O error occurs
BufferUnderflowException - if not enough data is available

readLong

public final long readLong()
                    throws IOException
read a long

Specified by:
readLong in interface IDataSource
Returns:
the long value
Throws:
IOException - If some other I/O error occurs
BufferUnderflowException - if not enough data is available

readInt

public final int readInt()
                  throws IOException
read an int

Specified by:
readInt in interface IDataSource
Returns:
the int value
Throws:
IOException - If some other I/O error occurs
BufferUnderflowException - if not enough data is available

readShort

public final short readShort()
                      throws IOException
read a short value

Specified by:
readShort in interface IDataSource
Returns:
the short value
Throws:
IOException - If some other I/O error occurs
BufferUnderflowException - if not enough data is available

readByte

public final byte readByte()
                    throws IOException
read a byte

Specified by:
readByte in interface IDataSource
Returns:
the byte value
Throws:
IOException - If some other I/O error occurs
BufferUnderflowException - if not enough data is available

markReadPosition

public final void markReadPosition()
Marks the read position in the connection. Subsequent calls to resetToReadMark() will attempt to reposition the connection to this point.


resetToReadMark

public final boolean resetToReadMark()
Resets to the marked read position. If the connection has been marked, then attempt to reposition it at the mark.

Returns:
true, if reset was successful

removeReadMark

public final void removeReadMark()
remove the read mark


indexOf

public final int indexOf(String str)
                  throws IOException
Returns the index of the first occurrence of the given string.

Parameters:
str - any string
Returns:
if the string argument occurs as a substring within this object, then the index of the first character of the first such substring is returned; if it does not occur as a substring, -1 is returned.
Throws:
IOException - If some other I/O error occurs

indexOf

public final int indexOf(String str,
                         String encoding)
                  throws IOException,
                         MaxReadSizeExceededException
Returns the index of the first occurrence of the given string.

Parameters:
str - any string
encoding - the encoding to use
Returns:
if the string argument occurs as a substring within this object, then the index of the first character of the first such substring is returned; if it does not occur as a substring, -1 is returned.
Throws:
IOException - If some other I/O error occurs
MaxReadSizeExceededException

getReadBufferVersion

public int getReadBufferVersion()
                         throws IOException
get the version of read buffer. The version number increases, if the read buffer queue has been modified

Returns:
the read buffer version
Throws:
IOException - if an exception occurs

getBodyDataReceiveTimeoutMillis

public long getBodyDataReceiveTimeoutMillis()
returns body data receive timeout

Returns:
the body data receive timeout or null

toString

public String toString()

Overrides:
toString in class Object