I have addressed the issues raised by Roman and Jeoren and attached an updated patch.

Cheers,
Mike.
Index: gnu/java/nio/PipeImpl.java
===================================================================
RCS file: /cvsroot/classpath/classpath/gnu/java/nio/PipeImpl.java,v
retrieving revision 1.11
diff -u -r1.11 PipeImpl.java
--- gnu/java/nio/PipeImpl.java	2 Jul 2005 20:32:13 -0000	1.11
+++ gnu/java/nio/PipeImpl.java	2 Mar 2006 19:34:22 -0000
@@ -37,6 +37,7 @@
 
 package gnu.java.nio;
 
+
 import java.io.IOException;
 import java.nio.ByteBuffer;
 import java.nio.channels.Pipe;
@@ -47,12 +48,14 @@
   public static final class SourceChannelImpl extends Pipe.SourceChannel
   {
     private int native_fd;
+    private VMChannel vmch;
     
     public SourceChannelImpl (SelectorProvider selectorProvider,
                               int native_fd)
     {
       super (selectorProvider);
       this.native_fd = native_fd;
+      vmch = VMChannel.getVMChannel(this);
     }
 
     protected final void implCloseSelectableChannel()
@@ -64,19 +67,19 @@
     protected void implConfigureBlocking (boolean blocking)
       throws IOException
     {
-      throw new Error ("Not implemented");
+      vmch.setBlocking(blocking);
     }
 
     public final int read (ByteBuffer src)
       throws IOException
     {
-      throw new Error ("Not implemented");
+      return vmch.read(src);
     }
 
     public final long read (ByteBuffer[] srcs)
       throws IOException
     {
-      return read (srcs, 0, srcs.length);
+      return vmch.readScattering(srcs, 0, srcs.length);
     }
 
     public final synchronized long read (ByteBuffer[] srcs, int offset,
@@ -89,13 +92,7 @@
 	  || len > srcs.length - offset)
 	throw new IndexOutOfBoundsException();
 
-      long bytesRead = 0;
-      
-      for (int index = 0; index < len; index++)
-	bytesRead += read (srcs [offset + index]);
-
-      return bytesRead;
-
+      return vmch.readScattering(srcs, offset, len);
     }
 
     public final int getNativeFD()
@@ -107,12 +104,14 @@
   public static final class SinkChannelImpl extends Pipe.SinkChannel
   {
     private int native_fd;
+    private VMChannel vmch;
     
     public SinkChannelImpl (SelectorProvider selectorProvider,
                             int native_fd)
     {
       super (selectorProvider);
       this.native_fd = native_fd;
+      vmch = VMChannel.getVMChannel(this);
     }
 
     protected final void implCloseSelectableChannel()
@@ -124,19 +123,19 @@
     protected final void implConfigureBlocking (boolean blocking)
       throws IOException
     {
-      throw new Error ("Not implemented");
+      vmch.setBlocking(blocking);
     }
 
     public final int write (ByteBuffer dst)
       throws IOException
     {
-      throw new Error ("Not implemented");
+      return vmch.write(dst);
     }
 
     public final long write (ByteBuffer[] srcs)
       throws IOException
     {
-      return write (srcs, 0, srcs.length);
+      return vmch.writeGathering(srcs, 0, srcs.length);
     }
 
     public final synchronized long write (ByteBuffer[] srcs, int offset, int len)
@@ -147,13 +146,8 @@
 	  || len < 0
 	  || len > srcs.length - offset)
 	throw new IndexOutOfBoundsException();
-
-      long bytesWritten = 0;
       
-      for (int index = 0; index < len; index++)
-	bytesWritten += write (srcs [offset + index]);
-
-      return bytesWritten;
+      return vmch.writeGathering(srcs, offset, len);
     }
 
     public final int getNativeFD()
Index: gnu/java/nio/SelectorImpl.java
===================================================================
RCS file: /cvsroot/classpath/classpath/gnu/java/nio/SelectorImpl.java,v
retrieving revision 1.21
diff -u -r1.21 SelectorImpl.java
--- gnu/java/nio/SelectorImpl.java	27 Dec 2005 08:32:12 -0000	1.21
+++ gnu/java/nio/SelectorImpl.java	2 Mar 2006 19:34:22 -0000
@@ -379,6 +379,8 @@
       result = new DatagramChannelSelectionKey (ch, this);
     else if (ch instanceof ServerSocketChannelImpl)
       result = new ServerSocketChannelSelectionKey (ch, this);
+    else if (ch instanceof gnu.java.nio.SocketChannelImpl)
+      result = new gnu.java.nio.SocketChannelSelectionKeyImpl((gnu.java.nio.SocketChannelImpl)ch, this);
     else
       throw new InternalError ("No known channel type");
 
Index: gnu/java/nio/SocketChannelImpl.java
===================================================================
RCS file: /cvsroot/classpath/classpath/gnu/java/nio/SocketChannelImpl.java,v
retrieving revision 1.29
diff -u -r1.29 SocketChannelImpl.java
--- gnu/java/nio/SocketChannelImpl.java	27 Dec 2005 02:27:01 -0000	1.29
+++ gnu/java/nio/SocketChannelImpl.java	2 Mar 2006 19:34:22 -0000
@@ -1,5 +1,5 @@
 /* SocketChannelImpl.java -- 
-   Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
+   Copyright (C) 2002, 2003, 2004, 2006 Free Software Foundation, Inc.
 
 This file is part of GNU Classpath.
 
@@ -39,10 +39,9 @@
 package gnu.java.nio;
 
 import gnu.java.net.PlainSocketImpl;
+import gnu.java.nio.VMChannel;
 
 import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
 import java.net.InetSocketAddress;
 import java.net.Socket;
 import java.net.SocketAddress;
@@ -52,7 +51,6 @@
 import java.nio.channels.ClosedChannelException;
 import java.nio.channels.ConnectionPendingException;
 import java.nio.channels.NoConnectionPendingException;
-import java.nio.channels.NotYetConnectedException;
 import java.nio.channels.SelectionKey;
 import java.nio.channels.Selector;
 import java.nio.channels.SocketChannel;
@@ -60,60 +58,55 @@
 import java.nio.channels.UnsupportedAddressTypeException;
 import java.nio.channels.spi.SelectorProvider;
 
-public final class SocketChannelImpl extends SocketChannel
+/**
+ * @author Michael Barker <[EMAIL PROTECTED]>
+ *
+ */
+public class SocketChannelImpl extends SocketChannel
 {
-  private PlainSocketImpl impl;
-  private NIOSocket socket;
+  private VMChannel vmch;
+  private PlainSocketImpl sImpl;
+  private Socket socket;
   private boolean connectionPending;
-
-  SocketChannelImpl (SelectorProvider provider)
+  
+  /**
+   * @param provider
+   * @throws IOException 
+   */
+  public SocketChannelImpl(SelectorProvider provider) 
     throws IOException
   {
-    super (provider);
-    impl = new PlainSocketImpl();
-    socket = new NIOSocket (impl, this);
-    configureBlocking(true);
+    super(provider);
+    this.sImpl = new PlainSocketImpl();
+    this.vmch = VMChannel.getVMChannel(sImpl);
+    this.socket = new NIOSocket(sImpl, this);
   }
   
-  SocketChannelImpl (SelectorProvider provider,
-                     NIOSocket socket)
+  /**
+   * Reads from the channel into a single buffer.
+   */
+  public int read(ByteBuffer dst) 
     throws IOException
   {
-    super (provider);
-    this.impl = socket.getPlainSocketImpl();
-    this.socket = socket;
-  }
-
-  public void finalizer()
-  {
-    if (isConnected())
-      {
-        try
-          {
-            close ();
-          }
-        catch (Exception e)
-          {
-          }
-      }
-  }
-
-  PlainSocketImpl getPlainSocketImpl()
-  {
-    return impl;
+    return vmch.read(dst);
   }
+  
 
-  protected void implCloseSelectableChannel () throws IOException
+  /**
+   * Performs a gather read into an array of buffers.  Currently limited
+   * to 16 buffers.
+   * 
+   * @see java.nio.channels.SocketChannel#read(java.nio.ByteBuffer[], int, int)
+   */
+  public long read(ByteBuffer[] dsts, int offset, int length) 
+    throws IOException
   {
-    socket.close();
+    return vmch.readScattering(dsts, offset, length);
   }
-
-  protected void implConfigureBlocking (boolean blocking) throws IOException
-  {
-    socket.setSoTimeout (blocking ? 0 : NIOConstants.DEFAULT_TIMEOUT);
-  }   
-
-  public boolean connect (SocketAddress remote) throws IOException
+  
+  
+  public boolean connect(SocketAddress remote) 
+    throws IOException
   {
     if (!isOpen())
       throw new ClosedChannelException();
@@ -132,7 +125,7 @@
     
     try
       {
-        socket.getPlainSocketImpl().setInChannelOperation(true);
+        sImpl.setInChannelOperation(true);
           // indicate that a channel is initiating the accept operation
           // so that the socket ignores the fact that we might be in
           // non-blocking mode.
@@ -158,11 +151,11 @@
       }
     finally
       {
-        socket.getPlainSocketImpl().setInChannelOperation(false);
+        sImpl.setInChannelOperation(false);
       }
   }
-    
-  public boolean finishConnect ()
+
+  public boolean finishConnect() 
     throws IOException
   {
     if (!isOpen())
@@ -174,8 +167,6 @@
     if (isConnected())
       return true;
 
-    // FIXME: Handle blocking/non-blocking mode.
-
     Selector selector = provider().openSelector();
     register(selector, SelectionKey.OP_CONNECT);
 
@@ -196,156 +187,73 @@
     return false;
   }
 
-  public boolean isConnected ()
+  public boolean isConnected()
   {
     return socket.isConnected();
   }
-    
-  public boolean isConnectionPending ()
+
+  public boolean isConnectionPending()
   {
     return connectionPending;
   }
-    
-  public Socket socket ()
+
+
+  public Socket socket()
   {
     return socket;
   }
 
-  public int read(ByteBuffer dst) throws IOException
+  public int write(ByteBuffer src) 
+    throws IOException
   {
-    if (!isConnected())
-      throw new NotYetConnectedException();
-    
-    byte[] data;
-    int offset = 0;
-    InputStream input = socket.getInputStream();
-    int available = input.available();
-    int len = dst.remaining();
-	
-    if ((! isBlocking()) && available == 0)
-      return 0;
-    
-    if (dst.hasArray())
-      {
-        offset = dst.arrayOffset() + dst.position();
-        data = dst.array();
-      }
-    else
-      {
-        data = new byte [len];
-      }
-
-    int readBytes = 0;
-    boolean completed = false;
-
-    try
-      {
-        begin();
-        socket.getPlainSocketImpl().setInChannelOperation(true);
-        readBytes = input.read (data, offset, len);
-        completed = true;
-      }
-    finally
-      {
-        end (completed);
-        socket.getPlainSocketImpl().setInChannelOperation(false);
-      }
-
-    if (readBytes > 0)
-      if (dst.hasArray())
-	{
-	  dst.position (dst.position() + readBytes);
-	}
-      else
-        {
-          dst.put (data, offset, readBytes);
-        }
-
-    return readBytes;
+    return vmch.write(src);
   }
-    
-  public long read (ByteBuffer[] dsts, int offset, int length)
+
+  public long write(ByteBuffer[] srcs, int offset, int length) 
     throws IOException
   {
-    if (!isConnected())
-      throw new NotYetConnectedException();
-    
-    if ((offset < 0)
-        || (offset > dsts.length)
-        || (length < 0)
-        || (length > (dsts.length - offset)))
-      throw new IndexOutOfBoundsException();
-      
-    long readBytes = 0;
-
-    for (int index = offset; index < length; index++)
-      readBytes += read (dsts [index]);
-
-    return readBytes;
+    return vmch.writeGathering(srcs, offset, length);
   }
-     
-  public int write (ByteBuffer src)
+
+  protected void implCloseSelectableChannel() 
     throws IOException
   {
-    if (!isConnected())
-      throw new NotYetConnectedException();
-    
-    byte[] data;
-    int offset = 0;
-    int len = src.remaining();
-    
-    if (!src.hasArray())
-      {
-        data = new byte [len];
-        src.get (data, 0, len);
-      }
-    else
-      {
-        offset = src.arrayOffset() + src.position();
-        data = src.array();
-      }
-
-    OutputStream output = socket.getOutputStream();
-    boolean completed = false;
-
-    try
-      {
-        begin();
-        socket.getPlainSocketImpl().setInChannelOperation(true);
-        output.write (data, offset, len);
-        completed = true;
-      }
-    finally
-      {
-        end (completed);
-        socket.getPlainSocketImpl().setInChannelOperation(false);
-      }
-
-    if (src.hasArray())
-      {
-	src.position (src.position() + len);
-      }
-    
-    return len;
+    socket.close();
   }
+  
 
-  public long write (ByteBuffer[] srcs, int offset, int length)
+  protected void implConfigureBlocking(boolean blocking) 
     throws IOException
   {
-    if (!isConnected())
-      throw new NotYetConnectedException();
-    
-    if ((offset < 0)
-        || (offset > srcs.length)
-        || (length < 0)
-        || (length > (srcs.length - offset)))
-      throw new IndexOutOfBoundsException();
-      
-    long writtenBytes = 0;
+    vmch.setBlocking(blocking);
+  }
+  
+  public int getFd()
+  {
+    return sImpl.getNativeFD();
+  }
 
-    for (int index = offset; index < length; index++)
-      writtenBytes += write (srcs [index]);
 
-    return writtenBytes;
+  /**
+   * @return The socket implemenation
+   */
+  public PlainSocketImpl getPlainSocketImpl()
+  {
+    return sImpl;
   }
+  
+  public void finalizer()
+  {
+    if (isConnected())
+      {
+        try
+          {
+            close ();
+          }
+        catch (Exception e)
+          {
+          }
+      }
+  }  
+
 }
Index: include/Makefile.am
===================================================================
RCS file: /cvsroot/classpath/classpath/include/Makefile.am,v
retrieving revision 1.53
diff -u -r1.53 Makefile.am
--- include/Makefile.am	13 Feb 2006 16:12:53 -0000	1.53
+++ include/Makefile.am	2 Mar 2006 19:34:25 -0000
@@ -119,6 +119,7 @@
 $(QTPEER_H_FILES) \
 $(top_srcdir)/include/gnu_java_net_PlainDatagramSocketImpl.h \
 $(top_srcdir)/include/gnu_java_net_PlainSocketImpl.h \
+$(top_srcdir)/include/gnu_java_nio_VMChannel.h \
 $(top_srcdir)/include/gnu_java_nio_VMPipe.h \
 $(top_srcdir)/include/gnu_java_nio_VMSelector.h \
 $(top_srcdir)/include/gnu_java_nio_channels_FileChannelImpl.h \
@@ -168,6 +169,8 @@
 	$(JAVAH) -o $@ gnu.java.net.PlainDatagramSocketImpl
 $(top_srcdir)/include/gnu_java_net_PlainSocketImpl.h: $(top_srcdir)/gnu/java/net/PlainSocketImpl.java
 	$(JAVAH) -o $@ gnu.java.net.PlainSocketImpl
+$(top_srcdir)/include/gnu_java_nio_VMChannel.h: $(top_srcdir)/vm/reference/gnu/java/nio/VMChannel.java
+	$(JAVAH) -o $@ gnu.java.nio.VMChannel
 $(top_srcdir)/include/gnu_java_nio_VMPipe.h: $(top_srcdir)/vm/reference/gnu/java/nio/VMPipe.java
 	$(JAVAH) -o $@ gnu.java.nio.VMPipe
 $(top_srcdir)/include/gnu_java_nio_VMSelector.h: $(top_srcdir)/vm/reference/gnu/java/nio/VMSelector.java
Index: java/nio/MappedByteBufferImpl.java
===================================================================
RCS file: /cvsroot/classpath/classpath/java/nio/MappedByteBufferImpl.java,v
retrieving revision 1.18
diff -u -r1.18 MappedByteBufferImpl.java
--- java/nio/MappedByteBufferImpl.java	3 Aug 2005 13:12:59 -0000	1.18
+++ java/nio/MappedByteBufferImpl.java	2 Mar 2006 19:34:31 -0000
@@ -48,7 +48,7 @@
 
   /** Posix uses this for the pointer returned by mmap;
    * Win32 uses it for the pointer returned by MapViewOfFile. */
-  public Pointer implPtr;
+  //public Pointer implPtr;
   /** Posix uses this for the actual length passed to mmap;
    * Win32 uses it for the pointer returned by CreateFileMapping. */
   public long implLen;
Index: native/jni/java-nio/Makefile.am
===================================================================
RCS file: /cvsroot/classpath/classpath/native/jni/java-nio/Makefile.am,v
retrieving revision 1.21
diff -u -r1.21 Makefile.am
--- native/jni/java-nio/Makefile.am	25 Jan 2006 10:40:12 -0000	1.21
+++ native/jni/java-nio/Makefile.am	2 Mar 2006 19:34:36 -0000
@@ -1,6 +1,7 @@
 nativelib_LTLIBRARIES = libjavanio.la
 
 libjavanio_la_SOURCES = gnu_java_nio_VMPipe.c \
+			gnu_java_nio_VMChannel.c \
 			gnu_java_nio_VMSelector.c \
 			gnu_java_nio_channels_FileChannelImpl.c \
 			gnu_java_nio_charset_iconv_IconvDecoder.c \
Index: gnu/java/nio/SocketChannelSelectionKeyImpl.java
===================================================================
RCS file: gnu/java/nio/SocketChannelSelectionKeyImpl.java
diff -N gnu/java/nio/SocketChannelSelectionKeyImpl.java
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ gnu/java/nio/SocketChannelSelectionKeyImpl.java	1 Jan 1970 00:00:00 -0000
@@ -0,0 +1,69 @@
+/* SocketChannelSelectionKey.java -- Selection key for Socket Channel
+   Copyright (C) 2005 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., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 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;
+
+
+/**
+ * @author Michael Barker <[EMAIL PROTECTED]>
+ *
+ */
+public class SocketChannelSelectionKeyImpl extends SelectionKeyImpl
+{
+
+  SocketChannelImpl ch;
+  
+  /**
+   * @param ch
+   * @param impl
+   */
+  public SocketChannelSelectionKeyImpl(SocketChannelImpl ch, SelectorImpl impl)
+  {
+    super(ch, impl);
+    this.ch = (SocketChannelImpl) ch;
+  }
+
+  /**
+   * Returns the native file/socket descriptor as an int.
+   */
+  public int getNativeFD()
+  {
+    return ch.getFd();
+  }
+
+}
Index: include/gnu_java_net_PlainDatagramSocketImpl.h
===================================================================
RCS file: include/gnu_java_net_PlainDatagramSocketImpl.h
diff -N include/gnu_java_net_PlainDatagramSocketImpl.h
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ include/gnu_java_net_PlainDatagramSocketImpl.h	1 Jan 1970 00:00:00 -0000
@@ -0,0 +1,18 @@
+/* DO NOT EDIT THIS FILE - it is machine generated */
+
+#ifndef __gnu_java_net_PlainDatagramSocketImpl__
+#define __gnu_java_net_PlainDatagramSocketImpl__
+
+#include <jni.h>
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __gnu_java_net_PlainDatagramSocketImpl__ */
Index: include/gnu_java_net_PlainSocketImpl.h
===================================================================
RCS file: include/gnu_java_net_PlainSocketImpl.h
diff -N include/gnu_java_net_PlainSocketImpl.h
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ include/gnu_java_net_PlainSocketImpl.h	1 Jan 1970 00:00:00 -0000
@@ -0,0 +1,18 @@
+/* DO NOT EDIT THIS FILE - it is machine generated */
+
+#ifndef __gnu_java_net_PlainSocketImpl__
+#define __gnu_java_net_PlainSocketImpl__
+
+#include <jni.h>
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __gnu_java_net_PlainSocketImpl__ */
Index: include/gnu_java_nio_VMChannel.h
===================================================================
RCS file: include/gnu_java_nio_VMChannel.h
diff -N include/gnu_java_nio_VMChannel.h
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ include/gnu_java_nio_VMChannel.h	1 Jan 1970 00:00:00 -0000
@@ -0,0 +1,24 @@
+/* DO NOT EDIT THIS FILE - it is machine generated */
+
+#ifndef __gnu_java_nio_VMChannel__
+#define __gnu_java_nio_VMChannel__
+
+#include <jni.h>
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
+JNIEXPORT void JNICALL Java_gnu_java_nio_VMChannel_setBlocking (JNIEnv *env, jobject, jint, jboolean);
+JNIEXPORT jint JNICALL Java_gnu_java_nio_VMChannel_read (JNIEnv *env, jobject, jint, jobject);
+JNIEXPORT jlong JNICALL Java_gnu_java_nio_VMChannel_readScattering (JNIEnv *env, jobject, jint, jobjectArray, jint, jint);
+JNIEXPORT jint JNICALL Java_gnu_java_nio_VMChannel_write (JNIEnv *env, jobject, jint, jobject);
+JNIEXPORT jlong JNICALL Java_gnu_java_nio_VMChannel_writeGathering (JNIEnv *env, jobject, jint, jobjectArray, jint, jint);
+JNIEXPORT void JNICALL Java_gnu_java_nio_VMChannel_initIDs (JNIEnv *env, jclass);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __gnu_java_nio_VMChannel__ */
Index: native/jni/java-nio/gnu_java_nio_VMChannel.c
===================================================================
RCS file: native/jni/java-nio/gnu_java_nio_VMChannel.c
diff -N native/jni/java-nio/gnu_java_nio_VMChannel.c
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ native/jni/java-nio/gnu_java_nio_VMChannel.c	1 Jan 1970 00:00:00 -0000
@@ -0,0 +1,483 @@
+/* gnu_java_nio_VMChannel.c -
+   Copyright (C) 2003, 2004, 2005, 2006  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., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 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. */
+
+
+#include <config.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <unistd.h>
+#include <sys/uio.h>
+#include <string.h>
+
+#include <jni.h>
+#include <jcl.h>
+
+#include "gnu_java_nio_VMChannel.h"
+
+#ifdef HAVE_FCNTL_H
+#include <fcntl.h>
+#endif /* HAVE_FCNTL_H */
+
+#define IO_EXCEPTION "java/io/IOException"
+/*
+ * Limit to maximum of 16 buffers
+ */
+#define JCL_IOV_MAX 16
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
+static jfieldID address_fid;
+static jmethodID get_position_mid;
+static jmethodID set_position_mid;
+static jmethodID get_limit_mid;
+static jmethodID set_limit_mid;
+static jmethodID has_array_mid;
+static jmethodID array_mid;
+static jmethodID array_offset_mid;
+/*
+static jmethodID put_mid;
+static jmethodID get_mid;
+*/
+
+jmethodID
+get_method_id(JNIEnv *env,  jclass clazz, const char *name, 
+	          const char *sig)
+{
+  jmethodID mid = (*env)->GetMethodID(env, clazz, name, sig);
+  if (mid == NULL)
+    {
+  	  JCL_ThrowException(env, "java/lang/InternalError", name);
+      return NULL;
+    }
+  
+  return mid;
+}
+
+enum JCL_buffer_type { DIRECT, ARRAY, UNKNOWN };
+
+struct JCL_buffer
+{
+  enum JCL_buffer_type type;
+  jbyte *ptr;
+  jbyteArray array;
+  jobject bbuf;
+  jint offset;
+  jint position;
+  jint limit;
+  jint count;
+};
+
+int
+JCL_init_buffer(JNIEnv *env, struct JCL_buffer *buf, jobject bbuf)
+{
+  jobject address = (*env)->GetObjectField(env, bbuf, address_fid);
+  
+  buf->position = (*env)->CallIntMethod(env, bbuf, get_position_mid);
+  buf->limit = (*env)->CallIntMethod(env, bbuf, get_limit_mid);
+  buf->bbuf = bbuf;
+  buf->offset = 0;
+  buf->array = NULL;
+  buf->count = 0;
+  buf->type = UNKNOWN;
+    
+  if (address != NULL)
+    {
+      buf->ptr = (jbyte *) JCL_GetRawData(env, address);
+      buf->type = DIRECT;
+    }
+  else
+    {
+      jboolean has_array;
+      
+      has_array = (*env)->CallBooleanMethod(env, bbuf, has_array_mid);
+      if (has_array == JNI_TRUE)
+        {
+          buf->offset = (*env)->CallIntMethod(env, bbuf, array_offset_mid);
+          buf->array = (*env)->CallObjectMethod(env, bbuf, array_mid);
+          buf->ptr = (*env)->GetByteArrayElements(env, buf->array, 0);
+          buf->type = ARRAY;
+        }
+      else
+        {
+          return -1;
+        }
+    }
+      
+  return 0;
+}
+
+void
+JCL_release_buffer(JNIEnv *env, struct JCL_buffer *buf, jint action)
+{
+  /* Set the position to the appropriate value */
+  if (buf->count > 0)
+    (*env)->CallObjectMethod(env, buf->bbuf, set_position_mid, 
+                             buf->position + buf->count);
+    
+  switch (buf->type)
+    {
+    case DIRECT:
+      break;
+    case ARRAY:
+      (*env)->ReleaseByteArrayElements(env, buf->array, buf->ptr, action);
+      break;
+    case UNKNOWN:
+      /* TODO: Handle buffers that are not direct or array backed */
+      break;
+    }
+}
+
+
+JNIEXPORT void JNICALL 
+Java_gnu_java_nio_VMChannel_initIDs  (JNIEnv *env, 
+	jclass clazz __attribute__ ((__unused__)))
+{
+  jclass bufferClass = JCL_FindClass(env, "java/nio/Buffer");
+  jclass byteBufferClass = JCL_FindClass(env, "java/nio/ByteBuffer");
+  
+  address_fid = (*env)->GetFieldID(env, bufferClass, "address", 
+                                   "Lgnu/classpath/Pointer;");
+  if (address_fid == NULL)
+    {
+  	  JCL_ThrowException(env, "java/lang/InternalError", 
+  	  	"Unable to find internal field");
+      return;
+    }
+  
+  get_position_mid = get_method_id(env, bufferClass, "position", "()I");
+  set_position_mid = get_method_id(env, bufferClass, "position", 
+                                   "(I)Ljava/nio/Buffer;");
+  get_limit_mid = get_method_id(env, bufferClass, "limit", "()I");
+  set_limit_mid = get_method_id(env, bufferClass, "limit", 
+                                "(I)Ljava/nio/Buffer;");
+  has_array_mid = get_method_id(env, byteBufferClass, "hasArray", "()Z");
+  array_mid = get_method_id(env, byteBufferClass, "array", "()[B");
+  array_offset_mid = get_method_id(env, byteBufferClass, "arrayOffset", "()I");
+  /*
+  get_mid = get_method_id(env, byteBufferClass, "get", 
+                          "([B)Ljava.nio.ByteBuffer;");
+  put_mid = get_method_id(env, byteBufferClass, "put", 
+                          "([B)Ljava.nio.ByteBuffer;");
+  */
+}
+
+JNIEXPORT void JNICALL 
+Java_gnu_java_nio_VMChannel_setBlocking (JNIEnv *env, 
+	jobject o __attribute__ ((__unused__)), 
+	jint fd, 
+	jboolean blocking)
+{
+  int opts;
+  
+  opts = fcntl(fd, F_GETFL);
+  if (opts < 0)
+    {
+      JCL_ThrowException(env, IO_EXCEPTION, 
+        "Failed to get flags for file desriptor");
+      return;
+    }
+  
+  if (blocking)
+    opts |= O_NONBLOCK;
+  else
+    opts &= ~(O_NONBLOCK);
+  
+  opts = fcntl(fd, F_SETFL, opts);
+  
+  if (opts < 0)
+    {
+      JCL_ThrowException(env, IO_EXCEPTION, 
+        "Failed to set flags for file desriptor");
+      return;
+    }  
+}
+
+
+JNIEXPORT jint JNICALL 
+Java_gnu_java_nio_VMChannel_read (JNIEnv *env, 
+	jobject o __attribute__ ((__unused__)), 
+	jint fd, 
+	jobject bbuf)
+{
+  jint len;
+  ssize_t result;
+  struct JCL_buffer buf;
+  
+  if (JCL_init_buffer(env, &buf, bbuf) < 0)
+    {
+      /* TODO: Rethrown exception */
+      JCL_ThrowException (env, IO_EXCEPTION, "Buffer initialisation failed");
+      return -1;
+    }
+  
+  len = buf.limit - buf.position;
+  
+  result = read(fd, &(buf.ptr[buf.position + buf.offset]), len);
+  buf.count = result;
+  
+  if (result == 0)
+    result = -1; /* End Of File */
+  else if (result == -1)
+    {
+      buf.count = 0;
+      if (errno == EAGAIN) /* Non-blocking */
+        result = 0;
+      else
+        {
+          JCL_release_buffer(env, &buf, JNI_ABORT);
+      	  JCL_ThrowException (env, IO_EXCEPTION, strerror(errno));
+      	  return -1;
+        }
+    }
+  else 
+    
+  JCL_release_buffer(env, &buf, JNI_COMMIT);
+  
+  return result;
+}
+
+JNIEXPORT jint JNICALL 
+Java_gnu_java_nio_VMChannel_write (JNIEnv *env, 
+	jobject o __attribute__ ((__unused__)), 
+	jint fd, 
+	jobject bbuf)
+{
+  jint len;
+  ssize_t result;
+  struct JCL_buffer buf;
+  
+  if (JCL_init_buffer(env, &buf, bbuf) < 0)
+    {
+      /* TODO: Rethrown exception */
+      JCL_ThrowException (env, IO_EXCEPTION, "Buffer initialisation failed");
+      return -1;
+    }
+  
+  len = buf.limit - buf.position;
+  
+  result = write(fd, &(buf.ptr[buf.offset]), len);
+  buf.count = result;
+  
+  if (result == -1)
+    {
+      if (errno == EAGAIN) /* Non-blocking */
+          result = 0;
+      else
+        {
+          JCL_release_buffer(env, &buf, JNI_ABORT);
+          JCL_ThrowException(env, IO_EXCEPTION, strerror(errno));
+          return -1;
+        }
+    }
+    
+  JCL_release_buffer(env, &buf, JNI_ABORT);
+  
+  return result;  
+}
+
+
+/*
+ * Implementation of a scattering read.  Will use the appropriate
+ * vector based read call (currently readv on Linux).
+ * 
+ * This has a limit to the number of buffers that will be read.  It
+ * will not make muliple readv calls.  This is to ensure that operations 
+ * are atomic.  Currently it is limited to 16 buffers.  This is for 
+ * compatibiliy with Sun.
+ */
+JNIEXPORT jlong JNICALL 
+Java_gnu_java_nio_VMChannel_readScattering (JNIEnv *env, 
+	jobject o __attribute__ ((__unused__)), 
+	jint fd, 
+	jobjectArray bbufs, 
+	jint offset, 
+	jint length)
+{
+  jint i;
+  jboolean is_error = JNI_FALSE;
+  char *error_msg;
+  struct iovec buffers[JCL_IOV_MAX];
+  struct JCL_buffer bi_list[JCL_IOV_MAX];
+  ssize_t result;
+  jint vec_len = length < JCL_IOV_MAX ? length : JCL_IOV_MAX;
+  jlong bytes_read = 0;      
+  
+   /* Build the vector of buffers to read into */
+  for (i = 0; i < vec_len; i++)
+    {
+      struct JCL_buffer buf = bi_list[i];
+      jobject bbuf = (*env)->GetObjectArrayElement(env, bbufs, offset + i);
+      
+      JCL_init_buffer(env, &buf, bbuf); 
+      
+      buffers[i].iov_base = &(buf.ptr[buf.position + buf.offset]);
+      buffers[i].iov_len = buf.limit - buf.position;
+    }
+    
+  /* Work the scattering magic */
+  result = readv(fd, buffers, vec_len);
+  bytes_read = (jlong) result;
+  
+  /* Handle the response */
+  if (result < 0)
+    {
+      if (errno == EAGAIN) /* Non blocking */
+        result = 0;
+      else
+        {
+          is_error = JNI_TRUE;
+          error_msg = strerror(errno);
+        }
+      bytes_read = 0;
+    }
+  else if (result == 0) /* EOF */
+    {
+      result = -1;
+    }
+    
+  /* Update all of the bbufs with the approriate information */
+  for (i = 0; i < vec_len; i++)
+    {
+      struct JCL_buffer buf = bi_list[i];
+
+      if (bytes_read > (buf.limit - buf.position))
+        buf.count = (buf.limit - buf.position);
+      else
+        buf.count = bytes_read;
+        
+      bytes_read -= buf.count;
+      
+      JCL_release_buffer(env, &buf, JNI_COMMIT);      
+    }
+              
+  if (is_error == JNI_TRUE)
+    {
+      JCL_ThrowException(env, IO_EXCEPTION, error_msg);
+      return -1;
+    }
+    
+  return (jlong) result;
+}
+
+/*
+ * Implementation of a gathering write.  Will use the appropriate
+ * vector based read call (currently readv on Linux).
+ * 
+ * This has a limit to the number of buffers that will be read.  It
+ * will not make muliple readv calls.  This is to ensure that operations 
+ * are atomic.  Currently it is limited to 16 buffers.  This is for 
+ * compatibiliy with Sun.
+ */
+JNIEXPORT jlong JNICALL 
+Java_gnu_java_nio_VMChannel_writeGathering (JNIEnv *env, 
+	jobject o __attribute__ ((__unused__)), 
+	jint fd, 
+	jobjectArray bbufs, 
+	jint offset, 
+	jint length)
+{
+  int i;
+  jboolean is_error = JNI_FALSE;
+  char *error_msg;
+  struct iovec buffers[JCL_IOV_MAX];
+  struct JCL_buffer bi_list[JCL_IOV_MAX];
+  ssize_t result;
+  jint vec_len = length < JCL_IOV_MAX ? length : JCL_IOV_MAX;
+  jlong bytes_written;
+  
+  /* Build the vector of buffers to read into */
+  for (i = 0; i < vec_len; i++)
+    {
+      struct JCL_buffer buf = bi_list[i];
+      jobject bbuf = (*env)->GetObjectArrayElement(env, bbufs, offset + i);
+      
+      JCL_init_buffer(env, &buf, bbuf); 
+      
+      buffers[i].iov_base = &(buf.ptr[buf.position + buf.offset]);
+      buffers[i].iov_len = buf.limit - buf.position;
+    }
+    
+  /* Work the gathering magic */
+  result = writev(fd, buffers, vec_len);
+  bytes_written = (jlong) result;
+
+  if (result < 0)
+    {
+      bytes_written = 0;
+      if (errno == EAGAIN) /* Non blocking */
+        result = 0;
+      else
+        {
+          is_error = JNI_TRUE;
+          error_msg = strerror(errno);
+        }
+    }
+  else if (result == 0) /* EOF??  Does this happen on a write */
+    result = -1;
+    
+  for (i = 0; i < vec_len; i++)
+    {
+      struct JCL_buffer buf = bi_list[i];
+
+      if (bytes_written > (buf.limit - buf.position))
+        buf.count = (buf.limit - buf.position);
+      else
+        buf.count = bytes_written;
+        
+      bytes_written -= buf.count;
+      
+      JCL_release_buffer(env, &buf, JNI_ABORT);
+    }
+      
+  if (is_error == JNI_TRUE)
+    {
+      JCL_ThrowException(env, IO_EXCEPTION, error_msg);
+      return -1;
+    }
+    
+  return (jlong) result;
+}
+
+
+
+
+#ifdef __cplusplus
+}
+#endif
Index: vm/reference/gnu/java/nio/VMChannel.java
===================================================================
RCS file: vm/reference/gnu/java/nio/VMChannel.java
diff -N vm/reference/gnu/java/nio/VMChannel.java
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ vm/reference/gnu/java/nio/VMChannel.java	1 Jan 1970 00:00:00 -0000
@@ -0,0 +1,182 @@
+/* VMChannel.java -- Native interface suppling channel operations.
+   Copyright (C) 2005 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., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 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.classpath.Configuration;
+import gnu.java.net.PlainSocketImpl;
+import gnu.java.nio.PipeImpl.SinkChannelImpl;
+import gnu.java.nio.PipeImpl.SourceChannelImpl;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+
+/**
+ * @author Michael Barker <[EMAIL PROTECTED]>
+ *
+ */
+public class VMChannel
+{
+  private final int fd;
+  
+  private VMChannel(int fd)
+  {
+    this.fd = fd;
+  }
+  
+  public static VMChannel getVMChannel(PlainSocketImpl socket)
+  {
+    return new VMChannel(socket.getNativeFD());
+  }
+  
+  public static VMChannel getVMChannel(SourceChannelImpl source)
+  {
+    return new VMChannel(source.getNativeFD());
+  }
+  
+  public static VMChannel getVMChannel(SinkChannelImpl sink)
+  {
+    return new VMChannel(sink.getNativeFD());
+  }
+
+  static
+  {
+    // load the shared library needed for native methods.
+    if (Configuration.INIT_LOAD_LIBRARY)
+      {
+        System.loadLibrary ("javanio");
+      }
+    initIDs();
+  }
+  
+  /**
+   * Set the file descriptor to have the required blocking
+   * setting.
+   * 
+   * @param fd
+   * @param blocking
+   */
+  public native void setBlocking(int fd, boolean blocking);
+  
+  public void setBlocking(boolean blocking)
+  {
+    setBlocking(fd, blocking);
+  }
+  
+
+  /**
+   * Reads a byte buffer directly using the supplied file descriptor.
+   * Assumes that the buffer is a DirectBuffer.
+   * 
+   * @param fd Native file descriptor to read from.
+   * @param dst Direct Byte Buffer to read to.
+   * @return Number of bytes read.
+   * @throws IOException If an error occurs or dst is not a direct buffers. 
+   */
+  native int read(int fd, ByteBuffer dst)
+    throws IOException;
+  
+  public int read(ByteBuffer dst)
+    throws IOException
+  {
+    return read(fd, dst);
+  }
+  
+  /**
+   * Reads into byte buffers directly using the supplied file descriptor.
+   * Assumes that the buffer list contains DirectBuffers.  Will perform a
+   * scattering read.
+   * 
+   * @param fd Native file descriptor to read from.
+   * @param dsts An array direct byte buffers.
+   * @param offset Index of the first buffer to read to.
+   * @param length The number of buffers to read to.
+   * @return Number of bytes read.
+   * @throws IOException If an error occurs or the dsts are not direct buffers. 
+   */
+  native long readScattering(int fd, ByteBuffer[] dsts, int offset, int length)
+    throws IOException;
+
+  public long readScattering(ByteBuffer[] dsts, int offset, int length)
+    throws IOException
+  {
+    return readScattering(fd, dsts, offset, length);
+  }
+  
+  /**
+   * Writes from a direct byte bufer using the supplied file descriptor.
+   * Assumes the buffer is a DirectBuffer.
+   * 
+   * @param fd
+   * @param src
+   * @return Number of bytes written.
+   * @throws IOException
+   */
+  native int write(int fd, ByteBuffer src)
+    throws IOException;
+
+  public int write(ByteBuffer src)
+    throws IOException
+  {
+    return write(fd, src);
+  }
+  
+  /**
+   * Writes from byte buffers directly using the supplied file descriptor.
+   * Assumes the that buffer list constains DirectBuffers.  Will perform
+   * as gathering write.
+   * 
+   * @param fd
+   * @param srcs
+   * @param offset
+   * @param length
+   * @return Number of bytes written.
+   * @throws IOException
+   */
+  native long writeGathering(int fd, ByteBuffer[] srcs, int offset, int length)
+    throws IOException;
+  
+  public long writeGathering(ByteBuffer[] srcs, int offset, int length)
+    throws IOException
+  {
+    return writeGathering(fd, srcs, offset, length);
+  }
+  
+  private native static void initIDs();
+
+}


Reply via email to