O'Reilly logo

Ivor Horton's Beginning Java™ 2, JDK™ 5th Edition by Ivor Horton

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

11.9. Memory-Mapped Files

A memory-mapped file is a file that has its contents mapped into an area of virtual memory in your computer. This enables you to reference or update the data in the file directly without performing any explicit file read or write operations on the physical file yourself. When you reference a part of the file that is not actually in real memory, it will be automatically paged in by your operating system. The memory that a file maps to may be paged in or out by the operating system, just like any other memory in your computer, so its immediate availability in real memory is not guaranteed. Because of the potentially immediate availability of the data it contains, a memory-mapped file is particularly useful when you need to access the file randomly. Your program code can reference the data in the file just as though it were all resident in memory.

Mapping a file into memory is implemented by a FileChannel object. The map() method for a FileChannel object will return a reference to a buffer of type MappedByteBuffer that will map to a specified part of the channel's file:

map(int mode,
    long position,
    long size)
Maps a region of the channel's file to a buffer of type MappedByteBuffer. The file region that is mapped starts at position in the file and is of length size bytes. The first argument, mode, specifies how the buffer's memory may be accessed and can be any of the following three constant values, which are defined in the MapMode class, which is a static ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required