org.xsocket.connection
Class BlockingConnection

Package class diagram package BlockingConnection
java.lang.Object
  extended by org.xsocket.connection.BlockingConnection
All Implemented Interfaces:
Closeable, Flushable, Channel, GatheringByteChannel, ReadableByteChannel, WritableByteChannel, IBlockingConnection, IConnection, IDataSink, IDataSource

public class BlockingConnection
extends Object
implements IBlockingConnection

Implementation of the IBlockingConnection interface. Internally a INonBlockingConnection will be used. A BlockingConnection wraps a INonBlockingConnection. There are two ways to create a BlockingConnection:



A newly created connection is in the open state. Write or read methods can be called immediately

The methods of this class are not thread-safe.

Author:
grro@xsocket.org

Nested Class Summary
 
Nested classes/interfaces inherited from interface org.xsocket.connection.IConnection
IConnection.FlushMode
 
Field Summary
 
Fields inherited from interface org.xsocket.connection.IBlockingConnection
DEFAULT_RECEIVE_TIMEOUT
 
Fields inherited from interface org.xsocket.connection.IConnection
DEFAULT_AUTOFLUSH, DEFAULT_CONNECTION_TIMEOUT_MILLIS, DEFAULT_FLUSH_MODE, DEFAULT_IDLE_TIMEOUT_MILLIS, INITIAL_DEFAULT_ENCODING, MAX_TIMEOUT_MILLIS, SO_KEEPALIVE, SO_LINGER, SO_RCVBUF, SO_REUSEADDR, SO_SNDBUF, SO_TIMEOUT, TCP_NODELAY
 
Constructor Summary
BlockingConnection(InetAddress address, int port)
          constructor
BlockingConnection(InetAddress address, int port, int connectTimeoutMillis)
          constructor
BlockingConnection(InetAddress address, int port, int connectTimeoutMillis, Map<String,Object> options, SSLContext sslContext, boolean sslOn)
          constructor
BlockingConnection(InetAddress address, int port, int connectTimeoutMillis, SSLContext sslContext, boolean sslOn)
          constructor
BlockingConnection(InetAddress address, int port, Map<String,Object> options, SSLContext sslContext, boolean sslOn)
          constructor
BlockingConnection(InetAddress address, int port, SSLContext sslContext, boolean sslOn)
          constructor
BlockingConnection(INonBlockingConnection delegee)
          Constructor.
BlockingConnection(String hostname, int port)
          constructor.
BlockingConnection(String hostname, int port, Map<String,Object> options)
          constructor.
BlockingConnection(String hostname, int port, SSLContext sslContext, boolean sslOn)
          constructor
 
Method Summary
 void activateSecuredMode()
          ad hoc activation of a secured mode (SSL).
 void close()
          
 void flush()
          flush the send buffer.
 Object getAttachment()
          Retrieves the current attachment.
 long getConnectionTimeoutMillis()
          gets the connection timeout
 String getEncoding()
          gets the encoding (used by string related methods like write(String) ...)
 IConnection.FlushMode getFlushmode()
          return the flush mode
 String getId()
          returns the id
 long getIdleTimeoutMillis()
          returns the idle timeout in millis.
 InetAddress getLocalAddress()
          returns the local address
 int getLocalPort()
          returns the local port
 int getMaxReadBufferThreshold()
          get the max app read buffer size.
 Object getOption(String name)
          returns the value of a option
 Map<String,Class> getOptions()
          Returns an unmodifiable map of the options supported by this end point.
 int getPendingWriteDataSize()
          returns the size of the data which have already been written, but not yet transferred to the underlying socket.
 int getReceiveTimeoutMillis()
          get the timeout for calling read methods in millis
 long getRemainingMillisToConnectionTimeout()
          returns the remaining time before a connection timeout occurs
 long getRemainingMillisToIdleTimeout()
          returns the remaining time before a idle timeout occurs
 InetAddress getRemoteAddress()
          returns the remote address
 int getRemotePort()
          returns the port of the remote end point
 boolean isAutoflush()
          get autoflush
 boolean isOpen()
          return if the data source is open.
 boolean isSecure()
          returns if the connection is in secured mode
 void markReadPosition()
          Marks the read position in the connection.
 void markWritePosition()
          Marks the write position in the connection.
 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
 ByteBuffer[] readByteBufferByDelimiter(String delimiter, String encoding)
          read a ByteBuffer by using a delimiter.
 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 by using a length definition
 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

For performance reasons, the ByteBuffer readByteBuffer method is generally preferable to get bytes
 double readDouble()
          read a double
 int readInt()
          read an int
 long readLong()
          read a long
 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
 void removeWriteMark()
          remove the write mark
 boolean resetToReadMark()
          Resets to the marked read position.
 boolean resetToWriteMark()
          Resets to the marked write position.
 void resumeRead()
          resume reading data from the underlying subsystem
 void setAttachment(Object obj)
          Attaches the given object to this connection
 void setAutoflush(boolean autoflush)
          set autoflush.
 void setConnectionTimeoutMillis(long timeoutMillis)
          sets the max time for a connections.
 void setEncoding(String defaultEncoding)
          sets the encoding (used by string related methods like write(String) ...)
 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.
 void setIdleTimeoutMillis(long timeoutInMillis)
          sets the idle timeout in millis
 void setMaxReadBufferThreshold(int size)
          set the max app read buffer threshold
 void setOption(String name, Object value)
          sets the value of a option.
 void setReceiveTimeoutMillis(int timeout)
          set the timeout for calling read methods in millis
 void suspendRead()
          suspend reading data from the underlying subsystem
 String toString()
           
 long transferFrom(FileChannel source)
          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 by using the default chunk size
 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
 long write(ArrayList<ByteBuffer> buffers)
          
 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)
          see 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)
          write a message
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

BlockingConnection

public BlockingConnection(String hostname,
                          int port)
                   throws IOException
constructor.

Parameters:
hostname - the remote host
port - the port of the remote host to connect
Throws:
IOException - If some other I/O error occurs

BlockingConnection

public BlockingConnection(String hostname,
                          int port,
                          Map<String,Object> options)
                   throws IOException
constructor.

Parameters:
hostname - the remote host
port - the port of the remote host to connect
options - the socket options
Throws:
IOException - If some other I/O error occurs

BlockingConnection

public BlockingConnection(InetAddress address,
                          int port)
                   throws IOException
constructor

Parameters:
address - the remote host address
port - the remote host port
Throws:
IOException - If some other I/O error occurs

BlockingConnection

public BlockingConnection(InetAddress address,
                          int port,
                          int connectTimeoutMillis)
                   throws IOException
constructor

Parameters:
address - the remote host address
port - the remote host port
connectTimeoutMillis - the timeout of the connect procedure
Throws:
IOException - If some other I/O error occurs

BlockingConnection

public BlockingConnection(InetAddress address,
                          int port,
                          SSLContext sslContext,
                          boolean sslOn)
                   throws IOException
constructor

Parameters:
address - the remote host name
port - the remote host port
sslContext - the sslContext to use
sslOn - true, activate SSL mode. false, ssl can be activated by user (see IReadWriteableConnection#activateSecuredMode())
Throws:
IOException - If some other I/O error occurs

BlockingConnection

public BlockingConnection(InetAddress address,
                          int port,
                          int connectTimeoutMillis,
                          SSLContext sslContext,
                          boolean sslOn)
                   throws IOException
constructor

Parameters:
address - the remote host name
port - the remote host port
connectTimeoutMillis - the timeout of the connect procedure
sslContext - the sslContext to use
sslOn - true, activate SSL mode. false, ssl can be activated by user (see IReadWriteableConnection#activateSecuredMode())
Throws:
IOException - If some other I/O error occurs

BlockingConnection

public BlockingConnection(InetAddress address,
                          int port,
                          Map<String,Object> options,
                          SSLContext sslContext,
                          boolean sslOn)
                   throws IOException
constructor

Parameters:
address - the remote host name
port - the remote host port
options - the socket options
sslContext - the sslContext to use
sslOn - true, activate SSL mode. false, ssl can be activated by user (see IReadWriteableConnection#activateSecuredMode())
Throws:
IOException - If some other I/O error occurs

BlockingConnection

public BlockingConnection(InetAddress address,
                          int port,
                          int connectTimeoutMillis,
                          Map<String,Object> options,
                          SSLContext sslContext,
                          boolean sslOn)
                   throws IOException
constructor

Parameters:
address - the remote host name
port - the remote host port
connectTimeoutMillis - the timeout of the connect procedure
options - the socket options
sslContext - the sslContext to use
sslOn - true, activate SSL mode. false, ssl can be activated by user (see IReadWriteableConnection#activateSecuredMode())
Throws:
IOException - If some other I/O error occurs

BlockingConnection

public BlockingConnection(String hostname,
                          int port,
                          SSLContext sslContext,
                          boolean sslOn)
                   throws IOException
constructor

Parameters:
hostname - the remote host name
port - the remote host port
sslContext - the sslContext to use
sslOn - true, activate SSL mode. false, ssl can be activated by user (see IReadWriteableConnection#activateSecuredMode()) * @throws IOException If some other I/O error occurs
Throws:
IOException

BlockingConnection

public BlockingConnection(INonBlockingConnection delegee)
                   throws IOException
Constructor.

By creating a BlokingConnection based on the given NonBlockingConnection the assigned handler of the NonBlockingConnection will be removed (if exists). Take care by using this on the server-side (see tutorial for more informations).

Parameters:
delegee - the underlying non blocking connection
Throws:
IOException - If some other I/O error occurs
Method Detail

setReceiveTimeoutMillis

public void setReceiveTimeoutMillis(int timeout)
                             throws IOException
set the timeout for calling read methods in millis

Specified by:
setReceiveTimeoutMillis in interface IBlockingConnection
Parameters:
timeout - the timeout in millis
Throws:
IOException - If some other I/O error occurs

getReceiveTimeoutMillis

public final int getReceiveTimeoutMillis()
                                  throws IOException
get the timeout for calling read methods in millis

Specified by:
getReceiveTimeoutMillis in interface IBlockingConnection
Returns:
the timeout in millis
Throws:
IOException - If some other I/O error occurs

getMaxReadBufferThreshold

public int getMaxReadBufferThreshold()
get the max app read buffer size. If the read buffer size exceeds this limit the connection will stop receiving data. The read buffer size can be higher the limit (max size = maxReadBufferThreshold + socket read buffer size * 2)

Specified by:
getMaxReadBufferThreshold in interface IBlockingConnection
Returns:
the max read buffer threshold

setMaxReadBufferThreshold

public void setMaxReadBufferThreshold(int size)
set the max app read buffer threshold

Specified by:
setMaxReadBufferThreshold in interface IBlockingConnection

setEncoding

public final void setEncoding(String defaultEncoding)
sets the encoding (used by string related methods like write(String) ...)

Specified by:
setEncoding in interface IBlockingConnection
Parameters:
defaultEncoding - the encoding

getEncoding

public final String getEncoding()
gets the encoding (used by string related methods like write(String) ...)

Specified by:
getEncoding in interface IBlockingConnection
Returns:
the encoding

isOpen

public final boolean isOpen()
return if the data source is open. Default is true

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

close

public final void close()
                 throws IOException

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

flush

public final void flush()
                 throws ClosedChannelException,
                        IOException,
                        SocketTimeoutException
flush the send buffer. The method call will block until the outgoing data has been flushed into the underlying os-specific send buffer.

Specified by:
flush in interface Flushable
Specified by:
flush in interface IBlockingConnection
Throws:
ClosedChannelException - if the underlying channel is closed
IOException - If some other I/O error occurs
SocketTimeoutException - If the timeout has been reached

getId

public String getId()
returns the id

Specified by:
getId in interface IConnection
Returns:
id

getRemoteAddress

public final InetAddress getRemoteAddress()
returns the remote address

Specified by:
getRemoteAddress in interface IConnection
Returns:
the remote address

getRemotePort

public final int getRemotePort()
returns the port of the remote end point

Specified by:
getRemotePort in interface IConnection
Returns:
the remote port

getLocalAddress

public final InetAddress getLocalAddress()
returns the local address

Specified by:
getLocalAddress in interface IConnection
Returns:
the local IP address or InetAddress.anyLocalAddress() if the socket is not bound yet.

getLocalPort

public final int getLocalPort()
returns the local port

Specified by:
getLocalPort in interface IConnection
Returns:
the local port

getPendingWriteDataSize

public final int getPendingWriteDataSize()
returns the size of the data which have already been written, but not yet transferred to the underlying socket.

Specified by:
getPendingWriteDataSize in interface IBlockingConnection
Returns:
the size of the pending data to write

suspendRead

public final void suspendRead()
                       throws IOException
suspend reading data from the underlying subsystem

Specified by:
suspendRead in interface IBlockingConnection
Throws:
IOException - If some other I/O error occurs

resumeRead

public final void resumeRead()
                      throws IOException
resume reading data from the underlying subsystem

Specified by:
resumeRead in interface IBlockingConnection
Throws:
IOException - If some other I/O error occurs

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.

Specified by:
setFlushmode in interface IBlockingConnection
Parameters:
flushMode - FlushMode#ASYNC if flush should be performed asynchronous, FlushMode#SYNC if flush should be perform synchronous

getFlushmode

public IConnection.FlushMode getFlushmode()
return the flush mode

Specified by:
getFlushmode in interface IBlockingConnection
Returns:
the flush mode

setOption

public final void setOption(String name,
                            Object value)
                     throws IOException
sets the value of a option.

A good article for tuning can be found here http://www.onlamp.com/lpt/a/6324

Specified by:
setOption in interface IConnection
Parameters:
name - the name of the option
value - the value of the option
Throws:
IOException - In an I/O error occurs

getOption

public final Object getOption(String name)
                       throws IOException
returns the value of a option

Specified by:
getOption in interface IConnection
Parameters:
name - the name of the option
Returns:
the value of the option
Throws:
IOException - In an I/O error occurs

getOptions

public final Map<String,Class> getOptions()
Returns an unmodifiable map of the options supported by this end point. The key in the returned map is the name of a option, and its value is the type of the option value. The returned map will never contain null keys or values.

Specified by:
getOptions in interface IConnection
Returns:
An unmodifiable map of the options supported by this channel

setIdleTimeoutMillis

public final void setIdleTimeoutMillis(long timeoutInMillis)
sets the idle timeout in millis

Specified by:
setIdleTimeoutMillis in interface IConnection

getIdleTimeoutMillis

public final long getIdleTimeoutMillis()
returns the idle timeout in millis.

Specified by:
getIdleTimeoutMillis in interface IConnection
Returns:
idle timeout in millis

setConnectionTimeoutMillis

public final void setConnectionTimeoutMillis(long timeoutMillis)
sets the max time for a connections. By exceeding this time the connection will be terminated

Specified by:
setConnectionTimeoutMillis in interface IConnection

getConnectionTimeoutMillis

public final long getConnectionTimeoutMillis()
gets the connection timeout

Specified by:
getConnectionTimeoutMillis in interface IConnection
Returns:
connection timeout

getRemainingMillisToConnectionTimeout

public long getRemainingMillisToConnectionTimeout()
returns the remaining time before a connection timeout occurs

Specified by:
getRemainingMillisToConnectionTimeout in interface IConnection
Returns:
the remaining time

getRemainingMillisToIdleTimeout

public long getRemainingMillisToIdleTimeout()
returns the remaining time before a idle timeout occurs

Specified by:
getRemainingMillisToIdleTimeout in interface IConnection
Returns:
the remaining time

setAttachment

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

Specified by:
setAttachment in interface IConnection
Parameters:
obj - The object to be attached; may be null

getAttachment

public final Object getAttachment()
Retrieves the current attachment.

Specified by:
getAttachment in interface IConnection
Returns:
The object currently attached to this key, or null if there is no attachment

setAutoflush

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

Specified by:
setAutoflush in interface IBlockingConnection
Parameters:
autoflush - true if autoflush should be activated

isAutoflush

public final boolean isAutoflush()
get autoflush

Specified by:
isAutoflush in interface IBlockingConnection
Returns:
true, if autoflush is activated

activateSecuredMode

public final void activateSecuredMode()
                               throws IOException
ad hoc activation of a secured mode (SSL). By performing of this method all remaining data to send will be flushed. After this all data will be sent and received in the secured mode

Specified by:
activateSecuredMode in interface IBlockingConnection
Throws:
IOException - If some other I/O error occurs

isSecure

public boolean isSecure()
returns if the connection is in secured mode

Specified by:
isSecure in interface IBlockingConnection
Returns:
true, if the connection is in secured mode

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.

Specified by:
markReadPosition in interface IBlockingConnection

markWritePosition

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

Specified by:
markWritePosition in interface IBlockingConnection

read

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

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

readByte

public final byte readByte()
                    throws IOException,
                           SocketTimeoutException
read a byte

Specified by:
readByte in interface IDataSource
Returns:
the byte value
Throws:
IOException - If an I/O error occurs
SocketTimeoutException

readShort

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

Specified by:
readShort in interface IDataSource
Returns:
the short value
Throws:
IOException - If an I/O error occurs
SocketTimeoutException

readInt

public final int readInt()
                  throws IOException,
                         SocketTimeoutException
read an int

Specified by:
readInt in interface IDataSource
Returns:
the int value
Throws:
IOException - If an I/O error occurs
SocketTimeoutException

readLong

public final long readLong()
                    throws IOException,
                           SocketTimeoutException
read a long

Specified by:
readLong in interface IDataSource
Returns:
the long value
Throws:
IOException - If an I/O error occurs
SocketTimeoutException

readDouble

public final double readDouble()
                        throws IOException,
                               SocketTimeoutException
read a double

Specified by:
readDouble in interface IDataSource
Returns:
the double value
Throws:
IOException - If an I/O error occurs
SocketTimeoutException

readByteBufferByDelimiter

public final ByteBuffer[] readByteBufferByDelimiter(String delimiter)
                                             throws IOException,
                                                    SocketTimeoutException
read a ByteBuffer by using a delimiter. To avoid memory leaks the IDataSource.readByteBufferByDelimiter(String, int) method is generally preferable.

Specified by:
readByteBufferByDelimiter in interface IDataSource
Parameters:
delimiter - the delimiter (by using the default encoding)
Returns:
the ByteBuffer
Throws:
IOException - If an I/O error occurs
SocketTimeoutException

readByteBufferByDelimiter

public final ByteBuffer[] readByteBufferByDelimiter(String delimiter,
                                                    int maxLength)
                                             throws IOException,
                                                    MaxReadSizeExceededException,
                                                    SocketTimeoutException
read a ByteBuffer by using a delimiter

Specified by:
readByteBufferByDelimiter in interface IDataSource
Parameters:
delimiter - the delimiter (by using the default 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
SocketTimeoutException

readByteBufferByDelimiter

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

Specified by:
readByteBufferByDelimiter in interface IBlockingConnection
Parameters:
delimiter - the delimiter
encoding - the encoding to use
Returns:
the ByteBuffer
Throws:
IOException - If some other I/O error occurs
SocketTimeoutException

readByteBufferByDelimiter

public final ByteBuffer[] readByteBufferByDelimiter(String delimiter,
                                                    String encoding,
                                                    int maxLength)
                                             throws IOException,
                                                    MaxReadSizeExceededException,
                                                    SocketTimeoutException
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 IBlockingConnection
Parameters:
delimiter - the delimiter
encoding - the encoding of 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
SocketTimeoutException

readByteBufferByLength

public final ByteBuffer[] readByteBufferByLength(int length)
                                          throws IOException,
                                                 SocketTimeoutException
read a ByteBuffer by using a length definition

Specified by:
readByteBufferByLength in interface IDataSource
Parameters:
length - the amount of bytes to read
Returns:
the ByteBuffer
Throws:
IOException - If some other I/O error occurs
SocketTimeoutException

readBytesByDelimiter

public final byte[] readBytesByDelimiter(String delimiter)
                                  throws IOException,
                                         SocketTimeoutException
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 (by using the default encoding)
Returns:
the read bytes
Throws:
IOException - If an I/O error occurs
SocketTimeoutException

readBytesByDelimiter

public final byte[] readBytesByDelimiter(String delimiter,
                                         int maxLength)
                                  throws IOException,
                                         MaxReadSizeExceededException,
                                         SocketTimeoutException
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 (by using the default 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
SocketTimeoutException

readBytesByDelimiter

public final byte[] readBytesByDelimiter(String delimiter,
                                         String encoding)
                                  throws IOException,
                                         SocketTimeoutException
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 IBlockingConnection
Parameters:
delimiter - the delimiter
encoding - the encoding to use
Returns:
the read bytes
Throws:
IOException - If some other I/O error occurs
SocketTimeoutException

readBytesByDelimiter

public final byte[] readBytesByDelimiter(String delimiter,
                                         String encoding,
                                         int maxLength)
                                  throws IOException,
                                         MaxReadSizeExceededException,
                                         SocketTimeoutException
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 IBlockingConnection
Parameters:
delimiter - the delimiter
encoding - the encoding to use
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 * @throws ClosedConnectionException If the underlying socket is already closed
IOException - If some other I/O error occurs
SocketTimeoutException

readBytesByLength

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

For performance reasons, the ByteBuffer readByteBuffer method is generally preferable to get bytes

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
SocketTimeoutException

readStringByDelimiter

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

Specified by:
readStringByDelimiter in interface IDataSource
Parameters:
delimiter - the delimiter (by using the default encoding)
Returns:
the string
Throws:
IOException - If some other I/O error occurs
UnsupportedEncodingException - if the default encoding is not supported
SocketTimeoutException

readStringByDelimiter

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

Specified by:
readStringByDelimiter in interface IDataSource
Parameters:
delimiter - the delimiter (by using the default 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
SocketTimeoutException

readStringByDelimiter

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

Specified by:
readStringByDelimiter in interface IBlockingConnection
Parameters:
delimiter - the delimiter
encoding - the encoding to use
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
SocketTimeoutException

readStringByDelimiter

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

Specified by:
readStringByDelimiter in interface IBlockingConnection
Parameters:
delimiter - the delimiter
encoding - the encoding to use
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 * @throws ClosedConnectionException If the underlying socket is already closed
IOException - If some other I/O error occurs
UnsupportedEncodingException - If the given encoding is not supported
SocketTimeoutException

readStringByLength

public final String readStringByLength(int length)
                                throws IOException,
                                       UnsupportedEncodingException,
                                       SocketTimeoutException
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
SocketTimeoutException

readStringByLength

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

Specified by:
readStringByLength in interface IBlockingConnection
Parameters:
length - the amount of bytes to read.
encoding - the encoding to use
Returns:
the string
Throws:
IOException - If some other I/O error occurs
UnsupportedEncodingException - if the given encoding is not supported
SocketTimeoutException

transferTo

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

Specified by:
transferTo in interface IDataSource
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
SocketTimeoutException

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.

Specified by:
resetToWriteMark in interface IBlockingConnection
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.

Specified by:
resetToReadMark in interface IBlockingConnection
Returns:
true, if reset was successful

removeReadMark

public final void removeReadMark()
remove the read mark

Specified by:
removeReadMark in interface IBlockingConnection

removeWriteMark

public final void removeWriteMark()
remove the write mark

Specified by:
removeWriteMark in interface IBlockingConnection

write

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

Specified by:
write in interface IDataSink
Parameters:
b - the byte to write
Returns:
the number of written bytes
Throws:
IOException - If some other I/O error occurs
BufferOverflowException - If the no enough space is available

write

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

Specified by:
write in interface IDataSink
Parameters:
bytes - the bytes to write
Returns:
the number of written bytes
Throws:
IOException - If some other I/O error occurs

write

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

Specified by:
write in interface IDataSink
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:
IOException - If some other I/O error occurs

write

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

Specified by:
write in interface IDataSink
Parameters:
s - the short value to write
Returns:
the number of written bytes
Throws:
IOException - If some other I/O error occurs

write

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

Specified by:
write in interface IDataSink
Parameters:
i - the int value to write
Returns:
the number of written bytes
Throws:
IOException - If some other I/O error occurs

write

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

Specified by:
write in interface IDataSink
Parameters:
l - the int value to write
Returns:
the number of written bytes
Throws:
IOException - If some other I/O error occurs

write

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

Specified by:
write in interface IDataSink
Parameters:
d - the int value to write
Returns:
the number of written bytes
Throws:
IOException - If some other I/O error occurs

write

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

Specified by:
write in interface IDataSink
Parameters:
message - the message to write
Returns:
the number of written bytes
Throws:
IOException - If some other I/O error occurs

write

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

Specified by:
write in interface IBlockingConnection
Parameters:
message - the message to write
encoding - the encoding which should be used th encode the chars into byte (e.g. `US-ASCII` or `UTF-8`)
Returns:
the number of written bytes
Throws:
IOException - If some other I/O error occurs

write

public final long write(ArrayList<ByteBuffer> buffers)
                 throws IOException

Throws:
IOException

write

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

Specified by:
write in interface IDataSink
Parameters:
buffers - the bytes to write
Returns:
the number of written bytes
Throws:
IOException - If some other I/O error occurs

write

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

Specified by:
write in interface GatheringByteChannel
Specified by:
write in interface IDataSink
Throws:
IOException

write

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

Specified by:
write in interface GatheringByteChannel
Specified by:
write in interface IDataSink
Throws:
IOException

write

public final int write(ByteBuffer buffer)
                throws IOException
see WritableByteChannel.write(ByteBuffer)

Specified by:
write in interface WritableByteChannel
Specified by:
write in interface IDataSink
Throws:
IOException

transferFrom

public final long transferFrom(ReadableByteChannel source)
                        throws IOException
transfer the data of the source channel to this data sink by using the default chunk size

Specified by:
transferFrom in interface IDataSink
Parameters:
source - the source channel
Returns:
the number of transfered bytes
Throws:
IOException - If some other I/O error occurs

transferFrom

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

Specified by:
transferFrom in interface IDataSink
Parameters:
source - the source channel
chunkSize - the chunk size to use
Returns:
the number of transfered bytes
Throws:
IOException - If some other I/O error occurs

transferFrom

public long transferFrom(FileChannel source)
                  throws IOException
Description copied from interface: IBlockingConnection
transfer the data of the file channel to this data sink

Specified by:
transferFrom in interface IBlockingConnection
Specified by:
transferFrom in interface IDataSink
Parameters:
source - the source channel
Returns:
the number of transfered bytes
Throws:
IOException - If some other I/O error occurs

toString

public String toString()
Overrides:
toString in class Object


Copyright 2008 xSocket.org