Java Reference
In-Depth Information
could count how many iterations have occurred in the while loop and bail out if it reaches the number
of primes in the file. You could also inspect the file first to see whether there are sufficient primes in the
file to fill the array. If there are exactly 10, you can fill the array immediately. I leave it to you to fill in
these details.
MEMORY-MAPPED FILES
A memory-mapped file is a file that has its contents mapped into an area of virtual memory in your com-
puter. 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 ac-
tually in real memory, it is automatically brought in by your operating system. The memory that a file maps
to might be paged in or out by the operating system, just like any other memory in your computer, so its im-
mediate 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 returns a reference to a buffer of type MappedByteBuffer that maps to a specified part
of the channel's file:
MappedByteBuffer map(int mode, long position, long size)
This method maps a region of the channel's file to a buffer of type MappedByteBuffer and returns a ref-
erence to the buffer. 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 follow-
ing three constant values, defined in the MapMode class, which is a static nested class of the FileChannel
class:
MapMode.READ_ONLY : This is valid if the channel was opened for reading the file. In this mode
the buffer is read-only. If you try to modify the buffer's contents, an exception of type
ReadOnlyBufferException is thrown.
MapMode.READ_WRITE : This is valid if the channel was for both reading and writing. You can
access and change the contents of the buffer and any changes to the contents are eventually be
propagated to the file. The changes might or might not be visible to other users who have mapped
the same file.
MapMode.PRIVATE : This mode is for a “copy-on-write” mapping. This option for mode is also valid
only if the channel was open for both reading and writing. You can access or change the buffer,
but changes are not propagated to the file and are not visible to users who have mapped the same
file. Private copies of modified portions of the buffer are created and used for subsequent buffer
accesses.
When you access or change data in the MappedByteBuffer object that is returned when you call the
map() method, you are effectively accessing the file that is mapped to the buffer. After you have called the
map() method, the file mapping and the buffer that you have established are independent of the FileChan-
nel object. You can close the channel, and the mapping of the file into the MappedByteBuffer object is still
valid and operational.
Search WWH ::




Custom Search