2003-11-26 Michael Koch <konqueror@gmx.de>
+ * java/net/DatagramSocket.java
+ (impl): Made private.
+ (bound): New private member variable.
+ (DatagramSocket): Fixed documentation, use getImpl().
+ (getImpl): New package-private method.
+ (isClosed): Use getImpl().
+ (getLocalAddress): Completed documentation, use getImpl().
+ (getLocalPort): Use getImpl().
+ (getSoTimeout): Likewise.
+ (setSoTimeout): Likewise.
+ (getSendBufferSize): Likewise.
+ (setSendBufferSize): Likewise.
+ (getReceiveBufferSize): Likewise.
+ (setReceiveBufferSize): Likewise.
+ (connect): Likewise.
+ (disconnect): Likewise.
+ (receive): Likewise.
+ (send): Likewise.
+ (setReuseAddress): Likewise.
+ (setTrafficClass): Likewise.
+ (bind): Added message to exception.
+ (isClosed): Completed documentation.
+ (getChannel): Likewise.
+ (connect): Added missing exception, refined exception message.
+ (isBound): Completed documentation, just return bound.
+ (isConnected): Completed documentation.
+ (getRemoteSocketAddress): Likewise.
+ (getReuseAddress): Completed documentation, use getImpl().
+ (setSoBroadcast): Likewise.
+ (getSoBroadcast): Likewise.
+ (getTrafficClass): Likewise.
+ (getLocalSocketAddress): Simplified.
+ * java/net/MulticastSocket.java
+ (MulticastSocket): Removed comment not applying anymore.
+ (getInterface): Use getImpl().
+ (getTTL): Likewise.
+ (getTimeToLive): Likewise.
+ (setInterface): Likewise.
+ (setNetworkInterface): Likewise.
+ (getNetworkInterface): Likewise.
+ (setLoopback): Likewise.
+ (getLoopback): Likewise.
+ (setTTL): Likewise.
+ (setTimeToLive): Likewise.
+ (joinGroup): Likewise.
+ (leaveGroup): Likewise.
+ (send): Likewise.
+
+2003-11-26 Michael Koch <konqueror@gmx.de>
+
* java/net/Socket.java
(implCreated): Dont set default value explicitely, added
documentation.
/**
* This is the implementation object used by this socket.
*/
- DatagramSocketImpl impl;
+ private DatagramSocketImpl impl;
+
+ /**
+ * True if socket implementation was created.
+ */
+ private boolean implCreated;
/**
* This is the address we are "connected" to
private int remotePort = -1;
/**
+ * True if socket is bound.
+ */
+ private boolean bound;
+
+ /**
* Creates a <code>DatagramSocket</code> from a specified
* <code>DatagramSocketImpl</code> instance
*
* Initializes a new instance of <code>DatagramSocket</code> that binds to
* the specified local port and address.
*
- * @param port The local port number to bind to.
- * @param laddr The local address to bind to.
+ * @param address The local address and port number to bind to.
*
* @exception SecurityException If a security manager exists and its
* <code>checkListen</code> method doesn't allow the operation.
propVal + "DatagramSocketImpl");
impl = new PlainDatagramSocketImpl();
}
- impl.create();
if (address == null)
return;
try
{
- impl.bind(port, addr);
+ getImpl().bind(port, addr);
}
catch (SocketException exception)
{
- impl.close();
+ getImpl().close();
throw exception;
}
catch (RuntimeException exception)
{
- impl.close();
+ getImpl().close();
throw exception;
}
catch (Error error)
{
- impl.close();
+ getImpl().close();
throw error;
}
}
+ // This needs to be accessible from java.net.MulticastSocket
+ DatagramSocketImpl getImpl()
+ throws SocketException
+ {
+ try
+ {
+ if (!implCreated)
+ {
+ impl.create();
+ implCreated = true;
+ }
+
+ return impl;
+ }
+ catch (IOException e)
+ {
+ throw new SocketException(e.getMessage());
+ }
+ }
+
/**
* Closes this datagram socket.
*/
{
if (!isClosed())
{
- impl.close();
- impl = null;
- remoteAddress = null;
- remotePort = -1;
+ try
+ {
+ getImpl().close();
+ }
+ catch (SocketException e)
+ {
+ // Ignore this case, just close the socket in finally clause.
+ }
+ finally
+ {
+ remoteAddress = null;
+ remotePort = -1;
+ impl = null;
+ }
}
}
/**
* Returns the local address this datagram socket is bound to.
*
+ * @return The local address is the socket is bound or null
+ *
* @since 1.1
*/
public InetAddress getLocalAddress()
try
{
- localAddr = (InetAddress) impl.getOption (SocketOptions.SO_BINDADDR);
+ localAddr = (InetAddress) getImpl().getOption(SocketOptions.SO_BINDADDR);
SecurityManager s = System.getSecurityManager();
if (s != null)
}
catch (SocketException e)
{
+ // This cannot happen as we are bound.
return null;
}
if (isClosed())
return -1;
- return impl.getLocalPort();
+ try
+ {
+ return getImpl().getLocalPort();
+ }
+ catch (SocketException e)
+ {
+ // This cannot happen as we are bound.
+ return 0;
+ }
}
/**
if (isClosed())
throw new SocketException("socket is closed");
- Object timeout = impl.getOption(SocketOptions.SO_TIMEOUT);
+ Object buf = getImpl().getOption(SocketOptions.SO_TIMEOUT);
- if (timeout instanceof Integer)
- return ((Integer)timeout).intValue();
- else
- return 0;
+ if (buf instanceof Integer)
+ return ((Integer) buf).intValue();
+
+ throw new SocketException("unexpected type");
}
/**
if (timeout < 0)
throw new IllegalArgumentException("Invalid timeout: " + timeout);
- impl.setOption(SocketOptions.SO_TIMEOUT, new Integer(timeout));
+ getImpl().setOption(SocketOptions.SO_TIMEOUT, new Integer(timeout));
}
/**
{
if (isClosed())
throw new SocketException("socket is closed");
+
+ Object buf = getImpl().getOption(SocketOptions.SO_SNDBUF);
- Object obj = impl.getOption(SocketOptions.SO_SNDBUF);
+ if (buf instanceof Integer)
+ return ((Integer) buf).intValue();
- if (obj instanceof Integer)
- return(((Integer)obj).intValue());
- else
- throw new SocketException("Unexpected type");
+ throw new SocketException("unexpected type");
}
/**
if (size < 0)
throw new IllegalArgumentException("Buffer size is less than 0");
- impl.setOption(SocketOptions.SO_SNDBUF, new Integer(size));
+ getImpl().setOption(SocketOptions.SO_SNDBUF, new Integer(size));
}
/**
if (isClosed())
throw new SocketException("socket is closed");
- Object obj = impl.getOption(SocketOptions.SO_RCVBUF);
+ Object buf = getImpl().getOption(SocketOptions.SO_RCVBUF);
- if (obj instanceof Integer)
- return(((Integer)obj).intValue());
- else
- throw new SocketException("Unexpected type");
+ if (buf instanceof Integer)
+ return ((Integer) buf).intValue();
+
+ throw new SocketException("unexpected type");
}
/**
if (size < 0)
throw new IllegalArgumentException("Buffer size is less than 0");
- impl.setOption(SocketOptions.SO_RCVBUF, new Integer(size));
+ getImpl().setOption(SocketOptions.SO_RCVBUF, new Integer(size));
}
/**
try
{
- impl.connect (address, port);
+ getImpl().connect (address, port);
remoteAddress = address;
remotePort = port;
}
*/
public void disconnect()
{
- impl.disconnect();
- remoteAddress = null;
- remotePort = -1;
+ if (!isConnected())
+ return;
+
+ try
+ {
+ getImpl().disconnect();
+ }
+ catch (SocketException e)
+ {
+ // This cannot happen as we are connected.
+ }
+ finally
+ {
+ remoteAddress = null;
+ remotePort = -1;
+ }
}
/**
&& !getChannel().isBlocking ())
throw new IllegalBlockingModeException ();
- impl.receive(p);
+ getImpl().receive(p);
SecurityManager s = System.getSecurityManager();
if (s != null && isConnected ())
&& !getChannel().isBlocking ())
throw new IllegalBlockingModeException ();
- impl.send(p);
+ getImpl().send(p);
}
/**
throw new SocketException("socket is closed");
if (! (address instanceof InetSocketAddress))
- throw new IllegalArgumentException ();
+ throw new IllegalArgumentException("unsupported address type");
InetSocketAddress tmp = (InetSocketAddress) address;
if (s != null)
s.checkListen(tmp.getPort ());
- impl.bind (tmp.getPort (), tmp.getAddress ());
+ getImpl().bind (tmp.getPort (), tmp.getAddress ());
+ bound = true;
}
/**
* Checks if the datagram socket is closed.
*
+ * @return True if socket is closed, false otherwise.
+ *
* @since 1.4
*/
public boolean isClosed()
/**
* Returns the datagram channel assoziated with this datagram socket.
*
+ * @return The associated <code>DatagramChannel</code> object or null
+ *
* @since 1.4
*/
public DatagramChannel getChannel()
public void connect (SocketAddress address) throws SocketException
{
if (isClosed())
+ throw new SocketException("socket is closed");
if ( !(address instanceof InetSocketAddress) )
- throw new IllegalArgumentException (
- "SocketAddress is not InetSocketAddress");
+ throw new IllegalArgumentException("unsupported address type");
InetSocketAddress tmp = (InetSocketAddress) address;
- connect( tmp.getAddress(), tmp.getPort());
+ connect(tmp.getAddress(), tmp.getPort());
}
/**
* Returns the binding state of the socket.
*
+ * @return True if socket bound, false otherwise.
+ *
* @since 1.4
*/
public boolean isBound()
{
- try
- {
- Object bindaddr = impl.getOption (SocketOptions.SO_BINDADDR);
- }
- catch (SocketException e)
- {
- return false;
- }
-
- return true;
+ return bound;
}
/**
* Returns the connection state of the socket.
*
+ * @return True if socket is connected, false otherwise.
+ *
* @since 1.4
*/
public boolean isConnected()
* Returns the SocketAddress of the host this socket is conneted to
* or null if this socket is not connected.
*
+ * @return The socket address of the remote host if connected or null
+ *
* @since 1.4
*/
public SocketAddress getRemoteSocketAddress()
}
/**
- * Returns the local SocketAddress this socket is bound to
- * or null if it is not bound.
+ * Returns the local SocketAddress this socket is bound to.
+ *
+ * @return The local SocketAddress or null if the socket is not bound.
*
* @since 1.4
*/
public SocketAddress getLocalSocketAddress()
{
- InetAddress addr;
+ if (!isBound())
+ return null;
- try
- {
- addr = (InetAddress) impl.getOption (SocketOptions.SO_BINDADDR);
- }
- catch (SocketException e)
- {
- return null;
- }
-
- return new InetSocketAddress (addr, impl.localPort);
+ return new InetSocketAddress (getLocalAddress(), getLocalPort());
}
/**
if (isClosed())
throw new SocketException("socket is closed");
- impl.setOption (SocketOptions.SO_REUSEADDR, new Boolean (on));
+ getImpl().setOption (SocketOptions.SO_REUSEADDR, new Boolean (on));
}
/**
* Checks if SO_REUSEADDR is enabled.
*
+ * @return True if SO_REUSEADDR is set on the socket, false otherwise.
+ *
* @exception SocketException If an error occurs.
*
* @since 1.4
if (isClosed())
throw new SocketException("socket is closed");
- Object obj = impl.getOption (SocketOptions.SO_REUSEADDR);
+ Object buf = getImpl().getOption (SocketOptions.SO_REUSEADDR);
- if (obj instanceof Boolean)
- return(((Boolean) obj).booleanValue ());
- else
- throw new SocketException ("Unexpected type");
+ if (buf instanceof Boolean)
+ return ((Boolean) buf).booleanValue();
+
+ throw new SocketException("unexpected type");
}
/**
* Enables/Disables SO_BROADCAST
*
- * @param on Whether or not to have SO_BROADCAST turned on
+ * @param enable True if SO_BROADCAST should be enabled, false otherwise.
*
* @exception SocketException If an error occurs
*
* @since 1.4
*/
- public void setBroadcast(boolean on) throws SocketException
+ public void setBroadcast(boolean enable) throws SocketException
{
if (isClosed())
throw new SocketException("socket is closed");
- impl.setOption (SocketOptions.SO_BROADCAST, new Boolean (on));
+ getImpl().setOption(SocketOptions.SO_BROADCAST, new Boolean(enable));
}
/**
* Checks if SO_BROADCAST is enabled
*
+ * @return Whether SO_BROADCAST is set
+ *
* @exception SocketException If an error occurs
*
* @since 1.4
if (isClosed())
throw new SocketException("socket is closed");
- Object obj = impl.getOption (SocketOptions.SO_BROADCAST);
+ Object buf = getImpl().getOption(SocketOptions.SO_BROADCAST);
- if (obj instanceof Boolean)
- return ((Boolean) obj).booleanValue ();
- else
- throw new SocketException ("Unexpected type");
+ if (buf instanceof Boolean)
+ return ((Boolean) buf).booleanValue();
+
+ throw new SocketException("unexpected type");
}
/**
if (tc < 0 || tc > 255)
throw new IllegalArgumentException();
- impl.setOption (SocketOptions.IP_TOS, new Integer (tc));
+ getImpl().setOption (SocketOptions.IP_TOS, new Integer (tc));
}
/**
* Returns the current traffic class
*
+ * @return The current traffic class.
+ *
* @see DatagramSocket#setTrafficClass(int tc)
*
* @exception SocketException If an error occurs
{
if (isClosed())
throw new SocketException("socket is closed");
+
+ Object buf = getImpl().getOption(SocketOptions.IP_TOS);
- Object obj = impl.getOption(SocketOptions.IP_TOS);
+ if (buf instanceof Integer)
+ return ((Integer) buf).intValue();
- if (obj instanceof Integer)
- return ((Integer) obj).intValue ();
- else
- throw new SocketException ("Unexpected type");
+ throw new SocketException("unexpected type");
}
/**
*/
public class MulticastSocket extends DatagramSocket
{
- // FIXME: the local addr bound to the multicast socket can be reused;
- // unlike unicast sockets. It binds to any available network interface.
- // See p.1159 JCL book.
-
/**
* Create a MulticastSocket that this not bound to any address
*
if (isClosed())
throw new SocketException("socket is closed");
- return (InetAddress) impl.getOption(SocketOptions.IP_MULTICAST_IF);
+ return (InetAddress) getImpl().getOption(SocketOptions.IP_MULTICAST_IF);
}
/**
// Use getTTL here rather than getTimeToLive in case we're using an impl
// other than the default PlainDatagramSocketImpl and it doesn't have
// getTimeToLive yet.
- return impl.getTTL();
+ return getImpl().getTTL();
}
/**
if (isClosed())
throw new SocketException("socket is closed");
- return impl.getTimeToLive();
+ return getImpl().getTimeToLive();
}
/**
if (isClosed())
throw new SocketException("socket is closed");
- impl.setOption(SocketOptions.IP_MULTICAST_IF, addr);
+ getImpl().setOption(SocketOptions.IP_MULTICAST_IF, addr);
}
/**
Enumeration e = netIf.getInetAddresses ();
if (!e.hasMoreElements ())
- throw new SocketException ("MulticastSocket: Error");
+ throw new SocketException("no network devices found");
InetAddress address = (InetAddress) e.nextElement ();
- impl.setOption (SocketOptions.IP_MULTICAST_IF, address);
+ getImpl().setOption (SocketOptions.IP_MULTICAST_IF, address);
}
/**
throw new SocketException("socket is closed");
InetAddress address =
- (InetAddress) impl.getOption (SocketOptions.IP_MULTICAST_IF);
+ (InetAddress) getImpl().getOption (SocketOptions.IP_MULTICAST_IF);
NetworkInterface netIf = NetworkInterface.getByInetAddress (address);
return netIf;
if (isClosed())
throw new SocketException("socket is closed");
- impl.setOption (SocketOptions.IP_MULTICAST_LOOP, new Boolean (disable));
+ getImpl().setOption (SocketOptions.IP_MULTICAST_LOOP, new Boolean (disable));
}
/**
if (isClosed())
throw new SocketException("socket is closed");
- Object obj = impl.getOption (SocketOptions.IP_MULTICAST_LOOP);
+ Object buf = getImpl().getOption (SocketOptions.IP_MULTICAST_LOOP);
- if (obj instanceof Boolean)
- return ((Boolean) obj).booleanValue ();
- else
- throw new SocketException ("Unexpected type");
+ if (buf instanceof Boolean)
+ return ((Boolean) buf).booleanValue();
+
+ throw new SocketException("unexpected type");
}
/**
// Use setTTL here rather than setTimeToLive in case we're using an impl
// other than the default PlainDatagramSocketImpl and it doesn't have
// setTimeToLive yet.
- impl.setTTL(ttl);
+ getImpl().setTTL(ttl);
}
/**
if (ttl <= 0 || ttl > 255)
throw new IllegalArgumentException("Invalid ttl: " + ttl);
- impl.setTimeToLive(ttl);
+ getImpl().setTimeToLive(ttl);
}
/**
if (s != null)
s.checkMulticast(mcastaddr);
- impl.join(mcastaddr);
+ getImpl().join(mcastaddr);
}
/**
if (s != null)
s.checkMulticast(mcastaddr);
- impl.leave(mcastaddr);
+ getImpl().leave(mcastaddr);
}
/**
if (s != null)
s.checkMulticast (tmp.getAddress ());
- impl.joinGroup (mcastaddr, netIf);
+ getImpl().joinGroup (mcastaddr, netIf);
}
/**
if (s != null)
s.checkMulticast (tmp.getAddress ());
- impl.leaveGroup (mcastaddr, netIf);
+ getImpl().leaveGroup (mcastaddr, netIf);
}
/**
s.checkConnect(addr.getHostAddress(), p.getPort());
}
- int oldttl = impl.getTimeToLive();
- impl.setTimeToLive(((int) ttl) & 0xFF);
- impl.send(p);
- impl.setTimeToLive(oldttl);
+ int oldttl = getImpl().getTimeToLive();
+ getImpl().setTimeToLive(((int) ttl) & 0xFF);
+ getImpl().send(p);
+ getImpl().setTimeToLive(oldttl);
}
} // class MulticastSocket