org.tentackle.net
Class SocketImplWrapper

java.lang.Object
  extended by java.net.SocketImpl
      extended by org.tentackle.net.SocketImplWrapper
All Implemented Interfaces:
SocketOptions

public class SocketImplWrapper
extends SocketImpl

A wrapping SocketImpl.
Nice to wrap SSL sockets to add compression, for example. Notice that wrapped sockets are always connected/bound!

Author:
harald

Field Summary
 
Fields inherited from class java.net.SocketImpl
address, fd, localport, port
 
Fields inherited from interface java.net.SocketOptions
IP_MULTICAST_IF, IP_MULTICAST_IF2, IP_MULTICAST_LOOP, IP_TOS, SO_BINDADDR, SO_BROADCAST, SO_KEEPALIVE, SO_LINGER, SO_OOBINLINE, SO_RCVBUF, SO_REUSEADDR, SO_SNDBUF, SO_TIMEOUT, TCP_NODELAY
 
Constructor Summary
SocketImplWrapper(Socket socket)
          Creates an impl wrapper that delegates all method invocations to the wrapped socket.
 
Method Summary
protected  void accept(SocketImpl s)
          Accepts a connection.
protected  int available()
          Returns the number of bytes that can be read from this socket without blocking.
protected  void bind(InetAddress host, int port)
          Binds this socket to the specified local IP address and port number.
protected  void close()
          Closes this socket.
protected  void connect(InetAddress address, int port)
          Connects this socket to the specified port number on the specified host.
protected  void connect(SocketAddress address, int timeout)
          Connects this socket to the specified port number on the specified host.
protected  void connect(String host, int port)
          Connects this socket to the specified port on the named host.
protected  void create(boolean stream)
          Creates either a stream or a datagram socket.
protected  FileDescriptor getFileDescriptor()
          Returns the value of this socket's fd field.
protected  InetAddress getInetAddress()
          Returns the value of this socket's address field.
protected  InputStream getInputStream()
          Returns an input stream for this socket.
protected  int getLocalPort()
          Returns the value of this socket's localport field.
 Object getOption(int optID)
          Fetch the value of an option.
protected  OutputStream getOutputStream()
          Returns an output stream for this socket.
protected  int getPort()
          Returns the value of this socket's port field.
protected  void listen(int backlog)
          Sets the maximum queue length for incoming connection indications (a request to connect) to the count argument.
protected  void sendUrgentData(int data)
          Send one byte of urgent data on the socket.
 void setOption(int optID, Object value)
          Enable/disable the option specified by optID.
protected  void shutdownInput()
          Places the input stream for this socket at "end of stream".
protected  void shutdownOutput()
          Disables the output stream for this socket.
 
Methods inherited from class java.net.SocketImpl
setPerformancePreferences, supportsUrgentData, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

SocketImplWrapper

public SocketImplWrapper(Socket socket)
Creates an impl wrapper that delegates all method invocations to the wrapped socket.

Parameters:
socket - the socket to wrap
Method Detail

shutdownInput

protected void shutdownInput()
                      throws IOException
Description copied from class: java.net.SocketImpl
Places the input stream for this socket at "end of stream". Any data sent to this socket is acknowledged and then silently discarded. If you read from a socket input stream after invoking shutdownInput() on the socket, the stream will return EOF.

Overrides:
shutdownInput in class SocketImpl
Throws:
IOException - if an I/O error occurs when shutting down this socket.
See Also:
Socket.shutdownOutput(), Socket.close(), Socket.setSoLinger(boolean, int)

shutdownOutput

protected void shutdownOutput()
                       throws IOException
Description copied from class: java.net.SocketImpl
Disables the output stream for this socket. For a TCP socket, any previously written data will be sent followed by TCP's normal connection termination sequence. If you write to a socket output stream after invoking shutdownOutput() on the socket, the stream will throw an IOException.

Overrides:
shutdownOutput in class SocketImpl
Throws:
IOException - if an I/O error occurs when shutting down this socket.
See Also:
Socket.shutdownInput(), Socket.close(), Socket.setSoLinger(boolean, int)

getFileDescriptor

protected FileDescriptor getFileDescriptor()
Description copied from class: java.net.SocketImpl
Returns the value of this socket's fd field.

Overrides:
getFileDescriptor in class SocketImpl
Returns:
the value of this socket's fd field.
See Also:
SocketImpl.fd

getInetAddress

protected InetAddress getInetAddress()
Description copied from class: java.net.SocketImpl
Returns the value of this socket's address field.

Overrides:
getInetAddress in class SocketImpl
Returns:
the value of this socket's address field.
See Also:
SocketImpl.address

getPort

protected int getPort()
Description copied from class: java.net.SocketImpl
Returns the value of this socket's port field.

Overrides:
getPort in class SocketImpl
Returns:
the value of this socket's port field.
See Also:
SocketImpl.port

getLocalPort

protected int getLocalPort()
Description copied from class: java.net.SocketImpl
Returns the value of this socket's localport field.

Overrides:
getLocalPort in class SocketImpl
Returns:
the value of this socket's localport field.
See Also:
SocketImpl.localport

create

protected void create(boolean stream)
               throws IOException
Description copied from class: java.net.SocketImpl
Creates either a stream or a datagram socket.

Specified by:
create in class SocketImpl
Parameters:
stream - if true, create a stream socket; otherwise, create a datagram socket.
Throws:
IOException - if an I/O error occurs while creating the socket.

connect

protected void connect(String host,
                       int port)
                throws IOException
Description copied from class: java.net.SocketImpl
Connects this socket to the specified port on the named host.

Specified by:
connect in class SocketImpl
Parameters:
host - the name of the remote host.
port - the port number.
Throws:
IOException - if an I/O error occurs when connecting to the remote host.

connect

protected void connect(InetAddress address,
                       int port)
                throws IOException
Description copied from class: java.net.SocketImpl
Connects this socket to the specified port number on the specified host.

Specified by:
connect in class SocketImpl
Parameters:
address - the IP address of the remote host.
port - the port number.
Throws:
IOException - if an I/O error occurs when attempting a connection.

connect

protected void connect(SocketAddress address,
                       int timeout)
                throws IOException
Description copied from class: java.net.SocketImpl
Connects this socket to the specified port number on the specified host. A timeout of zero is interpreted as an infinite timeout. The connection will then block until established or an error occurs.

Specified by:
connect in class SocketImpl
Parameters:
address - the Socket address of the remote host.
timeout - the timeout value, in milliseconds, or zero for no timeout.
Throws:
IOException - if an I/O error occurs when attempting a connection.

bind

protected void bind(InetAddress host,
                    int port)
             throws IOException
Description copied from class: java.net.SocketImpl
Binds this socket to the specified local IP address and port number.

Specified by:
bind in class SocketImpl
Parameters:
host - an IP address that belongs to a local interface.
port - the port number.
Throws:
IOException - if an I/O error occurs when binding this socket.

listen

protected void listen(int backlog)
               throws IOException
Description copied from class: java.net.SocketImpl
Sets the maximum queue length for incoming connection indications (a request to connect) to the count argument. If a connection indication arrives when the queue is full, the connection is refused.

Specified by:
listen in class SocketImpl
Parameters:
backlog - the maximum length of the queue.
Throws:
IOException - if an I/O error occurs when creating the queue.

accept

protected void accept(SocketImpl s)
               throws IOException
Description copied from class: java.net.SocketImpl
Accepts a connection.

Specified by:
accept in class SocketImpl
Parameters:
s - the accepted connection.
Throws:
IOException - if an I/O error occurs when accepting the connection.

getInputStream

protected InputStream getInputStream()
                              throws IOException
Description copied from class: java.net.SocketImpl
Returns an input stream for this socket.

Specified by:
getInputStream in class SocketImpl
Returns:
a stream for reading from this socket.
Throws:
IOException - if an I/O error occurs when creating the input stream.

getOutputStream

protected OutputStream getOutputStream()
                                throws IOException
Description copied from class: java.net.SocketImpl
Returns an output stream for this socket.

Specified by:
getOutputStream in class SocketImpl
Returns:
an output stream for writing to this socket.
Throws:
IOException - if an I/O error occurs when creating the output stream.

available

protected int available()
                 throws IOException
Description copied from class: java.net.SocketImpl
Returns the number of bytes that can be read from this socket without blocking.

Specified by:
available in class SocketImpl
Returns:
the number of bytes that can be read from this socket without blocking.
Throws:
IOException - if an I/O error occurs when determining the number of bytes available.

close

protected void close()
              throws IOException
Description copied from class: java.net.SocketImpl
Closes this socket.

Specified by:
close in class SocketImpl
Throws:
IOException - if an I/O error occurs when closing this socket.

sendUrgentData

protected void sendUrgentData(int data)
                       throws IOException
Description copied from class: java.net.SocketImpl
Send one byte of urgent data on the socket. The byte to be sent is the low eight bits of the parameter

Specified by:
sendUrgentData in class SocketImpl
Parameters:
data - The byte of data to send
Throws:
IOException - if there is an error sending the data.

setOption

public void setOption(int optID,
                      Object value)
               throws SocketException
Description copied from interface: java.net.SocketOptions
Enable/disable the option specified by optID. If the option is to be enabled, and it takes an option-specific "value", this is passed in value. The actual type of value is option-specific, and it is an error to pass something that isn't of the expected type:
 SocketImpl s;
 ...
 s.setOption(SO_LINGER, new Integer(10));
    // OK - set SO_LINGER w/ timeout of 10 sec.
 s.setOption(SO_LINGER, new Double(10));
    // ERROR - expects java.lang.Integer
If the requested option is binary, it can be set using this method by a java.lang.Boolean:
 s.setOption(TCP_NODELAY, new Boolean(true));
    // OK - enables TCP_NODELAY, a binary option
 

Any option can be disabled using this method with a Boolean(false):
 s.setOption(TCP_NODELAY, new Boolean(false));
    // OK - disables TCP_NODELAY
 s.setOption(SO_LINGER, new Boolean(false));
    // OK - disables SO_LINGER
 

For an option that has a notion of on and off, and requires a non-boolean parameter, setting its value to anything other than Boolean(false) implicitly enables it.
Throws SocketException if the option is unrecognized, the socket is closed, or some low-level error occurred

Parameters:
optID - identifies the option
value - the parameter of the socket option
Throws:
SocketException - if the option is unrecognized, the socket is closed, or some low-level error occurred
See Also:
SocketOptions.getOption(int)

getOption

public Object getOption(int optID)
                 throws SocketException
Description copied from interface: java.net.SocketOptions
Fetch the value of an option. Binary options will return java.lang.Boolean(true) if enabled, java.lang.Boolean(false) if disabled, e.g.:
 SocketImpl s;
 ...
 Boolean noDelay = (Boolean)(s.getOption(TCP_NODELAY));
 if (noDelay.booleanValue()) {
     // true if TCP_NODELAY is enabled...
 ...
 }
 

For options that take a particular type as a parameter, getOption(int) will return the paramter's value, else it will return java.lang.Boolean(false):

 Object o = s.getOption(SO_LINGER);
 if (o instanceof Integer) {
     System.out.print("Linger time is " + ((Integer)o).intValue());
 } else {
   // the true type of o is java.lang.Boolean(false);
 }
 

Parameters:
optID - an int identifying the option to fetch
Returns:
the value of the option
Throws:
SocketException - if the socket is closed
See Also:
SocketOptions.setOption(int, java.lang.Object)


Copyright © 2001-2008 Harald Krake, Bergstr. 48, 78098 Triberg, Germany, harald@krake.de