Wednesday, April 24, 2013

jni jnienv ReleaseShortArrayElements example c c++ java


Release<PrimitiveType>ArrayElements Routines - ReleaseShortArrayElements


void Release<PrimitiveType>ArrayElements(JNIEnv *env,ArrayType array, NativeType *elems, jint mode);

A family of functions that informs the VM that the native code no longer needs access to elems. The elems argument is a pointer derived from array using the corresponding Get<PrimitiveType>ArrayElements() function. If necessary, this function copies back all changes made to elems to the original array. (ReleaseShortArrayElements)

The mode argument provides information on how the array buffer should be released. mode has no effect if elems is not a copy of the elements in array. Otherwise, mode has the following impact, as shown in the following table:

Table 4-10 Primitive Array Release Modes (ReleaseShortArrayElements)

mode

actions

0

copy back the content and free the elems buffer

JNI_COMMIT

copy back the content but do not free the elems buffer

JNI_ABORT

free the buffer without copying back the possible changes

In most cases, programmers pass “0” to the mode argument to ensure consistent behavior for both pinned and copied arrays. The other options give the programmer more control over memory management and should be used with extreme care. (ReleaseShortArrayElements)

The next table describes the specific routines that comprise the family of primitive array disposers. You should make the following substitutions:

  • Replace Release<PrimitiveType>ArrayElements with one of the actual primitive array disposer routine names from Table 4-11.

  • Replace ArrayType with the corresponding array type.

  • Replace NativeType with the corresponding native type for that routine.

Table 4-11a Release<PrimitiveType>ArrayElements Family of Array Routines

Release<PrimitiveType>ArrayElements Routines

Array Type

Native Type

ReleaseBooleanArrayElements()

jbooleanArray

jboolean

ReleaseByteArrayElements()

jbyteArray

jbyte

ReleaseCharArrayElements()

jcharArray

jchar

ReleaseShortArrayElements()

jshortArray

jshort

ReleaseIntArrayElements()

jintArray

jint

ReleaseLongArrayElements()

jlongArray

jlong

ReleaseFloatArrayElements()

jfloatArray

jfloat

ReleaseDoubleArrayElements()

jdoubleArray

jdouble
LINKAGE:
Indices in the JNIEnv interface function table.
Table 4-11b Release<PrimitiveType>ArrayElements Family of Array Routines
Release<PrimitiveType>ArrayElements Routines
Index
ReleaseBooleanArrayElements()
191
ReleaseByteArrayElements()
192
ReleaseCharArrayElements()
193
ReleaseShortArrayElements()
194
ReleaseIntArrayElements()
195
ReleaseLongArrayElements()
196
ReleaseFloatArrayElements()
197
ReleaseDoubleArrayElements()
198
PARAMETERS of ReleaseShortArrayElements

env: the JNI interface pointer.

array: a Java array object.

elems: a pointer to array elements.

mode: the release mode.
Example of ReleaseShortArrayElements

JNIEXPORT jint JNICALL Java_com_motalenbubble_projectlucid_MPG123_GetPCM(
    JNIEnv* env,
    jobject obj,
    jshortArray data ) // jshortarray already has a size attribute
{
    // Call mpg123_read
    jint  err  = MPG123_OK, length;
    jshort *pcm;
    jsize len = (*env)->GetArrayLength( env, data );

    pcm = (*env)->GetShortArrayElements( env, data, NULL );
    err = mpg123_read( mh, (unsigned char*)pcm, len * sizeof( short ), &length ); // Read length is in bytes - we need in shorts. 
    (*env)->ReleaseShortArrayElements( env, data, pcm, 0 );
    return err;
}