KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > nio > MappedByteBuffer


1 /*
2  * @(#)MappedByteBuffer.java 1.23 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package java.nio;
9
10
11 /**
12  * A direct byte buffer whose content is a memory-mapped region of a file.
13  *
14  * <p> Mapped byte buffers are created via the {@link
15  * java.nio.channels.FileChannel#map FileChannel.map} method. This class
16  * extends the {@link ByteBuffer} class with operations that are specific to
17  * memory-mapped file regions.
18  *
19  * <p> A mapped byte buffer and the file mapping that it represents remain
20  * valid until the buffer itself is garbage-collected.
21  *
22  * <p> The content of a mapped byte buffer can change at any time, for example
23  * if the content of the corresponding region of the mapped file is changed by
24  * this program or another. Whether or not such changes occur, and when they
25  * occur, is operating-system dependent and therefore unspecified.
26  *
27  * <a name="inaccess"><p> All or part of a mapped byte buffer may become
28  * inaccessible at any time, for example if the mapped file is truncated. An
29  * attempt to access an inaccessible region of a mapped byte buffer will not
30  * change the buffer's content and will cause an unspecified exception to be
31  * thrown either at the time of the access or at some later time. It is
32  * therefore strongly recommended that appropriate precautions be taken to
33  * avoid the manipulation of a mapped file by this program, or by a
34  * concurrently running program, except to read or write the file's content.
35  *
36  * <p> Mapped byte buffers otherwise behave no differently than ordinary direct
37  * byte buffers. </p>
38  *
39  *
40  * @author Mark Reinhold
41  * @author JSR-51 Expert Group
42  * @version 1.23, 03/12/19
43  * @since 1.4
44  */

45
46 public abstract class MappedByteBuffer
47     extends ByteBuffer JavaDoc
48 {
49
50     // This is a little bit backwards: By rights MappedByteBuffer should be a
51
// subclass of DirectByteBuffer, but to keep the spec clear and simple, and
52
// for optimization purposes, it's easier to do it the other way around.
53
// This works because DirectByteBuffer is a package-private class.
54

55     // Volatile to make sure that the finalization thread sees the current
56
// value of this so that a region is not accidentally unmapped again later.
57
volatile boolean isAMappedBuffer; // package-private
58

59     // This should only be invoked by the DirectByteBuffer constructors
60
//
61
MappedByteBuffer(int mark, int pos, int lim, int cap, // package-private
62
boolean mapped)
63     {
64     super(mark, pos, lim, cap);
65     isAMappedBuffer = mapped;
66     }
67
68     MappedByteBuffer(int mark, int pos, int lim, int cap) { // package-private
69
super(mark, pos, lim, cap);
70     isAMappedBuffer = false;
71     }
72
73     private void checkMapped() {
74     if (!isAMappedBuffer)
75         // Can only happen if a luser explicitly casts a direct byte buffer
76
throw new UnsupportedOperationException JavaDoc();
77     }
78
79     /**
80      * Tells whether or not this buffer's content is resident in physical
81      * memory.
82      *
83      * <p> A return value of <tt>true</tt> implies that it is highly likely
84      * that all of the data in this buffer is resident in physical memory and
85      * may therefore be accessed without incurring any virtual-memory page
86      * faults or I/O operations. A return value of <tt>false</tt> does not
87      * necessarily imply that the buffer's content is not resident in physical
88      * memory.
89      *
90      * <p> The returned value is a hint, rather than a guarantee, because the
91      * underlying operating system may have paged out some of the buffer's data
92      * by the time that an invocation of this method returns. </p>
93      *
94      * @return <tt>true</tt> if it is likely that this buffer's content
95      * is resident in physical memory
96      */

97     public final boolean isLoaded() {
98     checkMapped();
99         if ((address == 0) || (capacity() == 0))
100             return true;
101     return isLoaded0(((DirectByteBuffer JavaDoc)this).address(), capacity());
102     }
103
104     /**
105      * Loads this buffer's content into physical memory.
106      *
107      * <p> This method makes a best effort to ensure that, when it returns,
108      * this buffer's content is resident in physical memory. Invoking this
109      * method may cause some number of page faults and I/O operations to
110      * occur. </p>
111      *
112      * @return This buffer
113      */

114     public final MappedByteBuffer JavaDoc load() {
115     checkMapped();
116         if ((address == 0) || (capacity() == 0))
117             return this;
118     load0(((DirectByteBuffer JavaDoc)this).address(), capacity(), Bits.pageSize());
119     return this;
120     }
121
122     /**
123      * Forces any changes made to this buffer's content to be written to the
124      * storage device containing the mapped file.
125      *
126      * <p> If the file mapped into this buffer resides on a local storage
127      * device then when this method returns it is guaranteed that all changes
128      * made to the buffer since it was created, or since this method was last
129      * invoked, will have been written to that device.
130      *
131      * <p> If the file does not reside on a local device then no such guarantee
132      * is made.
133      *
134      * <p> If this buffer was not mapped in read/write mode ({@link
135      * java.nio.channels.FileChannel.MapMode#READ_WRITE}) then invoking this
136      * method has no effect. </p>
137      *
138      * @return This buffer
139      */

140     public final MappedByteBuffer JavaDoc force() {
141     checkMapped();
142         if ((address == 0) || (capacity() == 0))
143             return this;
144     force0(((DirectByteBuffer JavaDoc)this).address(), capacity());
145     return this;
146     }
147
148     private native boolean isLoaded0(long address, long length);
149     private native int load0(long address, long length, int pageSize);
150     private native void force0(long address, long length);
151
152 }
153
Popular Tags