通过 JNI 将 Java 中的 byte[] 传递给 C 中的函数:如何使用 jarraybyte
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3421300/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Passing a byte[] in Java to a function in C through JNI: how to use jarraybyte
提问by blutech
This is the first time that I use the JNI and also the first time that I have to write some lines in C.
这是我第一次使用 JNI,也是我第一次必须用 C 编写一些行。
What I am trying to do is very simple. I'm just trying to switch the endiannes of a byte[] using a C routine.
我想要做的很简单。我只是想使用 C 例程切换 byte[] 的字节序。
In java it is done like this:
在java中它是这样完成的:
public void switchEndianness(byte[] array){
byte byte1;
byte byte2;
for(int i = 0; i < array.length ; i+=2){
byte1 = array[i];
byte2 = array[i+1];
array[i] = byte2;
array[i+1] = byte1;
}
}
So to do this using JNI, I've tried to imlpement the same routine in the JNICALL, but it doesn't compile. What I've written so far is this:
因此,为了使用 JNI 执行此操作,我尝试在 JNICALL 中实现相同的例程,但它无法编译。到目前为止我写的是这样的:
JNIEXPORT void JNICALL Java_CEndianness_switchEndianness(JNIEnv *env, jobject obj, jbyteArray array, jint offset, jint length){
char byte1;
char byte2;
int i;
for(i = offset; i < length ; i+=2){
byte1 = array[i];
byte2 = array[i+1];
array[i] = byte2;
array[i+1] = byte1;
}
}
I have no clue how to use the jbyteArray type of data. is it possible to store a jbyte in a char??? Another question is.. when this routine is over...will the byte[] in java be modified?? Or is it only modified inside the C call?
我不知道如何使用 jbyteArray 类型的数据。是否可以将 jbyte 存储在字符中???另一个问题是..当这个例程结束时......java中的byte[]会被修改吗??还是只在 C 调用中修改?
Any help???
任何帮助???
Thanks to everybody!
感谢大家!
回答by qrtt1
you can get jbyte* by GetByteArrayElements:
您可以通过 GetByteArrayElements 获取 jbyte*:
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:
有了 jbyte* 和 length,你就可以在 c-array 中做所有的事情。最后,发布它:
(*env)->ReleaseByteArrayElements(env, array, bufferPtr, 0);
回答by Jason LeBrun
qrtt has given you a great answer.
qrtt 给了你很好的答案。
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
但是,JNI 具有非常全面且(相对)易于理解的文档,如果您将来再次使用 JNI 功能,您应该从头到尾阅读这些文档。你可以在这里找到它: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
对于您的特定情况,这里是处理数组的部分:http: //docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/design.html#wp1265