I have a file I'm mapping into memory via 'FileChannel.map()'. However it seems a bit odd when reading a string to do the following:
1) read a int for the string length
2) allocate a byte[length] object
3) use .get to read length bytes
4) convert the byte[] to a string
Now I know from my C++ background that memory mapped files are given to the user as pointers to memory. So is there a good way to skip using a byte array and just have the string conversion go right off the mapped memory?
I suggest:
MappedByteBuffer mapped = fileChannel.map(mode, position, size);
String s = new String(mapped.array());
It is also possible to use the mapped.asCharBuffer() and get the chars by this way.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With