views:

344

answers:

2

I've got a method:

public native void doSomething(ByteBuffer in, ByteBuffer out);

Generated by javah C/C++ header of this method is:

JNIEXPORT void JNICALL Java__MyClass_doSomething (JNIEnv *, jobject, jobject, jobject, jint, jint);

How can I get a data array from jobject (that is a ByteBuffer instance) ?

+2  A: 

Assuming you allocated the ByteBuffer using ByteBuffer.allocateDirect() Use GetDirectByteBufferAddress

jbyte* bbuf_in;  jbyte* bbuf_out;

bbuf_in = (*env)->GetDirectBufferAddress(env, buf1);  
bbuf_out= (*env)->GetDirectBufferAddress(env, buf2); 
stacker
A: 

You can also access the values individually via Java methods but this might be slow:

JNIEXPORT void JNICALL Java_Foo_doSomething
  (JNIEnv * env, jobject obj, jobject in, jobject out)
{
  jclass bbclass = (*env)->FindClass(env, "java/nio/ByteBuffer");
  jmethodID getMethod = (*env)->GetMethodID(env, bbclass, "get", "(I)B");
  jbyte b0 = (*env)->CallByteMethod(env, bin, getMethod, 0);
  jbyte b1 = (*env)->CallByteMethod(env, bin, getMethod, 1);
  //...
}
maerics