Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the pointer of a Java ByteBuffer though JNI

How can I get a pointer to the inner array of a Java ByteBuffer?

JNIEXPORT void JNICALL test(JNIEnv *env, jobject thiso) {
    jclass cls = env->FindClass("java/nio/ByteBuffer");
    jmethodID aloc = env->GetStaticMethodID(cls, "allocateDirect", "(I)Ljava/nio/ByteBuffer;");
    jobject obj = env->CallStaticObjectMethod(cls, aloc, 1000);
}

PS: I'm doing that to share the memory used by Java and C++.

like image 876
Marcos Vasconcelos Avatar asked Nov 03 '11 19:11

Marcos Vasconcelos


1 Answers

void * data = env->GetDirectBufferAddress(obj);

The ByteBuffer must be a direct one for this to work.

like image 72
Alan Stokes Avatar answered Oct 30 '22 11:10

Alan Stokes