peerbase.socket
Class NormalSocket

java.lang.Object
  extended by peerbase.socket.NormalSocket
All Implemented Interfaces:
SocketInterface

public class NormalSocket
extends java.lang.Object
implements SocketInterface

Encapsulates the standard Socket object of the Java library to fit the SocketInterface of the PeerBase system.

Author:
Nadeem Abdul Hamid

Constructor Summary
NormalSocket(java.net.Socket socket)
          Encapsulates a normal Java API Socket object.
NormalSocket(java.lang.String host, int port)
          Creates a stream socket and connects it to the specified port number on the named host.
 
Method Summary
 void close()
          Closes this connection and releases any system resources associated with the socket.
 int read()
          Reads the next byte of data from the socket connection.
 int read(byte[] b)
          Reads some number of bytes from the socket connection and stores them into the buffer array b.
 void write(byte[] b)
          Writes b.length bytes from the specified byte array to this socket connection.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

NormalSocket

public NormalSocket(java.lang.String host,
                    int port)
             throws java.io.IOException,
                    java.net.UnknownHostException
Creates a stream socket and connects it to the specified port number on the named host.

Parameters:
host - the host name, or null for the loopback address
port - the port number
Throws:
java.io.IOException - if an I/O error occurs when creating the socket
java.net.UnknownHostException - if the IP address of the host could not be determined

NormalSocket

public NormalSocket(java.net.Socket socket)
             throws java.io.IOException
Encapsulates a normal Java API Socket object.

Parameters:
socket - an already-open socket connection
Throws:
java.io.IOException
Method Detail

close

public void close()
           throws java.io.IOException
Description copied from interface: SocketInterface
Closes this connection and releases any system resources associated with the socket.

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

read

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

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

read

public int read(byte[] b)
         throws java.io.IOException
Description copied from interface: SocketInterface
Reads some number of bytes from the socket connection and stores them into the buffer array b. The number of bytes actually read is returned as an integer. This method blocks until input data is available, end of file is detected, or an exception is thrown.

Specified by:
read in interface SocketInterface
Parameters:
b - the buffer into which the 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 input has been reached
Throws:
java.io.IOException - if an I/O error occurs
See Also:
InputStream#read()

write

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

Specified by:
write in interface SocketInterface
Parameters:
b - the data
Throws:
java.io.IOException - if an I/O error occurs