blob: 974db3eadaea3e482ea1e495e9bb0a2c3997d84a [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2000-2003 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26package java.nio;
27
28
29/**
30 * A direct byte buffer whose content is a memory-mapped region of a file.
31 *
32 * <p> Mapped byte buffers are created via the {@link
33 * java.nio.channels.FileChannel#map FileChannel.map} method. This class
34 * extends the {@link ByteBuffer} class with operations that are specific to
35 * memory-mapped file regions.
36 *
37 * <p> A mapped byte buffer and the file mapping that it represents remain
38 * valid until the buffer itself is garbage-collected.
39 *
40 * <p> The content of a mapped byte buffer can change at any time, for example
41 * if the content of the corresponding region of the mapped file is changed by
42 * this program or another. Whether or not such changes occur, and when they
43 * occur, is operating-system dependent and therefore unspecified.
44 *
45 * <a name="inaccess"><p> All or part of a mapped byte buffer may become
46 * inaccessible at any time, for example if the mapped file is truncated. An
47 * attempt to access an inaccessible region of a mapped byte buffer will not
48 * change the buffer's content and will cause an unspecified exception to be
49 * thrown either at the time of the access or at some later time. It is
50 * therefore strongly recommended that appropriate precautions be taken to
51 * avoid the manipulation of a mapped file by this program, or by a
52 * concurrently running program, except to read or write the file's content.
53 *
54 * <p> Mapped byte buffers otherwise behave no differently than ordinary direct
55 * byte buffers. </p>
56 *
57 *
58 * @author Mark Reinhold
59 * @author JSR-51 Expert Group
60 * @since 1.4
61 */
62
63public abstract class MappedByteBuffer
64 extends ByteBuffer
65{
66
67 // This is a little bit backwards: By rights MappedByteBuffer should be a
68 // subclass of DirectByteBuffer, but to keep the spec clear and simple, and
69 // for optimization purposes, it's easier to do it the other way around.
70 // This works because DirectByteBuffer is a package-private class.
71
72 // Volatile to make sure that the finalization thread sees the current
73 // value of this so that a region is not accidentally unmapped again later.
74 volatile boolean isAMappedBuffer; // package-private
75
76 // This should only be invoked by the DirectByteBuffer constructors
77 //
78 MappedByteBuffer(int mark, int pos, int lim, int cap, // package-private
79 boolean mapped)
80 {
81 super(mark, pos, lim, cap);
82 isAMappedBuffer = mapped;
83 }
84
85 MappedByteBuffer(int mark, int pos, int lim, int cap) { // package-private
86 super(mark, pos, lim, cap);
87 isAMappedBuffer = false;
88 }
89
90 private void checkMapped() {
91 if (!isAMappedBuffer)
92 // Can only happen if a luser explicitly casts a direct byte buffer
93 throw new UnsupportedOperationException();
94 }
95
96 /**
97 * Tells whether or not this buffer's content is resident in physical
98 * memory.
99 *
100 * <p> A return value of <tt>true</tt> implies that it is highly likely
101 * that all of the data in this buffer is resident in physical memory and
102 * may therefore be accessed without incurring any virtual-memory page
103 * faults or I/O operations. A return value of <tt>false</tt> does not
104 * necessarily imply that the buffer's content is not resident in physical
105 * memory.
106 *
107 * <p> The returned value is a hint, rather than a guarantee, because the
108 * underlying operating system may have paged out some of the buffer's data
109 * by the time that an invocation of this method returns. </p>
110 *
111 * @return <tt>true</tt> if it is likely that this buffer's content
112 * is resident in physical memory
113 */
114 public final boolean isLoaded() {
115 checkMapped();
116 if ((address == 0) || (capacity() == 0))
117 return true;
118 return isLoaded0(((DirectByteBuffer)this).address(), capacity());
119 }
120
121 /**
122 * Loads this buffer's content into physical memory.
123 *
124 * <p> This method makes a best effort to ensure that, when it returns,
125 * this buffer's content is resident in physical memory. Invoking this
126 * method may cause some number of page faults and I/O operations to
127 * occur. </p>
128 *
129 * @return This buffer
130 */
131 public final MappedByteBuffer load() {
132 checkMapped();
133 if ((address == 0) || (capacity() == 0))
134 return this;
135 load0(((DirectByteBuffer)this).address(), capacity(), Bits.pageSize());
136 return this;
137 }
138
139 /**
140 * Forces any changes made to this buffer's content to be written to the
141 * storage device containing the mapped file.
142 *
143 * <p> If the file mapped into this buffer resides on a local storage
144 * device then when this method returns it is guaranteed that all changes
145 * made to the buffer since it was created, or since this method was last
146 * invoked, will have been written to that device.
147 *
148 * <p> If the file does not reside on a local device then no such guarantee
149 * is made.
150 *
151 * <p> If this buffer was not mapped in read/write mode ({@link
152 * java.nio.channels.FileChannel.MapMode#READ_WRITE}) then invoking this
153 * method has no effect. </p>
154 *
155 * @return This buffer
156 */
157 public final MappedByteBuffer force() {
158 checkMapped();
159 if ((address == 0) || (capacity() == 0))
160 return this;
161 force0(((DirectByteBuffer)this).address(), capacity());
162 return this;
163 }
164
165 private native boolean isLoaded0(long address, long length);
166 private native int load0(long address, long length, int pageSize);
167 private native void force0(long address, long length);
168
169}