org.indy.io
Class IOHandlerSocket

java.lang.Object
  |
  +--org.indy.io.AbstractIOHandler
        |
        +--org.indy.io.IOHandlerSocket
All Implemented Interfaces:
IOHandler

public class IOHandlerSocket
extends AbstractIOHandler

An implementation of IOHandler that handles IO for sockets.

Author:
Owen Green
See Also:
IOHandler, AbstractIOHandler

Field Summary
protected  java.net.Socket binding
          Instance of java.net.Socket used by this IOHandler.
protected static int CR
          Carriage Return
protected static int LF
          Line Feed
 
Fields inherited from class org.indy.io.AbstractIOHandler
listenerList, maximumLineLength, readTimeOut
 
Constructor Summary
IOHandlerSocket()
          Constructs a new instance of IOHandlerSocket
IOHandlerSocket(java.net.Socket socket)
          Constructs a new instance of IOHandlerSocket around a pre-made java.net.Socket
 
Method Summary
 int available()
          Returns the number of bytes that be read from the socket and buffer without blocking
 void clearBuffer()
          Clears the internal buffer used by this socket
 void close()
          Closes this socket connection.
 void connectClient(java.util.Map parameters, int timeout)
          Connects to a remote socket using the parameters given in map.
 void connectClient(java.lang.String host, int port, java.lang.String boundIP, int boundPort, int timeout)
          Connects this IOHandlerSocket to a remote socket whose address is given by host as either an IP address or FQDN and whose port is given by port If boundIP or boundPort are not null or < 1 respectively, the socket will bind to this local address and / or port.
 byte[] currentReadBuffer()
          Returns the current data that can be read from the socket without blocking
 void flush()
          Flushes the output buffer of this IOHandlerSocket
 java.lang.String getBoundIP()
          Gets a string representation of the local IP address that this socket is bound to, or throws an exception if the socket is not bound.
 int getBoundPort()
          Gets the local port number that this socket is bound to.
 java.lang.String getPeerIP()
          Returns a string representing the IP address of the peer that this socket is connected to, or a blank string if the socket is not connected.
 int getPeerPort()
          Gets the remote port to which this socket is connected, or 0 if unconnected
 int getReadTimeOut()
          DOCUMENT ME!
protected  SocksInfo getSocksInfo()
          Gets the SOCKS data in use with this IOHandlerSocket
 boolean getUseNagle()
          Returns whether this socket is using Nagle's Algortihm.
 boolean isConnected()
          Returns whether or not this instance is connected.
 int read()
          Reads a single byte of data from the socket
 int read(byte[] b, int off, int len, int timeout)
          Reads a sequence of bytes from the socket, using the specified timeout.
 int read(int timeout)
          DOCUMENT ME!
 java.lang.String readLine(int timeout, int maxLineLength, java.lang.String encoding)
          Reads a line of data from the socket and assembles it using the given encoding.
 void setReadTimeOut(int newReadTimeOut)
          DOCUMENT ME!
protected  void setSocksInfo(SocksInfo info)
          Sets the SOCKS configuration to be used with this socket connection.
 void setUseNagle(boolean use)
          Sets whether to use Nagle's Algorithm with this socket or not.
 void write(byte[] b)
          Writes a sequence of bytes to the socket
 void write(byte[] b, int off, int len)
          Writes a seqeunce of bytes to the socket
 void write(int b)
          Writes a single byte to the socket
 
Methods inherited from class org.indy.io.AbstractIOHandler
addIOHandlerListener, afterAccept, doOnConnect, doOnDisconnect, doStatus, getMaximumLineLength, open, read, read, read, readLine, readLine, removeIOHandlerListener, setMaximumLineLength
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

CR

protected static final int CR
Carriage Return

LF

protected static final int LF
Line Feed

binding

protected java.net.Socket binding
Instance of java.net.Socket used by this IOHandler.
Constructor Detail

IOHandlerSocket

public IOHandlerSocket(java.net.Socket socket)
                throws IndyIOException
Constructs a new instance of IOHandlerSocket around a pre-made java.net.Socket
Parameters:
socket - The socket to use with the IOHandler
Throws:
IndyIOException - If an IO error occurs whilst configuring the socket

IOHandlerSocket

public IOHandlerSocket()
Constructs a new instance of IOHandlerSocket
Method Detail

setSocksInfo

protected void setSocksInfo(SocksInfo info)
Sets the SOCKS configuration to be used with this socket connection. NOTE: Setting this whilst the socket is connected will have no effect.
Parameters:
info - The new socks configuration data to be used.

setUseNagle

public void setUseNagle(boolean use)
                 throws IndyIOException
Sets whether to use Nagle's Algorithm with this socket or not. The default is true
Parameters:
use - true to use Nagle's Algorithm, false otherwise
Throws:
IndyIOException - if the underlying socket throws an exception whilst setting this property
See Also:
Socket.setTcpNoDelay(boolean)

getSocksInfo

protected SocksInfo getSocksInfo()
Gets the SOCKS data in use with this IOHandlerSocket
Returns:
The current SOCKS configuration

getPeerIP

public java.lang.String getPeerIP()
Returns a string representing the IP address of the peer that this socket is connected to, or a blank string if the socket is not connected.
Returns:
The IP address of the socket's peer, or a blank string if unconnected.

getPeerPort

public int getPeerPort()
Gets the remote port to which this socket is connected, or 0 if unconnected
Returns:
The remote port.

getBoundIP

public java.lang.String getBoundIP()
                            throws IndyException
Gets a string representation of the local IP address that this socket is bound to, or throws an exception if the socket is not bound.
Returns:
The local IP address that this socket is bound to
Throws:
IndyException - If the socket is not bound
See Also:
connectClient

getBoundPort

public int getBoundPort()
                 throws IndyException
Gets the local port number that this socket is bound to.
Returns:
The local port that this socket is bound to.
Throws:
IndyException - if the socket is not bound
See Also:
connectClient

getUseNagle

public boolean getUseNagle()
                    throws IndyIOException
Returns whether this socket is using Nagle's Algortihm.
Returns:
true if the socket is using Nagle's Algorithm, false otherwise

isConnected

public boolean isConnected()
Returns whether or not this instance is connected.
Returns:
true if the socket is connected.

flush

public void flush()
           throws IndyIOException
Flushes the output buffer of this IOHandlerSocket
Throws:
IndyIOException - If an IO error occurs whilst flushing
NotConnectedException - if the socket is not connected

write

public void write(byte[] b,
                  int off,
                  int len)
           throws IndyIOException
Writes a seqeunce of bytes to the socket
Parameters:
b - An array of bytes containing the data
off - The offset at which to start writing
len - The number of bytes to write
Throws:
IndyIOException - If an IO error occurs
NotConnectedException - if the socket is not connected
java.lang.IndexOutOfBoundsException - if there are illegal values of b,off,or len

write

public void write(byte[] b)
           throws IndyIOException
Writes a sequence of bytes to the socket
Parameters:
b - The data to write
Throws:
IndyIOException - If an IO error occurs
See Also:
write(byte[],int,int)

write

public void write(int b)
           throws IndyIOException
Writes a single byte to the socket
Parameters:
b - The byte to write
Throws:
IndyIOException - If an IO error occurs

read

public int read()
         throws IndyIOException,
                PeerDisconnectedException,
                ReadTimedOutException
Reads a single byte of data from the socket
Returns:
The byte that was read
Throws:
IndyIOException - if an IO error occurs.
PeerDisconnectedException - if the remote machine has disconnected
ReadTimedOutException - if the read opertaion timed out
NotConnectedException - if the socket is not connected

read

public int read(int timeout)
         throws IndyIOException
DOCUMENT ME!
Parameters:
timeout - DOCUMENT ME!
Returns:
DOCUMENT ME!
Throws:
IndyIOException - DOCUMENT ME!

read

public int read(byte[] b,
                int off,
                int len,
                int timeout)
         throws IndyIOException,
                ReadTimedOutException
Reads a sequence of bytes from the socket, using the specified timeout.
Parameters:
b - An array of bytes to read the data into
off - The offset at which to start writing to b
len - The amount of data to read
timeout - The timeout to use for reading
Returns:
The number of bytes actually read
Throws:
IndyIOException - If an IO error occurs.
PeerDisconnectedException - if the remote machine has disconnected
ReadTimedOutException - if the read opertaion timed out
NotConnectedException - if the socket is not connected
java.lang.IndexOutOfBoundsException - if b,off, or len are illegal
NullPointerException - if b is null
See Also:
AbstractIOHandler.read(byte[],int,int), AbstractIOHandler.read(byte[],int), AbstractIOHandler.read(byte[]), read()

available

public int available()
              throws IndyIOException
Returns the number of bytes that be read from the socket and buffer without blocking
Returns:
The number of bytes that can be read without blocking
Throws:
IndyIOException - if an IO error occurs

connectClient

public void connectClient(java.util.Map parameters,
                          int timeout)
                   throws IndyIOException
Connects to a remote socket using the parameters given in map. The map should take the following form:
KeyValueTypeRequired
"host" The IP address or FQDN of the remote host to connect to java.lang.String Yes
"port" The port of the remote host to connect to java.lang.Stringr Yes
"boundip" The local IP address to bind to java.lang.String No
"boundport" The local port to bind to java.lang.String No
Key names are case senstive (for now). Sorry.
Overrides:
connectClient in class AbstractIOHandler
Parameters:
parameters - The parameters to use for connection (see above)
timeout - The timeout to use for this connection attempt
Throws:
IndyIOException - if an IO error occurs.
java.lang.IllegalArgumentException - if parameters does not contain a required entry (see above)

connectClient

public void connectClient(java.lang.String host,
                          int port,
                          java.lang.String boundIP,
                          int boundPort,
                          int timeout)
                   throws IndyIOException,
                          ConnectException,
                          ConnectTimedOutException,
                          IndyUnknownHostException
Connects this IOHandlerSocket to a remote socket whose address is given by host as either an IP address or FQDN and whose port is given by port If boundIP or boundPort are not null or < 1 respectively, the socket will bind to this local address and / or port.
Parameters:
host - The remote host to connect to.
port - The remote port to connect to.
boundIP - The local address to bind to
boundPort - The local port to bind to.
timeout - The timeout to use for this connection attempt
Throws:
IndyIOException - If an IO error occurs.
ConnectException - If a connection error occurs, such as the connection being refused by the remote host
ConnectTimedOutException - If the connection attempt times out
IndyUnknownHostException - If the host can not be resolved.

close

public void close()
Closes this socket connection.
Overrides:
close in class AbstractIOHandler
Following copied from class: org.indy.io.AbstractIOHandler
See Also:
IOHandler.close()

clearBuffer

public void clearBuffer()
Clears the internal buffer used by this socket

readLine

public java.lang.String readLine(int timeout,
                                 int maxLineLength,
                                 java.lang.String encoding)
                          throws IndyIOException,
                                 PeerDisconnectedException,
                                 ReadTimedOutException,
                                 MaxLineLengthExceededException
Reads a line of data from the socket and assembles it using the given encoding. If enoding is null the default platform encoding is used.
Parameters:
timeout - The timeout to use for this operation
maxLineLength - The maxiumum line length to accept
encoding - The character encoding to use to assmble the string
Returns:
A line of data from the socket
Throws:
IndyIOException - if an IO error occurs
PeerDisconnectedException - if the remote machine disconnects normally
ReadTimedOutException - if the operation times out
MaxLineLengthExceededException - if the maximum line length is exceeded before a line break is found
See Also:
AbstractIOHandler.readLine(int,int)

currentReadBuffer

public byte[] currentReadBuffer()
                         throws IndyIOException
Returns the current data that can be read from the socket without blocking
Returns:
The data avilable without blocking
Throws:
IndyIOException - If an IO error occurs.

setReadTimeOut

public void setReadTimeOut(int newReadTimeOut)
                    throws IndyIOException
DOCUMENT ME!
Overrides:
setReadTimeOut in class AbstractIOHandler
Parameters:
newReadTimeOut - DOCUMENT ME!
Throws:
IndyIOException - DOCUMENT ME!

getReadTimeOut

public int getReadTimeOut()
                   throws IndyIOException
DOCUMENT ME!
Overrides:
getReadTimeOut in class AbstractIOHandler
Returns:
DOCUMENT ME!
Throws:
IndyIOException - DOCUMENT ME!


Copyright © 2002-2002 IndyJ. All Rights Reserved.