org.xsocket.stream
Class BlockingConnection

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

public final class BlockingConnection
extends java.lang.Object
implements IBlockingConnection

Implementation of the IBlockingConnection interface.

A newly created connection is in the open state. The methods of this class are not thread-safe.


Nested Class Summary
 
Nested classes/interfaces inherited from interface org.xsocket.stream.IConnection
IConnection.FlushMode
 
Field Summary
 
Fields inherited from interface org.xsocket.stream.IBlockingConnection
INITIAL_RECEIVE_TIMEOUT
 
Fields inherited from interface org.xsocket.stream.IConnection
INITIAL_AUTOFLUSH, INITIAL_DEFAULT_ENCODING, SO_KEEPALIVE, SO_LINGER, SO_RCVBUF, SO_REUSEADDR, SO_SNDBUF, TCP_NODELAY
 
Constructor Summary
BlockingConnection(java.net.InetAddress address, int port)
          constructor
BlockingConnection(java.net.InetAddress address, int port, java.util.Map<java.lang.String,java.lang.Object> options)
          Deprecated.  
BlockingConnection(java.net.InetAddress address, int port, java.util.Map<java.lang.String,java.lang.Object> options, javax.net.ssl.SSLContext sslContext, boolean sslOn)
          constructor
BlockingConnection(java.net.InetAddress address, int port, javax.net.ssl.SSLContext sslContext, boolean sslOn)
          constructor
BlockingConnection(java.net.InetAddress address, int port, StreamSocketConfiguration socketConf)
          Deprecated.  
BlockingConnection(java.net.InetAddress address, int port, StreamSocketConfiguration socketConf, javax.net.ssl.SSLContext sslContext, boolean sslOn)
          Deprecated.  
BlockingConnection(java.lang.String hostname, int port)
          constructor.
BlockingConnection(java.lang.String hostname, int port, java.util.Map<java.lang.String,java.lang.Object> options)
          constructor.
BlockingConnection(java.lang.String hostname, int port, java.util.Map<java.lang.String,java.lang.Object> options, javax.net.ssl.SSLContext sslContext, boolean sslOn)
          constructor
BlockingConnection(java.lang.String hostname, int port, javax.net.ssl.SSLContext sslContext, boolean sslOn)
          constructor
BlockingConnection(java.lang.String hostname, int port, StreamSocketConfiguration socketConfiguration)
          Deprecated.  
BlockingConnection(java.lang.String hostname, int port, StreamSocketConfiguration socketConf, javax.net.ssl.SSLContext sslContext, boolean sslOn)
          Deprecated.  
 
Method Summary
 void activateSecuredMode()
          ad hoc activation of a secured mode (SSL).
 java.lang.Object attach(java.lang.Object obj)
          
 java.lang.Object attachment()
          
 void close()
          
 void flush()
          flush the send buffer.
 java.lang.Object getAttachment()
          Retrieves the current attachment.
 boolean getAutoflush()
          get autoflush
 int getConnectionTimeoutSec()
          gets the connection timeout
 java.lang.String getDefaultEncoding()
          gets the default encoding for this connection (used by string related methods like readString...)
 IConnection.FlushMode getFlushmode()
          get the flushmode
 java.lang.String getId()
          returns the id
 int getIdleTimeoutSec()
          returns the idle timeout in sec.
 int getIndexOf(java.lang.String str)
          Returns the index of the first occurrence of the given string.
 int getIndexOf(java.lang.String str, int maxLength)
          Returns the index of the first occurrence of the given string.
 int getIndexOf(java.lang.String str, java.lang.String encoding, int maxLength)
          Returns the index of the first occurrence of the given string.
 java.net.InetAddress getLocalAddress()
          returns the local address
 int getLocalPort()
          returns the local port
 java.lang.Object getOption(java.lang.String name)
          returns the value of a option
 java.util.Map<java.lang.String,java.lang.Class> getOptions()
          Returns an unmodifiable map of the options supported by this endpoint.
 int getPendingWriteDataSize()
          returns the size of the data which have already been written, but not yet transferred to the underlying socket.
 java.net.InetAddress getRemoteAddress()
          returns the remote address
 int getRemotePort()
          returns the port of the remote endpoint
 int indexOf(java.lang.String str)
          Deprecated. uses getIndexOf instead
 boolean isOpen()
          returns, if the connection is open.
 void markReadPosition()
          Marks the present read position in the connection.
 void markWritePosition()
          Marks the present write position in the connection.
 void onWriteException(java.io.IOException ioException)
           
 void onWritten()
           
 int read(java.nio.ByteBuffer buffer)
          .
 byte readByte()
          receive a byte. the method will block, until data is available
 java.nio.ByteBuffer[] readByteBufferByDelimiter(java.lang.String delimiter)
          receive a ByteBuffer. the method will block, until the delimiter has been read.
 java.nio.ByteBuffer[] readByteBufferByDelimiter(java.lang.String delimiter, int maxLength)
          receive a ByteBuffer. the method will block, until the delimiter has been read.
 java.nio.ByteBuffer[] readByteBufferByDelimiter(java.lang.String delimiter, java.lang.String encoding, int maxLength)
          read a ByteBuffer by using a delimiter For performance reasons, the ByteBuffer readByteBuffer method is generally preferable to get bytes
 java.nio.ByteBuffer[] readByteBufferByLength(int length)
          receive a ByteBuffer. the method will block, until the required amount of bytes has been received For performance reasons, the ByteBuffer receiveByteBuffer method is generally preferable to get bytes
 byte[] readBytesByDelimiter(java.lang.String delimiter)
          receive a byte array. the method will block, until the delimiter has been read.
 byte[] readBytesByDelimiter(java.lang.String delimiter, int maxLength)
          receive a byte array. the method will block, until the delimiter has been read.
 byte[] readBytesByDelimiter(java.lang.String delimiter, java.lang.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)
          receive a byte array. the method will block, until the required amount of bytes has been received For performance reasons, the ByteBuffer receiveByteBuffer method is generally preferable to get bytes
 double readDouble()
          receive a double. the method will block, until data is available
 int readInt()
          receive an int. the method will block, until data is available
 long readLong()
          receive a long. the method will block, until data is available
 short readShort()
          read a short value
 java.lang.String readStringByDelimiter(java.lang.String delimiter)
          receive a string. the method will block, until the delimiter has been read.
 java.lang.String readStringByDelimiter(java.lang.String delimiter, int maxLength)
          receive a string. the method will block, until the delimiter has been read.
 java.lang.String readStringByDelimiter(java.lang.String delimiter, java.lang.String encoding)
          receive a string. the method will block, until the delimiter has been read To avoid memory leaks the IBlockingConnection.readStringByDelimiter(String, String) method is generally preferable
 java.lang.String readStringByDelimiter(java.lang.String delimiter, java.lang.String encoding, int maxLength)
          receive a string. the method will block, until the delimiter has been read
 java.lang.String readStringByLength(int length)
          receive a string.
 java.lang.String readStringByLength(int length, java.lang.String encoding)
          receive a string.
 void removeReadMark()
          remove the mark the present read position in the connection.
 void removeWriteMark()
          remove the mark the present write position in the connection.
 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(java.lang.Object obj)
          Attaches the given object to this connection
 void setAutoflush(boolean autoflush)
          set autoflush.
 void setConnectionTimeoutSec(int timeoutSec)
          sets the max time for a connections.
 void setDefaultEncoding(java.lang.String defaultEncoding)
          sets the default encoding for this connection (used by string related methods like readString...)
 void setFlushmode(IConnection.FlushMode flushMode)
          set the flushmode
 void setIdleTimeoutSec(int timeoutInSec)
          sets the idle timeout in sec
 IBlockingConnection setOption(java.lang.String name, java.lang.Object value)
          sets the value of a option
 void setReceiveTimeoutMillis(long timeout)
          set the timeout for calling read methods in millis
 void suspendRead()
          suspend reading data from the underlying subsystem
 java.lang.String toString()
          
 int write(byte... bytes)
          sends bytes to the remote endpoint
 int write(byte b)
          sends a byte to the remote endpoint
 int write(byte[] bytes, int offset, int length)
          sends bytes to the remote endpoint
 int write(java.nio.ByteBuffer buffer)
          sends a byte buffer to the remote endpoint.
 long write(java.nio.ByteBuffer[] buffers)
          sends an array of byte buffer to the remote endpoint.
 long write(java.nio.ByteBuffer[] srcs, int offset, int length)
          
 int write(double d)
          sends a double to the remote endpoint
 int write(int i)
          sends an int to the remote endpoint
 int write(long l)
          sends a long to the remote endpoint
 int write(short s)
          writes a short to the data sink
 int write(java.lang.String s)
          sends a message to the remote endpoint by using the connection default encoding
 int write(java.lang.String s, java.lang.String encoding)
          sends a message to the remote endpoint
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface org.xsocket.stream.IBlockingConnection
flush, write, write, write, write, write, write, write, write, write, write
 
Methods inherited from interface org.xsocket.stream.IConnection
activateSecuredMode, attach, attachment, getAttachment, getAutoflush, getConnectionTimeoutSec, getDefaultEncoding, getId, getIdleTimeoutSec, getLocalAddress, getLocalPort, getOption, getOptions, getPendingWriteDataSize, getRemoteAddress, getRemotePort, isOpen, markReadPosition, markWritePosition, removeReadMark, removeWriteMark, resetToReadMark, resetToWriteMark, resumeRead, setAttachment, setAutoflush, setConnectionTimeoutSec, setDefaultEncoding, setIdleTimeoutSec, suspendRead
 
Methods inherited from interface org.xsocket.IDataSink
write
 
Methods inherited from interface java.nio.channels.GatheringByteChannel
write
 
Methods inherited from interface java.nio.channels.Channel
close
 

Constructor Detail

BlockingConnection

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

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

BlockingConnection

public BlockingConnection(java.lang.String hostname,
                          int port,
                          StreamSocketConfiguration socketConfiguration)
                   throws java.io.IOException
Deprecated. 

constructor.

Parameters:
hostname - the remote host
port - the port of the remote host to connect
socketConfiguration - the socket configuration
Throws:
java.io.IOException - If some other I/O error occurs

BlockingConnection

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

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

BlockingConnection

public BlockingConnection(java.net.InetAddress address,
                          int port)
                   throws java.io.IOException
constructor

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

BlockingConnection

public BlockingConnection(java.net.InetAddress address,
                          int port,
                          StreamSocketConfiguration socketConf)
                   throws java.io.IOException
Deprecated. 

constructor

Parameters:
address - the remote host address
port - the remote host port
socketConf - the socket configuration
Throws:
java.io.IOException - If some other I/O error occurs

BlockingConnection

public BlockingConnection(java.net.InetAddress address,
                          int port,
                          java.util.Map<java.lang.String,java.lang.Object> options)
                   throws java.io.IOException
Deprecated. 

constructor

Parameters:
address - the remote host address
port - the remote host port
options - the socket options
Throws:
java.io.IOException - If some other I/O error occurs

BlockingConnection

public BlockingConnection(java.net.InetAddress address,
                          int port,
                          javax.net.ssl.SSLContext sslContext,
                          boolean sslOn)
                   throws java.io.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 IConnection.activateSecuredMode())
Throws:
java.io.IOException - If some other I/O error occurs

BlockingConnection

public BlockingConnection(java.net.InetAddress address,
                          int port,
                          StreamSocketConfiguration socketConf,
                          javax.net.ssl.SSLContext sslContext,
                          boolean sslOn)
                   throws java.io.IOException
Deprecated. 

constructor

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

BlockingConnection

public BlockingConnection(java.net.InetAddress address,
                          int port,
                          java.util.Map<java.lang.String,java.lang.Object> options,
                          javax.net.ssl.SSLContext sslContext,
                          boolean sslOn)
                   throws java.io.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 IConnection.activateSecuredMode())
Throws:
java.io.IOException - If some other I/O error occurs

BlockingConnection

public BlockingConnection(java.lang.String hostname,
                          int port,
                          javax.net.ssl.SSLContext sslContext,
                          boolean sslOn)
                   throws java.io.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 IConnection.activateSecuredMode()) * @throws IOException If some other I/O error occurs
Throws:
java.io.IOException

BlockingConnection

public BlockingConnection(java.lang.String hostname,
                          int port,
                          StreamSocketConfiguration socketConf,
                          javax.net.ssl.SSLContext sslContext,
                          boolean sslOn)
                   throws java.io.IOException
Deprecated. 

constructor

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

BlockingConnection

public BlockingConnection(java.lang.String hostname,
                          int port,
                          java.util.Map<java.lang.String,java.lang.Object> options,
                          javax.net.ssl.SSLContext sslContext,
                          boolean sslOn)
                   throws java.io.IOException
constructor

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

readByte

public byte readByte()
              throws java.io.IOException,
                     ClosedConnectionException,
                     java.net.SocketTimeoutException
receive a byte. the method will block, until data is available

Specified by:
readByte in interface IDataSource
Specified by:
readByte in interface IBlockingConnection
Specified by:
readByte in interface IConnection
Returns:
the received byte
Throws:
java.net.SocketTimeoutException - If the receive timeout has been reached
ClosedConnectionException - If the underlying socket is already closed
java.io.IOException - If some other I/O error occurs

setReceiveTimeoutMillis

public final void setReceiveTimeoutMillis(long timeout)
set the timeout for calling read methods in millis

Specified by:
setReceiveTimeoutMillis in interface IBlockingConnection
Parameters:
timeout - the timeout in millis

readByteBufferByDelimiter

public java.nio.ByteBuffer[] readByteBufferByDelimiter(java.lang.String delimiter)
                                                throws java.io.IOException,
                                                       ClosedConnectionException,
                                                       java.net.SocketTimeoutException
receive a ByteBuffer. the method will block, until the delimiter has been read. To avoid memory leaks the IBlockingConnection.readByteBufferByDelimiter(String, int) method is generally preferable
For performance reasons, the ByteBuffer receiveByteBuffer method is generally preferable to get bytes

Specified by:
readByteBufferByDelimiter in interface IBlockingConnection
Specified by:
readByteBufferByDelimiter in interface IConnection
Parameters:
delimiter - the delimiter
Returns:
the ByteBuffer
Throws:
java.net.SocketTimeoutException - If the receive timeout has been reached
ClosedConnectionException - If the underlying socket is already closed
java.io.IOException - If some other I/O error occurs

readByteBufferByDelimiter

public java.nio.ByteBuffer[] readByteBufferByDelimiter(java.lang.String delimiter,
                                                       int maxLength)
                                                throws java.io.IOException,
                                                       ClosedConnectionException,
                                                       java.net.SocketTimeoutException,
                                                       MaxReadSizeExceededException
receive a ByteBuffer. the method will block, until the delimiter has been read. For performance reasons, the ByteBuffer receiveByteBuffer method is generally preferable to get bytes

Specified by:
readByteBufferByDelimiter in interface IBlockingConnection
Specified by:
readByteBufferByDelimiter in interface IConnection
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:
java.net.SocketTimeoutException - If the receive timeout has been reached
ClosedConnectionException - If the underlying socket is already closed
java.io.IOException - If some other I/O error occurs
MaxReadSizeExceededException - If the max read length has been exceeded and the delimiter hasn’t been found

readByteBufferByDelimiter

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

Specified by:
readByteBufferByDelimiter in interface IConnection
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:
ClosedConnectionException - If the underlying socket is already closed
java.io.IOException - If some other I/O error occurs
MaxReadSizeExceededException - If the max read length has been exceeded and the delimiter hasn’t been found

readByteBufferByLength

public java.nio.ByteBuffer[] readByteBufferByLength(int length)
                                             throws java.io.IOException,
                                                    ClosedConnectionException,
                                                    java.net.SocketTimeoutException
receive a ByteBuffer. the method will block, until the required amount of bytes has been received For performance reasons, the ByteBuffer receiveByteBuffer method is generally preferable to get bytes

Specified by:
readByteBufferByLength in interface IBlockingConnection
Specified by:
readByteBufferByLength in interface IConnection
Parameters:
length - the number of bytes to read
Returns:
the received ByteBuffer
Throws:
java.net.SocketTimeoutException - If the receive timeout has been reached
ClosedConnectionException - If the underlying socket is already closed
java.io.IOException - If some other I/O error occurs

readBytesByDelimiter

public byte[] readBytesByDelimiter(java.lang.String delimiter)
                            throws java.io.IOException,
                                   ClosedConnectionException,
                                   java.net.SocketTimeoutException
receive a byte array. the method will block, until the delimiter has been read. To avoid memory leaks the IBlockingConnection.readBytesByDelimiter(String, int) method is generally preferable
For performance reasons, the ByteBuffer receiveByteBuffer method is generally preferable to get bytes

Specified by:
readBytesByDelimiter in interface IBlockingConnection
Specified by:
readBytesByDelimiter in interface IConnection
Parameters:
delimiter - the delimiter
Returns:
the read bytes
Throws:
java.net.SocketTimeoutException - If the receive timeout has been reached
ClosedConnectionException - If the underlying socket is already closed
java.io.IOException - If some other I/O error occurs

readBytesByDelimiter

public byte[] readBytesByDelimiter(java.lang.String delimiter,
                                   int maxLength)
                            throws java.io.IOException,
                                   ClosedConnectionException,
                                   java.net.SocketTimeoutException,
                                   MaxReadSizeExceededException
receive a byte array. the method will block, until the delimiter has been read. For performance reasons, the ByteBuffer receiveByteBuffer method is generally preferable to get bytes

Specified by:
readBytesByDelimiter in interface IDataSource
Specified by:
readBytesByDelimiter in interface IBlockingConnection
Specified by:
readBytesByDelimiter in interface IConnection
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:
java.net.SocketTimeoutException - If the receive timeout has been reached
ClosedConnectionException - If the underlying socket is already closed
java.io.IOException - If some other I/O error occurs
MaxReadSizeExceededException - If the max read length has been exceeded and the delimiter hasn’t been found

readBytesByDelimiter

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

Specified by:
readBytesByDelimiter in interface IConnection
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 read bytes
Throws:
ClosedConnectionException - If the underlying socket is already closed
java.io.IOException - If some other I/O error occurs
MaxReadSizeExceededException - If the max read length has been exceeded and the delimiter hasn’t been found

readBytesByLength

public byte[] readBytesByLength(int length)
                         throws java.io.IOException,
                                ClosedConnectionException,
                                java.net.SocketTimeoutException
receive a byte array. the method will block, until the required amount of bytes has been received For performance reasons, the ByteBuffer receiveByteBuffer method is generally preferable to get bytes

Specified by:
readBytesByLength in interface IDataSource
Specified by:
readBytesByLength in interface IBlockingConnection
Specified by:
readBytesByLength in interface IConnection
Parameters:
length - the number of bytes to read
Returns:
the received byte array
Throws:
java.net.SocketTimeoutException - If the receive timeout has been reached
ClosedConnectionException - If the underlying socket is already closed
java.io.IOException - If some other I/O error occurs

readDouble

public double readDouble()
                  throws java.io.IOException,
                         ClosedConnectionException,
                         java.net.SocketTimeoutException
receive a double. the method will block, until data is available

Specified by:
readDouble in interface IDataSource
Specified by:
readDouble in interface IBlockingConnection
Specified by:
readDouble in interface IConnection
Returns:
the received double
Throws:
java.net.SocketTimeoutException - If the receive timeout has been reached
ClosedConnectionException - If the underlying socket is already closed
java.io.IOException - If some other I/O error occurs

readInt

public int readInt()
            throws java.io.IOException,
                   ClosedConnectionException,
                   java.net.SocketTimeoutException
receive an int. the method will block, until data is available

Specified by:
readInt in interface IDataSource
Specified by:
readInt in interface IBlockingConnection
Specified by:
readInt in interface IConnection
Returns:
the received int
Throws:
java.net.SocketTimeoutException - If the receive timeout has been reached
ClosedConnectionException - If the underlying socket is already closed
java.io.IOException - If some other I/O error occurs

readShort

public short readShort()
                throws java.io.IOException,
                       ClosedConnectionException,
                       java.net.SocketTimeoutException
read a short value

Specified by:
readShort in interface IDataSource
Returns:
the short value
Throws:
java.io.IOException - If some other I/O error occurs
ClosedConnectionException
java.net.SocketTimeoutException

read

public final int read(java.nio.ByteBuffer buffer)
               throws java.io.IOException
.

Specified by:
read in interface java.nio.channels.ReadableByteChannel
Throws:
java.io.IOException

readLong

public long readLong()
              throws java.io.IOException,
                     ClosedConnectionException,
                     java.net.SocketTimeoutException
receive a long. the method will block, until data is available

Specified by:
readLong in interface IDataSource
Specified by:
readLong in interface IBlockingConnection
Specified by:
readLong in interface IConnection
Returns:
the received long
Throws:
java.net.SocketTimeoutException - If the receive timeout has been reached
ClosedConnectionException - If the underlying socket is already closed
java.io.IOException - If some other I/O error occurs

readStringByDelimiter

public java.lang.String readStringByDelimiter(java.lang.String delimiter)
                                       throws java.io.IOException,
                                              ClosedConnectionException,
                                              java.io.UnsupportedEncodingException,
                                              java.net.SocketTimeoutException
receive a string. the method will block, until the delimiter has been read. For the encoding the default encoding of the connection will be used. To avoid memory leaks the IBlockingConnection.readStringByDelimiter(String, int) method is generally preferable

Specified by:
readStringByDelimiter in interface IDataSource
Specified by:
readStringByDelimiter in interface IBlockingConnection
Specified by:
readStringByDelimiter in interface IConnection
Parameters:
delimiter - the delimiter
Returns:
the string
Throws:
ClosedConnectionException - If the underlying socket is already closed
java.io.IOException - If some other I/O error occurs
java.net.SocketTimeoutException - If the receive timeout has been reached
java.io.UnsupportedEncodingException - if the default encoding is not supported

readStringByDelimiter

public java.lang.String readStringByDelimiter(java.lang.String delimiter,
                                              int maxLength)
                                       throws java.io.IOException,
                                              ClosedConnectionException,
                                              java.io.UnsupportedEncodingException,
                                              java.net.SocketTimeoutException,
                                              MaxReadSizeExceededException
receive a string. the method will block, until the delimiter has been read. For the encoding the default encoding of the connection will be used

Specified by:
readStringByDelimiter in interface IDataSource
Specified by:
readStringByDelimiter in interface IBlockingConnection
Specified by:
readStringByDelimiter in interface IConnection
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:
ClosedConnectionException - If the underlying socket is already closed
java.io.IOException - If some other I/O error occurs
java.net.SocketTimeoutException - If the receive timeout has been reached
java.io.UnsupportedEncodingException - if the default encoding is not supported
MaxReadSizeExceededException - If the max read length has been exceeded and the delimiter hasn’t been found

readStringByDelimiter

public java.lang.String readStringByDelimiter(java.lang.String delimiter,
                                              java.lang.String encoding)
                                       throws java.io.IOException,
                                              ClosedConnectionException,
                                              java.io.UnsupportedEncodingException,
                                              java.net.SocketTimeoutException
receive a string. the method will block, until the delimiter has been read To avoid memory leaks the IBlockingConnection.readStringByDelimiter(String, String) method is generally preferable

Specified by:
readStringByDelimiter in interface IBlockingConnection
Specified by:
readStringByDelimiter in interface IConnection
Parameters:
delimiter - the delimiter
encoding - the encoding to use
Returns:
the string
Throws:
java.net.SocketTimeoutException - If the receive timeout has been reached
java.io.IOException - If some other I/O error occurs
java.io.UnsupportedEncodingException - If the given encoding is not supported
ClosedConnectionException - If the underlying socket is already closed

readStringByDelimiter

public java.lang.String readStringByDelimiter(java.lang.String delimiter,
                                              java.lang.String encoding,
                                              int maxLength)
                                       throws java.io.IOException,
                                              ClosedConnectionException,
                                              java.io.UnsupportedEncodingException,
                                              java.net.SocketTimeoutException,
                                              MaxReadSizeExceededException
receive a string. the method will block, until the delimiter has been read

Specified by:
readStringByDelimiter in interface IDataSource
Specified by:
readStringByDelimiter in interface IBlockingConnection
Specified by:
readStringByDelimiter in interface IConnection
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:
java.net.SocketTimeoutException - If the receive timeout has been reached
java.io.IOException - If some other I/O error occurs
java.io.UnsupportedEncodingException - If the given encoding is not supported
ClosedConnectionException - If the underlying socket is already closed
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

getIndexOf

public int getIndexOf(java.lang.String str)
               throws java.io.IOException,
                      ClosedConnectionException,
                      java.net.SocketTimeoutException
Returns the index of the first occurrence of the given string.

Specified by:
getIndexOf in interface IBlockingConnection
Specified by:
getIndexOf in interface IConnection
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;
Throws:
ClosedConnectionException - If the underlying socket is already closed
java.io.IOException - If some other I/O error occurs
java.net.SocketTimeoutException - If the receive timeout has been reached

getIndexOf

public int getIndexOf(java.lang.String str,
                      int maxLength)
               throws java.io.IOException,
                      ClosedConnectionException,
                      MaxReadSizeExceededException,
                      java.net.SocketTimeoutException
Returns the index of the first occurrence of the given string.

Specified by:
getIndexOf in interface IBlockingConnection
Specified by:
getIndexOf in interface IConnection
Parameters:
str - any string
maxLength - the max length of bytes that should be read. If the limit is exceeded a MaxReadSizeExceededException will been thrown
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;
Throws:
ClosedConnectionException - If the underlying socket is already closed
java.io.IOException - If some other I/O error occurs
java.net.SocketTimeoutException - If the receive timeout has been reached
MaxReadSizeExceededException - If the max read length has been exceeded and the delimiter hasn’t been found

getIndexOf

public int getIndexOf(java.lang.String str,
                      java.lang.String encoding,
                      int maxLength)
               throws java.io.IOException,
                      ClosedConnectionException,
                      MaxReadSizeExceededException
Returns the index of the first occurrence of the given string.

Specified by:
getIndexOf in interface IConnection
Parameters:
str - any string
encoding - the encoding of the string
maxLength - the max length of bytes that should be read. If the limit is exceeded a MaxReadSizeExceededException will been thrown
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;
Throws:
ClosedConnectionException - If the underlying socket is already closed
java.io.IOException - If some other I/O error occurs
MaxReadSizeExceededException - If the max read length has been exceeded and the delimiter hasn’t been found

readStringByLength

public java.lang.String readStringByLength(int length)
                                    throws java.io.IOException,
                                           ClosedConnectionException,
                                           java.net.SocketTimeoutException
receive a string. the method will block, until the required amount of bytes has been received

Specified by:
readStringByLength in interface IDataSource
Specified by:
readStringByLength in interface IBlockingConnection
Specified by:
readStringByLength in interface IConnection
Parameters:
length - the number of bytes to read
Returns:
the received string
Throws:
java.net.SocketTimeoutException - If the receive timeout has been reached
ClosedConnectionException - If the underlying socket is already closed
java.io.IOException - If some other I/O error occurs

readStringByLength

public java.lang.String readStringByLength(int length,
                                           java.lang.String encoding)
                                    throws java.io.IOException,
                                           ClosedConnectionException,
                                           java.net.SocketTimeoutException
receive a string. the method will block, until the required amount of bytes has been received For performance reasons, the ByteBuffer receiveRecord method is generally preferable to get bytes

Specified by:
readStringByLength in interface IDataSource
Specified by:
readStringByLength in interface IBlockingConnection
Specified by:
readStringByLength in interface IConnection
Parameters:
length - the number of bytes to read
encoding - the encoding
Returns:
the received string
Throws:
java.net.SocketTimeoutException - If the receive timeout has been reached
ClosedConnectionException - If the underlying socket is already closed
java.io.IOException - If some other I/O error occurs

setOption

public IBlockingConnection setOption(java.lang.String name,
                                     java.lang.Object value)
                              throws java.io.IOException
sets the value of a option

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

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 IConnection
Returns:
the size of the pending data to write

close

public final void close()
                 throws java.io.IOException

Specified by:
close in interface java.io.Closeable
Specified by:
close in interface java.nio.channels.Channel
Throws:
java.io.IOException

isOpen

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

Please note, that a connection could be closed, but reading of already received (and internally buffered) data wouldn`t fail. See also IDataHandler.onData(INonBlockingConnection)

Specified by:
isOpen in interface java.nio.channels.Channel
Specified by:
isOpen in interface IConnection
Returns:
true if the connection is open

suspendRead

public void suspendRead()
                 throws java.io.IOException
suspend reading data from the underlying subsystem

Specified by:
suspendRead in interface IConnection
Throws:
java.io.IOException - If some other I/O error occurs

resumeRead

public void resumeRead()
                throws java.io.IOException
resume reading data from the underlying subsystem

Specified by:
resumeRead in interface IConnection
Throws:
java.io.IOException - If some other I/O error occurs

flush

public void flush()
           throws ClosedConnectionException,
                  java.io.IOException
flush the send buffer.

Specified by:
flush in interface java.io.Flushable
Specified by:
flush in interface IConnection
Throws:
ClosedConnectionException - if the underlying channel is closed
java.io.IOException - If some other I/O error occurs

setFlushmode

public final void setFlushmode(IConnection.FlushMode flushMode)
set the flushmode

Parameters:
flushMode - the flushmode

getFlushmode

public final IConnection.FlushMode getFlushmode()
get the flushmode

Returns:
the flushmode

setIdleTimeoutSec

public void setIdleTimeoutSec(int timeoutInSec)
sets the idle timeout in sec

Specified by:
setIdleTimeoutSec in interface IConnection
Parameters:
timeoutInSec - idle timeout in sec

setConnectionTimeoutSec

public void setConnectionTimeoutSec(int timeoutSec)
sets the max time for a connections. By exceeding this time the connection will be terminated

Specified by:
setConnectionTimeoutSec in interface IConnection
Parameters:
timeoutSec - the connection timeout in sec

getConnectionTimeoutSec

public int getConnectionTimeoutSec()
gets the connection timeout

Specified by:
getConnectionTimeoutSec in interface IConnection
Returns:
connection timeout

getIdleTimeoutSec

public int getIdleTimeoutSec()
returns the idle timeout in sec.

Specified by:
getIdleTimeoutSec in interface IConnection
Returns:
idle timeout in sec

getDefaultEncoding

public final java.lang.String getDefaultEncoding()
gets the default encoding for this connection (used by string related methods like readString...)

Specified by:
getDefaultEncoding in interface IConnection
Returns:
the default encoding

setDefaultEncoding

public final void setDefaultEncoding(java.lang.String defaultEncoding)
sets the default encoding for this connection (used by string related methods like readString...)

Specified by:
setDefaultEncoding in interface IConnection
Parameters:
defaultEncoding - the default encoding

setAutoflush

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

By default the autoflush is deactivated

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

getAutoflush

public final boolean getAutoflush()
get autoflush

Specified by:
getAutoflush in interface IConnection
Returns:
true, if autoflush is activated

getId

public final java.lang.String getId()
returns the id

Specified by:
getId in interface IConnection
Returns:
id

getLocalAddress

public final java.net.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

getRemoteAddress

public final java.net.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 endpoint

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

activateSecuredMode

public void activateSecuredMode()
                         throws java.io.IOException
ad hoc activation of a secured mode (SSL). By perfoming 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 IConnection
Throws:
java.io.IOException - If some other I/O error occurs

write

public final int write(java.lang.String s)
                throws ClosedConnectionException,
                       java.io.IOException
sends a message to the remote endpoint by using the connection default encoding

Specified by:
write in interface IDataSink
Specified by:
write in interface IConnection
Parameters:
s - the message to send
Returns:
the number of send bytes
Throws:
ClosedConnectionException - if the underlying channel is closed
java.io.IOException - If some other I/O error occurs

write

public final int write(java.lang.String s,
                       java.lang.String encoding)
                throws ClosedConnectionException,
                       java.io.IOException
sends a message to the remote endpoint

Specified by:
write in interface IDataSink
Specified by:
write in interface IConnection
Parameters:
s - the message to send
encoding - the encoding which should be used th encode the chars into byte (e.g. `US-ASCII` or `UTF-8`)
Returns:
the number of send bytes
Throws:
ClosedConnectionException - if the underlying channel is closed
java.io.IOException - If some other I/O error occurs

write

public final int write(byte b)
                throws ClosedConnectionException,
                       java.io.IOException
sends a byte to the remote endpoint

Specified by:
write in interface IDataSink
Specified by:
write in interface IConnection
Parameters:
b - the byte to send
Returns:
the number of send bytes
Throws:
ClosedConnectionException - if the underlying channel is closed
java.io.IOException - If some other I/O error occurs

write

public final int write(byte... bytes)
                throws ClosedConnectionException,
                       java.io.IOException
sends bytes to the remote endpoint

Specified by:
write in interface IDataSink
Specified by:
write in interface IConnection
Parameters:
bytes - the bytes to send
Returns:
the number of send bytes
Throws:
ClosedConnectionException - if the underlying channel is closed
java.io.IOException - If some other I/O error occurs

write

public final int write(byte[] bytes,
                       int offset,
                       int length)
                throws ClosedConnectionException,
                       java.io.IOException
sends bytes to the remote endpoint

Specified by:
write in interface IDataSink
Specified by:
write in interface IConnection
Parameters:
bytes - the bytes to send
offset - The offset of the subarray 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 subarray 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 send bytes
Throws:
ClosedConnectionException - if the underlying channel is closed
java.io.IOException - If some other I/O error occurs

write

public final long write(java.nio.ByteBuffer[] buffers)
                 throws ClosedConnectionException,
                        java.io.IOException
sends an array of byte buffer to the remote endpoint. In case of activated autoflush (for default see IConnection.INITIAL_AUTOFLUSH) the behaviour is according to the WritableByteChannel specification. In case of user managed flushing (autoflush is off) the passed over buffers will only be queued internally and written after flushing the connection.

Specified by:
write in interface java.nio.channels.GatheringByteChannel
Specified by:
write in interface IDataSink
Specified by:
write in interface IConnection
Parameters:
buffers - the bytes to send
Returns:
the number of send bytes
Throws:
ClosedConnectionException - if the underlying channel is closed
java.io.IOException - If some other I/O error occurs

write

public final int write(java.nio.ByteBuffer buffer)
                throws ClosedConnectionException,
                       java.io.IOException
sends a byte buffer to the remote endpoint. In case of activated autoflush (for default see IConnection.INITIAL_AUTOFLUSH) the behaviour is according to the WritableByteChannel specification. In case of user managed flushing (autoflush is off) the passed over buffers will only be queued internally and written after flushing the connection.

Specified by:
write in interface java.nio.channels.WritableByteChannel
Specified by:
write in interface IDataSink
Specified by:
write in interface IConnection
Parameters:
buffer - the bytes to send
Returns:
the number of send bytes
Throws:
ClosedConnectionException - if the underlying channel is closed
java.io.IOException - If some other I/O error occurs

write

public final int write(int i)
                throws ClosedConnectionException,
                       java.io.IOException
sends an int to the remote endpoint

Specified by:
write in interface IDataSink
Specified by:
write in interface IConnection
Parameters:
i - the int value to send
Returns:
the number of send bytes
Throws:
ClosedConnectionException - if the underlying channel is closed
java.io.IOException - If some other I/O error occurs

write

public final int write(short s)
                throws ClosedConnectionException,
                       java.io.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 send bytes
Throws:
java.io.IOException - If some other I/O error occurs
ClosedConnectionException

write

public final int write(long l)
                throws ClosedConnectionException,
                       java.io.IOException
sends a long to the remote endpoint

Specified by:
write in interface IDataSink
Specified by:
write in interface IConnection
Parameters:
l - the int value to send
Returns:
the number of send bytes
Throws:
ClosedConnectionException - if the underlying channel is closed
java.io.IOException - If some other I/O error occurs

write

public final int write(double d)
                throws ClosedConnectionException,
                       java.io.IOException
sends a double to the remote endpoint

Specified by:
write in interface IDataSink
Specified by:
write in interface IConnection
Parameters:
d - the int value to send
Returns:
the number of send bytes
Throws:
ClosedConnectionException - if the underlying channel is closed
java.io.IOException - If some other I/O error occurs

write

public final long write(java.nio.ByteBuffer[] srcs,
                        int offset,
                        int length)
                 throws java.io.IOException

Specified by:
write in interface java.nio.channels.GatheringByteChannel
Throws:
java.io.IOException

indexOf

public int indexOf(java.lang.String str)
Deprecated. uses getIndexOf instead

Returns the index within this string of the first occurrence of the specified substring

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.

attach

public final java.lang.Object attach(java.lang.Object obj)

Specified by:
attach in interface IConnection

attachment

public final java.lang.Object attachment()

Specified by:
attachment in interface IConnection

setAttachment

public final void setAttachment(java.lang.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 java.lang.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

markReadPosition

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

Specified by:
markReadPosition in interface IConnection

markWritePosition

public void markWritePosition()
Marks the present write position in the connection. Subsequent calls to resetToReadMark() will attempt to reposition the connection to this point. The write read become in valid by flushing the connection.

write mark is only supported for autoflush off. E.g.
 ...
 con.setAutoflush(false);

 con.markWritePosition();  // mark current position
 con.write((int) 0);       // write "emtpy" length field

 int written = con.write("hello world");
 written += con.write(" it’s nice to be here");
 ...

 con.resetToWriteMark();  // return to length field position
 con.write(written);      // and update it

 con.flush(); // flush (marker will be removed implicit)
…    * 

Specified by:
markWritePosition in interface IConnection

resetToWriteMark

public 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 IConnection
Returns:
true, if reset was successful

resetToReadMark

public 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 IConnection
Returns:
true, if reset was successful

removeReadMark

public void removeReadMark()
remove the mark the present read position in the connection.

Specified by:
removeReadMark in interface IConnection

removeWriteMark

public void removeWriteMark()
remove the mark the present write position in the connection.

Specified by:
removeWriteMark in interface IConnection

onWritten

public void onWritten()

onWriteException

public void onWriteException(java.io.IOException ioException)

getOption

public final java.lang.Object getOption(java.lang.String name)
                                 throws java.io.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:
java.io.IOException - In an I/O error occurs

getOptions

public final java.util.Map<java.lang.String,java.lang.Class> getOptions()
Returns an unmodifiable map of the options supported by this endpoint. 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

toString

public java.lang.String toString()

Overrides:
toString in class java.lang.Object