[kaffe] CVS kaffe (dalibor): Resynced with GNU Classpath

Kaffe CVS cvs-commits at kaffe.org
Wed Dec 3 15:21:03 PST 2003


PatchSet 4190 
Date: 2003/12/03 23:18:01
Author: dalibor
Branch: HEAD
Tag: (none) 
Log:
Resynced with GNU Classpath

2003-12-03  Dalibor Topic <robilad at kaffe.org>

        * libraries/javalib/java/net/DatagramSocket.java,
        libraries/javalib/java/net/MulticastSocket.java,
        libraries/javalib/java/net/ServerSocket.java,
        libraries/javalib/java/net/Socket.java:
        Resynced with GNU Classpath.

        2003-12-02  Michael Koch  <konqueror at gmx.de>

        * java/net/DatagramSocket.java
        (close): Close associated DatagramChannel object.
        * java/net/Socket.java
        (close): Reset impl and bound before calling getChannel().close() to
        prevent from loops.

        2003-12-02  Michael Koch  <konqueror at gmx.de>

        * java/net/DatagramSocket.java
        (DatagramSocket): Call other sister constructor.
        (connect): Revised exception message.
        (setBroadcast): Renamed on to enable.
        (getLocalAddress): Completed documentation.
        (getChannel): Likewise.
        (isBound): Likewise.
        (isConnected): Likewise.
        (getRemoteSocketAddress): Likewise.
        (getLocalSocketAddress): Likewise.
        (getReuseAddress): Likewise.
        (setBroadcast): Likewise.
        (getTrafficClass): Likewise.
        * java/net/ServerSocket.java
        (getLocalSocketAddress): Simplified.
        (accept): Renamed s to socket.
        (implAccept): Likewise.
        (toString): Made it compliant to SUNs JDK.
        * java/net/Socket.java
        (impl): Make private.
        (getInetAddress): Check if socket is connected.
        (getPort): Likewise.
        (getInputStream): Likewise.
        (getOutputStream): Likewise.
        (getLocalPort): Check if socket is bound.
        (shutdownOutput):
        Readd accidently removed "getImpl().shutdownOutput()".
        (connect): Completed documentation.
        (getOOBInline): Likewise.
        (getReuseAddress): Likewise.
        (getTrafficClass): Likewise.
        (isConnected): Likewise.
        (isBound): Likewise.
        (isClosed): Likewise.
        (isInputShutdown): Likewise.
        (isOutputShutdown): Likewise.

        2003-12-02  Michael Koch  <konqueror at 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-12-02  Michael Koch  <konqueror at gmx.de>

        * java/net/DatagramSocket.java
        (factory): Made private.
        (closed): Removed.
        (DatagramSocket): Check impl argument, use constructor with
        SocketAddress argument.
        (close): Set impl to null, use isClosed().
        (isClosed): Check for impl == null.
        (getLocalAddress): Use isClosed().
        (getLocalPort): Check if socket is closed.
        (getSoTimeout): Likewise.
        (setSoTimeout): Likewise.
        (getSendBufferSize): Likewise.
        (setSendBufferSize): Likewise.
        (getReceiveBufferSize): Likewise.
        (setReceiveBufferSize): Likewise.
        (receive): Likewise.
        (send): Likewise.
        (bind): Likewise.
        (connect): Likewise.
        (setReuseAddress): Likewise.
        (getReuseAddress): Likewise.
        (setBroadcast): Likewise.
        (getBroadcast): Likewise.
        (setTrafficClass): Likewise.
        (getTrafficClass): Likewise.
        * java/net/MulticastSocket.java
        (getInterface): Check if socket is closed.
        (getTTL): Likewise.
        (getTimeToLive): Likewise.
        (setInterface): Likewise.
        (setNetworkInterface): Likewise.
        (getNetworkInterface): Likewise.
        (setLoopbackMode): Likewise.
        (setTTL): Likewise.
        (setTimeToLive): Likewise.
        (joinGroup): Likewise.
        (leaveGroup): Likewise.
        (send): Likewise.
        * java/net/ServerSocket.java
        (closed): Removed.
        (close): Check if socket is closed, set impl to null.
        (isClosed): Check impl == null;
        (ServerSocket): Check impl argument.
        (getInetAddress): Check if socket is bound.
        (getLocalPort): Likewise.
        (getLocalSocketAddress): Likewise.
        (bind): Check if socket is closed.
        (implAccept): Likewise.
        (setSoTimeout): Likewise.
        (getSoTimeout): Likewise.
        (setReuseAddress): Likewise.
        (getReuseAddress): Likewise.
        (setReceiveBufferSize): Likewise.
        (getReceiveBufferSize): Likewise.
        * java/net/Socket.java
        (closed): Removed.
        (Socket): Fixed documentation.
        (connect): Check if socket is closed, changed exception text,
        fixed documentation.
        (getInputStream): Check of socket is closed  and connected.
        (getOutputStream): Likewise.
        (bind): Check if socket is closed.
        (setTcpNoDelay): Likewise.
        (getTcpNoDelay): Likewise.
        (setSoLinger): Likewise.
        (getSoLinger): Likewise.
        (sendUrgentData): Likewise.
        (setOOBInline): Likewise.
        (getOOBInline): Likewise.
        (setSoTimeout): Likewise.
        (getSoTimeout): Likewise.
        (setSendBufferSize): Likewise.
        (getSendBufferSize): Likewise.
        (setReceiveBufferSize): Likewise.
        (getReceiveBufferSize): Likewise.
        (setKeepAlive): Likewise.
        (getKeepAlive): Likewise.
        (close): Likewise.
        (shutdownInput): Likewise.
        (shutdownOutput): Likewise.
        (getReuseAddress): Likewise.
        (getTrafficClass): Likewise.
        (setTrafficClass): Likewise.
        (isClosed): Check impl == null.
        (toString): Added missing ']'.

        2003-12-02  Michael Koch  <konqueror at gmx.de>

        * java/net/DatagramSocket.java
        (DatagramSocket): Move binding code to bind(), simplify constructors.
        * java/net/MulticastSocket.java
        (MulticastSocket): Call parent constructor with null argument,
        bind socket after setReuseAddress is called, simplify constructors.
        * java/net/Socket.java
        (various): Some more getImpl() fixes.

        2003-12-02  Michael Koch  <konqueror at gmx.de>

        * java/net/Socket.java
        (implCreated): New variable that indicates created impl.
        (getImpl): New method.
        (toString): Return more SUN compliant string representation.
        (various): Use getImpl() instead of impl.

        2003-12-02  Michael Koch  <konqueror at gmx.de>

        * java/net/Socket.java
        (implCreated): Dont set default value explicitely, added
        documentation.
        (inputShutdown): Likewise.
        (outputShutdown): Likewise.
        (bound): New private member variable.
        (bind): Set bound to true.
        (close): Set bound to false.
        (isBound): Return bound.
        * java/net/ServerSocket.java
        (bound): New private member variable.
        (bind): Set bound to true.
        (close): Set bound to false.
        (isBound): Return bound.

Members: 
	ChangeLog:1.1781->1.1782 
	libraries/javalib/java/net/DatagramSocket.java:1.18->1.19 
	libraries/javalib/java/net/MulticastSocket.java:1.11->1.12 
	libraries/javalib/java/net/ServerSocket.java:1.18->1.19 
	libraries/javalib/java/net/Socket.java:1.27->1.28 

Index: kaffe/ChangeLog
diff -u kaffe/ChangeLog:1.1781 kaffe/ChangeLog:1.1782
--- kaffe/ChangeLog:1.1781	Wed Dec  3 21:54:07 2003
+++ kaffe/ChangeLog	Wed Dec  3 23:18:01 2003
@@ -1,5 +1,233 @@
 2003-12-03  Dalibor Topic <robilad at kaffe.org>
 
+	* libraries/javalib/java/net/DatagramSocket.java,
+	libraries/javalib/java/net/MulticastSocket.java,
+	libraries/javalib/java/net/ServerSocket.java,
+	libraries/javalib/java/net/Socket.java:
+	Resynced with GNU Classpath.
+
+	2003-12-02  Michael Koch  <konqueror at gmx.de>
+
+        * java/net/DatagramSocket.java
+        (close): Close associated DatagramChannel object.
+        * java/net/Socket.java
+        (close): Reset impl and bound before calling getChannel().close() to
+        prevent from loops.
+
+	2003-12-02  Michael Koch  <konqueror at gmx.de>
+
+        * java/net/DatagramSocket.java
+        (DatagramSocket): Call other sister constructor.
+        (connect): Revised exception message.
+        (setBroadcast): Renamed on to enable.
+        (getLocalAddress): Completed documentation.
+        (getChannel): Likewise.
+        (isBound): Likewise.
+        (isConnected): Likewise.
+        (getRemoteSocketAddress): Likewise.
+        (getLocalSocketAddress): Likewise.
+        (getReuseAddress): Likewise.
+        (setBroadcast): Likewise.
+        (getTrafficClass): Likewise.
+        * java/net/ServerSocket.java
+        (getLocalSocketAddress): Simplified.
+        (accept): Renamed s to socket.
+        (implAccept): Likewise.
+        (toString): Made it compliant to SUNs JDK.
+        * java/net/Socket.java
+        (impl): Make private.
+        (getInetAddress): Check if socket is connected.
+        (getPort): Likewise.
+        (getInputStream): Likewise.
+        (getOutputStream): Likewise.
+        (getLocalPort): Check if socket is bound.
+        (shutdownOutput):
+        Readd accidently removed "getImpl().shutdownOutput()".
+        (connect): Completed documentation.
+        (getOOBInline): Likewise.
+        (getReuseAddress): Likewise.
+        (getTrafficClass): Likewise.
+        (isConnected): Likewise.
+        (isBound): Likewise.
+        (isClosed): Likewise.
+        (isInputShutdown): Likewise.
+        (isOutputShutdown): Likewise.
+
+	2003-12-02  Michael Koch  <konqueror at 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-12-02  Michael Koch  <konqueror at gmx.de>
+
+        * java/net/DatagramSocket.java
+        (factory): Made private.
+        (closed): Removed.
+        (DatagramSocket): Check impl argument, use constructor with
+        SocketAddress argument.
+        (close): Set impl to null, use isClosed().
+        (isClosed): Check for impl == null.
+        (getLocalAddress): Use isClosed().
+        (getLocalPort): Check if socket is closed.
+        (getSoTimeout): Likewise.
+        (setSoTimeout): Likewise.
+        (getSendBufferSize): Likewise.
+        (setSendBufferSize): Likewise.
+        (getReceiveBufferSize): Likewise.
+        (setReceiveBufferSize): Likewise.
+        (receive): Likewise.
+        (send): Likewise.
+        (bind): Likewise.
+        (connect): Likewise.
+        (setReuseAddress): Likewise.
+        (getReuseAddress): Likewise.
+        (setBroadcast): Likewise.
+        (getBroadcast): Likewise.
+        (setTrafficClass): Likewise.
+        (getTrafficClass): Likewise.
+        * java/net/MulticastSocket.java
+        (getInterface): Check if socket is closed.
+        (getTTL): Likewise.
+        (getTimeToLive): Likewise.
+        (setInterface): Likewise.
+        (setNetworkInterface): Likewise.
+        (getNetworkInterface): Likewise.
+        (setLoopbackMode): Likewise.
+        (setTTL): Likewise.
+        (setTimeToLive): Likewise.
+        (joinGroup): Likewise.
+        (leaveGroup): Likewise.
+        (send): Likewise.
+        * java/net/ServerSocket.java
+        (closed): Removed.
+        (close): Check if socket is closed, set impl to null.
+        (isClosed): Check impl == null;
+        (ServerSocket): Check impl argument.
+        (getInetAddress): Check if socket is bound.
+        (getLocalPort): Likewise.
+        (getLocalSocketAddress): Likewise.
+        (bind): Check if socket is closed.
+        (implAccept): Likewise.
+        (setSoTimeout): Likewise.
+        (getSoTimeout): Likewise.
+        (setReuseAddress): Likewise.
+        (getReuseAddress): Likewise.
+        (setReceiveBufferSize): Likewise.
+        (getReceiveBufferSize): Likewise.
+        * java/net/Socket.java
+        (closed): Removed.
+        (Socket): Fixed documentation.
+        (connect): Check if socket is closed, changed exception text,
+        fixed documentation.
+        (getInputStream): Check of socket is closed  and connected.
+        (getOutputStream): Likewise.
+        (bind): Check if socket is closed.
+        (setTcpNoDelay): Likewise.
+        (getTcpNoDelay): Likewise.
+        (setSoLinger): Likewise.
+        (getSoLinger): Likewise.
+        (sendUrgentData): Likewise.
+        (setOOBInline): Likewise.
+        (getOOBInline): Likewise.
+        (setSoTimeout): Likewise.
+        (getSoTimeout): Likewise.
+        (setSendBufferSize): Likewise.
+        (getSendBufferSize): Likewise.
+        (setReceiveBufferSize): Likewise.
+        (getReceiveBufferSize): Likewise.
+        (setKeepAlive): Likewise.
+        (getKeepAlive): Likewise.
+        (close): Likewise.
+        (shutdownInput): Likewise.
+        (shutdownOutput): Likewise.
+        (getReuseAddress): Likewise.
+        (getTrafficClass): Likewise.
+        (setTrafficClass): Likewise.
+        (isClosed): Check impl == null.
+        (toString): Added missing ']'.
+
+	2003-12-02  Michael Koch  <konqueror at gmx.de>
+
+        * java/net/DatagramSocket.java
+        (DatagramSocket): Move binding code to bind(), simplify constructors.
+        * java/net/MulticastSocket.java
+        (MulticastSocket): Call parent constructor with null argument,
+        bind socket after setReuseAddress is called, simplify constructors.
+        * java/net/Socket.java
+        (various): Some more getImpl() fixes.
+
+	2003-12-02  Michael Koch  <konqueror at gmx.de>
+
+        * java/net/Socket.java
+        (implCreated): New variable that indicates created impl.
+        (getImpl): New method.
+        (toString): Return more SUN compliant string representation.
+        (various): Use getImpl() instead of impl.
+
+	2003-12-02  Michael Koch  <konqueror at gmx.de>
+
+        * java/net/Socket.java
+        (implCreated): Dont set default value explicitely, added
+        documentation.
+        (inputShutdown): Likewise.
+        (outputShutdown): Likewise.
+        (bound): New private member variable.
+        (bind): Set bound to true.
+        (close): Set bound to false.
+        (isBound): Return bound.
+        * java/net/ServerSocket.java
+        (bound): New private member variable.
+        (bind): Set bound to true.
+        (close): Set bound to false.
+        (isBound): Return bound.
+
+2003-12-03  Dalibor Topic <robilad at kaffe.org>
+
 	* libraries/javalib/java/net/DatagramPacket.java:
 	Resynced with GNU Classpath.
 
Index: kaffe/libraries/javalib/java/net/DatagramSocket.java
diff -u kaffe/libraries/javalib/java/net/DatagramSocket.java:1.18 kaffe/libraries/javalib/java/net/DatagramSocket.java:1.19
--- kaffe/libraries/javalib/java/net/DatagramSocket.java:1.18	Sun Nov  2 13:29:37 2003
+++ kaffe/libraries/javalib/java/net/DatagramSocket.java	Wed Dec  3 23:18:03 2003
@@ -67,12 +67,17 @@
    * This is the user DatagramSocketImplFactory for this class.  If this
    * variable is null, a default factory is used.
    */
-  static DatagramSocketImplFactory factory;
+  private static DatagramSocketImplFactory factory;
 	  
   /**
    * 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
@@ -85,9 +90,9 @@
   private int remotePort = -1;
 
   /**
-   * Indicates when the socket is closed.
+   * True if socket is bound.
    */
-  private boolean closed = false;
+  private boolean bound;
 
   /**
    * Creates a <code>DatagramSocket</code> from a specified 
@@ -100,6 +105,9 @@
    */
   protected DatagramSocket (DatagramSocketImpl impl)
   {
+    if (impl == null)
+      throw new NullPointerException("impl may not be null");
+
     this.impl = impl;
     this.remoteAddress = null;
     this.remotePort = -1;
@@ -115,7 +123,7 @@
    */
   public DatagramSocket() throws SocketException
   {
-    this(0, null);
+    this(new InetSocketAddress(0));
   }
 
   /**
@@ -130,7 +138,7 @@
    */
   public DatagramSocket(int port) throws SocketException
   {
-    this(port, null);
+    this(new InetSocketAddress(port));
   }
 
   /**
@@ -138,23 +146,22 @@
    * the specified local port and address.
    *
    * @param port The local port number to bind to.
-   * @param laddr The local address to bind to.
+   * @param addr The local address to bind to.
    *
    * @exception SecurityException If a security manager exists and its
    * checkListen method doesn't allow the operation.
    * @exception SocketException If an error occurs.
    */
-  public DatagramSocket(int port, InetAddress laddr) throws SocketException
+  public DatagramSocket(int port, InetAddress addr) throws SocketException
   {
-    this(new InetSocketAddress (laddr, port));
+    this(new InetSocketAddress(addr, port));
   }
 
   /**
    * Initializes a new instance of <code>DatagramSocket</code> that binds to 
    * the specified local port and address.
    *
-   * @param address address to bind the socket to. If address is null,
-   * the socket is left unbound.
+   * @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.
@@ -179,43 +186,28 @@
 	    propVal + "DatagramSocketImpl");
 	  impl = new PlainDatagramSocketImpl();
 	}
-    impl.create();
-
-    // If address is null just return immediately.
-    if (address == null)
-      return;
 
-    InetSocketAddress is_addr = (InetSocketAddress)address;
-    InetAddress laddr;
-
-    laddr = is_addr.getAddress();
-    if (laddr == null)
-      laddr = InetAddress.ANY_IF;
-    if (is_addr.getPort() < 0 || is_addr.getPort() > 65535)
-      throw new IllegalArgumentException("Invalid port: " + is_addr.getPort());
-
-    SecurityManager s = System.getSecurityManager();
-    if (s != null)
-      s.checkListen (is_addr.getPort());
- 
+    if (address != null)
+      bind(address);
+  }
+  
+  // This needs to be accessible from java.net.MulticastSocket
+  DatagramSocketImpl getImpl()
+    throws SocketException
+  {
     try
       {
-        impl.bind (is_addr.getPort(), laddr);
-      }
-    catch (SocketException exception)
-      {
-        impl.close ();
-        throw exception;
-      }
-    catch (RuntimeException exception)
-      {
-        impl.close ();
-        throw exception;
+	if (!implCreated)
+	  {
+	    impl.create();
+	    implCreated = true;
+	  }
+
+	return impl;
       }
-    catch (Error error)
+    catch (IOException e)
       {
-        impl.close ();
-        throw error;
+	throw new SocketException(e.getMessage());
       }
   }
   
@@ -224,12 +216,32 @@
    */
   public void close()
   {
-    if (!closed)
+    if (!isClosed())
       {
-        impl.close();
-        remoteAddress = null;
-        remotePort = -1;
-        closed = true;
+	try
+	  {
+	    getImpl().close();
+	  }
+	catch (SocketException e)
+	  {
+	    // Ignore this case, just close the socket in finally clause.
+	  }
+	finally
+	  {
+	    remoteAddress = null;
+	    remotePort = -1;
+	    impl = null;
+	  }
+
+	try
+	  {
+	    if (getChannel() != null)
+	      getChannel().close();
+	  }
+	catch (IOException e)
+	  {
+	    // Do nothing.
+	  }
       }
   }
 
@@ -264,19 +276,20 @@
   /**
    * 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()
   {
-    if (impl == null
-	|| closed)
+    if (isClosed())
       return null;
     
     InetAddress localAddr;
     
     try
       {
-	localAddr = (InetAddress) impl.getOption (SocketOptions.SO_BINDADDR);
+	localAddr = (InetAddress) getImpl().getOption(SocketOptions.SO_BINDADDR);
 
 	SecurityManager s = System.getSecurityManager();
 	if (s != null)
@@ -288,6 +301,7 @@
       }
     catch (SocketException e)
       {
+	// This cannot happen as we are bound.
         return null;
       }
 
@@ -301,7 +315,18 @@
    */
   public int getLocalPort()
   {
-    return impl.getLocalPort();
+    if (isClosed())
+      return -1;
+	      
+    try
+      {
+	return getImpl().getLocalPort();
+      }
+    catch (SocketException e)
+      {
+	// This cannot happen as we are bound.
+	return 0;
+      }
   }
 
   /**
@@ -316,15 +341,15 @@
    */
   public synchronized int getSoTimeout() throws SocketException
   {
-    if (impl == null)
-      throw new SocketException ("Cannot initialize Socket implementation");
+    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");
   }
 
   /**
@@ -340,10 +365,13 @@
    */
   public synchronized void setSoTimeout(int timeout) throws SocketException
   {
+    if (isClosed())
+      throw new SocketException("socket is closed");
+    
     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));
   }
 
   /**
@@ -359,15 +387,15 @@
    */
   public int getSendBufferSize() throws SocketException
   {
-    if (impl == null)
-      throw new SocketException ("Cannot initialize Socket implementation");
+    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");
   }
 
   /**
@@ -384,10 +412,13 @@
    */
   public void setSendBufferSize(int size) throws SocketException
   {
+    if (isClosed())
+      throw new SocketException("socket is closed");
+    
     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));
   }
 
   /**
@@ -403,15 +434,15 @@
    */
   public int getReceiveBufferSize() throws SocketException
   {
-    if (impl == null)
-      throw new SocketException ("Cannot initialize Socket implementation");
+    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");
   }
 
   /**
@@ -428,13 +459,13 @@
    */
   public void setReceiveBufferSize(int size) throws SocketException
   {
-    if (impl == null)
-      throw new SocketException ("Cannot initialize Socket implementation");
+    if (isClosed())
+      throw new SocketException("socket is closed");
 
     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));
   }
 
   /**
@@ -467,7 +498,7 @@
 
     try
       {
-        impl.connect (address, port);
+        getImpl().connect (address, port);
         remoteAddress = address;
         remotePort = port;
       }
@@ -486,9 +517,22 @@
    */
   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;
+      }
   }
 
   /**
@@ -512,18 +556,19 @@
    */
   public synchronized void receive(DatagramPacket p) throws IOException
   {
-    if (impl == null)
-      throw new IOException ("Cannot initialize Socket implementation");
-
-    if (remoteAddress != null && remoteAddress.isMulticastAddress ())
-      throw new IOException (
-        "Socket connected to a multicast address my not receive");
+    if (isClosed())
+      throw new SocketException("socket is closed");
+    
+    if (remoteAddress != null
+        && remoteAddress.isMulticastAddress())
+      throw new IOException
+        ("Socket connected to a multicast address my not receive");
 
     if (getChannel() != null
         && !getChannel().isBlocking ())
       throw new IllegalBlockingModeException ();
 
-    impl.receive(p);
+    getImpl().receive(p);
 
     SecurityManager s = System.getSecurityManager();
     if (s != null && isConnected ())
@@ -547,6 +592,9 @@
    */
   public void send(DatagramPacket p) throws IOException
   {
+    if (isClosed())
+      throw new SocketException("socket is closed");
+    
     // JDK1.2: Don't do security checks if socket is connected; see jdk1.2 api.
     SecurityManager s = System.getSecurityManager();
     if (s != null && !isConnected ())
@@ -573,14 +621,13 @@
         && !getChannel().isBlocking ())
       throw new IllegalBlockingModeException ();
 
-    impl.send(p);
+    getImpl().send(p);
   }
 
   /**
    * Binds the socket to the given socket address.
    *
-   * @param address The socket address to bind to. If address is null, bind
-   * will take an ephemeral port and a valid local address to bind the socket.
+   * @param address The socket address to bind to.
    *
    * @exception SocketException If an error occurs.
    * @exception SecurityException If a security manager exists and
@@ -592,34 +639,64 @@
   public void bind (SocketAddress address)
     throws SocketException
   {
-    if (address == null)
-      address = new InetSocketAddress (InetAddress.ANY_IF, 0);
+    if (isClosed())
+      throw new SocketException("socket is closed");
     
     if (! (address instanceof InetSocketAddress))
-      throw new IllegalArgumentException ();
+      throw new IllegalArgumentException("unsupported address type");
 
-    InetSocketAddress tmp = (InetSocketAddress) address;
+    InetAddress addr = ((InetSocketAddress) address).getAddress();
+    int port = ((InetSocketAddress) address).getPort();
+
+    if (port < 0 || port > 65535)
+      throw new IllegalArgumentException("Invalid port: " + port);
 
     SecurityManager s = System.getSecurityManager ();
     if (s != null)
-      s.checkListen(tmp.getPort ());
+      s.checkListen(port);
 
-    impl.bind (tmp.getPort (), tmp.getAddress ());
+    if (addr == null)
+      addr = InetAddress.ANY_IF;
+    
+    try
+      {
+        getImpl().bind(port, addr);
+	bound = true;
+      }
+    catch (SocketException exception)
+      {
+        getImpl().close();
+        throw exception;
+      }
+    catch (RuntimeException exception)
+      {
+        getImpl().close();
+        throw exception;
+      }
+    catch (Error error)
+      {
+        getImpl().close();
+        throw error;
+      }
   }
 
   /**
    * Checks if the datagram socket is closed.
    *
+   * @return True if socket is closed, false otherwise.
+   * 
    * @since 1.4
    */
   public boolean isClosed()
   {
-    return closed;
+    return impl == null;
   }
 
   /**
    * Returns the datagram channel assoziated with this datagram socket.
    * 
+   * @return The associated <code>DatagramChannel</code> object or null
+   * 
    * @since 1.4
    */

*** Patch too long, truncated ***




More information about the kaffe mailing list