Using GetDirectBufferAddress from JNI

malat :

I am trying to understand how to use GetDirectBufferAddress from the JNI layer. To understand I've build a very simple example:

public class my_image_info {
  static {
      System.loadLibrary("my_jni");
  }
  private java.nio.ByteBuffer image_info_bb;
  native static void initc( java.nio.ByteBuffer bb );
  my_image_info()
    {
    image_info_bb = java.nio.ByteBuffer.allocateDirect( 5 * 4 );
    initc( image_info_bb );
    }
  public java.nio.ByteBuffer getBB() {
    return image_info_bb;
  }
  static public void main(String argv[]) {
    my_image_info fii = new my_image_info();
    java.nio.ByteBuffer bb = fii.getBB();
    System.out.println("1: " + bb.getInt(0));
    System.out.println("2: " + bb.getInt(4));
    System.out.println("3: " + bb.getInt(8));
    System.out.println("4: " + bb.getInt(12));
    System.out.println("5: " + bb.getInt(16));
}

And then from the native JNI layer:

JNIEXPORT void JNICALL Java_my_1image_1info_initc
  (JNIEnv *env, jclass cls, jobject jobj)
{
  int *iBuf = (*env)->GetDirectBufferAddress(env, jobj);
  iBuf[0] = -2;
  iBuf[1] = -1;
  iBuf[2] = 0;
  iBuf[3] = 1;
  iBuf[4] = 2;
}

If I run this example over here (debian/linux wheezy amd64) with openjdk :

$ java -version
java version "1.6.0_34"
OpenJDK Runtime Environment (IcedTea6 1.13.6) (6b34-1.13.6-1~deb7u1)
OpenJDK 64-Bit Server VM (build 23.25-b01, mixed mode)

Here is what I see:

1: -16777217
2: -1
3: 0
4: 16777216
5: 33554432

I understand the values for index 2 & 3. But all other values do not make any sense to me, I would have expected something like:

1: -2
2: -1
3: 0
4: 1
5: 2

What did I misunderstood from the ByteBuffer usage in JNI ?

malat :

What I missed from the documentation is that by default java.nio.ByteBuffer is actually using BIG_ENDIAN byte order. Which explains the behavior I was seeing on my LITTLE_ENDIAN system. See ref here.

My code now reads as:

image_info_bb = java.nio.ByteBuffer.allocateDirect( 5 * 4 );
image_info_bb.order( java.nio.ByteOrder.LITTLE_ENDIAN );

It appears that by default it is always BIG_ENDIAN, and no effort has been made so far to provide an API for LITTLE_ENDIAN, as explained in the bug report here (JDK-5043362 : (bf) NewDirectByteBuffer always has order ByteOrder.BIG_ENDIAN).


Documentation has been updated recently to reflect that:

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

JNI GetDirectBufferAddress vs. ByteBuffer.array()

From Java

Android: Using JNI from NativeActivity

From Java

Passing a pointer from JNI to Java using a long

From Java

Using jvm.h from JNI

From Dev

Call fortran dll from java using JNI

From Dev

Unable to make JNI call from c++ to java in android lollipop using jni

From Java

didDiscoverCharacteristics not getting called when using CoreBluetooth from Java via JNI

From Java

Call static Java method from separate thread using JNI

From Dev

How to call a function with arguments in C++ from JAVA using JNI?

From Java

Passing strings from java to c++ using JNI

From Java

Copying Data from Java to C++ Objects Array Using JNI

From Java

Using a java socket from JNI / C++ code

From Dev

Passing float[] from C++ to Java using JNI

From Dev

Keeping a native object in memory between calls from Java using JNI

From Dev

Return list<unsigned char*> from C++ to Java using JNI

From Dev

Android ffmpeg: create video from sequence of images using jni

From Dev

How to preserve class variables accessed from JNI when using ProGuard

From Dev

Calling a Java Method from the native code using jni

From Dev

Accessing field using JNI

From Dev

Error while calling C# from java using jni4net from 64 bit Os

From Java

Using JNI to load another JNI library?

From Java

JNI: From C code to Java and JNI

From Dev

Call a JNI method from another JNI method

From Dev

GetDirectBufferAddress: Address out by 4

From Dev

Efficiently passing GPB serialized data from Java to C++ using JNI

From Dev

Calling java function from c++ using jni: Failed to find static method id

From Dev

Can you create a new JVM in a c++ function called from java using JNI?

From Java

Xlib: Adding a window to the save-set using XAddToChangeSet does not work from Java/JNI

From Java

Getting text data from C++ using JNI through std::ostream into Java

Related Related

  1. 1

    JNI GetDirectBufferAddress vs. ByteBuffer.array()

  2. 2

    Android: Using JNI from NativeActivity

  3. 3

    Passing a pointer from JNI to Java using a long

  4. 4

    Using jvm.h from JNI

  5. 5

    Call fortran dll from java using JNI

  6. 6

    Unable to make JNI call from c++ to java in android lollipop using jni

  7. 7

    didDiscoverCharacteristics not getting called when using CoreBluetooth from Java via JNI

  8. 8

    Call static Java method from separate thread using JNI

  9. 9

    How to call a function with arguments in C++ from JAVA using JNI?

  10. 10

    Passing strings from java to c++ using JNI

  11. 11

    Copying Data from Java to C++ Objects Array Using JNI

  12. 12

    Using a java socket from JNI / C++ code

  13. 13

    Passing float[] from C++ to Java using JNI

  14. 14

    Keeping a native object in memory between calls from Java using JNI

  15. 15

    Return list<unsigned char*> from C++ to Java using JNI

  16. 16

    Android ffmpeg: create video from sequence of images using jni

  17. 17

    How to preserve class variables accessed from JNI when using ProGuard

  18. 18

    Calling a Java Method from the native code using jni

  19. 19

    Accessing field using JNI

  20. 20

    Error while calling C# from java using jni4net from 64 bit Os

  21. 21

    Using JNI to load another JNI library?

  22. 22

    JNI: From C code to Java and JNI

  23. 23

    Call a JNI method from another JNI method

  24. 24

    GetDirectBufferAddress: Address out by 4

  25. 25

    Efficiently passing GPB serialized data from Java to C++ using JNI

  26. 26

    Calling java function from c++ using jni: Failed to find static method id

  27. 27

    Can you create a new JVM in a c++ function called from java using JNI?

  28. 28

    Xlib: Adding a window to the save-set using XAddToChangeSet does not work from Java/JNI

  29. 29

    Getting text data from C++ using JNI through std::ostream into Java

HotTag

Archive