I am very new to Java, and trying to use Mathematica's Java interface to access a file using memory mapping (in hope of a performance improvement).
The Mathematica code I have is (I believe) equivalent to the following Java code (based on this):
import java.io.FileInputStream;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
public class MainClass {
private static final int LENGTH = 8*100;
public static void main(String[] args) throws Exception {
MappedByteBuffer buffer = new FileInputStream("test.bin").getChannel().map(FileChannel.MapMode.READ_ONLY, 0, LENGTH);
buffer.load();
buffer.isLoaded(); // returns false, why?
}
}
I would like to use the array()
method on buffer, so I am trying to load the buffers contents into memory first using load()
. However, even after load()
, isLoaded()
returns false
, and buffer.array()
throws an exception: java.lang.UnsupportedOperationException
at java.nio.ByteBuffer.array(ByteBuffer.java:940)
.
Why doesn't the buffer load and how can I call the array()
method?
My ultimate aim here is to get an array of double
s using asDoubleBuffer().array()
. The method getDouble()
does work correctly, but I was hoping to get this done in one go for good performance. What am I doing wrong?
As I am doing this from Mathematica, I'll post the actual Mathematica code I used too (equivalent to the above in Java):
Needs["JLink`"]
LoadJavaClass["java.nio.channels.FileChannel$MapMode"]
buffer = JavaNew["java.io.FileInputStream", "test.bin"]@getChannel[]@map[FileChannel$MapMode`READUONLY, 0, 8*100]
buffer@load[]
buffer@isLoaded[] (* returns False *)