How do I call multiple data types from GetDirectBufferAddress in JNI?

后端 未结 1 1124
别跟我提以往
别跟我提以往 2021-01-16 22:33

I get a bytebuffer via the native methods.

The bytebuffer starts with 3 ints, then contains only doubles. The third int<

相关标签:
1条回答
  • 2021-01-16 23:23

    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
    }
    
    0 讨论(0)
提交回复
热议问题