I’m doing memory-mapped IO in Java. The FileChannel class allows you to map a ByteBuffer to a particular part of a file. I’m doing that with a file opened read only.
The problem I am having is that I’m getting an exception when I attempt to call the .array() method on the resulting ByteBuffer. Perhaps that’s because the .array() returns a byte[] array, and I really want a finalized byte array?
Is there any way around this?
I’m going to assume this is about the
FileChannel.mapmethod which can map a file to memory which can be accessed by aMappedByteBuffer.In the documentation for the
FileChannel.mapmethod, if the file is mapped as read-only, the any attempt to modify the buffer will result in aReadOnlyBufferException:In terms of the exceptions thrown by the
ByteBuffer.arraymethod, there are two types of exceptions which are thrown depending on the reason for the problem:Although the exception being thrown is not mentioned in the question, perhaps the file being read-only is causing the
ReadOnlyBufferExceptionto be thrown by thearraymethod.Also, it should also be mentioned that the
ByteBuffer.arraymethod is an optional operation:To be sure that the
arraymethod will return abyte[]that can be used, invoke thehasArraymethod as suggested in the documentation for thearraymethod: