Wednesday, April 24, 2013

jni jnienv NewIntArray example c c++ java


New<PrimitiveType>Array Routines - NewIntArray


ArrayType New<PrimitiveType>Array(JNIEnv *env, jsize length);

A family of operations used to construct a new primitive array object. Table 4-8 describes the specific primitive array constructors. You should replace New<PrimitiveType>Array with one of the actual primitive array constructor routine names from the following table, and replace ArrayType with the corresponding array type for that routine.(NewIntArray)

Table 4-8a New<PrimitiveType>Array Family of Array Constructors

New<PrimitiveType>Array Routines

Array Type

NewBooleanArray()

jbooleanArray

NewByteArray()

jbyteArray

NewCharArray()

jcharArray

NewShortArray()

jshortArray

NewIntArray()

jintArray

NewLongArray()

jlongArray

NewFloatArray()

jfloatArray

NewDoubleArray()

jdoubleArray
LINKAGE:
Indices in the JNIEnv interface function table.
Table 4-8b New<PrimitiveType>Array Family of Array Constructors
New<PrimitiveType>Array Routines
Index
NewBooleanArray()
175
NewByteArray()
176
NewCharArray()
177
NewShortArray()
178
NewIntArray()
179
NewLongArray()
180
NewFloatArray()
181
NewDoubleArray()
182
PARAMETERS of NewIntArray

env: the JNI interface pointer.

length: the array length.

RETURNS of NewIntArray

Returns a Java array, or NULL if the array cannot be constructed.
Example of NewIntArray

JNIEXPORT jintArray JNICALL Java_ArrayTest_initIntArray(JNIEnv *env, jclass cls, int size)
{
 jintArray result;
 result = (*env)->NewIntArray(env, size);
 if (result == NULL) {
     return NULL; /* out of memory error thrown */
 }
 int i;
 // fill a temp structure to use to populate the java int array
 jint fill[256];
 for (i = 0; i < size; i++) {
     fill[i] = 0; // put whatever logic you want to populate the values here.
 }
 // move from the temp structure to the java structure
 (*env)->SetIntArrayRegion(env, result, 0, size, fill);
 return result;
}