This is the mail archive of the java-patches@gcc.gnu.org mailing list for the Java project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Patch: RFC: java.net updates


This patch adds some new functionality to java.net.  I'm not checking
it in yet for two reasons:

* Some of this could be merged back to Classpath, but first I'd need
  to write JNI versions of the new native code.  I don't know when I
  can do this.  Is this sufficient reason to hold off checking this in
  to libgcj?

* This will affect the Windows build but I have no way of testing
  that.  Can someone volunteer to try this patch on Windows?

Tom

Index: ChangeLog
from  Tom Tromey  <tromey@redhat.com>
	* java/net/SocketImpl.java (shutdownInput, shutdownOutput): New
	methods.
	* java/net/URL.java (getUserInfo): New method.
	(set(String,String,int,String,String,String,String,String)): New
	method.
	* java/net/PlainSocketImpl.java (_Jv_SO_KEEPALIVE_): Define.
	(shutdownInput, shutdownOutput): Declare.
	* java/net/PlainDatagramSocketImpl.java (_Jv_SO_KEEPALIVE_):
	Define.
	* java/net/natPlainSocketImpl.cc (setOption): Handle keepalive.
	(getOption): Likewise.
	(shutdownInput): New method.
	(shutdownOutput): Likewise.
	* java/net/natPlainDatagramSocketImpl.cc (setOption): Handle
	keepalive.
	(getOption): Likewise.
	* java/net/SocketOptions.java (SO_KEEPALIVE): New constant.
	* java/net/Socket.java (setKeepAlive): New method.
	(getKeepAlive): Likewise.
	(shutdownInput, shutdownOutput): New methods.

Index: java/net/PlainDatagramSocketImpl.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/net/PlainDatagramSocketImpl.java,v
retrieving revision 1.10
diff -u -r1.10 PlainDatagramSocketImpl.java
--- java/net/PlainDatagramSocketImpl.java 28 Mar 2002 02:08:36 -0000 1.10
+++ java/net/PlainDatagramSocketImpl.java 23 Aug 2002 02:48:25 -0000
@@ -1,6 +1,6 @@
 // PlainDatagramSocketImpl.java - Implementation of DatagramSocketImpl.
 
-/* Copyright (C) 1999  Free Software Foundation
+/* Copyright (C) 1999, 2002  Free Software Foundation
 
    This file is part of libgcj.
 
@@ -33,7 +33,8 @@
                    _Jv_SO_LINGER_ = SocketOptions.SO_LINGER,
                    _Jv_SO_TIMEOUT_ = SocketOptions.SO_TIMEOUT,
                    _Jv_SO_SNDBUF_ = SocketOptions.SO_SNDBUF,
-                   _Jv_SO_RCVBUF_ = SocketOptions.SO_RCVBUF;
+                   _Jv_SO_RCVBUF_ = SocketOptions.SO_RCVBUF,
+                   _Jv_SO_KEEPALIVE_ = SocketOptions.SO_KEEPALIVE;
 
   int fnum = -1;
 
Index: java/net/PlainSocketImpl.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/net/PlainSocketImpl.java,v
retrieving revision 1.10
diff -u -r1.10 PlainSocketImpl.java
--- java/net/PlainSocketImpl.java 28 Mar 2002 02:08:36 -0000 1.10
+++ java/net/PlainSocketImpl.java 23 Aug 2002 02:48:25 -0000
@@ -32,7 +32,8 @@
                    _Jv_SO_LINGER_ = SocketOptions.SO_LINGER,
                    _Jv_SO_TIMEOUT_ = SocketOptions.SO_TIMEOUT,
                    _Jv_SO_SNDBUF_ = SocketOptions.SO_SNDBUF,
-                   _Jv_SO_RCVBUF_ = SocketOptions.SO_RCVBUF;
+                   _Jv_SO_RCVBUF_ = SocketOptions.SO_RCVBUF,
+                   _Jv_SO_KEEPALIVE_ = SocketOptions.SO_KEEPALIVE;
 
   /**
    * The OS file handle representing the socket.
@@ -52,6 +53,10 @@
   public native void setOption(int optID, Object value) throws SocketException;
 
   public native Object getOption(int optID) throws SocketException;
+
+  public native void shutdownInput () throws IOException;
+
+  public native void shutdownOutput () throws IOException;
 
   protected native void create (boolean stream)  throws IOException;
 
Index: java/net/Socket.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/net/Socket.java,v
retrieving revision 1.9
diff -u -r1.9 Socket.java
--- java/net/Socket.java 22 Jan 2002 22:40:23 -0000 1.9
+++ java/net/Socket.java 23 Aug 2002 02:48:26 -0000
@@ -618,6 +618,47 @@
   }
 
   /**
+   * This method sets the value for the socket level socket option
+   * SO_KEEPALIVE.
+   *
+   * @param on True if SO_KEEPALIVE should be enabled
+   *
+   * @exception SocketException If an error occurs or Socket is not connected
+   *
+   * @since Java 1.3
+   */
+  public void setKeepAlive (boolean on) throws SocketException
+  {
+    if (impl == null)
+      throw new SocketException("Not connected");
+
+    impl.setOption(SocketOptions.SO_RCVBUF, new Boolean(on));
+  }
+
+  /**
+   * This method returns the value of the socket level socket option
+   * SO_KEEPALIVE.
+   *
+   * @return The setting
+   *
+   * @exception SocketException If an error occurs or Socket is not connected
+   *
+   * @since Java 1.3
+   */
+  public boolean getKeepAlive () throws SocketException
+  {
+    if (impl == null)
+      throw new SocketException("Not connected");
+
+    Object buf = impl.getOption(SocketOptions.SO_RCVBUF);
+
+    if (buf instanceof Boolean)
+      return(((Boolean)buf).booleanValue());
+    else
+      throw new SocketException("Internal Error: Unexpected type");
+  }
+
+  /**
    * Closes the socket.
    *
    * @exception IOException If an error occurs
@@ -666,5 +707,27 @@
       sm.checkSetFactory();
 
     factory = fac;
+  }
+
+  /**
+   * Closes the input side of the socket stream.
+   *
+   * @exception IOException If an error occurs.
+   */
+  public void shutdownInput() throws IOException 
+  {
+    if (impl != null)
+      impl.shutdownInput();
+  }
+
+  /**
+   * Closes the output side of the socket stream.
+   *
+   * @exception IOException If an error occurs.
+   */
+  public void shutdownOutput() throws IOException
+  {
+    if (impl != null)
+      impl.shutdownOutput();
   }
 }
Index: java/net/SocketImpl.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/net/SocketImpl.java,v
retrieving revision 1.8
diff -u -r1.8 SocketImpl.java
--- java/net/SocketImpl.java 14 Feb 2002 23:16:06 -0000 1.8
+++ java/net/SocketImpl.java 23 Aug 2002 02:48:26 -0000
@@ -264,4 +264,20 @@
    * @XXX This redeclaration from SocketOptions is a workaround to a gcj bug.
    */
   public abstract Object getOption(int option_id) throws SocketException;
+
+  /**
+   * Shut down the input side of this socket.  Subsequent reads will
+   * return end-of-file.
+   *
+   * @exception IOException if an error occurs
+   */
+  public abstract void shutdownInput () throws IOException;
+
+  /**
+   * Shut down the output side of this socket.  Subsequent writes will
+   * fail with an IOException.
+   *
+   * @exception IOException if an error occurs
+   */
+  public abstract void shutdownOutput () throws IOException;
 }
Index: java/net/SocketOptions.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/net/SocketOptions.java,v
retrieving revision 1.6
diff -u -r1.6 SocketOptions.java
--- java/net/SocketOptions.java 22 Jan 2002 22:40:23 -0000 1.6
+++ java/net/SocketOptions.java 23 Aug 2002 02:48:26 -0000
@@ -1,5 +1,5 @@
 /* SocketOptions.java -- Implements options for sockets (duh!)
-   Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
+   Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
 
 This file is part of GNU Classpath.
 
@@ -60,6 +60,11 @@
 /*
  * Static Variables
  */
+
+/**
+ * Option id for the SO_KEEPALIVE value
+ */
+static final int SO_KEEPALIVE = 0x8;
 
 /**
   * Option id for the SO_LINGER value
Index: java/net/URL.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/net/URL.java,v
retrieving revision 1.10
diff -u -r1.10 URL.java
--- java/net/URL.java 23 Feb 2002 00:15:48 -0000 1.10
+++ java/net/URL.java 23 Aug 2002 02:48:26 -0000
@@ -175,7 +175,8 @@
       this.handler = setURLStreamHandler(protocol);
 
     if (this.handler == null)
-      throw new MalformedURLException("Protocol handler not found: " + protocol);
+      throw new MalformedURLException("Protocol handler not found: "
+				      + protocol);
 
     // JDK 1.2 doc for parseURL specifically states that any '#' ref
     // is to be excluded by passing the 'limit' as the indexOf the '#'
@@ -245,6 +246,12 @@
     return ref;
   }
 
+  public String getUserInfo ()
+  {
+    int at = host.indexOf('@');
+    return at < 0 ? null : host.substring(0, at);
+  }
+
   public int hashCode()
   {
     // JCL book says this is computed using (only) the hashcodes of the 
@@ -295,6 +302,30 @@
     this.port = port;
     this.host = host;
     this.file = file;
+    this.ref = ref;
+    hashCode = hashCode();			// Used for serialization.
+  }
+
+  /** @since 1.3 */
+  protected void set(String protocol, String host, int port,
+		     String authority, String userInfo,
+		     String path, String query, String ref)
+  {
+    // TBD: Theoretically, a poorly written StreamHandler could pass an
+    // invalid protocol.  It will cause the handler to be set to null
+    // thus overriding a valid handler.  Callers of this method should
+    // be aware of this.
+    this.handler = setURLStreamHandler(protocol);
+    this.protocol = protocol;
+    if (userInfo == null)
+      this.host = host;
+    else
+      this.host = userInfo + "@" + host;
+    this.port = port;
+    if (query == null)
+      this.file = path;
+    else
+      this.file = path + "?" + query;
     this.ref = ref;
     hashCode = hashCode();			// Used for serialization.
   }
Index: java/net/natPlainDatagramSocketImpl.cc
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/net/natPlainDatagramSocketImpl.cc,v
retrieving revision 1.39
diff -u -r1.39 natPlainDatagramSocketImpl.cc
--- java/net/natPlainDatagramSocketImpl.cc 25 Jun 2002 05:29:22 -0000 1.39
+++ java/net/natPlainDatagramSocketImpl.cc 23 Aug 2002 02:48:27 -0000
@@ -524,6 +524,10 @@
         throw new java::net::SocketException (
           JvNewStringUTF ("SO_LINGER not valid for UDP"));
         return;
+      case _Jv_SO_KEEPALIVE_ :
+        throw new java::net::SocketException (
+          JvNewStringUTF ("SO_KEEPALIVE not valid for UDP"));
+        return;
       case _Jv_SO_SNDBUF_ :
       case _Jv_SO_RCVBUF_ :
 #if defined(SO_SNDBUF) && defined(SO_RCVBUF)
@@ -613,11 +617,14 @@
         throw new java::net::SocketException (
           JvNewStringUTF ("TCP_NODELAY not valid for UDP"));
         break;
-
       case _Jv_SO_LINGER_ :
         throw new java::net::SocketException (
           JvNewStringUTF ("SO_LINGER not valid for UDP"));
         break;    
+      case _Jv_SO_KEEPALIVE_ :
+        throw new java::net::SocketException (
+          JvNewStringUTF ("SO_KEEPALIVE not valid for UDP"));
+        break;
       case _Jv_SO_RCVBUF_ :
       case _Jv_SO_SNDBUF_ :
 #if defined(SO_SNDBUF) && defined(SO_RCVBUF)
Index: java/net/natPlainSocketImpl.cc
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/net/natPlainSocketImpl.cc,v
retrieving revision 1.42
diff -u -r1.42 natPlainSocketImpl.cc
--- java/net/natPlainSocketImpl.cc 18 Jun 2002 16:25:00 -0000 1.42
+++ java/net/natPlainSocketImpl.cc 23 Aug 2002 02:48:27 -0000
@@ -224,6 +224,20 @@
     JvNewStringLatin1 ("SocketImpl.close: unimplemented"));
 }
 
+void
+java::net::PlainSocketImpl::shutdownInput (void)
+{
+  throw new SocketException (
+    JvNewStringLatin1 ("SocketImpl.shutdownInput: unimplemented"));
+}
+
+void
+java::net::PlainSocketImpl::shutdownOutput (void)
+{
+  throw new SocketException (
+    JvNewStringLatin1 ("SocketImpl.shutdownOutput: unimplemented"));
+}
+
 #else /* DISABLE_JAVA_NET */
 
 union SockAddr
@@ -724,12 +738,18 @@
 #ifdef TCP_NODELAY
         if (::setsockopt (fnum, IPPROTO_TCP, TCP_NODELAY, (char *) &val,
 	    val_len) != 0)
-	  goto error;    
+	  goto error;
 #else
         throw new java::lang::InternalError (
           JvNewStringUTF ("TCP_NODELAY not supported"));
 #endif /* TCP_NODELAY */
         return;
+
+      case _Jv_SO_KEEPALIVE_ :
+        if (::setsockopt (fnum, SOL_SOCKET, SO_KEEPALIVE, (char *) &val,
+	    val_len) != 0)
+	  goto error;
+
       case _Jv_SO_LINGER_ :
 #ifdef SO_LINGER
         struct linger l_val;
@@ -818,6 +838,14 @@
           JvNewStringUTF ("SO_LINGER not supported"));
 #endif
         break;    
+
+      case _Jv_SO_KEEPALIVE_ :
+        if (::getsockopt (fnum, SOL_SOCKET, SO_KEEPALIVE, (char *) &val,
+	    &val_len) != 0)
+          goto error;
+        else
+	  return new java::lang::Boolean (val != 0);
+
       case _Jv_SO_RCVBUF_ :
       case _Jv_SO_SNDBUF_ :
 #if defined(SO_SNDBUF) && defined(SO_RCVBUF)
@@ -876,6 +904,20 @@
  error:
   char* strerr = strerror (errno);
   throw new java::net::SocketException (JvNewStringUTF (strerr));
+}
+
+void
+java::net::PlainSocketImpl::shutdownInput (void)
+{
+  if (::shutdown (fnum, 0))
+    throw new SocketException (JvNewStringUTF (strerror (errno)));
+}
+
+void
+java::net::PlainSocketImpl::shutdownOutput (void)
+{
+  if (::shutdown (fnum, 1))
+    throw new SocketException (JvNewStringUTF (strerror (errno)));
 }
 
 #endif /* DISABLE_JAVA_NET */


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]