java - 从 jni 返回字节缓冲区是复制还是引用?

标签 java android java-native-interface

我正在将字节缓冲区从 jni 返回到 java 层。

    getData(JNIENV, *env, jobject obj ) {

    pthread_mutex_lock(&mutexA);


    while(dataAvailable == 0){
        pthread_cond_wait (&cond, &MutexA);
    }

    dataAvailable = 0;

    pthread_mutex_unlock(&MutexA);

    return jnv->NewDirectByteBuffer(DataPointer, dataSize);
}

来自 Java:

  while (1) {
     ByteBuffer byteBuffer = getData();

  }

这个 byteBuffer 是否引用了 DataPointer,或者,它是否复制到 java 层中的 'byteBuffer' 变量?

最佳答案

Java guide NewDirectByteBuffer

Allocates and returns a direct java.nio.ByteBuffer referring to the block of memory starting at the memory address address and extending capacity bytes.

Native code that calls this function and returns the resulting byte-buffer object to Java-level code should ensure that the buffer refers to a valid region of memory that is accessible for reading and, if appropriate, writing. An attempt to access an invalid memory location from Java code will either return an arbitrary value, have no visible effect, or cause an unspecified exception to be thrown.

关于java - 从 jni 返回字节缓冲区是复制还是引用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34460860/

相关文章:

android - 使用 TimePicker 或 NumberPicker 查看对话框首选项时旋转 Android 设备会导致应用程序崩溃

c++ - 使用 JNI 在 C++ 中调用方法?

java - 在 Valgrind 下的 Sun VM 中运行 JNI 应用程序

java - Eclipse 插件 - 刷新 eclipse 项目中的链接文件夹

java - 如何使用 jackson 将 Json 数组写入文件

java - 自定义 JScrollPane 中的 JRadioButtons 未出现?

java - 使用 jni 从 native 代码调用 Java 方法

java - 如何使用调制解调器或网关从 java Web 应用程序发送短信?

android - 使用 Emacs 代替 Eclipse 进行 Android 开发有什么优势?

java - 长时间运行后,Volley 导致服务崩溃应用程序