10

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++.

Jonas
  • 121,568
  • 97
  • 310
  • 388
Marcos Vasconcelos
  • 18,136
  • 30
  • 106
  • 167

2 Answers2

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

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

Alan Stokes
  • 18,815
  • 3
  • 45
  • 64
0

If you're trying to return the address of the first element within m_buffer, then you can just do:

return m_buffer;

..or:

return &m_buffer[0]

John Dibling
  • 99,718
  • 31
  • 186
  • 324