有没有办法通过JNI获得一个Java数组的直接指针? [英] Is there any way to get a direct pointer to a Java array via JNI?

查看:526
本文介绍了有没有办法通过JNI获得一个Java数组的直接指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要通过JNI获取一个包含Java数组的直接内存地址的指针,而不调用某种复制(即直接访问)。

I need to get a pointer containing the direct memory address of a Java array, via JNI, without invoking some sort of copying (ie direct access).

GetArrayElements返回指向复制数组的指针 - 我需要能够直接从本地层修改Java层上的int []。

GetArrayElements returns a pointer to a copied array - I need to be able to modify an int[] on the Java layer directly from a the native layer.

jintArray 投射到 int * 内存地址成功,但我不知道如果这是特别稳定...?

Casting from a jintArray to an int* returns the memory address successfully, but I'm not sure if this is particularly stable...?

我可以在这里做什么吗?

Is there anything I can do here...?

推荐答案

您可以使用直接内存(使用本地字节顺序)的IntBuffer。在JNI中,您可以使用地址作为指针。在Java中,你必须使用get()和put()。

You can use an IntBuffer using direct memory (with native byte order). In JNI you can use the address as a pointer. In Java you have to use the get() and put().

这篇关于有没有办法通过JNI获得一个Java数组的直接指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆