hamsam.net
Class SocksConnection

java.lang.Object
  |
  +--hamsam.net.SocksConnection
All Implemented Interfaces:
Connection

public class SocksConnection
extends java.lang.Object
implements Connection

A SocksConnection connects to a specified host and port through a socks (version 4 or 5) proxy server. This type of connection is used if the user is connecting through a socks proxy server.

See Also:
DirectConnection, HttpConnection

Constructor Summary
SocksConnection(java.lang.String socksHost, int socksPort, java.lang.String host, int port)
          Construct a connection to the specified host and port using socks version 4 protocol.
SocksConnection(java.lang.String socksHost, int socksPort, java.lang.String username, java.lang.String password, java.lang.String host, int port)
          Construct a connection to the specified host and port using socks version 5 protocol.
 
Method Summary
 int available()
          Returns the number of bytes that can be read from this connection without blocking by the next caller of a method for this connection.
 void close()
          Closes this connection and releases any resources associated with it.
 void flush()
          Flushes this connection and forces any buffered output bytes to be written out.
 java.io.InputStream getInputStream()
          Returns an InputStream that can be used to indirectly call the read methods.
 java.net.Socket getSocket()
          Returns the socket used by this connection.
 int read()
          Reads the next byte of data from the input stream.
 int read(byte[] b)
          Reads some number of bytes from the connection and stores them into the buffer array b.
 int read(byte[] b, int off, int len)
          Reads up to len bytes of data from the connection into an array of bytes.
 void write(byte[] b)
          Writes b.length bytes from the specified byte array to this connection.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SocksConnection

public SocksConnection(java.lang.String socksHost,
                       int socksPort,
                       java.lang.String host,
                       int port)
                throws java.net.UnknownHostException,
                       java.io.IOException
Construct a connection to the specified host and port using socks version 4 protocol.

Parameters:
socksHost - The socks proxy server host name.
socksPort - The socks proxy server port number.
host - The host name to be connected.
port - The port number on which to be connected.
Throws:
java.net.UnknownHostException - If the IP address of the host could not be determined.
java.io.IOException - If an I/O error occurs when creating the connection.
java.lang.SecurityException - If a security manager exists and its checkConnect method doesn't allow the operation.

SocksConnection

public SocksConnection(java.lang.String socksHost,
                       int socksPort,
                       java.lang.String username,
                       java.lang.String password,
                       java.lang.String host,
                       int port)
                throws java.net.UnknownHostException,
                       java.io.IOException
Construct a connection to the specified host and port using socks version 5 protocol. A version 5 proxy may optionally require a user name and password for establishing connection. Those can be passed to this constructor. If the proxy does not require a user name and password, pass null for the parameters username and password.

Parameters:
socksHost - The socks proxy server host name.
socksPort - The socks proxy server port number.
username - The username to connect to the proxy.
password - The password to connect to the proxy.
host - The host name to be connected.
port - The port number on which to be connected.
Throws:
java.net.UnknownHostException - If the IP address of the host could not be determined.
java.io.IOException - If an I/O error occurs when creating the connection.
java.lang.SecurityException - If a security manager exists and its checkConnect method doesn't allow the operation.
Method Detail

available

public int available()
              throws java.io.IOException
Returns the number of bytes that can be read from this connection without blocking by the next caller of a method for this connection. The next caller might be the same thread or or another thread.

This method has exactly the same semantics as that of java.io.InputStream.available().

Specified by:
available in interface Connection
Returns:
the number of bytes that can be read from this connection without blocking.
Throws:
java.io.IOException - if an I/O error occurs.

read

public int read()
         throws java.io.IOException
Reads the next byte of data from the input stream. The value byte is returned as an int in the range 0 to 255. If no byte is available because the end of the stream has been reached, the value -1 is returned. This method blocks until input data is available, the end of the stream is detected, or an exception is thrown.

Specified by:
read in interface Connection
Returns:
the next byte of data, or -1 if the end of the stream is reached.
Throws:
java.io.IOException - if an I/O error occurs.

read

public int read(byte[] b)
         throws java.io.IOException
Reads some number of bytes from the connection and stores them into the buffer array b. The number of bytes actually read is returned as an integer. If no bytes are available because of EOF, returns -1. This call has exactly the same semantics as that of java.io.InputStream.read(byte[] b).

Specified by:
read in interface Connection
Parameters:
b - The buffer to which data is read.
Returns:
The total number of bytes read into the buffer, or -1 if there is no more data because the end of the stream has been reached.
Throws:
java.io.IOException - If an I/O error occurs.

read

public int read(byte[] b,
                int off,
                int len)
         throws java.io.IOException
Reads up to len bytes of data from the connection into an array of bytes. An attempt is made to read as many as len bytes, but a smaller number may be read, possibly zero. The number of bytes actually read is returned as an integer. If no bytes are available because of EOF, returns -1. This call has exactly the same semantics as that of java.io.InputStream.read(byte[] b, int off, int len).

Specified by:
read in interface Connection
Parameters:
b - The buffer to which data is read.
off - The start offset in array b at which data is written.
len - The maximum number of bytes to be read.
Returns:
The total number of bytes read into the buffer, or -1 if there is no more data because the end of the stream has been reached.
Throws:
java.io.IOException - If an I/O error occurs.

write

public void write(byte[] b)
           throws java.io.IOException
Writes b.length bytes from the specified byte array to this connection.

Specified by:
write in interface Connection
Parameters:
b - The data to be sent through this connection.
Throws:
java.io.IOException - If an I/O error occurs.

flush

public void flush()
           throws java.io.IOException
Flushes this connection and forces any buffered output bytes to be written out. The general contract of flush is that calling it is an indication that, if any bytes previously written have been buffered by the implementation of the connection, such bytes should immediately be written to their intended destination.

Specified by:
flush in interface Connection
Throws:
java.io.IOException - If an I/O error occurs.

close

public void close()
           throws java.io.IOException
Closes this connection and releases any resources associated with it.

Specified by:
close in interface Connection
Throws:
java.io.IOException - If an I/O error occurs.

getInputStream

public java.io.InputStream getInputStream()
                                   throws java.io.IOException
Returns an InputStream that can be used to indirectly call the read methods.

Specified by:
getInputStream in interface Connection
Returns:
An InputStream for reading from this connection.
Throws:
java.io.IOException - If an I/O error occurs.

getSocket

public java.net.Socket getSocket()
                          throws java.io.IOException
Returns the socket used by this connection.

Specified by:
getSocket in interface Connection
Returns:
the socket used by this connection
Throws:
java.io.IOException - If an I/O error occurs.