用从GetDirectBufferAddress在JNI多种数据

用从GetDirectBufferAddress在JNI多种数据

本文介绍了我如何调用从GetDirectBufferAddress在JNI多种数据类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过本地方法获得的ByteBuffer

的ByteBuffer 始于3 INT s,则仅包含增加一倍。
第三 INT 告诉我,跟随双打的数量。

我能读前三 INT 秒。

为什么code崩溃当我尝试读取双打?

相关code率先拿到三个整数:

  JNIEXPORT无效JNICALL测试(JNIEnv的* ENV,jobject的ByteBuffer)
{
   为int *数据=(INT *)env-> GetDirectBufferAddress(ByteBuffer的);
}

相关code,以获得剩下的双打:

 双*休息=(双*)env-> GetDirectBufferAddress(ByteBuffer的+ 12);


解决方案

在您的贴code,你是调用此:

 双*休息=(双*)env-> GetDirectBufferAddress(ByteBuffer的+ 12);

这增加了12到的ByteBuffer jobject ,这不是一个数字。

GetDirectBufferAddress()返回的地址;由于前3 INT 是4个字节,我相信你是正确添加12,但你是不是在正确的地方将它添加

你是什么意思大概做是这样的:

 双*休息=(双*)((字符*)env-> GetDirectBufferAddress(ByteBuffer的)+ 12);

有关你的整体code,获得最初的3 INT 和其余双击 S,尝试类似的措施:

  void *的地址= env-> GetDirectBufferAddress(ByteBuffer的);
为int * firstInt =(INT *)地址;
为int * secondInt =(INT *)地址+ 1;
为int * doubleCount =(INT *)地址+ 2;
双*休息=(双*)((字符*)地址+ 3 * sizeof的(INT));//你说的第三INT再presents以下双打的数量
的for(int i = 0; I< doubleCount;我++){
    双D = *休息+我; //或休息[I]
    //做一些与D双
}

I get a bytebuffer via the native methods.

The bytebuffer starts with 3 ints, then contains only doubles.The third int tells me the number of doubles that follow.

I am able to read the first three ints.

Why is the code crashing when I try to read the doubles?

Relevant code to get the first three integers:

JNIEXPORT void JNICALL test(JNIEnv *env, jobject bytebuffer)
{
   int * data = (int *)env->GetDirectBufferAddress(bytebuffer);
}

Relevant code to get the remaining doubles:

double * rest = (double *)env->GetDirectBufferAddress(bytebuffer + 12);
解决方案

In your posted code, you are calling this:

double * rest = (double *)env->GetDirectBufferAddress(bytebuffer + 12);

This adds 12 to the bytebuffer jobject, which not a number.

GetDirectBufferAddress() returns an address; since the first 3 int are 4 bytes each, I believe you are correctly adding 12, but you are not adding it in the right place.

What you probably meant to do is this:

double * rest = (double *)((char *)env->GetDirectBufferAddress(bytebuffer) + 12);

For your overall code, to get the initial three ints and the remaining doubles, try something similar to this:

void * address = env->GetDirectBufferAddress(bytebuffer);
int * firstInt = (int *)address;
int * secondInt = (int *)address + 1;
int * doubleCount = (int *)address + 2;
double * rest = (double *)((char *)address + 3 * sizeof(int));

// you said the third int represents the number of doubles following
for (int i = 0; i < doubleCount; i++) {
    double d = *rest + i; // or rest[i]
    // do something with the d double
}

这篇关于我如何调用从GetDirectBufferAddress在JNI多种数据类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 06:10