我需要帮助将二进制数据传递给
Java.我正在尝试使用jbytearray但是当数据进入Java时它看起来很糟糕.有人可以帮我一把吗?
这是一些示例代码的片段.首先是原生C面:
printf("Building audio array copy\n");
jbyteArray rawAudioCopy = env->NewByteArray(10);
jbyte toCopy[10];
printf("Filling audio array copy\n");
char theBytes[10] = {0,1,2,3,4,5,6,7,8,9};
for (int i = 0; i < sizeof(theBytes); i++) {
toCopy[i] = theBytes[i];
}
env->SetByteArrayRegion(rawAudioCopy,0,10,toCopy);
printf("Finding object callback\n");
jmethodID aMethodId = env->GetMethodID(env->GetObjectClass(obj),"handleAudio","([B)V");
if(0==aMethodId) throw MyRuntimeException("Method not found error",99);
printf("Invoking the callback\n");
env->CallVoidMethod(obj,aMethodId, &rawAudioCopy);
然后是Java回调方法:
public void handleAudio(byte[] audio){
System.out.println("Audio supplied to Java [" + audio.length + "] bytes");
byte[] expectedAudio = {0,1,2,3,4,5,6,7,8,9};
for (int i = 0; i < audio.length; i++) {
if(audio[i]!= expectedAudio[i])
System.err.println("Expected byte " + expectedAudio[i]
+ " at byte " + i + " but got byte " + audio[i]);
else System.out.print('.');
}
System.out.println("Audio passed back accordingly!");
}
调用回调时,我得到以下输出:
library loaded!
Audio supplied to Java [-2019659176] bytes
Audio passed back accordingly!