org.xlightweb
Class BodyDataSource

Package class diagram package BodyDataSource
java.lang.Object
  extended by org.xlightweb.BodyDataSource
All Implemented Interfaces:
Closeable, Channel, ReadableByteChannel, IDataSource
Direct Known Subclasses:
BlockingBodyDataSource

public class BodyDataSource
extends Object
implements IDataSource, ReadableByteChannel, Closeable

I/O resource capable of providing body data in a blocking way. Read operations will be suspended, if not enough data is available. The BBodyDataSource wraps a NonBlockingBodyDataSource


Field Summary
static int DEFAULT_RECEIVE_TIMEOUT
           
 
Method Summary
 void close()
          
 int getReceiveTimeoutSec()
          gets receive time out by reading data
 boolean isMultipart()
          return true if the body is a mulipart
 boolean isOpen()
          returns, if the connection is open.
 void markReadPosition()
          Marks the read position in the connection.
 int read(ByteBuffer buffer)
          .
 byte readByte()
          
 ByteBuffer[] readByteBuffer()
          read the body
 ByteBuffer[] readByteBufferByDelimiter(String delimiter)
          
 ByteBuffer[] readByteBufferByDelimiter(String delimiter, int maxLength)
          
 ByteBuffer[] readByteBufferByLength(int length)
          
 byte[] readBytes()
          read the body
 byte[] readBytesByDelimiter(String delimiter)
          
 byte[] readBytesByDelimiter(String delimiter, int maxLength)
          
 byte[] readBytesByLength(int length)
          
 double readDouble()
          
 int readInt()
          
 long readLong()
          
 IPart readPart()
          read the next part of the mulipart body.
 List<IPart> readParts()
          return all parts of the multipart body.
 short readShort()
          
 String readString()
          read the body
 String readString(String encoding)
          read the body
 String readStringByDelimiter(String delimiter)
          
 String readStringByDelimiter(String delimiter, int maxLength)
          
 String readStringByDelimiter(String delimiter, int maxLength, String encoding)
          read a string by using a delimiter
 String readStringByDelimiter(String delimiter, String encoding)
          read a string by using a delimiter
 String readStringByLength(int length)
          
 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 setReceiveTimeoutSec(int timeout)
          sets the receive time out by reading data
 int size()
          get the body size
 InputStream toInputStream()
          returns this ReadableByteChannel as InputStream
 Reader toReader()
          returns this ReadableByteChannel as Reader
 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
 long transferTo(OutputStream dataSink)
          transfer the available data of the this source channel to the given data sink
 long transferTo(WritableByteChannel target)
          transfers the content to the given channel
 long transferTo(WritableByteChannel target, int length)
          
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

DEFAULT_RECEIVE_TIMEOUT

public static final int DEFAULT_RECEIVE_TIMEOUT
See Also:
Constant Field Values
Method Detail

setReceiveTimeoutSec

public void setReceiveTimeoutSec(int timeout)
sets the receive time out by reading data

Parameters:
timeout - the receive timeout

getReceiveTimeoutSec

public int getReceiveTimeoutSec()
gets receive time out by reading data

Returns:
the receive timeout

isOpen

public boolean isOpen()
returns, if the connection is open.

Specified by:
isOpen in interface Channel
Returns:
true if the connection is open

isMultipart

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

Returns:
true, if the body is a mulipart

close

public void close()
           throws IOException

Specified by:
close in interface Closeable
Specified by:
close in interface Channel
Throws:
IOException

size

public int size()
         throws IOException
get the body size

Returns:
the body size
Throws:
IOException - if an exception occurs

markReadPosition

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


resetToReadMark

public 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 void removeReadMark()
remove the read mark


readPart

public IPart readPart()
               throws NoMultipartTypeException,
                      IOException
read the next part of the mulipart body. isMultipart() can be used to verify if the body is a multipart one
  // ...
   
  BlockingBodyDataSource body = response.getBlockingBody();
  if (body.isMultipart()) {
      IPart part = body.readPart();
      // ...
  } else {
      // ...
  }
 

Returns:
the next part
Throws:
IOException - if an exception occurs
NoMultipartTypeException - if the body type is not a multipart type

readParts

public List<IPart> readParts()
                      throws NoMultipartTypeException,
                             IOException
return all parts of the multipart body. isMultipart() can be used to verify if the body is a multipart one
  // ...
   
  BlockingBodyDataSource body = response.getBlockingBody();
  if (body.isMultipart()) {
      List parts = body.readParts();
      // ...
  } else {
      // ...
  }
 

Returns:
the list of all parts
Throws:
IOException - if an exception occurs
NoMultipartTypeException - if the surrounding body type is not a multipart type

readByteBuffer

public ByteBuffer[] readByteBuffer()
                            throws IOException
read the body

Returns:
the body as byte buffer
Throws:
IOException - if an exception occurs

readBytes

public byte[] readBytes()
                 throws IOException
read the body

Returns:
the body as bytes
Throws:
IOException - if an exception occurs

readString

public String readString()
                  throws IOException
read the body

Returns:
the body as string
Throws:
IOException - if an exception occurs

readString

public String readString(String encoding)
                  throws IOException
read the body

Parameters:
encoding - the encoding
Returns:
the body as string
Throws:
IOException - if an exception occurs

read

public int read(ByteBuffer buffer)
         throws IOException
.

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

readByte

public byte readByte()
              throws IOException,
                     BufferUnderflowException,
                     SocketTimeoutException

Specified by:
readByte in interface IDataSource
Throws:
IOException
BufferUnderflowException
SocketTimeoutException

readShort

public short readShort()
                throws IOException,
                       BufferUnderflowException,
                       SocketTimeoutException

Specified by:
readShort in interface IDataSource
Throws:
IOException
BufferUnderflowException
SocketTimeoutException

readInt

public int readInt()
            throws IOException,
                   BufferUnderflowException,
                   SocketTimeoutException

Specified by:
readInt in interface IDataSource
Throws:
IOException
BufferUnderflowException
SocketTimeoutException

readLong

public long readLong()
              throws IOException,
                     BufferUnderflowException,
                     SocketTimeoutException

Specified by:
readLong in interface IDataSource
Throws:
IOException
BufferUnderflowException
SocketTimeoutException

readDouble

public double readDouble()
                  throws IOException,
                         BufferUnderflowException,
                         SocketTimeoutException

Specified by:
readDouble in interface IDataSource
Throws:
IOException
BufferUnderflowException
SocketTimeoutException

readByteBufferByDelimiter

public ByteBuffer[] readByteBufferByDelimiter(String delimiter)
                                       throws IOException,
                                              BufferUnderflowException,
                                              SocketTimeoutException

Specified by:
readByteBufferByDelimiter in interface IDataSource
Throws:
IOException
BufferUnderflowException
SocketTimeoutException

readByteBufferByDelimiter

public ByteBuffer[] readByteBufferByDelimiter(String delimiter,
                                              int maxLength)
                                       throws IOException,
                                              BufferUnderflowException,
                                              MaxReadSizeExceededException,
                                              SocketTimeoutException

Specified by:
readByteBufferByDelimiter in interface IDataSource
Throws:
IOException
BufferUnderflowException
MaxReadSizeExceededException
SocketTimeoutException

readByteBufferByLength

public ByteBuffer[] readByteBufferByLength(int length)
                                    throws IOException,
                                           BufferUnderflowException,
                                           SocketTimeoutException

Specified by:
readByteBufferByLength in interface IDataSource
Throws:
IOException
BufferUnderflowException
SocketTimeoutException

readBytesByDelimiter

public byte[] readBytesByDelimiter(String delimiter)
                            throws IOException,
                                   BufferUnderflowException,
                                   SocketTimeoutException

Specified by:
readBytesByDelimiter in interface IDataSource
Throws:
IOException
BufferUnderflowException
SocketTimeoutException

readBytesByDelimiter

public byte[] readBytesByDelimiter(String delimiter,
                                   int maxLength)
                            throws IOException,
                                   BufferUnderflowException,
                                   MaxReadSizeExceededException,
                                   SocketTimeoutException

Specified by:
readBytesByDelimiter in interface IDataSource
Throws:
IOException
BufferUnderflowException
MaxReadSizeExceededException
SocketTimeoutException

readBytesByLength

public byte[] readBytesByLength(int length)
                         throws IOException,
                                BufferUnderflowException,
                                SocketTimeoutException

Specified by:
readBytesByLength in interface IDataSource
Throws:
IOException
BufferUnderflowException
SocketTimeoutException

readStringByDelimiter

public String readStringByDelimiter(String delimiter)
                             throws IOException,
                                    BufferUnderflowException,
                                    UnsupportedEncodingException,
                                    SocketTimeoutException

Specified by:
readStringByDelimiter in interface IDataSource
Throws:
IOException
BufferUnderflowException
UnsupportedEncodingException
SocketTimeoutException

readStringByDelimiter

public String readStringByDelimiter(String delimiter,
                                    String encoding)
                             throws IOException,
                                    BufferUnderflowException,
                                    UnsupportedEncodingException,
                                    SocketTimeoutException
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
SocketTimeoutException - if a timeout occurs

readStringByDelimiter

public String readStringByDelimiter(String delimiter,
                                    int maxLength)
                             throws IOException,
                                    BufferUnderflowException,
                                    UnsupportedEncodingException,
                                    MaxReadSizeExceededException,
                                    SocketTimeoutException

Specified by:
readStringByDelimiter in interface IDataSource
Throws:
IOException
BufferUnderflowException
UnsupportedEncodingException
MaxReadSizeExceededException
SocketTimeoutException

readStringByDelimiter

public String readStringByDelimiter(String delimiter,
                                    int maxLength,
                                    String encoding)
                             throws IOException,
                                    BufferUnderflowException,
                                    UnsupportedEncodingException,
                                    MaxReadSizeExceededException,
                                    SocketTimeoutException
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
SocketTimeoutException - if the timout is reached

readStringByLength

public String readStringByLength(int length)
                          throws IOException,
                                 BufferUnderflowException,
                                 UnsupportedEncodingException,
                                 SocketTimeoutException

Specified by:
readStringByLength in interface IDataSource
Throws:
IOException
BufferUnderflowException
UnsupportedEncodingException
SocketTimeoutException

readStringByLength

public String readStringByLength(int length,
                                 String encoding)
                          throws IOException,
                                 BufferUnderflowException,
                                 UnsupportedEncodingException,
                                 SocketTimeoutException
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
SocketTimeoutException - if the timeout is reached
BufferUnderflowException - if not enough data is available
UnsupportedEncodingException

transferTo

public long transferTo(WritableByteChannel target,
                       int length)
                throws IOException,
                       BufferUnderflowException,
                       SocketTimeoutException

Specified by:
transferTo in interface IDataSource
Throws:
IOException
BufferUnderflowException
SocketTimeoutException

transferTo

public long transferTo(WritableByteChannel target)
                throws IOException,
                       BufferUnderflowException,
                       SocketTimeoutException
transfers the content to the given channel

Parameters:
target - the target channel
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
BufferUnderflowException
SocketTimeoutException

transferTo

public long transferTo(BodyDataSink dataSink)
                throws ProtocolException,
                       IOException,
                       ClosedChannelException,
                       BufferUnderflowException
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
BufferUnderflowException - if not enough data is available
ProtocolException

transferTo

public long transferTo(OutputStream dataSink)
                throws ProtocolException,
                       IOException,
                       ClosedChannelException,
                       BufferUnderflowException
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
BufferUnderflowException - if not enough data is available
ProtocolException

transferTo

public long transferTo(BodyDataSink dataSink,
                       int length)
                throws ProtocolException,
                       IOException,
                       ClosedChannelException,
                       BufferUnderflowException
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
BufferUnderflowException - if not enough data is available
ProtocolException

toInputStream

public InputStream toInputStream()
returns this ReadableByteChannel as InputStream

Returns:
the input stream

toReader

public Reader toReader()
returns this ReadableByteChannel as Reader

Returns:
the input stream

toString

public String toString()

Overrides:
toString in class Object