GetByteArrayElements和GetByteArrayRegion的使用示例

GetByteArrayElements
官方解释为
获取数组内容,直到ReleaseByteArrayElements()被调用。
言下之意,就是在 ReleaseByteArrayElements 被调用之前 这个数据一直有效。
所以必须伴随有 ReleaseByteArrayElements 进行使用,是否会导致指针不被释放。

GetByteArrayRegion
官方解释为
从缓冲区中获取数组的数据。
其中 ReleaseByteArrayElements 官方释义
拷贝数组到第一个参数,并且释放第二个指针参数。
所以对应的 GetByteArrayElements 是申请内存 并拷贝数据后返回。

示例一 使用 GetByteArrayElements

// demo.cpp
#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);
	// g_buf_out 可以使用 GetByteArrayElements 进行初始化,也可以在后面使用 malloc 进行初始化
	//g_buf_out = (uint8_t *)jenv->GetByteArrayElements(buf_out, 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);
	// buf_in len: 100
	ehome_printf("[%s]buf_in len:%d\n", __FUNCTION__, len_arr);
	
	len_arr = jenv->GetArrayLength(buf_out);
	// buf_in len: 120
	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]);
	}

	// ReleaseByteArrayElements:
	// copy back the content and free the elems[g_buf_in] buffer
	jenv->ReleaseByteArrayElements(buf_in, (int8_t*)g_buf_in, 0);
	jenv->ReleaseByteArrayElements(buf_out, (int8_t*)g_buf_out, 0);
	
	return 1;
}

示例二 使用 GetByteArrayRegion【推荐使用】

// demo.cpp
#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);
	// len_arr: 100
	ehome_printf("[%s]buf_in len:%d\n", __FUNCTION__, len_arr);
	
	len_arr = jenv->GetArrayLength(buf_out);
	// len_arr: 120
	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 使用调用


// C 声明 {"jni_debug_bytes","([B[B)I",(void *) jni_debug_bytes},
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

附其中的基本类型

typedefunsigned charjboolean
typedefsigned charjbyte
typedefunsigned shortjchar
typedefshortjshort
typedefintjint
typedefintjsize
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值