0N/A/*
3261N/A * Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage java.nio;
0N/A
2984N/Aimport java.io.FileDescriptor;
2625N/Aimport sun.misc.Unsafe;
2625N/A
0N/A
0N/A/**
0N/A * A direct byte buffer whose content is a memory-mapped region of a file.
0N/A *
0N/A * <p> Mapped byte buffers are created via the {@link
0N/A * java.nio.channels.FileChannel#map FileChannel.map} method. This class
0N/A * extends the {@link ByteBuffer} class with operations that are specific to
0N/A * memory-mapped file regions.
0N/A *
0N/A * <p> A mapped byte buffer and the file mapping that it represents remain
0N/A * valid until the buffer itself is garbage-collected.
0N/A *
0N/A * <p> The content of a mapped byte buffer can change at any time, for example
0N/A * if the content of the corresponding region of the mapped file is changed by
0N/A * this program or another. Whether or not such changes occur, and when they
0N/A * occur, is operating-system dependent and therefore unspecified.
0N/A *
0N/A * <a name="inaccess"><p> All or part of a mapped byte buffer may become
0N/A * inaccessible at any time, for example if the mapped file is truncated. An
0N/A * attempt to access an inaccessible region of a mapped byte buffer will not
0N/A * change the buffer's content and will cause an unspecified exception to be
0N/A * thrown either at the time of the access or at some later time. It is
0N/A * therefore strongly recommended that appropriate precautions be taken to
0N/A * avoid the manipulation of a mapped file by this program, or by a
0N/A * concurrently running program, except to read or write the file's content.
0N/A *
0N/A * <p> Mapped byte buffers otherwise behave no differently than ordinary direct
0N/A * byte buffers. </p>
0N/A *
0N/A *
0N/A * @author Mark Reinhold
0N/A * @author JSR-51 Expert Group
0N/A * @since 1.4
0N/A */
0N/A
0N/Apublic abstract class MappedByteBuffer
0N/A extends ByteBuffer
0N/A{
0N/A
0N/A // This is a little bit backwards: By rights MappedByteBuffer should be a
0N/A // subclass of DirectByteBuffer, but to keep the spec clear and simple, and
0N/A // for optimization purposes, it's easier to do it the other way around.
0N/A // This works because DirectByteBuffer is a package-private class.
0N/A
2984N/A // For mapped buffers, a FileDescriptor that may be used for mapping
2984N/A // operations if valid; null if the buffer is not mapped.
2984N/A private final FileDescriptor fd;
0N/A
0N/A // This should only be invoked by the DirectByteBuffer constructors
0N/A //
0N/A MappedByteBuffer(int mark, int pos, int lim, int cap, // package-private
2984N/A FileDescriptor fd)
0N/A {
0N/A super(mark, pos, lim, cap);
2984N/A this.fd = fd;
0N/A }
0N/A
0N/A MappedByteBuffer(int mark, int pos, int lim, int cap) { // package-private
0N/A super(mark, pos, lim, cap);
2984N/A this.fd = null;
0N/A }
0N/A
0N/A private void checkMapped() {
2984N/A if (fd == null)
0N/A // Can only happen if a luser explicitly casts a direct byte buffer
0N/A throw new UnsupportedOperationException();
0N/A }
0N/A
2625N/A // Returns the distance (in bytes) of the buffer from the page aligned address
2625N/A // of the mapping. Computed each time to avoid storing in every direct buffer.
2625N/A private long mappingOffset() {
2625N/A int ps = Bits.pageSize();
2625N/A long offset = address % ps;
2625N/A return (offset >= 0) ? offset : (ps + offset);
2625N/A }
2625N/A
2625N/A private long mappingAddress(long mappingOffset) {
2625N/A return address - mappingOffset;
2625N/A }
2625N/A
2625N/A private long mappingLength(long mappingOffset) {
2625N/A return (long)capacity() + mappingOffset;
2625N/A }
2625N/A
0N/A /**
0N/A * Tells whether or not this buffer's content is resident in physical
0N/A * memory.
0N/A *
0N/A * <p> A return value of <tt>true</tt> implies that it is highly likely
0N/A * that all of the data in this buffer is resident in physical memory and
0N/A * may therefore be accessed without incurring any virtual-memory page
0N/A * faults or I/O operations. A return value of <tt>false</tt> does not
0N/A * necessarily imply that the buffer's content is not resident in physical
0N/A * memory.
0N/A *
0N/A * <p> The returned value is a hint, rather than a guarantee, because the
0N/A * underlying operating system may have paged out some of the buffer's data
0N/A * by the time that an invocation of this method returns. </p>
0N/A *
0N/A * @return <tt>true</tt> if it is likely that this buffer's content
0N/A * is resident in physical memory
0N/A */
0N/A public final boolean isLoaded() {
0N/A checkMapped();
0N/A if ((address == 0) || (capacity() == 0))
0N/A return true;
2625N/A long offset = mappingOffset();
2625N/A long length = mappingLength(offset);
2625N/A return isLoaded0(mappingAddress(offset), length, Bits.pageCount(length));
0N/A }
0N/A
5060N/A // not used, but a potential target for a store, see load() for details.
5060N/A private static byte unused;
5060N/A
0N/A /**
0N/A * Loads this buffer's content into physical memory.
0N/A *
0N/A * <p> This method makes a best effort to ensure that, when it returns,
0N/A * this buffer's content is resident in physical memory. Invoking this
0N/A * method may cause some number of page faults and I/O operations to
0N/A * occur. </p>
0N/A *
0N/A * @return This buffer
0N/A */
0N/A public final MappedByteBuffer load() {
0N/A checkMapped();
0N/A if ((address == 0) || (capacity() == 0))
0N/A return this;
2625N/A long offset = mappingOffset();
2625N/A long length = mappingLength(offset);
2625N/A load0(mappingAddress(offset), length);
2625N/A
5060N/A // Read a byte from each page to bring it into memory. A checksum
5060N/A // is computed as we go along to prevent the compiler from otherwise
5060N/A // considering the loop as dead code.
2625N/A Unsafe unsafe = Unsafe.getUnsafe();
2625N/A int ps = Bits.pageSize();
2625N/A int count = Bits.pageCount(length);
2625N/A long a = mappingAddress(offset);
5060N/A byte x = 0;
2625N/A for (int i=0; i<count; i++) {
5060N/A x ^= unsafe.getByte(a);
2625N/A a += ps;
2625N/A }
5060N/A if (unused != 0)
5060N/A unused = x;
2625N/A
0N/A return this;
0N/A }
0N/A
0N/A /**
0N/A * Forces any changes made to this buffer's content to be written to the
0N/A * storage device containing the mapped file.
0N/A *
0N/A * <p> If the file mapped into this buffer resides on a local storage
0N/A * device then when this method returns it is guaranteed that all changes
0N/A * made to the buffer since it was created, or since this method was last
0N/A * invoked, will have been written to that device.
0N/A *
0N/A * <p> If the file does not reside on a local device then no such guarantee
0N/A * is made.
0N/A *
0N/A * <p> If this buffer was not mapped in read/write mode ({@link
0N/A * java.nio.channels.FileChannel.MapMode#READ_WRITE}) then invoking this
0N/A * method has no effect. </p>
0N/A *
0N/A * @return This buffer
0N/A */
0N/A public final MappedByteBuffer force() {
0N/A checkMapped();
2625N/A if ((address != 0) && (capacity() != 0)) {
2625N/A long offset = mappingOffset();
2984N/A force0(fd, mappingAddress(offset), mappingLength(offset));
2625N/A }
0N/A return this;
0N/A }
0N/A
2625N/A private native boolean isLoaded0(long address, long length, int pageCount);
2625N/A private native void load0(long address, long length);
2984N/A private native void force0(FileDescriptor fd, long address, long length);
0N/A}