简体   繁体   English

将通过jbytearray传递的byte []写入文件

[英]Write byte[] passed through jbytearray to a file

I need to write into a file a byte array that i pass to a function in C++ using jni. 我需要将使用jni传递给C ++函数的字节数组写入文件。

That's my Java code 那是我的Java代码

private void writeData(byte[] array) {
    Native nativeobject = new Native();

    long tsize = this.size;
    long incremental = 0;

    while(tsize != 0 && !this.stopped) {
        nativeobject.writeFile(whereToSave, array);

        publishProgress(incremental);
        incremental += TRANCE_SIZE;
        tsize -= TRANCE_SIZE;

    }
}

And that's my function header 那就是我的函数头

JNIEXPORT jbyteArray JNICALL Java_it_mls_secureeraser_algorithms_Native_writeFile(JNIEnv *jni, jobject thiz,
        jstring jfileName, jbyteArray jarray) {

    const char *fileName = jni->GetStringUTFChars(jfileName, 0);

    int len = jni->GetArrayLength (jarray);
    unsigned char* buf = new unsigned char[len];
    jni->GetByteArrayRegion (jarray, 0, len, reinterpret_cast<jbyte*>(buf));

    FILE *output = fopen(fileName, "a+");
    fwrite(buf, sizeof(unsigned char*), sizeof(buf), output);
    fclose (output);
}

how can I pass from jbytearray to something that I can write to my file? 如何从jbytearray传递到可以写入文件的内容? Thanks for help 感谢帮助

Have a look here: A correct way to convert byte[] in java to unsigned char* in C++, and vice versa? 在这里看看: 将Java中的byte []转换为C ++中的unsigned char *的正确方法,反之亦然? . I faced similar problem before and it was the solution. 我之前也遇到过类似的问题,这就是解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM