GetByteArrayElements
官方解释为
获取数组内容,直到ReleaseByteArrayElements()被调用。
言下之意,就是在 ReleaseByteArrayElements 被调用之前 这个数据一直有效。
所以必须伴随有 ReleaseByteArrayElements 进行使用,是否会导致指针不被释放。
GetByteArrayRegion
官方解释为
从缓冲区中获取数组的数据。
其中 ReleaseByteArrayElements 官方释义
拷贝数组到第一个参数,并且释放第二个指针参数。
所以对应的 GetByteArrayElements 是申请内存 并拷贝数据后返回。
示例一 使用 GetByteArrayElements
#include "jni_main.h"
static unsigned char* g_buf_in = NULL;
static unsigned char* g_buf_out = NULL;
int jni_debug_bytes(JNIEnv *jenv, jobject thiz,
jbyteArray buf_in, jbyteArray buf_out){
ehome_printf("[%s]GetByteArrayElements\n", __FUNCTION__);
int len_arr = 0;
g_buf_in = (uint8_t *)jenv->GetByteArrayElements(buf_in, NULL);
if(g_buf_out){
for(int i=0; i<10; i++){
ehome_printf("g_buf_out : %c", g_buf_out[i]);
}else{
ehome_printf("[%s]g_buf_out:NULL\n", __FUNCTION__);
g_buf_out = (unsigned char*)malloc(1024);
for(int i=0; i<50; i++){
g_buf_out[i] = i+20;
len_arr = jenv->GetArrayLength(buf_in);
ehome_printf("[%s]buf_in len:%d\n", __FUNCTION__, len_arr);
len_arr = jenv->GetArrayLength(buf_out);
ehome_printf("[%s]buf_out len:%d\n", __FUNCTION__, len_arr);
for(int i=0; i<10; i++){
ehome_printf("%c", g_buf_in[i]);
jenv->ReleaseByteArrayElements(buf_in, (int8_t*)g_buf_in, 0);
jenv->ReleaseByteArrayElements(buf_out, (int8_t*)g_buf_out, 0);
return 1;
示例二 使用 GetByteArrayRegion【推荐使用】
#include "jni_main.h"
static unsigned char* g_buf_in = NULL;
static unsigned char* g_buf_out = NULL;
static void init_buffer(){
g_buf_in = (unsigned char*)malloc(1024);
g_buf_out = (unsigned char*)malloc(1024);
memset(g_buf_out, 0, 1024);
for(int i=0; i<50; i++){
g_buf_out[i] = i;
int jni_debug_bytes(JNIEnv *jenv, jobject thiz,
jbyteArray buf_in, jbyteArray buf_out){
if(NULL == g_buf_in){
init_buffer();
int len_arr = 0;
jenv->GetByteArrayRegion(buf_in, 0, 100, (jbyte*)g_buf_in);
len_arr = jenv->GetArrayLength(buf_in);
ehome_printf("[%s]buf_in len:%d\n", __FUNCTION__, len_arr);
len_arr = jenv->GetArrayLength(buf_out);
ehome_printf("[%s]buf_out len:%d\n", __FUNCTION__, len_arr);
for(int i=0; i<10; i++){
ehome_printf("%c", g_buf_in[i]);
jenv->SetByteArrayRegion(buf_out, 0, 100,(jbyte *)g_buf_out);
return 1;
上层 JAVA 使用调用
public native int jni_debug_bytes(byte[] buf_in, byte[] buf_out);
private void on_set_byte_debug() {
byte[] buf_in = new byte[100];
byte[] buf_out = new byte[120];
for(byte i=0; i<100; i++) {
buf_in[i] = (byte) ('A'+ i);
buf_out[i] = (byte) ('a'+ i);
jniclass.jni_debug_bytes(buf_in, buf_out);
for(byte i=0; i<10; i++) {
Log.i("MainAcitivty", "buf_out : " + buf_out[i]);
运行后会把 A B C D… J 传入
得到结果为 20 21 22 23 … 29
附其中的基本类型
typedef | unsigned char | jboolean |
typedef | signed char | jbyte |
typedef | unsigned short | jchar |
typedef | short | jshort |
typedef | int | jint |
typedef | int | jsize |
GetByteArrayElements官方解释为获取数组内容,直到ReleaseByteArrayElements()被调用。言下之意,就是在 ReleaseByteArrayElements 被调用之前 这个数据一直有效。所以必须伴随有 ReleaseByteArrayElements 进行使用,是否会导致指针不被释放。GetByteArrayRegion官方解释为从缓冲区中获取数组的数据。其中 ReleaseByteArrayElements 官方释义拷贝数组到第一个参数,并且释放第
GetArrayElements
GetArrayElements 和ReleaseArrayElements成对使用
jbyte* data = env->GetByteArrayElements(array, NULL);
if (data != NULL) {
memcpy(buffer, data, len);
env->ReleaseByteArrayElements(array, data, JNI_ABORT);
GetArrayReg
如果你是为了让Java调用C语言,使用复杂的JNI大可不必,可以选择更简单便捷安全的方式——JNA。当然,如果用C调用Java,那就只能用JNI了。JNI使用复杂,尤其是提供的函数使用时一定要注意内存问题。因为Java层申请内存后是没有内存释放的,完全依赖java虚拟机来释放,而C层不然,必须时刻谨记申请的内存一定要及时释放。
JNI函数大全可参考:https://www.c...
只有C++时才用
在C中的写法应该是
jbyte * arr = (*env)-> GetByteArrayElements(env,jarr, NULL);
--------------------------------------------
GetByteArrayElements的官方解释:
Returns the body of the primitive array. The result is valid until
the corresponding ReleaseArrayElements function is
called.Since the returned array may be a copy of t
今天想看看android 虚拟机 GetByteArrayElements 的实现,一直没发现。分析才知,它被藏在宏里面了。
PRIMITIVE_ARRAY_FUNCTIONS(jbyte, Byte); =》展开了宏包含了一系列函数
PRIMITIVE_ARRAY_FUNCTIONS 宏定义是
#define PRIMITIVE_ARRAY_FUNCTIONS(_ctype, _jname
本章为 JNI 函数提供参考信息。其中列出了全部 JNI 函数,同时也给出了 JNI 函数表的准确布局。
注意:“必须”一词用于约束 JNI 编程人员。例如,当说明某个 JNI 函数必须接收非空对象时,就应确保不要向该 JNI 函数传递 NULL。这时,JNI 实现将无需在该 JNI 函数中执行 NULL 指针检查。
本章的部分资料改编自 Netscap
翻译原文来自:http://developer.android.com/intl/zh-cn/training/articles/perf-jni.html
JNI全称是Java Native Interface, 它是一种使用java语言和原生C/C++语言相互调用,混合编程的方法. 它支持从动态链接库中加载代码, 并能使用C/C++的高效的特性
如果你之前对这个还不熟悉, 完整的读一遍
错误request for member 'GetByteArrayElements' in something is not a structure or union
或错误too few arguements to function (*env)->GetByteArrayElements
原因:在C的定义中,env是一个两级指针,而在C++的定义中,env是个...
在开发中常常会遇到从Java层传递数据到JNI层,然后在JNI拿到数据后就可以用C语言进行操作了,操作完数据后通常还需要把处理后的数据传回Java层。下面分别进行小结。从Java层传到JNI层
使用GetByteArrayRegion的方式。
该方法的本质是将Java端数组数据拷贝到本地的数组中,所以在JNI对数据修改后Java端的数据并没有改变。
使用GetPrimitiveArrayCriti