|
|
In the jbyteArray jb; jb=(*env)->NewByteArray(env, finfo.st_size); (*env)->SetByteArrayRegion(env, jb, 0, finfo.st_size, (jbyte *)m); close(fd);The array is returned to the calling Java language method, which in turn, garbage collects the reference to the array when it is no longer used. The array can be explicitly freed with the following call. (*env)-> ReleaseByteArrayElements(env, jb, (jbyte *)m, 0);The last argument to the ReleaseByteArrayElements function
above can have the following values:
Pinning ArrayWhen retrieving an array, you can specify if this is a copy (JNI_TRUE ) or a reference to the array residing in
your Java language program (JNI_FALSE ).
If you use a reference to the array, you will want the array to stay
where it is in the Java heap and not get moved by the garbage
collector when it compacts heap memory. To prevent the array references
from being moved, the Java virtual machine pins the array into
memory. Pinning the array ensures that when the array is released,
the correct elements are updated in the Java VM.
In the Object ArraysYou can store any Java language object in an array with theNewObjectArray and SetObjectArrayElement
function calls. The main difference between an object array and an
array of primitive types
is that when constructing a jobjectarray type,
the Java language class is used as a parameter.
This next C++ example shows how to call #include <jni.h> #include "ArrayHandler.h" JNIEXPORT jobjectArray JNICALL Java_ArrayHandler_returnArray (JNIEnv *env, jobject jobj){ jobjectArray ret; int i; char *message[5]= {"first", "second", "third", "fourth", "fifth"}; ret= (jobjectArray)env->NewObjectArray(5, env->FindClass("java/lang/String"), env->NewStringUTF("")); for(i=0;i<5;i++) { env->SetObjectArrayElement( ret,i,env->NewStringUTF(message[i])); } return(ret); }The Java class that calls this native method is as follows: public class ArrayHandler { public native String[] returnArray(); static{ System.loadLibrary("nativelib"); } public static void main(String args[]) { String ar[]; ArrayHandler ah= new ArrayHandler(); ar = ah.returnArray(); for (int i=0; i<5; i++) { System.out.println("array element"+i+ "=" + ar[i]); } } } Multi-Dimensional ArraysYou might need to call existing numerical and mathematical libraries such as the linear algebra library CLAPACK/LAPACK or other matrix crunching programs from your Java language program using native methods. Many of these libraries and programs use two-dimensional and higher order arrays.In the Java programming language, any array that has more than one dimension is treated as an array of arrays. For example, a two-dimensional integer array is handled as an array of integer arrays. The array is read horizontally, or what is also termed as row order. Other languages such as FORTRAN use column ordering so extra care is needed if your program hands a Java language array to a FORTRAN function. Also, the array elements in an application written in the Java programming language are not guaranteed to be contigous in memory. Some numerical libraries use the knowledge that the array elements are stored next to each other in memory to perform speed optimizations, so you might need to make an additional local copy of the array to pass to those functions. The next example passes a two-dimensional array to a native method which then extracts the elements, performs a calculation, and calls a Java language method to return the results.
The array is passed as an object array that contains an array of
The example uses a fixed size
matrix. If you do not know the size of the array being used, the
The new array sent back to the program written in the Java langauge
is built in reverse.
First, a public class ArrayManipulation { private int arrayResults[][]; Boolean lock=new Boolean(true); int arraySize=-1; public native void manipulateArray( int[][] multiplier, Boolean lock); static{ System.loadLibrary("nativelib"); } public void sendArrayResults(int results[][]) { arraySize=results.length; arrayResults=new int[results.length][]; System.arraycopy(results,0,arrayResults, 0,arraySize); } public void displayArray() { for (int i=0; i<arraySize; i++) { for(int j=0; j <arrayResults[i].length;j++) { System.out.println("array element "+i+","+j+ "= " + arrayResults[i][j]); } } } public static void main(String args[]) { int[][] ar = new int[3][3]; int count=3; for(int i=0;i<3;i++) { for(int j=0;j<3;j++) { ar[i][j]=count; } count++; } ArrayManipulation am= new ArrayManipulation(); am.manipulateArray(ar, am.lock); am.displayArray(); } } #include <jni.h> #include <iostream.h> #include "ArrayManipulation.h" JNIEXPORT void JNICALL Java_ArrayManipulation_manipulateArray (JNIEnv *env, jobject jobj, jobjectArray elements, jobject lock){ jobjectArray ret; int i,j; jint arraysize; int asize; jclass cls; jmethodID mid; jfieldID fid; long localArrayCopy[3][3]; long localMatrix[3]={4,4,4}; for(i=0; i<3; i++) { jintArray oneDim= (jintArray)env->GetObjectArrayElement( elements, i); jint *element=env->GetIntArrayElements(oneDim, 0); for(j=0; j<3; j++) { localArrayCopy[i][j]= element[j]; } } // With the C++ copy of the array, // process the array with LAPACK, BLAS, etc. for (i=0;i<3;i++) { for (j=0; j<3 ; j++) { localArrayCopy[i][j]= localArrayCopy[i][j]*localMatrix[i]; } } // Create array to send back jintArray row= (jintArray)env->NewIntArray(3); ret=(jobjectArray)env->NewObjectArray( 3, env->GetObjectClass(row), 0); for(i=0;i<3;i++) { row= (jintArray)env->NewIntArray(3); env->SetIntArrayRegion((jintArray)row,( jsize)0,3,(jint *)localArrayCopy[i]); env->SetObjectArrayElement(ret,i,row); } cls=env->GetObjectClass(jobj); mid=env->GetMethodID(cls, "sendArrayResults", "([[I)V"); if (mid == 0) { cout <<"Can't find method sendArrayResults"; return; } env->ExceptionClear(); env->MonitorEnter(lock); env->CallVoidMethod(jobj, mid, ret); env->MonitorExit(lock); if(env->ExceptionOccurred()) { cout << "error occured copying array back" << endl; env->ExceptionDescribe(); env->ExceptionClear(); } fid=env->GetFieldID(cls, "arraySize", "I"); if (fid == 0) { cout <<"Can't find field arraySize"; return; } asize=env->GetIntField(jobj,fid); if(!env->ExceptionOccurred()) { cout<< "Java array size=" << asize << endl; } else { env->ExceptionClear(); } return; }
_______ [TOP] |