13

Is there a way to expand the Java memory-mapped byte buffer such that the new size is reflected back to the mapped file on disk?

geeko
  • 2,649
  • 4
  • 32
  • 59

1 Answers1

13

No, you will need to adjust the size of the underlying file and recreate the Memory Mapped Byte Buffer.

RandomAccessFile file = new RandomAccessFile(/* some file */);
MappedByteBuffer buffer = file.getChannel().map(MapMode.READ_WRITE, 0, file.length());

// Some stuff happens...

// adjust the size
file.setLength(newLength);

// recreate the memory mapped buffer
buffer = file.getChannel().map(MapMode.READ_WRITE, 0, file.length());

Note: Setting the file length has some slightly odd behaviour. If you write to the file via the map at a specific position that is beyond the end of the file (either using map.position() or map.putX(position, ...)) the values will be appended to the end of the file and not written at the position you expect (on linux at least). If this is undesired behaviour you will need to append data to the file in order to truly grow the file.

Michael Barker
  • 14,153
  • 4
  • 48
  • 55
  • Would you please explain the odd behavior in other words ? Is it not expected to append to the end of file if I write beyond the end of it (via the mapped buffer assuming it maps the whole file size) ? – geeko Jun 12 '10 at 13:44
  • 4
    Start with a file 50 bytes in size. Call setLength(1000) to increase the size of the file. Then create a new map from the file (file.getChannel().map(..., 0, 1000). If on the new MappedByteBuffer you call putByte(500, (byte) 'x') the value 'x' will appear as the 50th byte in the file (0-based index) not at position 500 (maybe not odd, but potentially unexpected behaviour). – Michael Barker Jun 12 '10 at 14:12
  • 1
    Do I need to close the buffer or something? – Karussell Jun 14 '12 at 16:35
  • Is creating a mappedbuffer a heavy task. Would doing this hit performance if done frequently? – Sohaib Dec 10 '16 at 11:33
  • @Sohaib I guess, it's about as expensive as opening a file, or maybe less when you reuse the `Channel`. So I think, it's fine to "resize" a `MappedByteBuffer` a few times, but surely not on every appending of a few bytes. What's usually a problem, is that there's no `unmap` and you may run out of file descriptors when dealing with too many different files. The unmapping happens only when the buffer gets GC'ed. – maaartinus Feb 03 '17 at 17:40