I am developing an Android application for which I am using one native C library. This library is used for file decompression.
The definition of the native method is as follows:
public static native Long decompress(ByteBuffer src, ByteBuffer dst);
My application code to use this function is as follows:
try { File inFile = new File(Environment.getExternalStorageDirectory().getPath() +"/1"); // source file ByteBuffer buf = ByteBuffer.allocateDirect((int)inFile.length()); ByteBuffer buf_out = ByteBuffer.allocateDirect((int)inFile.length()*20); InputStream is = new FileInputStream(inFile); int b; while ((b=is.read())!=-1) { buf.put((byte)b); } Log.d("nims","source buffer zie"+buf.position()); File file = new File(Environment.getExternalStorageDirectory().getPath() +"/2"); // append or overwrite the file boolean append = false; FileChannel channel = new FileOutputStream(file, append).getChannel(); Long t = decompress(buf,buf_out); // t is some XYZ number Log.d("nims","dest buffer zie"+buf_out.position()); // buf_out.position() returns 0 buf_out.flip(); // Writes a sequence of bytes to this channel from the given buffer. channel.write(buf_out); // close the channel channel.close(); } catch (IOException e) { System.out.println("I/O Error: " + e.getMessage()); }
JNI code:
JNIEXPORT jlong JNICALL Java_com_company_appName_MainActivity_decompress(JNIEnv* env, jclass cls, jobject src, jobject dst) { uint8_t* src_buffer = (*env)->GetDirectBufferAddress(env,src); const size_t src_size = (*env)->GetDirectBufferCapacity(env, src); uint8_t* dst_buffer = (*env)->GetDirectBufferAddress(env,dst); size_t dst_size = (*env)->GetDirectBufferCapacity(env, dst); jlong test = _decode_buffer(dst_buffer, dst_size, src_buffer, src_size, NULL); return test; }
The destination buffer doesn’t contain any data.
I have following questions:
- How can I read the destination bytebuffer back from JNI code to application’s code?
- How can I write the destination bytebuffer data to a file?
I would appreciate any suggestion and thoughts on this topic.