re PR libgcj/18115 (JNI nio buffer functions only work with byte buffers)
authorMichael Koch <konqueror@gmx.de>
Fri, 7 Jan 2005 11:32:07 +0000 (11:32 +0000)
committerMichael Koch <mkoch@gcc.gnu.org>
Fri, 7 Jan 2005 11:32:07 +0000 (11:32 +0000)
2005-01-07  Michael Koch  <konqueror@gmx.de>

PR libgcj/18115
* java/nio/Buffer.java (address): New field.
* java/nio/DirectByteBufferImpl.java (address): Removed.
* java/nio/MappedByteBufferImpl.java (address): Likewise.
* java/nio/CharViewBufferImpl.java (CharViewBufferImpl):
Explicitly initialize Buffer.address if needed.
* java/nio/DoubleViewBufferImpl.java (DoubleViewBufferImpl): Likewise.
* java/nio/FloatViewBufferImpl.java (FloatViewBufferImpl): Likewise.
* java/nio/IntViewBufferImpl.java (IntViewBufferImpl): Likewise.
* java/nio/LongViewBufferImpl.java (LongViewBufferImpl): Likewise.
* java/nio/ShortViewBufferImpl.java (ShortViewBufferImpl): Likewise.
* jni.cc (_Jv_JNI_GetDirectBufferAddress): Don't assume buffer is a
DirectByteBufferImpl object.
(_Jv_JNI_GetDirectBufferCapacity): Likewise.
* testsuite/libjava.jni/directbuffer.c,
testsuite/libjava.jni/directbuffer.java,
testsuite/libjava.jni/directbuffer.out,
testsuite/libjava.jni/bytebuffer.c,
testsuite/libjava.jni/bytebuffer.java,
testsuite/libjava.jni/bytebuffer.out: New files.

From-SVN: r93046

17 files changed:
libjava/ChangeLog
libjava/java/nio/Buffer.java
libjava/java/nio/CharViewBufferImpl.java
libjava/java/nio/DirectByteBufferImpl.java
libjava/java/nio/DoubleViewBufferImpl.java
libjava/java/nio/FloatViewBufferImpl.java
libjava/java/nio/IntViewBufferImpl.java
libjava/java/nio/LongViewBufferImpl.java
libjava/java/nio/MappedByteBufferImpl.java
libjava/java/nio/ShortViewBufferImpl.java
libjava/jni.cc
libjava/testsuite/libjava.jni/bytebuffer.c [new file with mode: 0644]
libjava/testsuite/libjava.jni/bytebuffer.java [new file with mode: 0644]
libjava/testsuite/libjava.jni/bytebuffer.out [new file with mode: 0644]
libjava/testsuite/libjava.jni/directbuffer.c [new file with mode: 0644]
libjava/testsuite/libjava.jni/directbuffer.java [new file with mode: 0644]
libjava/testsuite/libjava.jni/directbuffer.out [new file with mode: 0644]

index d3d419106a39b06ef03f1f172ddda30d19a2925d..f60f481317ce3b87a608f752c8c0b137a40b0f9d 100644 (file)
@@ -1,3 +1,26 @@
+2005-01-07  Michael Koch  <konqueror@gmx.de>
+
+       PR libgcj/18115
+       * java/nio/Buffer.java (address): New field.
+       * java/nio/DirectByteBufferImpl.java (address): Removed.
+       * java/nio/MappedByteBufferImpl.java (address): Likewise.
+       * java/nio/CharViewBufferImpl.java (CharViewBufferImpl):
+       Explicitly initialize Buffer.address if needed.
+       * java/nio/DoubleViewBufferImpl.java (DoubleViewBufferImpl): Likewise.
+       * java/nio/FloatViewBufferImpl.java (FloatViewBufferImpl): Likewise.
+       * java/nio/IntViewBufferImpl.java (IntViewBufferImpl): Likewise.
+       * java/nio/LongViewBufferImpl.java (LongViewBufferImpl): Likewise.
+       * java/nio/ShortViewBufferImpl.java (ShortViewBufferImpl): Likewise.
+       * jni.cc (_Jv_JNI_GetDirectBufferAddress): Don't assume buffer is a
+       DirectByteBufferImpl object.
+       (_Jv_JNI_GetDirectBufferCapacity): Likewise.
+       * testsuite/libjava.jni/directbuffer.c,
+       testsuite/libjava.jni/directbuffer.java,
+       testsuite/libjava.jni/directbuffer.out,
+       testsuite/libjava.jni/bytebuffer.c,
+       testsuite/libjava.jni/bytebuffer.java,
+       testsuite/libjava.jni/bytebuffer.out: New files.
+
 2005-01-05  Tom Tromey  <tromey@redhat.com>
 
        * java/util/zip/ZipEntry.java (setCompressedSize): Allow any
index f86829bf36251aeec91fbbb09b0897184383f473..dff7f60c7441c1fac9d1c35dd4a988a0731dbfa6 100644 (file)
@@ -38,12 +38,18 @@ exception statement from your version. */
 
 package java.nio;
 
+import gnu.gcj.RawData;
+
+/**
+ * @since 1.4
+ */
 public abstract class Buffer
 {
   int cap = 0;
   int limit = 0;
   int pos = 0;
   int mark = -1;
+  RawData address;
 
   /**
    * Creates a new Buffer.
index 3c02108debccc3f2d6920cb346c25500bcc151b8..2701983ead530c08eb10b8dc9c9fa19473e6b45c 100644 (file)
@@ -53,6 +53,8 @@ class CharViewBufferImpl extends CharBuffer
     this.offset = bb.position();
     this.readOnly = bb.isReadOnly();
     this.endian = bb.order();
+    if (bb.isDirect())
+      this.address = VMDirectByteBuffer.adjustAddress(bb.address, offset);
   }
   
   public CharViewBufferImpl (ByteBuffer bb, int offset, int capacity,
@@ -64,6 +66,8 @@ class CharViewBufferImpl extends CharBuffer
     this.offset = offset;
     this.readOnly = readOnly;
     this.endian = endian;
+    if (bb.isDirect())
+      this.address = VMDirectByteBuffer.adjustAddress(bb.address, offset);
   }
 
   /**
index d73f090cdb7ffbe31c9de0dff037146568734b31..c272bacbf72887d516ac9e4ded7fb2da015ba49b 100644 (file)
@@ -55,7 +55,6 @@ abstract class DirectByteBufferImpl extends ByteBuffer
     *                                 memory and should free it.
     */
   private final Object owner;
-  final RawData address;
 
   final static class ReadOnly extends DirectByteBufferImpl
   {
index d23b14ab472cc6b49997b299497543b6222007f3..276e275704528d9faec36e127a61bfc8d7bb3b44 100644 (file)
@@ -53,6 +53,8 @@ final class DoubleViewBufferImpl extends DoubleBuffer
     this.offset = bb.position();
     this.readOnly = bb.isReadOnly();
     this.endian = bb.order();
+    if (bb.isDirect())
+      this.address = VMDirectByteBuffer.adjustAddress(bb.address, offset);
   }
   
   public DoubleViewBufferImpl (ByteBuffer bb, int offset, int capacity,
@@ -64,6 +66,8 @@ final class DoubleViewBufferImpl extends DoubleBuffer
     this.offset = offset;
     this.readOnly = readOnly;
     this.endian = endian;
+    if (bb.isDirect())
+      this.address = VMDirectByteBuffer.adjustAddress(bb.address, offset);
   }
 
   /**
index 40b7339dd4eaa33b16a6b5290f04206aa912c77a..3dd0736fb491cc0afb181b043ee346a90319a0b6 100644 (file)
@@ -53,6 +53,8 @@ final class FloatViewBufferImpl extends FloatBuffer
     this.offset = bb.position();
     this.readOnly = bb.isReadOnly();
     this.endian = bb.order();
+    if (bb.isDirect())
+      this.address = VMDirectByteBuffer.adjustAddress(bb.address, offset);
   }
   
   public FloatViewBufferImpl (ByteBuffer bb, int offset, int capacity,
@@ -64,6 +66,8 @@ final class FloatViewBufferImpl extends FloatBuffer
     this.offset = offset;
     this.readOnly = readOnly;
     this.endian = endian;
+    if (bb.isDirect())
+      this.address = VMDirectByteBuffer.adjustAddress(bb.address, offset);
   }
 
   /**
index 1f3f9348aaee393e2c29fad8ce9001c0a19a8f79..ff8b27be1a48dce903d1c5863254e985377d8020 100644 (file)
@@ -53,6 +53,8 @@ final class IntViewBufferImpl extends IntBuffer
     this.offset = bb.position();
     this.readOnly = bb.isReadOnly();
     this.endian = bb.order();
+    if (bb.isDirect())
+      this.address = VMDirectByteBuffer.adjustAddress(bb.address, offset);
   }
   
   public IntViewBufferImpl (ByteBuffer bb, int offset, int capacity,
@@ -64,6 +66,8 @@ final class IntViewBufferImpl extends IntBuffer
     this.offset = offset;
     this.readOnly = readOnly;
     this.endian = endian;
+    if (bb.isDirect())
+      this.address = VMDirectByteBuffer.adjustAddress(bb.address, offset);
   }
 
   /**
index 8762578a7bf628ea706504c267a9de35909439f4..bfa64d0ad9a37a9352342cbf36cc7a26e9310d09 100644 (file)
@@ -53,6 +53,8 @@ final class LongViewBufferImpl extends LongBuffer
     this.offset = bb.position();
     this.readOnly = bb.isReadOnly();
     this.endian = bb.order();
+    if (bb.isDirect())
+      this.address = VMDirectByteBuffer.adjustAddress(bb.address, offset);
   }
   
   public LongViewBufferImpl (ByteBuffer bb, int offset, int capacity,
@@ -64,6 +66,8 @@ final class LongViewBufferImpl extends LongBuffer
     this.offset = offset;
     this.readOnly = readOnly;
     this.endian = endian;
+    if (bb.isDirect())
+      this.address = VMDirectByteBuffer.adjustAddress(bb.address, offset);
   }
 
   /**
index bc8ee80d5cdb71dd0412a4c86546a7f936adf1b3..63e0225bc511d244eee243125872bf439f589f55 100644 (file)
@@ -45,7 +45,6 @@ import java.io.IOException;
 final class MappedByteBufferImpl extends MappedByteBuffer
 {
   boolean readOnly;
-  RawData address;
 
   /** Posix uses this for the pointer returned by mmap;
    * Win32 uses it for the pointer returned by MapViewOfFile. */
index a9d086d212756b8230e6ef24d9e6008dfeedd500..acd6c233d07902159ad80dbf91b668343de164b4 100644 (file)
@@ -53,6 +53,8 @@ final class ShortViewBufferImpl extends ShortBuffer
     this.offset = bb.position();
     this.readOnly = bb.isReadOnly();
     this.endian = bb.order();
+    if (bb.isDirect())
+      this.address = VMDirectByteBuffer.adjustAddress(bb.address, offset);
   }
   
   public ShortViewBufferImpl (ByteBuffer bb, int offset, int capacity,
@@ -64,6 +66,8 @@ final class ShortViewBufferImpl extends ShortBuffer
     this.offset = offset;
     this.readOnly = readOnly;
     this.endian = endian;
+    if (bb.isDirect())
+      this.address = VMDirectByteBuffer.adjustAddress(bb.address, offset);
   }
 
   /**
index 6138334ebaffce829ed884d9cdda77c06b93d349..2f4c3e4fc84c966938fe109fe6b40d8ac01f1fa0 100644 (file)
@@ -41,6 +41,7 @@ details.  */
 #include <java/lang/ThreadGroup.h>
 #include <java/lang/Thread.h>
 #include <java/lang/IllegalAccessError.h>
+#include <java/nio/Buffer.h>
 #include <java/nio/DirectByteBufferImpl.h>
 #include <java/nio/DirectByteBufferImpl$ReadWrite.h>
 #include <java/util/IdentityHashMap.h>
@@ -1733,16 +1734,22 @@ static void * JNICALL
 _Jv_JNI_GetDirectBufferAddress (JNIEnv *, jobject buffer)
 {
   using namespace java::nio;
-  DirectByteBufferImpl* bb = static_cast<DirectByteBufferImpl *> (buffer);
-  return reinterpret_cast<void *> (bb->address);
+  if (! _Jv_IsInstanceOf (buffer, &Buffer::class$))
+    return NULL;
+  Buffer *tmp = static_cast<Buffer *> (buffer);
+  return reinterpret_cast<void *> (tmp->address);
 }
 
 static jlong JNICALL
 _Jv_JNI_GetDirectBufferCapacity (JNIEnv *, jobject buffer)
 {
   using namespace java::nio;
-  DirectByteBufferImpl* bb = static_cast<DirectByteBufferImpl *> (buffer);
-  return bb->capacity();
+  if (! _Jv_IsInstanceOf (buffer, &Buffer::class$))
+    return -1;
+  Buffer *tmp = static_cast<Buffer *> (buffer);
+  if (tmp->address == NULL)
+    return -1;
+  return tmp->capacity();
 }
 
 \f
diff --git a/libjava/testsuite/libjava.jni/bytebuffer.c b/libjava/testsuite/libjava.jni/bytebuffer.c
new file mode 100644 (file)
index 0000000..146c6a8
--- /dev/null
@@ -0,0 +1,62 @@
+#include "bytebuffer.h"
+
+static void
+test_buffer (JNIEnv *env, jobject buffer, const char *name)
+{
+  void *tmp = (*env)->GetDirectBufferAddress (env, buffer);
+
+  if (tmp == NULL)
+    printf ("PASS: address of %s\n", name);
+  else
+    printf ("FAIL: address of %s\n", name);
+
+  int tmplen = (*env)->GetDirectBufferCapacity (env, buffer);
+
+  if (tmplen == -1)
+    printf ("PASS: length of %s\n", name);
+  else
+    printf ("FAIL: length of %s\n", name);
+}
+
+JNIEXPORT void JNICALL
+Java_bytebuffer_testByteBuffer (JNIEnv *env, jclass k, jobject buffer)
+{
+  test_buffer (env, buffer, "java.nio.ByteBuffer");
+}
+
+JNIEXPORT void JNICALL
+Java_bytebuffer_testCharBuffer (JNIEnv *env, jclass k, jobject buffer)
+{
+  test_buffer (env, buffer, "java.nio.CharBuffer");
+}
+
+JNIEXPORT void JNICALL
+Java_bytebuffer_testDoubleBuffer (JNIEnv *env, jclass k, jobject buffer)
+{
+  test_buffer (env, buffer, "java.nio.DoubleBuffer");
+}
+
+JNIEXPORT void JNICALL
+Java_bytebuffer_testFloatBuffer (JNIEnv *env, jclass k, jobject buffer)
+{
+  test_buffer (env, buffer, "java.nio.FloatBuffer");
+}
+
+JNIEXPORT void JNICALL
+Java_bytebuffer_testIntBuffer (JNIEnv *env, jclass k, jobject buffer)
+{
+  test_buffer (env, buffer, "java.nio.IntBuffer");
+}
+
+JNIEXPORT void JNICALL
+Java_bytebuffer_testLongBuffer (JNIEnv *env, jclass k, jobject buffer)
+{
+  test_buffer (env, buffer, "java.nio.LongBuffer");
+}
+
+JNIEXPORT void JNICALL
+Java_bytebuffer_testShortBuffer (JNIEnv *env, jclass k, jobject buffer)
+{
+  test_buffer (env, buffer, "java.nio.ShortBuffer");
+}
+
diff --git a/libjava/testsuite/libjava.jni/bytebuffer.java b/libjava/testsuite/libjava.jni/bytebuffer.java
new file mode 100644 (file)
index 0000000..0e54112
--- /dev/null
@@ -0,0 +1,38 @@
+// Test to make sure JNI implementation catches exceptions.
+
+import java.nio.*;
+
+public class bytebuffer
+{
+  static
+  {
+    System.loadLibrary("bytebuffer");
+  }
+
+  public static native void testByteBuffer(ByteBuffer bb);
+  public static native void testCharBuffer(CharBuffer b);
+  public static native void testDoubleBuffer(DoubleBuffer b);
+  public static native void testFloatBuffer(FloatBuffer b);
+  public static native void testIntBuffer(IntBuffer b);
+  public static native void testLongBuffer(LongBuffer b);
+  public static native void testShortBuffer(ShortBuffer b);
+
+  public static void main(String[] args)
+  {
+    ByteBuffer bb = ByteBuffer.allocate(1024);
+    testByteBuffer(bb);
+    testCharBuffer(bb.asCharBuffer());
+    testDoubleBuffer(bb.asDoubleBuffer());
+    testFloatBuffer(bb.asFloatBuffer());
+    testIntBuffer(bb.asIntBuffer());
+    testLongBuffer(bb.asLongBuffer());
+    testShortBuffer(bb.asShortBuffer());
+
+    testCharBuffer(CharBuffer.allocate(1024));
+    testDoubleBuffer(DoubleBuffer.allocate(1024));
+    testFloatBuffer(FloatBuffer.allocate(1024));
+    testIntBuffer(IntBuffer.allocate(1024));
+    testLongBuffer(LongBuffer.allocate(1024));
+    testShortBuffer(ShortBuffer.allocate(1024));
+  }
+}
diff --git a/libjava/testsuite/libjava.jni/bytebuffer.out b/libjava/testsuite/libjava.jni/bytebuffer.out
new file mode 100644 (file)
index 0000000..5af92e2
--- /dev/null
@@ -0,0 +1,26 @@
+PASS: address of java.nio.ByteBuffer
+PASS: length of java.nio.ByteBuffer
+PASS: address of java.nio.CharBuffer
+PASS: length of java.nio.CharBuffer
+PASS: address of java.nio.DoubleBuffer
+PASS: length of java.nio.DoubleBuffer
+PASS: address of java.nio.FloatBuffer
+PASS: length of java.nio.FloatBuffer
+PASS: address of java.nio.IntBuffer
+PASS: length of java.nio.IntBuffer
+PASS: address of java.nio.LongBuffer
+PASS: length of java.nio.LongBuffer
+PASS: address of java.nio.ShortBuffer
+PASS: length of java.nio.ShortBuffer
+PASS: address of java.nio.CharBuffer
+PASS: length of java.nio.CharBuffer
+PASS: address of java.nio.DoubleBuffer
+PASS: length of java.nio.DoubleBuffer
+PASS: address of java.nio.FloatBuffer
+PASS: length of java.nio.FloatBuffer
+PASS: address of java.nio.IntBuffer
+PASS: length of java.nio.IntBuffer
+PASS: address of java.nio.LongBuffer
+PASS: length of java.nio.LongBuffer
+PASS: address of java.nio.ShortBuffer
+PASS: length of java.nio.ShortBuffer
diff --git a/libjava/testsuite/libjava.jni/directbuffer.c b/libjava/testsuite/libjava.jni/directbuffer.c
new file mode 100644 (file)
index 0000000..3d32aba
--- /dev/null
@@ -0,0 +1,75 @@
+#include <stdlib.h>
+
+#include "directbuffer.h"
+
+#define BUFFER_SIZE 1024
+
+static void *address;
+
+JNIEXPORT jobject JNICALL
+Java_directbuffer_createDirectByteBuffer (JNIEnv *env, jclass k)
+{
+  address = malloc (BUFFER_SIZE);
+  return (*env)->NewDirectByteBuffer (env, address, 1024);
+}
+
+static void
+test_buffer (JNIEnv *env, jobject buffer, const char *name, int len)
+{
+  void *tmp = (*env)->GetDirectBufferAddress (env, buffer);
+
+  if (address == tmp)
+    printf ("PASS: address of %s\n", name);
+  else
+    printf ("FAIL: address of %s\n", name);
+
+  int tmplen = (*env)->GetDirectBufferCapacity (env, buffer);
+
+  if (len == tmplen)
+    printf ("PASS: length of %s\n", name);
+  else
+    printf ("FAIL: length of %s\n", name);
+}
+
+JNIEXPORT void JNICALL
+Java_directbuffer_testDirectByteBuffer (JNIEnv *env, jclass k, jobject buffer, jint len)
+{
+  test_buffer (env, buffer, "direct java.nio.ByteBuffer", len);
+}
+
+JNIEXPORT void JNICALL
+Java_directbuffer_testCharBuffer (JNIEnv *env, jclass k, jobject buffer, jint len)
+{
+  test_buffer (env, buffer, "java.nio.CharBuffer view", len);
+}
+
+JNIEXPORT void JNICALL
+Java_directbuffer_testDoubleBuffer (JNIEnv *env, jclass k, jobject buffer, jint len)
+{
+  test_buffer (env, buffer, "java.nio.DoubleBuffer view", len);
+}
+
+JNIEXPORT void JNICALL
+Java_directbuffer_testFloatBuffer (JNIEnv *env, jclass k, jobject buffer, jint len)
+{
+  test_buffer (env, buffer, "java.nio.FloatBuffer view", len);
+}
+
+JNIEXPORT void JNICALL
+Java_directbuffer_testIntBuffer (JNIEnv *env, jclass k, jobject buffer, jint len)
+{
+  test_buffer (env, buffer, "java.nio.IntBuffer view", len);
+}
+
+JNIEXPORT void JNICALL
+Java_directbuffer_testLongBuffer (JNIEnv *env, jclass k, jobject buffer, jint len)
+{
+  test_buffer (env, buffer, "java.nio.LongBuffer view", len);
+}
+
+JNIEXPORT void JNICALL
+Java_directbuffer_testShortBuffer (JNIEnv *env, jclass k, jobject buffer, jint len)
+{
+  test_buffer (env, buffer, "java.nio.ShortBuffer view", len);
+}
+
diff --git a/libjava/testsuite/libjava.jni/directbuffer.java b/libjava/testsuite/libjava.jni/directbuffer.java
new file mode 100644 (file)
index 0000000..ee844b9
--- /dev/null
@@ -0,0 +1,40 @@
+// Test to make sure JNI implementation catches exceptions.
+
+import java.nio.*;
+
+public class directbuffer
+{
+  static
+  {
+    System.loadLibrary("directbuffer");
+  }
+
+  public static native ByteBuffer createDirectByteBuffer();
+  
+  public static native void testDirectByteBuffer(ByteBuffer bb, int len);
+  public static native void testCharBuffer(CharBuffer b, int len);
+  public static native void testDoubleBuffer(DoubleBuffer b, int len);
+  public static native void testFloatBuffer(FloatBuffer b, int len);
+  public static native void testIntBuffer(IntBuffer b, int len);
+  public static native void testLongBuffer(LongBuffer b, int len);
+  public static native void testShortBuffer(ShortBuffer b, int len);
+
+  public static void main(String[] args)
+  {
+    ByteBuffer bb = createDirectByteBuffer();
+    CharBuffer cb = bb.asCharBuffer();
+    DoubleBuffer db = bb.asDoubleBuffer();
+    FloatBuffer fb = bb.asFloatBuffer();
+    IntBuffer ib = bb.asIntBuffer();
+    LongBuffer lb = bb.asLongBuffer();
+    ShortBuffer sb = bb.asShortBuffer();
+
+    testDirectByteBuffer(bb, 1024);
+    testCharBuffer(cb, 512);
+    testDoubleBuffer(db, 128);
+    testFloatBuffer(fb, 256);
+    testIntBuffer(ib, 256);
+    testLongBuffer(lb, 128);
+    testShortBuffer(sb, 512);
+  }
+}
diff --git a/libjava/testsuite/libjava.jni/directbuffer.out b/libjava/testsuite/libjava.jni/directbuffer.out
new file mode 100644 (file)
index 0000000..c1404b9
--- /dev/null
@@ -0,0 +1,14 @@
+PASS: address of direct java.nio.ByteBuffer
+PASS: length of direct java.nio.ByteBuffer
+PASS: address of java.nio.CharBuffer view
+PASS: length of java.nio.CharBuffer view
+PASS: address of java.nio.DoubleBuffer view
+PASS: length of java.nio.DoubleBuffer view
+PASS: address of java.nio.FloatBuffer view
+PASS: length of java.nio.FloatBuffer view
+PASS: address of java.nio.IntBuffer view
+PASS: length of java.nio.IntBuffer view
+PASS: address of java.nio.LongBuffer view
+PASS: length of java.nio.LongBuffer view
+PASS: address of java.nio.ShortBuffer view
+PASS: length of java.nio.ShortBuffer view