Question

I need to write into a file a byte array that i pass to a function in C++ using jni.

That's my Java code

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? Thanks for help

Was it helpful?

Solution

Have a look here: A correct way to convert byte[] in java to unsigned char* in C++, and vice versa?. I faced similar problem before and it was the solution.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top