2010-04-20 4 views
16

Ho un metodo:JNI - metodo nativo con il parametro ByteBuffer

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

Generato da javah C/C++ intestazione di questo metodo è:

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

Come posso ottenere un array di dati da jobject (che è un'istanza ByteBuffer)?

risposta

25

Supponendo di destinare il ByteBuffer utilizzando ByteBuffer.allocateDirect() Use GetDirectByteBufferAddress

jbyte* bbuf_in; jbyte* bbuf_out; 

bbuf_in = (*env)->GetDirectBufferAddress(env, buf1); 
bbuf_out= (*env)->GetDirectBufferAddress(env, buf2); 
+1

Il tipo di ritorno di GetDirectBufferAddress è void *, è necessario lanciare ad un jbyte *: bbuf_in = (jbyte *) (env *) -> GetDirectBufferAddress (env, buf1); // C bbuf_in = (jbyte *) env-> GetDirectBufferAddress (buf1); // C++ –

+1

@ Error454 Questo è C, non è necessario eseguire il cast 'void *' s –

+0

È necessario eseguire il cast se si dispone di -Wpedantic –