Passing a byte[] in Java to a function in C through JNI: how to use jarraybyte

前端 未结 2 691
说谎
说谎 2020-12-13 13:50

This is the first time that I use the JNI and also the first time that I have to write some lines in C.

What I am trying to do is very simple. I\'m just trying to sw

相关标签:
2条回答
  • 2020-12-13 14:22

    qrtt has given you a great answer.

    However, the JNI has very comprehensive and (relatively) easy-to-understand documentation that you should read front-to-back if you will be using JNI features again in the future. You can find it here: http://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/jniTOC.html

    For your particular case, here's the section on dealing with arrays: http://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/design.html#wp1265

    0 讨论(0)
  • 2020-12-13 14:41

    you can get jbyte* by GetByteArrayElements:

    jbyte* bufferPtr = (*env)->GetByteArrayElements(env, array, NULL);
    

    And it is important to know the length of your array:

    jsize lengthOfArray = (*env)->GetArrayLength(env, array);
    

    Having jbyte* and length, you can do all the things in c-array. Finally, releasing it:

    (*env)->ReleaseByteArrayElements(env, array, bufferPtr, 0);
    
    0 讨论(0)
提交回复
热议问题