Reputation: 329
I have a question about data-types, I want to have it clear before I start coding! Let's say I have a method in C++ that returns me an array, or a buffer of doubles. Then I need to convert this double[] into jdouble, using JNI header, so Java can process the data.
My first question is should I configure my C++ method to return an array of doubles, or a buffer of doubles. I know they are almost the same, but don't know which one is better. Also, how do I convert a buffer in a way Java can understand?
This is what I have so far:
JNIEXPORT jdouble[] JNICALL Java_Test_getDouble[](JNIEnv *env, jobject obj)
{
double[] temp= new double[someSize];
temp = // call my C++ code
jdouble result = new jdouble[someSize];
for(i = 0; i < someSize; i++)
converting double in temp to jdouble in result;
return result;
}
and then Java can do whatever it wants with the data.
Does is my code make sense? I read some post about ByteGetElement()
but I do not know if it can be applied here. If you can provide me a simple example that would be really helpful. And ultimately, I still need to know whether I should go with an array or buffer of doubles.
Thank you very much.
Upvotes: 2
Views: 2225
Reputation: 954
A jdouble is actually the same as a double as far as C is concerned.
// just for the sake of the example this buffer exists and holds the double values in C
double* sourceBuffer;
// allocate the jdouble Array
jdoubleArray output = (*env)->NewdoubleArray(env, someSize);
// check if it was allocated correctly
if (NULL == output) return NULL;
// commit the contents of the double* array (sourceBuffer in this example) into the Java array
(*env)->SetDoubleArrayRegion(env, result, 0, someSize, sourceBuffer);
return result;
Upvotes: 2