Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4528170

(spec) GetDirectBufferCapacity does not work on some unaligned view buffers

XMLWordPrintable

    • b69
    • generic, sparc
    • generic, solaris_8

      Name: viR10068 Date: 11/16/2001


      The current specification at http://java.sun.com/j2se/1.4/docs/guide/jni/jni-14.html
      for function GetDirectBufferCapacity says:
      "RETURNS

            Returns the capacity in bytes of the memory region associated with the
            buffer. Returns -1 if the given object is not a direct
            java.nio.Buffer, or if JNI access to direct buffers is not supported
            by this virtual machine."

      However the call of this function for misaligned short (or int) buffer returns
      -1 instead of buffer capacity. Note, that for aligned buffer
      GetDirectBufferCapacity returns positive number as expected. See log for details.

      Please, change the JVM behavior or clarify the specification.

      Log:
      %java -version && javac -d . gdbc00101m1.java && cc -G -KPIC -o libjckjni.so -Iinclude
      gdbc00101m1.c && java -Xfuture javasoft.sqe.tests.vm.jni.gdbc001.gdbc00101m1.gdbc00101m1
      java version "1.4.0-rc"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-rc-b86)
      Java HotSpot(TM) Client VM (build 1.4.0-rc-b86, mixed mode)
      misaligned buffer
      shortBuf.capacity() is 2
      getDirectBufferCapacity(shortBuf) is -1
      aligned buffer
      shortBuf1.capacity() is 2
      getDirectBufferCapacity(shortBuf1) is 2
      %

      Sources:
      =============================== gdbc00101m1.java
      package javasoft.sqe.tests.vm.jni.gdbc001.gdbc00101m1;

      import java.io.PrintStream;
      import java.nio.ByteBuffer;
      import java.nio.ShortBuffer;
      import java.nio.IntBuffer;

      public class gdbc00101m1 {

          private static native long getDirectBufferCapacity(Object buf);

          public static int testChecks(PrintStream out) {

              boolean res = false;

              // Positive tests
              try {
                  ByteBuffer buf = ByteBuffer.allocateDirect(8);

                  out.println("misaligned buffer");
                  buf.position(3);
                  ShortBuffer shortBuf = buf.asShortBuffer();
                  out.println("shortBuf.capacity() is " + shortBuf.capacity());
                  out.println("getDirectBufferCapacity(shortBuf) is " +
      getDirectBufferCapacity(shortBuf));
                  
                  out.println("aligned buffer");
                  buf.position(4);
                  ShortBuffer shortBuf1 = buf.asShortBuffer();
                  out.println("shortBuf1.capacity() is " + shortBuf1.capacity());
                  out.println("getDirectBufferCapacity(shortBuf1) is " +
      getDirectBufferCapacity(shortBuf1));
              } catch (Exception e) {
                  e.printStackTrace();
                  out.println("Unexpected exception: " + e);
                  return 2/*STATUS_FAILED*/;
              }
              return 0/*STATUS_PASSED*/;
          }

          static String loadLibraryStatus = "";
          static {
              if(!loadLib("gdbc00101m1"))
                  loadLib("jckjni");
          }

          static boolean loadLib(String libName){
              try {
                  System.loadLibrary(libName);
                  loadLibraryStatus = null;
                  return true;
              } catch (SecurityException e) {
                  loadLibraryStatus += "loadLibrary(\"" + libName + "\") throws: " + e + "\n";
              } catch (UnsatisfiedLinkError e) {
                  loadLibraryStatus += "loadLibrary(\"" + libName + "\") throws: " + e + "\n";
              }
              return false;
          }

          public static int run(String argv[], PrintStream out) {

              if (loadLibraryStatus!=null) {
                  out.println("Library loading:\n" + loadLibraryStatus);
              }
              return testChecks(out);
          }

          public static void main(String argv[]) {
              System.exit(run(argv, System.out) + 95/*STATUS_TEMP*/);
          }
      }
      =============================== gdba00101m1.c
      #include <stdlib.h>
      #include <string.h>
      #include <stdio.h>
      #include "jckjni.h"

      #ifdef __cplusplus
      extern "C" {
      #endif

      #ifndef JNI_ENV_ARG

      #ifdef __cplusplus
      #define JNI_ENV_ARG(x, y) y
      #define JNI_ENV_PTR(x) x
      #else
      #define JNI_ENV_ARG(x,y) x, y
      #define JNI_ENV_PTR(x) (*x)
      #endif

      #endif


      JNIEXPORT jlong JNICALL
      Java_javasoft_sqe_tests_vm_jni_gdbc001_gdbc00101m1_gdbc00101m1_getDirectBufferCapacity
      (JNIEnv *env, jobject obj, jobject buf) {
      return JNI_ENV_PTR(env)->GetDirectBufferCapacity(JNI_ENV_ARG(env,buf));
      }

      #ifdef __cplusplus
      }
      #endif
      ==================================================================

      ======================================================================

            iris Iris Clark
            vivsunw Viv Viv (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: