The best you can do is use the Get <PrimitiveType> ArrayElements Procedures . Hope VM supports pinning :
jdoubleArray in2 = env->NewDoubleArray(1000); jboolean isCopy; jdouble *elems2 = env->GetDoubleArrayElements(in2, &isCopy); for(int i = 0; i < 1000; i++) { elems2[i] = (jdouble)inputArray[i]; } env->ReleaseDoubleArrayElements(in2, elems2, 0); elems2 = NULL;
If isCopy is JNI_FALSE after calling GetDoubleArrayElements (), then no copy was made.
EDIT: After reading your question again, you may consider implementing the Archie idea . It depends on how your Java methods use the data. If Java methods do not use the entire array, or not all at once, then the Archie solution to create a Java class wrapper from a C ++ array using its own accessories can be a good solution. If, however, the Java method requires all the data, then you might need the fastest way to get the data into the VM that GetDoubleArrayElements () provides.
If you make changes to some elements of a C ++ array and want to make the same changes to a copy of Java that SetDoubleArrayRegion () comes into play.
EDIT2: I believe Archie is referring to something like:
public class NativeDoubleArrayProxy {
The exact data depends on the type of your inputArray (is it an array of source C-types or std::vector<double> , something else?). But the idea is to build a NativeDoubleArrayProxy object on the JNI side, passing it the pointer passed to jlong . JNI implementations for getDouble () and getDoubles () implement copy code from C ++ to Java.
Of course, you need to be very careful to make sure that the pointer remains valid.
See also: What is the correct way to store an inline pointer inside a Java object?
source share