001 /*
002 * Copyright 2000-2003 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package java.nio;
027
028 /**
029 * A direct byte buffer whose content is a memory-mapped region of a file.
030 *
031 * <p> Mapped byte buffers are created via the {@link
032 * java.nio.channels.FileChannel#map FileChannel.map} method. This class
033 * extends the {@link ByteBuffer} class with operations that are specific to
034 * memory-mapped file regions.
035 *
036 * <p> A mapped byte buffer and the file mapping that it represents remain
037 * valid until the buffer itself is garbage-collected.
038 *
039 * <p> The content of a mapped byte buffer can change at any time, for example
040 * if the content of the corresponding region of the mapped file is changed by
041 * this program or another. Whether or not such changes occur, and when they
042 * occur, is operating-system dependent and therefore unspecified.
043 *
044 * <a name="inaccess"><p> All or part of a mapped byte buffer may become
045 * inaccessible at any time, for example if the mapped file is truncated. An
046 * attempt to access an inaccessible region of a mapped byte buffer will not
047 * change the buffer's content and will cause an unspecified exception to be
048 * thrown either at the time of the access or at some later time. It is
049 * therefore strongly recommended that appropriate precautions be taken to
050 * avoid the manipulation of a mapped file by this program, or by a
051 * concurrently running program, except to read or write the file's content.
052 *
053 * <p> Mapped byte buffers otherwise behave no differently than ordinary direct
054 * byte buffers. </p>
055 *
056 *
057 * @author Mark Reinhold
058 * @author JSR-51 Expert Group
059 * @version 1.30, 07/05/05
060 * @since 1.4
061 */
062
063 public abstract class MappedByteBuffer extends ByteBuffer {
064
065 // This is a little bit backwards: By rights MappedByteBuffer should be a
066 // subclass of DirectByteBuffer, but to keep the spec clear and simple, and
067 // for optimization purposes, it's easier to do it the other way around.
068 // This works because DirectByteBuffer is a package-private class.
069
070 // Volatile to make sure that the finalization thread sees the current
071 // value of this so that a region is not accidentally unmapped again later.
072 volatile boolean isAMappedBuffer; // package-private
073
074 // This should only be invoked by the DirectByteBuffer constructors
075 //
076 MappedByteBuffer(int mark, int pos, int lim, int cap, // package-private
077 boolean mapped) {
078 super (mark, pos, lim, cap);
079 isAMappedBuffer = mapped;
080 }
081
082 MappedByteBuffer(int mark, int pos, int lim, int cap) { // package-private
083 super (mark, pos, lim, cap);
084 isAMappedBuffer = false;
085 }
086
087 private void checkMapped() {
088 if (!isAMappedBuffer)
089 // Can only happen if a luser explicitly casts a direct byte buffer
090 throw new UnsupportedOperationException();
091 }
092
093 /**
094 * Tells whether or not this buffer's content is resident in physical
095 * memory.
096 *
097 * <p> A return value of <tt>true</tt> implies that it is highly likely
098 * that all of the data in this buffer is resident in physical memory and
099 * may therefore be accessed without incurring any virtual-memory page
100 * faults or I/O operations. A return value of <tt>false</tt> does not
101 * necessarily imply that the buffer's content is not resident in physical
102 * memory.
103 *
104 * <p> The returned value is a hint, rather than a guarantee, because the
105 * underlying operating system may have paged out some of the buffer's data
106 * by the time that an invocation of this method returns. </p>
107 *
108 * @return <tt>true</tt> if it is likely that this buffer's content
109 * is resident in physical memory
110 */
111 public final boolean isLoaded() {
112 checkMapped();
113 if ((address == 0) || (capacity() == 0))
114 return true;
115 return isLoaded0(((DirectByteBuffer) this ).address(),
116 capacity());
117 }
118
119 /**
120 * Loads this buffer's content into physical memory.
121 *
122 * <p> This method makes a best effort to ensure that, when it returns,
123 * this buffer's content is resident in physical memory. Invoking this
124 * method may cause some number of page faults and I/O operations to
125 * occur. </p>
126 *
127 * @return This buffer
128 */
129 public final MappedByteBuffer load() {
130 checkMapped();
131 if ((address == 0) || (capacity() == 0))
132 return this ;
133 load0(((DirectByteBuffer) this ).address(), capacity(), Bits
134 .pageSize());
135 return this ;
136 }
137
138 /**
139 * Forces any changes made to this buffer's content to be written to the
140 * storage device containing the mapped file.
141 *
142 * <p> If the file mapped into this buffer resides on a local storage
143 * device then when this method returns it is guaranteed that all changes
144 * made to the buffer since it was created, or since this method was last
145 * invoked, will have been written to that device.
146 *
147 * <p> If the file does not reside on a local device then no such guarantee
148 * is made.
149 *
150 * <p> If this buffer was not mapped in read/write mode ({@link
151 * java.nio.channels.FileChannel.MapMode#READ_WRITE}) then invoking this
152 * method has no effect. </p>
153 *
154 * @return This buffer
155 */
156 public final MappedByteBuffer force() {
157 checkMapped();
158 if ((address == 0) || (capacity() == 0))
159 return this ;
160 force0(((DirectByteBuffer) this ).address(), capacity());
161 return this ;
162 }
163
164 private native boolean isLoaded0(long address, long length);
165
166 private native int load0(long address, long length, int pageSize);
167
168 private native void force0(long address, long length);
169
170 }
|