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]

FYI: Patch: Java NIO: select() now works with channels


Hi list,


I commited the attached patch to add some more working functionality to Java 
NIO. Selection of channels now somehow work. I can use the SelectSockets 
example from the Java NIO book successfully.


Michael
Index: ChangeLog
===================================================================
RCS file: /cvs/gcc/gcc/libjava/ChangeLog,v
retrieving revision 1.2236
diff -u -b -B -r1.2236 ChangeLog
--- ChangeLog	11 Oct 2003 17:23:15 -0000	1.2236
+++ ChangeLog	11 Oct 2003 18:00:29 -0000
@@ -1,5 +1,36 @@
 2003-10-11  Michael Koch  <konqueror@gmx.de>
 
+	* gnu/java/nio/NIOSocket.java (setChannel): Initialize impl.
+	* gnu/java/nio/ServerSocketChannelImpl.java
+	(serverSocket): Made it a NIOServerSocket.
+	(impl): Removed.
+	(ServerSocketChannelImpl): Initialize only serverSocket.
+	(initServerSocket): Removed.
+	(getNativeFD): Rewritten.
+	(implConfigureBlocking): Set socket timeout and removed comment.
+	(accept): Rewritten.
+	* gnu/java/nio/SocketChannelImpl.java
+	(impl): New variable.
+	(connected): Removed.
+	(SocketChannelImpl): Initialize impl too.
+	(getImpl): New method.
+	(isConnected): Rewritten.
+	(read): Rewritten, set position in buffer correctly.
+	(write): Set position in buffer correctly.
+	* java/net/ServerSocket.java (getImpl): New method.
+	* gnu/java/nio/NIOServerSocket.java,
+	gnu/java/nio/natNIOServerSocket.cc: New files.
+	* gnu/java/nio/natServerSocketChannelImpl.cc: Removed.
+	* Makefile.am
+	(ordinary_java_source_files):
+	Added gnu/java/nio/NIOServerSocket.java.
+	(nat_source_files):
+	Removed gnu/java/nio/natServerSocketChannelImpl.cc
+	and added gnu/java/nio/natNIOServerSocket.cc.
+	* Makefile.in: Regenerated.
+
+2003-10-11  Michael Koch  <konqueror@gmx.de>
+
 	* java/awt/ActiveEvent.java,
 	java/awt/datatransfer/ClipboardOwner.java,
 	java/awt/datatransfer/FlavorMap.java,
Index: Makefile.am
===================================================================
RCS file: /cvs/gcc/gcc/libjava/Makefile.am,v
retrieving revision 1.330
diff -u -b -B -r1.330 Makefile.am
--- Makefile.am	9 Oct 2003 18:01:08 -0000	1.330
+++ Makefile.am	11 Oct 2003 18:00:30 -0000
@@ -2187,6 +2187,7 @@
 gnu/java/nio/FileLockImpl.java \
 gnu/java/nio/NIOConstants.java \
 gnu/java/nio/NIODatagramSocket.java \
+gnu/java/nio/NIOServerSocket.java \
 gnu/java/nio/NIOSocket.java \
 gnu/java/nio/PipeImpl.java \
 gnu/java/nio/SelectionKeyImpl.java \
@@ -2659,7 +2660,7 @@
 gnu/java/net/protocol/core/natCoreInputStream.cc \
 gnu/java/nio/natFileLockImpl.cc \
 gnu/java/nio/natSelectorImpl.cc \
-gnu/java/nio/natServerSocketChannelImpl.cc \
+gnu/java/nio/natNIOServerSocket.cc \
 java/io/natFile.cc \
 java/io/natFileDescriptor.cc \
 java/io/natObjectInputStream.cc \
Index: Makefile.in
===================================================================
RCS file: /cvs/gcc/gcc/libjava/Makefile.in,v
retrieving revision 1.354
diff -u -b -B -r1.354 Makefile.in
--- Makefile.in	9 Oct 2003 18:01:08 -0000	1.354
+++ Makefile.in	11 Oct 2003 18:00:32 -0000
@@ -1906,6 +1906,7 @@
 gnu/java/nio/FileLockImpl.java \
 gnu/java/nio/NIOConstants.java \
 gnu/java/nio/NIODatagramSocket.java \
+gnu/java/nio/NIOServerSocket.java \
 gnu/java/nio/NIOSocket.java \
 gnu/java/nio/PipeImpl.java \
 gnu/java/nio/SelectionKeyImpl.java \
@@ -2377,7 +2378,7 @@
 gnu/java/net/protocol/core/natCoreInputStream.cc \
 gnu/java/nio/natFileLockImpl.cc \
 gnu/java/nio/natSelectorImpl.cc \
-gnu/java/nio/natServerSocketChannelImpl.cc \
+gnu/java/nio/natNIOServerSocket.cc \
 java/io/natFile.cc \
 java/io/natFileDescriptor.cc \
 java/io/natObjectInputStream.cc \
@@ -2555,7 +2556,7 @@
 gnu/java/net/natPlainSocketImpl.lo \
 gnu/java/net/protocol/core/natCoreInputStream.lo \
 gnu/java/nio/natFileLockImpl.lo gnu/java/nio/natSelectorImpl.lo \
-gnu/java/nio/natServerSocketChannelImpl.lo java/io/natFile.lo \
+gnu/java/nio/natNIOServerSocket.lo java/io/natFile.lo \
 java/io/natFileDescriptor.lo java/io/natObjectInputStream.lo \
 java/io/natVMObjectStreamClass.lo java/lang/natCharacter.lo \
 java/lang/natClass.lo java/lang/natClassLoader.lo \
@@ -3035,7 +3036,8 @@
 .deps/gnu/java/nio/DatagramChannelImpl.P \
 .deps/gnu/java/nio/DatagramChannelSelectionKey.P \
 .deps/gnu/java/nio/FileLockImpl.P .deps/gnu/java/nio/NIOConstants.P \
-.deps/gnu/java/nio/NIODatagramSocket.P .deps/gnu/java/nio/NIOSocket.P \
+.deps/gnu/java/nio/NIODatagramSocket.P \
+.deps/gnu/java/nio/NIOServerSocket.P .deps/gnu/java/nio/NIOSocket.P \
 .deps/gnu/java/nio/PipeImpl.P .deps/gnu/java/nio/SelectionKeyImpl.P \
 .deps/gnu/java/nio/SelectorImpl.P \
 .deps/gnu/java/nio/SelectorProviderImpl.P \
@@ -3052,8 +3054,8 @@
 .deps/gnu/java/nio/charset/UTF_16Encoder.P \
 .deps/gnu/java/nio/charset/UTF_16LE.P \
 .deps/gnu/java/nio/charset/UTF_8.P .deps/gnu/java/nio/natFileLockImpl.P \
+.deps/gnu/java/nio/natNIOServerSocket.P \
 .deps/gnu/java/nio/natSelectorImpl.P \
-.deps/gnu/java/nio/natServerSocketChannelImpl.P \
 .deps/gnu/java/rmi/RMIMarshalledObjectInputStream.P \
 .deps/gnu/java/rmi/RMIMarshalledObjectOutputStream.P \
 .deps/gnu/java/rmi/dgc/DGCImpl.P .deps/gnu/java/rmi/dgc/DGCImpl_Skel.P \
Index: gnu/java/nio/NIOServerSocket.java
===================================================================
RCS file: gnu/java/nio/NIOServerSocket.java
diff -N gnu/java/nio/NIOServerSocket.java
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ gnu/java/nio/NIOServerSocket.java	11 Oct 2003 18:00:33 -0000
@@ -0,0 +1,80 @@
+/* NIOServerSocket.java -- 
+   Copyright (C) 2003 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING.  If not, write to the
+Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+02111-1307 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library.  Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module.  An independent module is a module which is not derived from
+or based on this library.  If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so.  If you do not wish to do so, delete this
+exception statement from your version. */
+
+
+package gnu.java.nio;
+
+import gnu.java.net.PlainSocketImpl;
+import java.io.IOException;
+import java.net.ServerSocket;
+import java.net.Socket;
+import java.nio.channels.ServerSocketChannel;
+import java.nio.channels.SocketChannel;
+
+/**
+ * @author Michael Koch
+ */
+public final class NIOServerSocket extends ServerSocket
+{
+  private PlainSocketImpl impl;
+  private ServerSocketChannelImpl channel;
+    
+  protected NIOServerSocket (ServerSocketChannelImpl channel)
+    throws IOException
+  {
+    super();
+    this.channel = channel;
+  }
+
+  public native PlainSocketImpl getPlainSocketImpl();
+
+  public ServerSocketChannel getChannel()
+  {
+    return channel;
+  }
+
+  public Socket accept() throws IOException
+  {
+    SecurityManager sm = System.getSecurityManager();
+    if (sm != null)
+      sm.checkListen (getLocalPort());
+
+    SocketChannel socketChannel = channel.provider().openSocketChannel();
+    implAccept (socketChannel.socket());
+    return socketChannel.socket();
+  }
+}
Index: gnu/java/nio/NIOSocket.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/gnu/java/nio/NIOSocket.java,v
retrieving revision 1.1
diff -u -b -B -r1.1 NIOSocket.java
--- gnu/java/nio/NIOSocket.java	19 Sep 2003 21:12:58 -0000	1.1
+++ gnu/java/nio/NIOSocket.java	11 Oct 2003 18:00:33 -0000
@@ -66,6 +66,7 @@
 
   final void setChannel (SocketChannelImpl channel)
   {
+    this.impl = channel.getImpl();
     this.channel = channel;
   }
   
Index: gnu/java/nio/ServerSocketChannelImpl.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/gnu/java/nio/ServerSocketChannelImpl.java,v
retrieving revision 1.8
diff -u -b -B -r1.8 ServerSocketChannelImpl.java
--- gnu/java/nio/ServerSocketChannelImpl.java	9 Oct 2003 18:01:08 -0000	1.8
+++ gnu/java/nio/ServerSocketChannelImpl.java	11 Oct 2003 18:00:33 -0000
@@ -1,5 +1,5 @@
 /* ServerSocketChannelImpl.java -- 
-   Copyright (C) 2002 Free Software Foundation, Inc.
+   Copyright (C) 2002, 2003 Free Software Foundation, Inc.
 
 This file is part of GNU Classpath.
 
@@ -44,14 +44,17 @@
 import java.net.ServerSocket;
 import java.net.Socket;
 import java.net.SocketAddress;
+import java.net.SocketException;
+import java.net.SocketTimeoutException;
+import java.nio.channels.ClosedChannelException;
+import java.nio.channels.NotYetBoundException;
 import java.nio.channels.ServerSocketChannel;
 import java.nio.channels.SocketChannel;
 import java.nio.channels.spi.SelectorProvider;
 
 public final class ServerSocketChannelImpl extends ServerSocketChannel
 {
-  ServerSocket serverSocket;
-  PlainSocketImpl impl;
+  NIOServerSocket serverSocket;
   boolean blocking = true;
   boolean connected = false;
 
@@ -59,20 +62,12 @@
     throws IOException
   {
     super (provider);
-    impl = new PlainSocketImpl();
-    initServerSocket();
+    serverSocket = new NIOServerSocket (this);
   }
 
-  /*
-   * This method is only need to call a package private constructor
-   * of java.net.ServerSocket. It only initializes the member variables
-   * "serverSocket".
-   */
-  private native void initServerSocket() throws IOException;
-
   public int getNativeFD()
   {
-    return impl.getNativeFD();
+    return serverSocket.getPlainSocketImpl().getNativeFD();
   }
  
   public void finalizer()
@@ -97,15 +92,34 @@
 
   protected void implConfigureBlocking (boolean blocking) throws IOException
   {
-    this.blocking = blocking; // FIXME
+    serverSocket.setSoTimeout (blocking ? 0 : NIOConstants.DEFAULT_TIMEOUT);
+    this.blocking = blocking;
   }
 
   public SocketChannel accept () throws IOException
   {
-    SocketChannelImpl result = new SocketChannelImpl (provider ());
-    Socket socket = serverSocket.accept();
-    //socket.setChannel (result); // FIXME
-    return result;
+    if (!isOpen())
+      throw new ClosedChannelException();
+
+    if (!serverSocket.isBound())
+      throw new NotYetBoundException();
+
+    boolean completed = false;
+    
+    try
+      {
+        NIOSocket socket = (NIOSocket) serverSocket.accept();
+        completed = true;
+        return socket.getChannel();
+      }
+    catch (SocketTimeoutException e)
+      {
+        return null;
+      }
+    finally
+      {
+        end (completed);
+      }
   }
 
   public ServerSocket socket ()
Index: gnu/java/nio/SocketChannelImpl.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/gnu/java/nio/SocketChannelImpl.java,v
retrieving revision 1.10
diff -u -b -B -r1.10 SocketChannelImpl.java
--- gnu/java/nio/SocketChannelImpl.java	8 Oct 2003 16:03:06 -0000	1.10
+++ gnu/java/nio/SocketChannelImpl.java	11 Oct 2003 18:00:33 -0000
@@ -63,16 +63,17 @@
 
 public final class SocketChannelImpl extends SocketChannel
 {
+  private PlainSocketImpl impl;
   private NIOSocket socket;
   private boolean blocking = true;
-  private boolean connected = false;
   private boolean connectionPending = false;
 
   SocketChannelImpl (SelectorProvider provider)
     throws IOException
   {
     super (provider);
-    socket = new NIOSocket (new PlainSocketImpl(), this);
+    impl = new PlainSocketImpl();
+    socket = new NIOSocket (impl, this);
   }
   
   SocketChannelImpl (SelectorProvider provider,
@@ -80,8 +81,8 @@
     throws IOException
   {
     super (provider);
+    this.impl = socket.getImpl();
     this.socket = socket;
-    this.connected = socket.isConnected();
   }
 
   public void finalizer()
@@ -98,6 +99,11 @@
       }
   }
 
+  PlainSocketImpl getImpl()
+  {
+    return impl;
+  }
+
   int getNativeFD()
   {
     return socket.getImpl().getNativeFD();
@@ -105,7 +111,6 @@
 
   protected void implCloseSelectableChannel () throws IOException
   {
-    connected = false;
     socket.close();
   }
 
@@ -136,7 +141,6 @@
       {
         // Do blocking connect.
         socket.connect (remote);
-        connected = true;
         return true;
       }
 
@@ -144,7 +148,6 @@
     try
       {
         socket.connect (remote, NIOConstants.DEFAULT_TIMEOUT);
-        connected = true;
         return true;
       }
     catch (SocketTimeoutException e)
@@ -174,7 +177,6 @@
     if (isBlocking())
       {
         selector.select(); // blocking until channel is connected.
-        connected = true;
         connectionPending = false;
         return true;
       }
@@ -182,7 +184,6 @@
     int ready = selector.selectNow(); // non-blocking
     if (ready == 1)
       {
-        connected = true;
         connectionPending = false;
         return true;
       }
@@ -192,7 +193,7 @@
 
   public boolean isConnected ()
   {
-    return connected;
+    return socket.isConnected();
   }
     
   public boolean isConnectionPending ()
@@ -207,13 +208,21 @@
 
   public int read (ByteBuffer dst) throws IOException
   {
-    if (!connected)
+    if (!isConnected())
       throw new NotYetConnectedException();
     
     byte[] data;
     int offset = 0;
+    InputStream input = socket.getInputStream();
+    int available = input.available();
     int len = dst.remaining();
 	
+    if (available == 0)
+      return 0;
+    
+    if (len > available)
+      len = available;
+
     if (dst.hasArray())
       {
         offset = dst.arrayOffset() + dst.position();
@@ -224,15 +233,6 @@
         data = new byte [len];
       }
 
-    InputStream input = socket.getInputStream();
-    int available = input.available();
-
-    if (available == 0)
-      return 0;
-    
-    if (len > available)
-      len = available;
-
     int readBytes = 0;
     boolean completed = false;
 
@@ -247,8 +247,12 @@
         end (completed);
       }
 
-    if (readBytes > 0
-        && !dst.hasArray())
+    if (readBytes > 0)
+      if (dst.hasArray())
+	{
+	  dst.position (dst.position() + readBytes);
+	}
+      else
       {
         dst.put (data, offset, len);
       }
@@ -259,7 +263,7 @@
   public long read (ByteBuffer[] dsts, int offset, int length)
     throws IOException
   {
-    if (!connected)
+    if (!isConnected())
       throw new NotYetConnectedException();
     
     if ((offset < 0)
@@ -279,7 +283,7 @@
   public int write (ByteBuffer src)
     throws IOException
   {
-    if (!connected)
+    if (!isConnected())
       throw new NotYetConnectedException();
     
     byte[] data;
@@ -301,13 +305,19 @@
     
     OutputStream output = socket.getOutputStream();
     output.write (data, offset, len);
+
+    if (src.hasArray())
+      {
+	src.position (src.position() + len);
+      }
+    
     return len;
   }
 
   public long write (ByteBuffer[] srcs, int offset, int length)
     throws IOException
   {
-    if (!connected)
+    if (!isConnected())
       throw new NotYetConnectedException();
     
     if ((offset < 0)
Index: gnu/java/nio/natNIOServerSocket.cc
===================================================================
RCS file: gnu/java/nio/natNIOServerSocket.cc
diff -N gnu/java/nio/natNIOServerSocket.cc
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ gnu/java/nio/natNIOServerSocket.cc	11 Oct 2003 18:00:33 -0000
@@ -0,0 +1,24 @@
+// natNIOServerSocket.cc
+
+/* Copyright (C) 2003  Free Software Foundation
+
+   This file is part of libgcj.
+
+This software is copyrighted work licensed under the terms of the
+Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
+details.  */
+
+#include <config.h>
+#include <platform.h>
+
+#include <gnu/java/net/PlainSocketImpl.h>
+#include <gnu/java/nio/NIOServerSocket.h>
+#include <java/net/ServerSocket.h>
+#include <java/net/SocketImpl.h>
+
+gnu::java::net::PlainSocketImpl*
+gnu::java::nio::NIOServerSocket::getPlainSocketImpl()
+{
+  return (gnu::java::net::PlainSocketImpl*)
+    ::java::net::ServerSocket::getImpl();
+}
Index: java/net/ServerSocket.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/net/ServerSocket.java,v
retrieving revision 1.27
diff -u -b -B -r1.27 ServerSocket.java
--- java/net/ServerSocket.java	2 Oct 2003 15:17:13 -0000	1.27
+++ java/net/ServerSocket.java	11 Oct 2003 18:00:33 -0000
@@ -76,7 +76,7 @@
   private boolean closed = false;
 
   /*
-   * This is only used by java.nio.
+   * This constructor is only used by java.nio.
    */
   // FIXME: Workaround a bug in gcj.
   //ServerSocket (PlainSocketImpl impl) throws IOException
@@ -84,6 +84,16 @@
   {
     this.impl = impl;
     this.impl.create (true);
+  }
+
+  /*
+   * This method is only used by java.nio.
+   */
+  // FIXME: Workaround a bug in gcj.
+  //PlainSocketImpl getImpl()
+  SocketImpl getImpl()
+  {
+    return impl;
   }
   
   /**

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