2002-09-25 Michael Koch <konqueror@gmx.de>
authorMichael Koch <konqueror@gmx.de>
Wed, 25 Sep 2002 17:14:09 +0000 (17:14 +0000)
committerMichael Koch <mkoch@gcc.gnu.org>
Wed, 25 Sep 2002 17:14:09 +0000 (17:14 +0000)
* java/net/DatagramSocket.java
(DatagramSocket): Initialize new instance variables.
(close): Reset new instance variables.
(getLocalAddress): Remove unneeded SecurityManager usage.
(getLocalPort): Check if socket is already bound.
(isConnected): New method.
(getInetAddress): Implemented.
(getPort): Better Implementation, documentation fixed.
(getRemoteSocketAddress): New method.
* java/net/JarURLConnection.java
(element): Typo fixed.
(getMainAttributes): New method.
(getAttributes): New method (stub only).
(getManifest): New method (stub only).
* java/net/NetPermission.java: Added serialVersionsUID.
* java/net/Socket.java
(connect): Check blocking mode of associated channel,
documentation added.
(getLocalSocketAddress): Better implementation.
(getRemoteSocketAddress): Implemented.
(isBound): New method.
(setSendBufferSize): Documentation added.
* java/net/SocketAddress.java: Added serialVersionsUID.
* java/net/SocketPermission.java: Added serialVersionsUID.
* java/net/URL.java
(URL): Wrap for shorter lines, initialize new instance variables,
documentation added.
(equals): Check new instance variables too.
(getContent): Documentation added.
(getPath): Documentation added.
(getAuthority): New method.
(getHost): Documentation added.
(getPort): Documentation added.
(getDefaultPort): New method.
(getProtocol): Documentation added.
(getUserInfo): Documentation added.
(set): Initialize new instance variables, documentation added.
* java/net/URLStreamHandler.java
(setURL): New method.
* java/net/natPlainDatagramSocketImpl.cc
(connect): Fix exception name.
(disconnect): Fix exception name.

From-SVN: r57501

libjava/ChangeLog
libjava/java/net/DatagramSocket.java
libjava/java/net/JarURLConnection.java
libjava/java/net/NetPermission.java
libjava/java/net/Socket.java
libjava/java/net/SocketAddress.java
libjava/java/net/SocketPermission.java
libjava/java/net/URL.java
libjava/java/net/URLStreamHandler.java
libjava/java/net/natPlainDatagramSocketImpl.cc

index c6507521501b3e87aa87da1c18e8ced342cec82a..3132150f7ee9611b804bef872f0b780bf36f1577 100644 (file)
@@ -1,3 +1,48 @@
+2002-09-25  Michael Koch  <konqueror@gmx.de>
+
+       * java/net/DatagramSocket.java
+       (DatagramSocket): Initialize new instance variables.
+       (close): Reset new instance variables.
+       (getLocalAddress): Remove unneeded SecurityManager usage.
+       (getLocalPort): Check if socket is already bound.
+       (isConnected): New method.
+       (getInetAddress): Implemented.
+       (getPort): Better Implementation, documentation fixed.
+       (getRemoteSocketAddress): New method.
+       * java/net/JarURLConnection.java
+       (element): Typo fixed.
+       (getMainAttributes): New method.
+       (getAttributes): New method (stub only).
+       (getManifest): New method (stub only).
+       * java/net/NetPermission.java: Added serialVersionsUID.
+       * java/net/Socket.java
+       (connect): Check blocking mode of associated channel,
+       documentation added.
+       (getLocalSocketAddress): Better implementation.
+       (getRemoteSocketAddress): Implemented.
+       (isBound): New method.
+       (setSendBufferSize): Documentation added.
+       * java/net/SocketAddress.java: Added serialVersionsUID.
+       * java/net/SocketPermission.java: Added serialVersionsUID.
+       * java/net/URL.java
+       (URL): Wrap for shorter lines, initialize new instance variables,
+       documentation added.
+       (equals): Check new instance variables too.
+       (getContent): Documentation added.
+       (getPath): Documentation added.
+       (getAuthority): New method.
+       (getHost): Documentation added.
+       (getPort): Documentation added.
+       (getDefaultPort): New method.
+       (getProtocol): Documentation added.
+       (getUserInfo): Documentation added.
+       (set): Initialize new instance variables, documentation added.
+       * java/net/URLStreamHandler.java
+       (setURL): New method.
+       * java/net/natPlainDatagramSocketImpl.cc
+       (connect): Fix exception name.
+       (disconnect): Fix exception name.
+
 2002-09-25  Michael Koch  <konqueror@gmx.de>
 
        * java/nio/channels/spi/AbstractSelectableChannel.java: New file.
index eef638db091dbb0edfdf032a228f22683d1ab77e..dfbce3bcf7d14038de30470d9e194a325651165b 100644 (file)
@@ -37,6 +37,9 @@ public class DatagramSocket
 
   DatagramChannel ch;
 
+  private InetAddress remoteAddress;
+  private int remotePort;
+
   /**
    * Creates a DatagramSocket
    *
@@ -59,6 +62,8 @@ public class DatagramSocket
   protected DatagramSocket (DatagramSocketImpl impl)
   {
     this.impl = impl;
+    this.remoteAddress = null;
+    this.remotePort = -1;
   }
 
   /**
@@ -134,6 +139,9 @@ public class DatagramSocket
       impl.setOption(SocketOptions.SO_REUSEADDR, new Boolean(true));
 
     impl.bind(port, laddr == null ? InetAddress.ANY_IF : laddr);
+    
+    remoteAddress = null;
+    remotePort = -1;
   }
 
   /**
@@ -169,6 +177,8 @@ public class DatagramSocket
   public void close()
   {
     impl.close();
+    remoteAddress = null;
+    remotePort = -1;
   }
 
   /**
@@ -198,7 +208,6 @@ public class DatagramSocket
    */
   public InetAddress getLocalAddress()
   {
-    SecurityManager s = System.getSecurityManager();
     // FIXME: JCL p. 510 says this should call checkConnect.  But what
     // string should be used as the hostname?  Maybe this is just a side
     // effect of calling InetAddress.getLocalHost.
@@ -241,6 +250,9 @@ public class DatagramSocket
    */
   public int getLocalPort()
   {
+    if (!isBound ())
+      return -1;
+
     return impl.getLocalPort();
   }
 
@@ -416,6 +428,16 @@ public class DatagramSocket
     return true;
   }
 
+  /**
+   * Returns the connection state of the socket
+   * 
+   * @since 1.4
+   */
+  public boolean isConnected()
+  {
+    return remoteAddress != null;
+  }
+
   /**
    * Returns the InetAddress the socket is connected to
    * or null if the socket is not connected
@@ -424,18 +446,37 @@ public class DatagramSocket
    */
   public InetAddress getInetAddress()
   {
-    // FIXME:
-    return null;
+    if (!isConnected ())
+      return null;
+
+    return remoteAddress;
   }
 
   /**
-   * Returns the local port number of the socket
+   * Returns the port number the socket is connected to or -1 if not connected
    * 
    * @since 1.2
    */
   public int getPort()
   {
-    return impl.localPort;
+    if (!isConnected ())
+      return -1;
+    
+    return remotePort;
+  }
+
+  /**
+   * Returns the SocketAddress of the host this socket is conneted to
+   * or null if this socket is not connected
+   * 
+   * @since 1.4
+   */
+  public SocketAddress getRemoteSocketAddress()
+  {
+    if (!isConnected ())
+      return null;
+
+    return new InetSocketAddress (remoteAddress, remotePort);
   }
 
   /**
index 8f75622975f0ff9f553253149093f09891e56656..a90c7f3303715b8ce4e1a72ed617c067d96907dc 100644 (file)
@@ -34,7 +34,7 @@ public abstract class JarURLConnection extends URLConnection
    *  either case this describes just the jar file itself. */
   protected URLConnection jarFileURLConnection;
 
-  // If this is a connection to a jar file element this is set, otherwose null.
+  // If this is a connection to a jar file element this is set, otherwise null.
   private final String element;
 
   // Cached JarURLConnection's 
@@ -349,4 +349,39 @@ public abstract class JarURLConnection extends URLConnection
   {
     return getJarEntry().getCertificates();
   }
+
+  /**
+   * Returns the main Attributes for the JAR file for this connection
+   *
+   * @exception IOException If an error occurs
+   */
+  public Attributes getMainAttributes () throws IOException
+  {
+    return getManifest ().getMainAttributes ();
+  }
+
+  /**
+   * Return the Attributes object for this connection if the URL for it points
+   * to a JAR file entry, null otherwise
+   *
+   * @exception IOException If an error occurs
+   */
+  public Attributes getAttributes () throws IOException
+  {
+    // FIXME: implement this
+    return null;
+  }
+
+  /**
+   * Returns the Manifest for this connection, or null if none
+   *
+   * @exception IOException If an error occurs
+   */
+  public Manifest getManifest () throws IOException
+  {
+    JarFile file = getJarFile ();
+
+    // FIXME: implement this
+    return null;
+  }
 }
index 14732dea49c380d9492726757e996ce6655c3630..f5b625a0d6f2df220b45d084f5ae34b0c4a97cfa 100644 (file)
@@ -50,6 +50,8 @@ import java.security.BasicPermission;
 public final class NetPermission extends BasicPermission
   implements java.io.Serializable
 {
+  static final long serialVersionUID = -8343910153355041693L;
+
   /**
    * Initializes a new instance of <code>NetPermission</code> with the
    * specified name.
index 9f01b78012aa3cbada2193be8a22f8e8ec73a4b7..82265dd32f904cab9c5f25b6f59d9269c66de82e 100644 (file)
@@ -39,6 +39,7 @@ package java.net;
 
 import java.io.*;
 import java.nio.channels.SocketChannel;
+import java.nio.channels.IllegalBlockingModeException;
 
 /* Written using on-line Java Platform 1.2 API Specification.
  * Status:  I believe all methods are implemented.
@@ -80,7 +81,7 @@ public class Socket
   SocketImpl impl;
 
   SocketChannel ch; // this field must have been set if created by SocketChannel
-  
+
   // Constructors
 
   /**
@@ -310,7 +311,8 @@ public class Socket
    *
    * @exception IOException If an error occurs
    * @exception IllegalArgumentException If the addess type is not supported
-   * @exception IllegalBlockingModeException FIXME
+   * @exception IllegalBlockingModeException If this socket has an associated
+   * channel, and the channel is in non-blocking mode
    * 
    * @since 1.4
    */
@@ -320,6 +322,9 @@ public class Socket
     if (! (endpoint instanceof InetSocketAddress))
       throw new IllegalArgumentException ("Address type not supported");
 
+    if (ch != null && !ch.isBlocking ())
+      throw new IllegalBlockingModeException ();
+    
     impl.connect (endpoint, 0);
   }
 
@@ -332,7 +337,8 @@ public class Socket
    *
    * @exception IOException If an error occurs
    * @exception IllegalArgumentException If the address type is not supported
-   * @exception IllegalBlockingModeException FIXME
+   * @exception IllegalBlockingModeException If this socket has an associated
+   * channel, and the channel is in non-blocking mode
    * @exception SocketTimeoutException If the timeout is reached
    * 
    * @since 1.4
@@ -343,6 +349,9 @@ public class Socket
     if (! (endpoint instanceof InetSocketAddress))
       throw new IllegalArgumentException ("Address type not supported");
 
+    if (ch != null && !ch.isBlocking ())
+      throw new IllegalBlockingModeException ();
+    
     impl.connect (endpoint, timeout);
   }
 
@@ -432,16 +441,10 @@ public class Socket
    */
   public SocketAddress getLocalSocketAddress()
   {
-    InetAddress addr;
+    InetAddress addr = getLocalAddress ();
 
-    try
-      {
-        addr = (InetAddress) impl.getOption (SocketOptions.SO_BINDADDR);
-      }
-    catch (SocketException e)
-      {
-       return null;
-      }
+    if (addr == null)
+      return null;
     
     return new InetSocketAddress (addr, impl.getLocalPort());
   }
@@ -454,8 +457,7 @@ public class Socket
    */
   public SocketAddress getRemoteSocketAddress()
   {
-    // FIXME: Implement this
-    return null;
+    return new InetSocketAddress (impl.getInetAddress (), impl.getPort ());
   }
 
   /**
@@ -701,7 +703,7 @@ public class Socket
    * @param size The new send buffer size.
    *
    * @exception SocketException If an error occurs or Socket not connected
-   * @exception IllegalArgumentException FIXME
+   * @exception IllegalArgumentException If size is 0 or negative
    *
    * @since 1.2
    */
@@ -990,4 +992,12 @@ public class Socket
 
     impl.setOption (SocketOptions.IP_TOS, new Integer (tc));
   }
+
+  /**
+   * Checks if the socket is already bound.
+   */
+  public boolean isBound ()
+  {
+    return getLocalAddress () != null;
+  }
 }
index d37020b8e71e85a66a646f4d57be81700f11c729..af20fba7bfa0e4fa811a505ac9f2c48742f16553 100644 (file)
@@ -47,8 +47,10 @@ import java.io.*;
 
 public abstract class SocketAddress implements Serializable
 {
-    public SocketAddress()
-    {
-    }
+  static final long serialVersionUID = 5215720748342549866L;
+
+  public SocketAddress()
+  {
+  }
 }
 
index 002a99c15cf77ca03002f1d50ab9613e442c9b8e..061ec9c80fe609899963613ecf20682e00b37284 100644 (file)
@@ -37,6 +37,7 @@ exception statement from your version. */
 
 package java.net;
 
+import java.io.Serializable;
 import java.security.Permission;
 import java.security.PermissionCollection;
 
@@ -100,8 +101,9 @@ import java.security.PermissionCollection;
  * @author Aaron M. Renn (arenn@urbanophile.com)
  */
 public final class SocketPermission extends Permission
-  implements java.io.Serializable
+  implements Serializable
 {
+  static final long serialVersionUID = -7204263841984476862L;
 
 // FIXME: Needs serialization work, including readObject/writeObject methods.
   /**
index 73edeca415d5140b1406d106019c768429e6142f..226ad6ca68c5430fe0ea7449ca689186bfab889f 100644 (file)
@@ -28,9 +28,12 @@ import java.util.StringTokenizer;
 public final class URL implements Serializable
 {
   private String protocol;
+  private String authority;
+  private String userInfo;
   private String host;
   private int port = -1;       // Initialize for constructor using context.
   private String file;
+  private String query;
   private String ref;
   private int hashCode = 0;
   transient private URLStreamHandler handler;
@@ -39,19 +42,50 @@ public final class URL implements Serializable
 
   private static final long serialVersionUID = -7627629688361524110L;
 
+  /**
+   * Creates an URL object from the given arguments
+   *
+   * @param protocol The protocol of the URL
+   * @param host The host of the URL
+   * @param port The port of the URL
+   * @param file The file of the URL
+   *
+   * @exception MalformedURLException If an error occurs
+   */
   public URL(String protocol, String host, int port, String file)
     throws MalformedURLException
   {
     this(protocol, host, port, file, null);
   }
 
+  /**
+   * Creates an URL object from the given arguments
+   *
+   * @param protocol The protocol of the URL
+   * @param host The host of the URL
+   * @param file The file of the URL
+   *
+   * @exception MalformedURLException If an error occurs
+   */
   public URL(String protocol, String host, String file)
     throws MalformedURLException
   {
     this(protocol, host, -1, file, null);
   }
 
-  // JDK1.2
+  /**
+   * Creates an URL object from the given arguments
+   *
+   * @param protocol The protocol of the URL
+   * @param host The host of the URL
+   * @param port The port of the URL
+   * @param file The file of the URL
+   * @param handler The stream handler for the URL
+   *
+   * @exception MalformedURLException If an error occurs
+   *
+   * @since 1.2
+   */
   public URL(String protocol, String host, int port, String file,
     URLStreamHandler handler) throws MalformedURLException
   {
@@ -76,11 +110,14 @@ public final class URL implements Serializable
       this.handler = setURLStreamHandler(protocol);
 
     if (this.handler == null)
-      throw new MalformedURLException("Protocol handler not found: " + protocol);
+      throw new MalformedURLException (
+                     "Protocol handler not found: " + protocol);
 
     this.host = host;
-
     this.port = port;
+    this.userInfo = null;
+    this.authority = null;
+    this.query = null;
 
     int hashAt = file.indexOf('#');
     if (hashAt < 0)
@@ -96,17 +133,42 @@ public final class URL implements Serializable
     hashCode = hashCode();                     // Used for serialization.
   }
 
+  /**
+   * Creates an URL object from the given arguments
+   * 
+   * @param spec The string to parse an URL
+   *
+   * @exception MalformedURLException If an error occurs
+   */
   public URL(String spec) throws MalformedURLException
   {
     this((URL) null, spec, (URLStreamHandler) null);
   }
 
+  /**
+   * Creates an URL object from the given arguments
+   * 
+   * @param context The context on which to parse the specification
+   * @param spec The string to parse an URL
+   *
+   * @exception MalformedURLException If an error occurs
+   */
   public URL(URL context, String spec) throws MalformedURLException
   {
     this(context, spec, (URLStreamHandler) null);
   }
 
-  // JDK1.2
+  /**
+   * Creates an URL from given arguments
+   *
+   * @param context The context in which to parse the specification
+   * @param spec The string to parse as an URL
+   * @param handler The stream handler for the URL
+   *
+   * @exception MalformedURLException If an error occurs
+   * 
+   * @since 1.2
+   */
   public URL(URL context, String spec, URLStreamHandler handler)
     throws MalformedURLException
   {
@@ -142,6 +204,9 @@ public final class URL implements Serializable
            host = context.host;
            port = context.port;
            file = context.file;
+           userInfo = context.userInfo;
+           authority = context.authority;
+           query = context.query;
          }
       }
     else if (context != null)
@@ -153,6 +218,9 @@ public final class URL implements Serializable
        host = context.host;
        port = context.port;
        file = context.file;
+        userInfo = context.userInfo;
+        authority = context.authority;
+        query = context.query;
       }
     else       // Protocol NOT specified in spec. and no context available.
       throw new
@@ -202,14 +270,25 @@ public final class URL implements Serializable
     return (port == uObj.port
            && ((protocol == null && uObj.protocol == null)
                || (protocol != null && protocol.equals(uObj.protocol)))
+           && ((userInfo == null && uObj.userInfo == null)
+                || (userInfo != null && userInfo.equals(uObj.userInfo)))
+           && ((authority == null && uObj.authority == null)
+                || (authority != null && authority.equals(uObj.authority)))
            && ((host == null && uObj.host == null)
                || (host != null && host.equals(uObj.host)))
            && ((file == null && uObj.file == null)
                || (file != null && file.equals(uObj.file)))
+           && ((query == null && uObj.query == null)
+                || (query != null && query.equals(uObj.query)))
            && ((ref == null && uObj.ref == null)
                || (ref != null && ref.equals(uObj.ref))));
   }
 
+  /**
+   * Gets the contents of this URL
+   *
+   * @since 1.3
+   */
   public final Object getContent() throws IOException
   {
     return openConnection().getContent();
@@ -220,22 +299,54 @@ public final class URL implements Serializable
     return file;
   }
 
+  /**
+   * Returns the path of the URL
+   *
+   * @since 1.3
+   */
   public String getPath()
   {
     int quest = file.indexOf('?');
     return quest < 0 ? file : file.substring(0, quest);
   }
 
+  /**
+   * Returns the authority of the URL
+   * 
+   * @since 1.3
+   */
+  public String getAuthority()
+  {
+    return authority;
+  }
+
+  /**
+   * Returns the host of the URL
+   */
   public String getHost()
   {
     return host;
   }
 
+  /**
+   * Returns of port of the URL
+   */
   public int getPort()
   {
     return port;
   }
 
+  /**
+   * Returns the default port of the URL
+   */
+  public int getDefaultPort()
+  {
+    return 0;
+  }
+
+  /**
+   * Returns the protocol of the URL
+   */
   public String getProtocol()
   {
     return protocol;
@@ -246,6 +357,9 @@ public final class URL implements Serializable
     return ref;
   }
 
+  /**
+   * Returns the user information of the URL
+   */
   public String getUserInfo ()
   {
     int at = host.indexOf('@');
@@ -290,6 +404,11 @@ public final class URL implements Serializable
     return handler.sameFile(this, other);
   }
 
+  /**
+   * Sets the specified fields of the URL. This is not a public method so
+   * that only URLStreamHandlers can modify URL fields. URLs are otherwise
+   * constant
+   */
   protected void set(String protocol, String host, int port, String file,
                     String ref)
   {
@@ -299,14 +418,23 @@ public final class URL implements Serializable
     // be aware of this.
     this.handler = setURLStreamHandler(protocol);
     this.protocol = protocol;
+    this.authority = null;
+    this.userInfo = null;
     this.port = port;
     this.host = host;
     this.file = file;
+    this.query = null;
     this.ref = ref;
     hashCode = hashCode();                     // Used for serialization.
   }
 
-  /** @since 1.3 */
+  /**
+   * Sets the specified fields of the URL. This is not a public method so
+   * that only URLStreamHandlers can modify URL fields. URLs are otherwise
+   * constant
+   *
+   * @since 1.3
+   */
   protected void set(String protocol, String host, int port,
                     String authority, String userInfo,
                     String path, String query, String ref)
index ea21ee928e5da130a8134adc1d835ed9b79eb4a5..cade4f3d066ecceddfc67f7cdc31edaa3fa5c1f7 100644 (file)
@@ -183,6 +183,29 @@ public abstract class URLStreamHandler
     u.set(protocol, host, port, file, ref);
   }
 
+  /**
+   * Sets the fields of the URL argument to the indicated values
+   *
+   * @param u The URL to modify
+   * @param protocol The protocol to set
+   * @param host The host name to set
+   * @param port The port number to set
+   * @param authority The authority to set
+   * @param userInfo The user information to set
+   * @param path The path/filename to set
+   * @param query The query part to set
+   * @param ref The reference
+   *
+   * @exception SecurityException If the protocol handler of the URL is
+   * different from this one
+   */
+  protected void setURL(URL u, String protocol, String host, int port,
+                       String authority, String userInfo, String path,
+                       String query, String ref)
+  {
+    u.set(protocol, host, port, authority, userInfo, path, query, ref);
+  }
+
   /**
    * Converts an URL of a specific protocol to a string
    *
index 82e280631a80c7b638ea2a11c15cace9f1e32d07..291243928f23cc386c5349ec9c69ba25fe4c1716 100644 (file)
@@ -89,14 +89,14 @@ java::net::PlainDatagramSocketImpl::bind (jint, java::net::InetAddress *)
 void
 java::net::PlainDatagramSocketImpl::connect (java::net::InetAddress *, jint)
 {
-  throw new java::io::SocketException (
+  throw new SocketException (
     JvNewStringLatin1 ("DatagramSocketImpl.connect: unimplemented"));
 }
 
 void
 java::net::PlainDatagramSocketImpl::disconnect ()
 {
-  throw new java::io::SocketException (
+  throw new SocketException (
     JvNewStringLatin1 ("DatagramSocketImpl.disconnect: unimplemented"));
 }