GetByteArrayElementsとReleaseByteArrayElements

1898 ワード

GetByteArrayElementsの公式解釈:
Returns the body of the primitive array. The result is valid until the corresponding ReleaseArrayElements function is called.Since the returned array may be a copy of the original array, changes made to the returned array will not necessarily be reflected in the original array until a corresponding ReleaseArrayElementsis called. IfisCopyis notNULL, then*isCopyis set toJNI_TRUEif a copy is made; if no copy is made, it is set toJNI_FALSE.
使用中
GetByteArrayElements
:
byteBuffer = env->GetByteArrayElements(buffer, JNI_FALSE);
/*         0 ,               。
  ,       byteBuffer       buffer  (      ),
    : env->ReleaseByteArrayElements(buffer, byteBuffer, 0);
*/
使用中
ReleaseByteArrayElements
:
env->ReleaseByteArrayElements(buffer, byteBuffer, JNI_FALSE);
/*        ,   0     m        .   M             ,
     Release               .   JNI_COMMIT   .
*/

解決方法:http://blog.csdn.net/rainlight/article/details/818964
実は現代コードは次のようなものかもしれません
+void +KaffeJNI_ReleaseByteArrayElements(JNIEnv* env UNUSED, jbyteArray arr, jbyte* elems, jint mode) +{ + BEGIN_EXCEPTION_HANDLING_VOID();++ if (elems != unhand_array((HArrayOfByte*)arr)->body) { + switch (mode) { + case JNI_COMMIT: + memcpy(unhand_array((HArrayOfByte*)arr)->body, elems, obj_length((HArrayOfByte*)arr) * sizeof(jbyte)); + break; + case 0: + memcpy(unhand_array((HArrayOfByte*)arr)->body, elems, obj_length((HArrayOfByte*)arr) * sizeof(jbyte)); + KFREE(elems); + break; + case JNI_ABORT: + KFREE(elems); + break; + } + } + END_EXCEPTION_HANDLING(); +}
JNI_COMMIT forces the native array to be copied back to the original array in the Java virtual machine.JNI_ABORTfrees the memory allocated for the native array without copying back the new contents