org.xsocket.connection
Class AbstractNonBlockingStream

Package class diagram package AbstractNonBlockingStream
java.lang.Object
  extended by org.xsocket.connection.AbstractNonBlockingStream
All Implemented Interfaces:
Closeable, Channel, WritableByteChannel
Direct Known Subclasses:
NonBlockingConnection

public abstract class AbstractNonBlockingStream
extends Object
implements WritableByteChannel, Closeable

implementation base of a data stream.

This is a xSocket internal class and subject to change

Author:
grro@xsocket.org

Constructor Summary
AbstractNonBlockingStream()
           
 
Method Summary
protected  void appendDataToReadBuffer(ByteBuffer[] data)
          Deprecated.  
protected  void appendDataToReadBuffer(ByteBuffer[] data, int size)
          append data to the read buffer
 int available()
          get the number of available bytes to read
 void close()
           
protected  ByteBuffer[] drainWriteQueue()
          drains the write buffer
 Object getAttachment()
          Retrieves the current attachment.
 String getEncoding()
          gets the default encoding
 IConnection.FlushMode getFlushmode()
          return the flush mode
 int getReadBufferVersion()
          get the version of read buffer.
protected  int getReadQueueSize()
          returns the read queue size without additional check
protected  int getWriteBufferSize()
          gets the write buffer size
protected  int getWriteTransferChunkeSize()
          returns the default chunk size for writing
 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 isAutoflush()
          get autoflush
protected abstract  boolean isDataWriteable()
          returns true, if the underlying data sink is open
protected abstract  boolean isMoreInputDataExpected()
          returns true, if the underlying data source is open
protected  boolean isReadBufferEmpty()
          returns if the read buffer is empty
protected  boolean isWriteBufferEmpty()
          returns if the write buffer is empty
 void markReadPosition()
          Marks the read position in the connection.
 void markWritePosition()
          Marks the write position in the connection.
protected  void onPostAppend()
           
protected  void onPostRead()
          notification method which will be called after data has been read
protected  void onPreWrite(int size)
          call back method which will be called before writing data into the write queue
protected  void onWriteDataInserted()
          notification, that data has been inserted
protected  String printReadBuffer(String encoding)
          prints the read buffer content
protected  String printWriteBuffer(String encoding)
          prints the write buffer content
 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[] readByteBufferByDelimiter(String delimiter, String encoding)
          read a ByteBuffer by using a delimiter For performance reasons, the ByteBuffer readByteBuffer method is generally preferable to get bytes
 ByteBuffer[] readByteBufferByDelimiter(String delimiter, String encoding, 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[] readBytesByDelimiter(String delimiter, String encoding)
          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, String encoding, 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
 short readShort()
          read a short value
protected  ByteBuffer readSingleByteBuffer(int length)
          read a byte buffer by length.
 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
 void removeWriteMark()
          remove the write mark
protected  boolean reset()
          resets the stream
 boolean resetToReadMark()
          Resets to the marked read position.
 boolean resetToWriteMark()
          Resets to the marked write position.
 void setAttachment(Object obj)
          Attaches the given object to this connection
 void setAutoflush(boolean autoflush)
          set autoflush.
 void setEncoding(String defaultEncoding)
          sets the default encoding
 void setFlushmode(IConnection.FlushMode flushMode)
          set the flush mode

By setting the flush mode with ASYNC (default is SYNC) the data will be transferred to the underlying connection in a asynchronous way.
 long transferFrom(FileChannel fileChannel)
          transfer the data of the file channel to this data sink
 long transferFrom(ReadableByteChannel source)
          transfer the data of the source channel to this data sink
 long transferFrom(ReadableByteChannel source, int chunkSize)
          transfer the data of the source channel to this data sink
 long transferTo(WritableByteChannel target, int length)
          transfer the data of the this source channel to the given data sink
 int write(byte... bytes)
          writes bytes to the data sink
 int write(byte b)
          writes a byte to the data sink
 int write(byte[] bytes, int offset, int length)
          writes bytes to the data sink
 int write(ByteBuffer buffer)
          WritableByteChannel.write(ByteBuffer)
 long write(ByteBuffer[] buffers)
          see GatheringByteChannel.write(ByteBuffer[])
 long write(ByteBuffer[] srcs, int offset, int length)
          see GatheringByteChannel.write(ByteBuffer[], int, int)
 int write(double d)
          writes a double to the data sink
 int write(int i)
          writes a int to the data sink
 long write(List<ByteBuffer> buffers)
          writes a list of bytes to the data sink
 int write(long l)
          writes a long to the data sink
 int write(short s)
          writes a short to the data sink
 int write(String message)
          writes a message
 int write(String message, String encoding)
          writes a message
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface java.nio.channels.Channel
isOpen
 

Constructor Detail

AbstractNonBlockingStream

public AbstractNonBlockingStream()
Method Detail

close

public void close()
           throws IOException
Specified by:
close in interface Closeable
Specified by:
close in interface Channel
Throws:
IOException

setAttachment

public final void setAttachment(Object obj)
Attaches the given object to this connection

Parameters:
obj - The object to be attached; may be null

getAttachment

public final Object getAttachment()
Retrieves the current attachment.

Returns:
The object currently attached to this key, or null if there is no attachment

setEncoding

public final void setEncoding(String defaultEncoding)
sets the default encoding

Parameters:
defaultEncoding - the default encoding

getEncoding

public final String getEncoding()
gets the default encoding

Returns:
the default encoding

setFlushmode

public void setFlushmode(IConnection.FlushMode flushMode)
set the flush mode

By setting the flush mode with ASYNC (default is SYNC) the data will be transferred to the underlying connection in a asynchronous way.

Parameters:
flushMode - IConnection.FlushMode.ASYNC if flush should be performed asynchronous, IConnection.FlushMode.SYNC if flush should be perform synchronous

getFlushmode

public final IConnection.FlushMode getFlushmode()
return the flush mode

Returns:
the flush mode

getWriteTransferChunkeSize

protected int getWriteTransferChunkeSize()
returns the default chunk size for writing

Returns:
write chunk size

setAutoflush

public final void setAutoflush(boolean autoflush)
set autoflush. If autoflush is activated, each write call will cause a flush.

Parameters:
autoflush - true if autoflush should be activated

isAutoflush

public final boolean isAutoflush()
get autoflush

Returns:
true, if autoflush is activated

isMoreInputDataExpected

protected abstract boolean isMoreInputDataExpected()
returns true, if the underlying data source is open

Returns:
true, if the underlying data source is open

isDataWriteable

protected abstract boolean isDataWriteable()
returns true, if the underlying data sink is open

Returns:
true, if the underlying data sink is open

indexOf

public int indexOf(String str)
            throws IOException,
                   ClosedChannelException
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
ClosedChannelException - If the stream is closed

indexOf

public int indexOf(String str,
                   String encoding)
            throws IOException,
                   ClosedChannelException
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
ClosedChannelException - If the stream is closed

available

public int available()
              throws IOException
get the number of available bytes to read

Returns:
the number of available bytes or -1 if the end of stream is reached
Throws:
IOException - if an exception has been occurred

getReadQueueSize

protected int getReadQueueSize()
returns the read queue size without additional check

Returns:
the read queue size

getReadBufferVersion

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


onPostRead

protected void onPostRead()
                   throws IOException
notification method which will be called after data has been read

Throws:
IOException - If some other I/O error occurs

readByte

public byte readByte()
              throws IOException,
                     BufferUnderflowException,
                     ClosedChannelException
read a byte

Returns:
the byte value
Throws:
IOException - If some other I/O error occurs
BufferUnderflowException - if not enough data is available
ClosedChannelException - If the stream is closed

readShort

public short readShort()
                throws IOException,
                       BufferUnderflowException,
                       ClosedChannelException
read a short value

Returns:
the short value
Throws:
IOException - If some other I/O error occurs
BufferUnderflowException - if not enough data is available
ClosedChannelException - If the stream is closed

readInt

public int readInt()
            throws IOException,
                   BufferUnderflowException,
                   ClosedChannelException
read an int

Returns:
the int value
Throws:
IOException - If some other I/O error occurs
BufferUnderflowException - if not enough data is available
ClosedChannelException - If the stream is closed

readLong

public long readLong()
              throws IOException,
                     BufferUnderflowException,
                     ClosedChannelException
read a long

Returns:
the long value
Throws:
IOException - If some other I/O error occurs
BufferUnderflowException - if not enough data is available
ClosedChannelException - If the stream is closed

readDouble

public double readDouble()
                  throws IOException,
                         BufferUnderflowException,
                         ClosedChannelException
read a double

Returns:
the double value
Throws:
IOException - If some other I/O error occurs
BufferUnderflowException - if not enough data is available
ClosedChannelException - If the stream is closed

read

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

Throws:
IOException
ClosedChannelException

readByteBufferByDelimiter

public ByteBuffer[] readByteBufferByDelimiter(String delimiter)
                                       throws IOException,
                                              BufferUnderflowException,
                                              ClosedChannelException
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

Parameters:
delimiter - the delimiter
Returns:
the ByteBuffer
Throws:
IOException - If some other I/O error occurs
BufferUnderflowException - if not enough data is available
ClosedChannelException - If the stream is closed

readByteBufferByDelimiter

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

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
ClosedChannelException - If the stream is closed

readByteBufferByDelimiter

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

Parameters:
delimiter - the delimiter
encoding - the delimiter encoding
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
ClosedChannelException - If the stream is closed

readByteBufferByDelimiter

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

Parameters:
delimiter - the delimiter
encoding - the delimiter encoding
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
ClosedChannelException - If the stream is closed

readByteBufferByLength

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

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
ClosedChannelException - If the stream is closed

readBytesByDelimiter

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

Parameters:
delimiter - the delimiter
Returns:
the read bytes
Throws:
IOException - If some other I/O error occurs
BufferUnderflowException - if not enough data is available
ClosedChannelException - If the stream is closed

readBytesByDelimiter

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

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
ClosedChannelException - If the stream is closed

readBytesByDelimiter

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

Parameters:
delimiter - the delimiter
encoding - the delimiter encoding
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
ClosedChannelException - If the stream is closed

readBytesByDelimiter

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

Parameters:
delimiter - the delimiter
encoding - the delimiter encoding
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
ClosedChannelException - If the stream is closed

readBytesByLength

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

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
ClosedChannelException - If the stream is closed

readStringByDelimiter

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

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
ClosedChannelException - If the stream is closed

readStringByDelimiter

public String readStringByDelimiter(String delimiter,
                                    int maxLength)
                             throws IOException,
                                    BufferUnderflowException,
                                    UnsupportedEncodingException,
                                    MaxReadSizeExceededException,
                                    ClosedChannelException
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
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
ClosedChannelException - If the stream is closed

readStringByDelimiter

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

Parameters:
delimiter - the delimiter
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
ClosedChannelException - If the stream is closed

readStringByDelimiter

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

Parameters:
delimiter - the delimiter
encoding - the encoding
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
ClosedChannelException - If the stream is closed

readStringByLength

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

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
ClosedChannelException - If the stream is closed

readStringByLength

public String readStringByLength(int length,
                                 String encoding)
                          throws IOException,
                                 BufferUnderflowException,
                                 UnsupportedEncodingException,
                                 ClosedChannelException
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
ClosedChannelException - If the stream is closed

transferTo

public long transferTo(WritableByteChannel target,
                       int length)
                throws IOException,
                       ClosedChannelException,
                       BufferUnderflowException,
                       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
BufferUnderflowException - if not enough data is available
ClosedChannelException - If the stream is closed

readSingleByteBuffer

protected ByteBuffer readSingleByteBuffer(int length)
                                   throws IOException,
                                          ClosedChannelException,
                                          BufferUnderflowException,
                                          ClosedChannelException
read a byte buffer by length. If the underlying data is fragmented over several ByteBuffer, the ByteBuffers will be merged

Parameters:
length - the length
Returns:
the byte buffer
Throws:
IOException - If some other I/O error occurs
BufferUnderflowException - if not enough data is available
ClosedChannelException - If the stream is closed

write

public final int write(byte b)
                throws IOException,
                       BufferOverflowException,
                       ClosedChannelException
writes a byte to the data sink

Parameters:
b - the byte to write
Returns:
the number of written bytes
Throws:
BufferOverflowException - If the no enough space is available
IOException - If some other I/O error occurs
ClosedChannelException - If the stream is closed

write

public final int write(byte... bytes)
                throws IOException,
                       BufferOverflowException,
                       ClosedChannelException
writes bytes to the data sink

Parameters:
bytes - the bytes to write
Returns:
the number of written bytes
Throws:
BufferOverflowException - If the no enough space is available
IOException - If some other I/O error occurs
ClosedChannelException - If the stream is closed

write

public final int write(byte[] bytes,
                       int offset,
                       int length)
                throws IOException,
                       BufferOverflowException,
                       ClosedChannelException
writes bytes to the data sink

Parameters:
bytes - the bytes to write
offset - The offset of the sub array to be used; must be non-negative and no larger than array.length. The new buffer`s position will be set to this value.
length - The length of the sub array to be used; must be non-negative and no larger than array.length - offset. The new buffer`s limit will be set to offset + length.
Returns:
the number of written bytes
Throws:
BufferOverflowException - If the no enough space is available
IOException - If some other I/O error occurs
ClosedChannelException - If the stream is closed

write

public final int write(short s)
                throws IOException,
                       BufferOverflowException,
                       ClosedChannelException
writes a short to the data sink

Parameters:
s - the short value to write
Returns:
the number of written bytes
Throws:
BufferOverflowException - If the no enough space is available
IOException - If some other I/O error occurs
ClosedChannelException - If the stream is closed

write

public final int write(int i)
                throws IOException,
                       BufferOverflowException,
                       ClosedChannelException
writes a int to the data sink

Parameters:
i - the int value to write
Returns:
the number of written bytes
Throws:
BufferOverflowException - If the no enough space is available
IOException - If some other I/O error occurs
ClosedChannelException - If the stream is closed

write

public final int write(long l)
                throws IOException,
                       BufferOverflowException,
                       ClosedChannelException
writes a long to the data sink

Parameters:
l - the int value to write
Returns:
the number of written bytes
Throws:
BufferOverflowException - If the no enough space is available
IOException - If some other I/O error occurs
ClosedChannelException - If the stream is closed

write

public final int write(double d)
                throws IOException,
                       BufferOverflowException,
                       ClosedChannelException
writes a double to the data sink

Parameters:
d - the int value to write
Returns:
the number of written bytes
Throws:
BufferOverflowException - If the no enough space is available
IOException - If some other I/O error occurs
ClosedChannelException - If the stream is closed

write

public final int write(String message)
                throws IOException,
                       BufferOverflowException,
                       ClosedChannelException
writes a message

Parameters:
message - the message to write
Returns:
the number of written bytes
Throws:
BufferOverflowException - If the no enough space is available
IOException - If some other I/O error occurs
ClosedChannelException - If the stream is closed

write

public final int write(String message,
                       String encoding)
                throws IOException,
                       BufferOverflowException,
                       ClosedChannelException
writes a message

Parameters:
message - the message to write
encoding - the encoding
Returns:
the number of written bytes
Throws:
BufferOverflowException - If the no enough space is available
IOException - If some other I/O error occurs
ClosedChannelException - If the stream is closed

write

public final long write(List<ByteBuffer> buffers)
                 throws IOException,
                        BufferOverflowException,
                        ClosedChannelException
writes a list of bytes to the data sink

Parameters:
buffers - the bytes to write
Returns:
the number of written bytes
Throws:
BufferOverflowException - If the no enough space is available
IOException - If some other I/O error occurs
ClosedChannelException - If the stream is closed

write

public final long write(ByteBuffer[] srcs,
                        int offset,
                        int length)
                 throws IOException,
                        ClosedChannelException
see GatheringByteChannel.write(ByteBuffer[], int, int)

Throws:
IOException
ClosedChannelException

write

public final long write(ByteBuffer[] buffers)
                 throws IOException,
                        BufferOverflowException,
                        ClosedChannelException
see GatheringByteChannel.write(ByteBuffer[])

Throws:
IOException
BufferOverflowException
ClosedChannelException

write

public final int write(ByteBuffer buffer)
                throws IOException,
                       BufferOverflowException,
                       ClosedChannelException
WritableByteChannel.write(ByteBuffer)

Specified by:
write in interface WritableByteChannel
Throws:
IOException
BufferOverflowException
ClosedChannelException

onPreWrite

protected void onPreWrite(int size)
                   throws BufferOverflowException
call back method which will be called before writing data into the write queue

Parameters:
size - the size to write
Throws:
BufferOverflowException - if the write buffer max size is exceeded

transferFrom

public final long transferFrom(FileChannel fileChannel)
                        throws ClosedChannelException,
                               IOException,
                               SocketTimeoutException,
                               ClosedChannelException
transfer the data of the file channel to this data sink

Parameters:
fileChannel - the file channel
Returns:
the number of transfered bytes
Throws:
BufferOverflowException - If the no enough space is available
IOException - If some other I/O error occurs
ClosedChannelException - If the stream is closed
SocketTimeoutException

transferFrom

public long transferFrom(ReadableByteChannel source)
                  throws IOException,
                         BufferOverflowException,
                         ClosedChannelException
transfer the data of the source channel to this data sink

Parameters:
source - the source channel
Returns:
the number of transfered bytes
Throws:
BufferOverflowException - If the no enough space is available
IOException - If some other I/O error occurs
ClosedChannelException - If the stream is closed

transferFrom

public final long transferFrom(ReadableByteChannel source,
                               int chunkSize)
                        throws IOException,
                               BufferOverflowException,
                               ClosedChannelException
transfer the data of the source channel to this data sink

Parameters:
source - the source channel
chunkSize - the chunk size to use
Returns:
the number of transfered bytes
Throws:
BufferOverflowException - If the no enough space is available
IOException - If some other I/O error occurs
ClosedChannelException - If the stream is closed

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.


markWritePosition

public final void markWritePosition()
Marks the write position in the connection.


resetToWriteMark

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

Returns:
true, if reset was successful

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


removeWriteMark

public final void removeWriteMark()
remove the write mark


reset

protected boolean reset()
resets the stream

Returns:
true, if the stream has been reset

onWriteDataInserted

protected void onWriteDataInserted()
                            throws IOException,
                                   ClosedChannelException
notification, that data has been inserted

Throws:
IOException - if an exception occurs
ClosedChannelException - if the stream is closed

getWriteBufferSize

protected final int getWriteBufferSize()
gets the write buffer size

Returns:
the write buffer size

isWriteBufferEmpty

protected final boolean isWriteBufferEmpty()
returns if the write buffer is empty

Returns:
true, if the write buffer is empty

drainWriteQueue

protected ByteBuffer[] drainWriteQueue()
drains the write buffer

Returns:
the write buffer content

isReadBufferEmpty

protected final boolean isReadBufferEmpty()
returns if the read buffer is empty

Returns:
true, if the read buffer is empty

appendDataToReadBuffer

protected final void appendDataToReadBuffer(ByteBuffer[] data,
                                            int size)
append data to the read buffer

Parameters:
data - the data to append

appendDataToReadBuffer

protected final void appendDataToReadBuffer(ByteBuffer[] data)
Deprecated. 


onPostAppend

protected void onPostAppend()

printReadBuffer

protected final String printReadBuffer(String encoding)
prints the read buffer content

Parameters:
encoding - the encoding
Returns:
the read buffer content

printWriteBuffer

protected final String printWriteBuffer(String encoding)
prints the write buffer content

Parameters:
encoding - the encoding
Returns:
the write buffer content


Copyright 2008 xSocket.org