Search code examples
javamemory-mapped-filesbytebufferdatainputstream

DataInputStream from a MappedByteBuffer


I wonder if it is possible to create DataInputStream backed by a MappedByteBuffer instance ?

MappedByteBuffer buff = (initialize...);
ByteArrayInputStream bis = new ByteArrayInputStream(buff.array());

This code snippet throws UnsupportedOperationException because mapped byte buffer is not backed by an accessible array.

Then I tried to load bytes to pyhsical memory by calling load method by it is no use.

Any suggestions ?


Solution

  • You can override methods in DataInputStream to use the MappedByteBuffer.

    However, the simplest approach is to use the MappedByteBuffer directly as it support almost all the same methods.