JNI - 带有 ByteBuffer 参数的本机方法

2022-01-25 00:00:00 c java java-native-interface bytebuffer

我有一个方法:

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

这个方法的javah C/C++头生成的是:

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

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

如何从 jobject 获取数据数组(即 ByteBuffer 实例)?

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

推荐答案

假设你使用 ByteBuffer.allocateDirect() 分配了 ByteBuffer您可以使用 GetDirectBufferAddress

Assuming you allocated the ByteBuffer using ByteBuffer.allocateDirect() you can use GetDirectBufferAddress

jbyte* bbuf_in;  jbyte* bbuf_out;

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

相关文章