Using regular arrays can be inefficient, since the virtual machine can copy the array when transferring it to its own code and can also use intermediate memory during I / O.
For fast I / O, use ByteBuffer.allocateDirect to allocate a byte buffer. The main array is βspecialβ in the sense that it is not part of the regular JVM heap. Source code and I / O can directly access the array.
To read data into the buffer,
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(randomAccessFile.length()); RandomAccessFile.getChannel().read(byteBuffer, 0);
To get the base array to go to JNI, use
byte[] byteArray = byteBuffer.array();
Then you can pass this array and file length to JNI.
Direct buffers are extremely difficult to create, since all your files are 1 MB (or so), you should be able to reuse the same buffer for multiple files.
Hope this helps!
source share