[Patch] java.nio - direct buffers

Michael Koch konqueror@gmx.de
Wed Nov 24 11:13:00 GMT 2004


Hi list,


I just commited the attached patch to merge java.nio with classpath.
This introduces a new class VMDirectByteBuffer which contains all static
methods needed to handle direct buffers. This patch contained some other
little fixes too.


Michael


2004-11-24  Michael Koch  <konqueror@gmx.de>

	* gnu/java/nio/NIOServerSocket.java: Added email to @author tag.
	* java/nio/DirectByteBufferImpl.java:
	Moved native methods to java.nio.VMDirectByteBuffer class.
	* java/nio/MappedByteBufferImpl.java:
	Use native methods from java.nio.VMDirectByteBuffer class.
	* java/nio/VMDirectByteBuffer.java: New file,
	* java/nio/natDirectByteBufferImpl.cc:
	Moved all methods into java.nio.VMDirectByteBuffer class.
	* java/nio/channels/spi/AbstractSelectableChannel.java
	(register): Only re-use valid keys.
	* Makefile.am: Added java/nio/VMDirectByteBuffer.java.
	* Makefile.in: Regenerated.

-------------- next part --------------
Index: Makefile.am
===================================================================
RCS file: /cvs/gcc/gcc/libjava/Makefile.am,v
retrieving revision 1.427
diff -u -r1.427 Makefile.am
--- Makefile.am	18 Nov 2004 05:37:09 -0000	1.427
+++ Makefile.am	24 Nov 2004 10:36:20 -0000
@@ -2957,6 +2957,7 @@
 java/nio/ShortBuffer.java \
 java/nio/ShortBufferImpl.java \
 java/nio/ShortViewBufferImpl.java \
+java/nio/VMDirectByteBuffer.java \
 java/nio/channels/AlreadyConnectedException.java \
 java/nio/channels/AsynchronousCloseException.java \
 java/nio/channels/ByteChannel.java \
Index: Makefile.in
===================================================================
RCS file: /cvs/gcc/gcc/libjava/Makefile.in,v
retrieving revision 1.456
diff -u -r1.456 Makefile.in
--- Makefile.in	23 Nov 2004 21:20:37 -0000	1.456
+++ Makefile.in	24 Nov 2004 10:36:26 -0000
@@ -949,6 +949,7 @@
 	java/nio/ReadOnlyBufferException.java \
 	java/nio/ShortBuffer.java java/nio/ShortBufferImpl.java \
 	java/nio/ShortViewBufferImpl.java \
+	java/nio/VMDirectByteBuffer.java \
 	java/nio/channels/AlreadyConnectedException.java \
 	java/nio/channels/AsynchronousCloseException.java \
 	java/nio/channels/ByteChannel.java \
@@ -3918,6 +3919,7 @@
 	java/nio/MappedByteBuffer.lo java/nio/MappedByteBufferImpl.lo \
 	java/nio/ReadOnlyBufferException.lo java/nio/ShortBuffer.lo \
 	java/nio/ShortBufferImpl.lo java/nio/ShortViewBufferImpl.lo \
+	java/nio/VMDirectByteBuffer.lo \
 	java/nio/channels/AlreadyConnectedException.lo \
 	java/nio/channels/AsynchronousCloseException.lo \
 	java/nio/channels/ByteChannel.lo \
@@ -6830,6 +6832,7 @@
 java/nio/ShortBuffer.java \
 java/nio/ShortBufferImpl.java \
 java/nio/ShortViewBufferImpl.java \
+java/nio/VMDirectByteBuffer.java \
 java/nio/channels/AlreadyConnectedException.java \
 java/nio/channels/AsynchronousCloseException.java \
 java/nio/channels/ByteChannel.java \
@@ -9997,6 +10000,8 @@
 	java/nio/$(DEPDIR)/$(am__dirstamp)
 java/nio/ShortViewBufferImpl.lo: java/nio/$(am__dirstamp) \
 	java/nio/$(DEPDIR)/$(am__dirstamp)
+java/nio/VMDirectByteBuffer.lo: java/nio/$(am__dirstamp) \
+	java/nio/$(DEPDIR)/$(am__dirstamp)
 java/nio/channels/AlreadyConnectedException.lo:  \
 	java/nio/channels/$(am__dirstamp) \
 	java/nio/channels/$(DEPDIR)/$(am__dirstamp)
@@ -16839,6 +16844,8 @@
 	-rm -f java/nio/ShortBufferImpl.lo
 	-rm -f java/nio/ShortViewBufferImpl.$(OBJEXT)
 	-rm -f java/nio/ShortViewBufferImpl.lo
+	-rm -f java/nio/VMDirectByteBuffer.$(OBJEXT)
+	-rm -f java/nio/VMDirectByteBuffer.lo
 	-rm -f java/nio/channels/AlreadyConnectedException.$(OBJEXT)
 	-rm -f java/nio/channels/AlreadyConnectedException.lo
 	-rm -f java/nio/channels/AsynchronousCloseException.$(OBJEXT)
@@ -20700,6 +20707,7 @@
 @AMDEP_TRUE@@am__include@ @am__quote@java/nio/$(DEPDIR)/ShortBuffer.Plo@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@java/nio/$(DEPDIR)/ShortBufferImpl.Plo@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@java/nio/$(DEPDIR)/ShortViewBufferImpl.Plo@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@java/nio/$(DEPDIR)/VMDirectByteBuffer.Plo@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@java/nio/$(DEPDIR)/natDirectByteBufferImpl.Plo@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@java/nio/channels/$(DEPDIR)/AlreadyConnectedException.Plo@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@java/nio/channels/$(DEPDIR)/AsynchronousCloseException.Plo@am__quote@
Index: gnu/java/nio/NIOServerSocket.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/gnu/java/nio/NIOServerSocket.java,v
retrieving revision 1.2
diff -u -r1.2 NIOServerSocket.java
--- gnu/java/nio/NIOServerSocket.java	5 Feb 2004 15:57:01 -0000	1.2
+++ gnu/java/nio/NIOServerSocket.java	24 Nov 2004 10:36:26 -0000
@@ -39,6 +39,7 @@
 package gnu.java.nio;
 
 import gnu.java.net.PlainSocketImpl;
+
 import java.io.IOException;
 import java.net.ServerSocket;
 import java.net.Socket;
@@ -46,7 +47,7 @@
 import java.nio.channels.SocketChannel;
 
 /**
- * @author Michael Koch
+ * @author Michael Koch (konqueror@gmx.de)
  */
 public final class NIOServerSocket extends ServerSocket
 {
Index: java/nio/DirectByteBufferImpl.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/nio/DirectByteBufferImpl.java,v
retrieving revision 1.13
diff -u -r1.13 DirectByteBufferImpl.java
--- java/nio/DirectByteBufferImpl.java	24 Nov 2004 09:13:33 -0000	1.13
+++ java/nio/DirectByteBufferImpl.java	24 Nov 2004 10:36:26 -0000
@@ -38,20 +38,10 @@
 
 package java.nio;
 
-import gnu.classpath.Configuration;
 import gnu.gcj.RawData;
 
 final class DirectByteBufferImpl extends ByteBuffer
 {
-  static
-  {
-    // load the shared library needed for native methods.
-    if (Configuration.INIT_LOAD_LIBRARY)
-      {
-        System.loadLibrary ("javanio");
-      }
-  }
-  
   /** Used by MappedByteBufferImpl and when slicing to prevent premature GC. */
   protected Object owner;
 
@@ -78,26 +68,21 @@
    */ 
   public static ByteBuffer allocate(int capacity)
   {
-    return new DirectByteBufferImpl(allocateImpl(capacity), capacity);
+    return new DirectByteBufferImpl(VMDirectByteBuffer.allocate(capacity),
+				    capacity);
   }
 
-  private static native RawData allocateImpl (int capacity);
-  private static native void freeImpl (RawData address);
-  
   protected void finalize() throws Throwable
   {
-    freeImpl (address);
+    VMDirectByteBuffer.free(address);
   }
   
-  static native byte getImpl (RawData address, int index);
-  static native void putImpl (RawData address, int index, byte value);
-
   public byte get()
   {
     checkForUnderflow();
 
     int pos = position();
-    byte result = getImpl (address, pos);
+    byte result = VMDirectByteBuffer.get(address, pos);
     position(pos + 1);
     return result;
   }
@@ -106,19 +91,16 @@
   {
     checkIndex(index);
 
-    return getImpl (address, index);
+    return VMDirectByteBuffer.get(address, index);
   }
 
-  static native void getImpl (RawData address, int index,
-			      byte[] dst, int offset, int length);
-
   public ByteBuffer get(byte[] dst, int offset, int length)
   {
     checkArraySize(dst.length, offset, length);
     checkForUnderflow(length);
 
     int index = position();
-    getImpl(address, index, dst, offset, length);
+    VMDirectByteBuffer.get(address, index, dst, offset, length);
     position(index+length);
 
     return this;
@@ -130,7 +112,7 @@
     checkForOverflow();
 
     int pos = position();
-    putImpl (address, pos, value);
+    VMDirectByteBuffer.put(address, pos, value);
     position(pos + 1);
     return this;
   }
@@ -140,15 +122,13 @@
     checkIfReadOnly();
     checkIndex(index);
 
-    putImpl (address, index, value);
+    VMDirectByteBuffer.put(address, index, value);
     return this;
   }
   
-  static native void shiftDown(RawData address, int dst_offset, int src_offset, int count);
-
   void shiftDown(int dst_offset, int src_offset, int count)
   {
-    shiftDown(address, dst_offset, src_offset, count);
+    VMDirectByteBuffer.shiftDown(address, dst_offset, src_offset, count);
   }
   
   public ByteBuffer compact()
@@ -157,21 +137,19 @@
     if (pos > 0)
       {
 	int count = remaining();
-	shiftDown(address, 0, pos, count);
+	VMDirectByteBuffer.shiftDown(address, 0, pos, count);
 	position(count);
 	limit(capacity());
       }
     return this;
   }
 
-  public static native RawData adjustAddress(RawData address, int offset);
-
   public ByteBuffer slice()
   {
     int rem = remaining();
-    return new DirectByteBufferImpl (owner,
-				     adjustAddress(address, position()),
-				     rem, rem, 0, isReadOnly ());
+    return new DirectByteBufferImpl
+      (owner, VMDirectByteBuffer.adjustAddress(address, position()),
+       rem, rem, 0, isReadOnly());
   }
 
   private ByteBuffer duplicate(boolean readOnly)
Index: java/nio/MappedByteBufferImpl.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/nio/MappedByteBufferImpl.java,v
retrieving revision 1.12
diff -u -r1.12 MappedByteBufferImpl.java
--- java/nio/MappedByteBufferImpl.java	24 Nov 2004 09:13:33 -0000	1.12
+++ java/nio/MappedByteBufferImpl.java	24 Nov 2004 10:36:26 -0000
@@ -72,8 +72,8 @@
     checkForUnderflow();
 
     int pos = position();
-    byte result = DirectByteBufferImpl.getImpl(address, pos);
-    position (pos + 1);
+    byte result = VMDirectByteBuffer.get(address, pos);
+    position(pos + 1);
     return result;
   }
 
@@ -83,7 +83,7 @@
     checkForOverflow();
 
     int pos = position();
-    DirectByteBufferImpl.putImpl(address, pos, value);
+    VMDirectByteBuffer.put(address, pos, value);
     position(pos + 1);
     return this;
   }
@@ -92,7 +92,7 @@
   {
     checkIndex(index);
 
-    return DirectByteBufferImpl.getImpl(address, index);
+    return VMDirectByteBuffer.get(address, index);
   }
 
   public ByteBuffer get(byte[] dst, int offset, int length)
@@ -101,7 +101,7 @@
     checkForUnderflow(length);
 
     int index = position();
-    DirectByteBufferImpl.getImpl(address, index, dst, offset, length);
+    VMDirectByteBuffer.get(address, index, dst, offset, length);
     position(index+length);
 
     return this;
@@ -112,7 +112,7 @@
     checkIfReadOnly();
     checkIndex(index);
 
-    DirectByteBufferImpl.putImpl(address, index, value);
+    VMDirectByteBuffer.put(address, index, value);
     return this;
   }
 
@@ -123,7 +123,7 @@
       {
 	int count = remaining();
 	// Call shiftDown method optimized for direct buffers.
-	DirectByteBufferImpl.shiftDown(address, 0, pos, count);
+	VMDirectByteBuffer.shiftDown(address, 0, pos, count);
 	position(count);
 	limit(capacity());
       }
@@ -138,10 +138,9 @@
   public ByteBuffer slice()
   {
     int rem = remaining();
-    return new DirectByteBufferImpl (this,
-				     DirectByteBufferImpl
-				     .adjustAddress(address, position()),
-				     rem, rem, 0, isReadOnly ());
+    return new DirectByteBufferImpl
+      (this, VMDirectByteBuffer.adjustAddress(address, position()),
+       rem, rem, 0, isReadOnly());
   }
 
   private ByteBuffer duplicate(boolean readOnly)
Index: java/nio/VMDirectByteBuffer.java
===================================================================
RCS file: java/nio/VMDirectByteBuffer.java
diff -N java/nio/VMDirectByteBuffer.java
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ java/nio/VMDirectByteBuffer.java	24 Nov 2004 10:36:26 -0000
@@ -0,0 +1,53 @@
+/* VMDirectByteBuffer.java -- 
+   Copyright (C) 2004 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 java.nio;
+
+import gnu.classpath.Configuration;
+import gnu.gcj.RawData;
+
+final class VMDirectByteBuffer
+{
+  static native RawData allocate (int capacity);
+  static native void free(RawData address);
+  static native byte get(RawData address, int index);
+  static native void get(RawData address, int index, byte[] dst, int offset, int length);
+  static native void put(RawData address, int index, byte value);
+  static native RawData adjustAddress(RawData address, int offset);
+  static native void shiftDown(RawData address, int dst_offset, int src_offset, int count);
+}
Index: java/nio/natDirectByteBufferImpl.cc
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/nio/natDirectByteBufferImpl.cc,v
retrieving revision 1.5
diff -u -r1.5 natDirectByteBufferImpl.cc
--- java/nio/natDirectByteBufferImpl.cc	21 Apr 2004 15:33:53 -0000	1.5
+++ java/nio/natDirectByteBufferImpl.cc	24 Nov 2004 10:36:26 -0000
@@ -16,56 +16,55 @@
 #include <stdlib.h>
 
 #include <gnu/gcj/RawData.h>
-#include <java/nio/DirectByteBufferImpl.h>
+#include <java/nio/VMDirectByteBuffer.h>
 
 using gnu::gcj::RawData;
-using java::nio::DirectByteBufferImpl;
 
 RawData*
-java::nio::DirectByteBufferImpl::allocateImpl (jint capacity)
+java::nio::VMDirectByteBuffer::allocate (jint capacity)
 {
   return reinterpret_cast<gnu::gcj::RawData*> (::malloc (capacity));
 }
 
 void
-java::nio::DirectByteBufferImpl::freeImpl (gnu::gcj::RawData* address)
+java::nio::VMDirectByteBuffer::free (gnu::gcj::RawData* address)
 {
   ::free (reinterpret_cast<void*> (address));
 }
 
 jbyte
-DirectByteBufferImpl::getImpl (RawData* address, jint index)
+java::nio::VMDirectByteBuffer::get (RawData* address, jint index)
 {
   jbyte* pointer = reinterpret_cast<jbyte*> (address) + index;
   return *pointer;
 }
 
 void
-DirectByteBufferImpl::getImpl (RawData* address, jint index,
-			       jbyteArray dst, jint offset, jint length)
+java::nio::VMDirectByteBuffer::get (RawData* address, jint index,
+				    jbyteArray dst, jint offset, jint length)
 {
   jbyte* src = reinterpret_cast<jbyte*> (address) + index;
   memcpy (elements (dst) + offset, src, length);
 }
 
 void
-java::nio::DirectByteBufferImpl::putImpl (gnu::gcj::RawData* address,
-					  jint index, jbyte value)
+java::nio::VMDirectByteBuffer::put (gnu::gcj::RawData* address,
+				    jint index, jbyte value)
 {
   jbyte* pointer = reinterpret_cast<jbyte*> (address) + index;
   *pointer = value;
 }
 
 RawData*
-java::nio::DirectByteBufferImpl::adjustAddress (RawData* address, jint offset)
+java::nio::VMDirectByteBuffer::adjustAddress (RawData* address, jint offset)
 {
   jbyte* start = reinterpret_cast<jbyte*> (address) + offset;
   return reinterpret_cast<RawData*>(start);
 }
 
 void
-java::nio::DirectByteBufferImpl::shiftDown
-(RawData* address, jint dst_offset, jint src_offset, jint count)
+java::nio::VMDirectByteBuffer::shiftDown (RawData* address, jint dst_offset,
+					  jint src_offset, jint count)
 {
   jbyte* dst = reinterpret_cast<jbyte*> (address) + dst_offset;
   jbyte* src = reinterpret_cast<jbyte*> (address) + src_offset;
Index: java/nio/channels/spi/AbstractSelectableChannel.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/nio/channels/spi/AbstractSelectableChannel.java,v
retrieving revision 1.11
diff -u -r1.11 AbstractSelectableChannel.java
--- java/nio/channels/spi/AbstractSelectableChannel.java	20 Apr 2004 15:27:38 -0000	1.11
+++ java/nio/channels/spi/AbstractSelectableChannel.java	24 Nov 2004 10:36:26 -0000
@@ -35,6 +35,7 @@
 obligated to do so.  If you do not wish to do so, delete this
 exception statement from your version. */
 
+
 package java.nio.channels.spi;
 
 import java.io.IOException;
@@ -45,7 +46,6 @@
 import java.util.LinkedList;
 import java.util.ListIterator;
 
-
 public abstract class AbstractSelectableChannel extends SelectableChannel
 {
   private boolean blocking = true;
@@ -226,7 +226,7 @@
       {
 	key = locate(selector);
 
-	if (key != null)
+	if (key != null && key.isValid())
 	  {
 	    if (att != null)
 	      key.attach(att);


More information about the Java-patches mailing list