Source Code Cross Referenced for MappedByteBuffer.java in  » 6.0-JDK-Core » io-nio » java » nio » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Home
Java Source Code / Java Documentation
1.6.0 JDK Core
2.6.0 JDK Modules
3.6.0 JDK Modules com.sun
4.6.0 JDK Modules com.sun.java
5.6.0 JDK Modules sun
6.6.0 JDK Platform
7.Ajax
8.Apache Harmony Java SE
9.Aspect oriented
10.Authentication Authorization
11.Blogger System
12.Build
13.Byte Code
14.Cache
15.Chart
16.Chat
17.Code Analyzer
18.Collaboration
19.Content Management System
20.Database Client
21.Database DBMS
22.Database JDBC Connection Pool
23.Database ORM
24.Development
25.EJB Server
26.ERP CRM Financial
27.ESB
28.Forum
29.Game
30.GIS
31.Graphic 3D
32.Graphic Library
33.Groupware
34.HTML Parser
35.IDE
36.IDE Eclipse
37.IDE Netbeans
38.Installer
39.Internationalization Localization
40.Inversion of Control
41.Issue Tracking
42.J2EE
43.J2ME
44.JBoss
45.JMS
46.JMX
47.Library
48.Mail Clients
49.Music
50.Net
51.Parser
52.PDF
53.Portal
54.Profiler
55.Project Management
56.Report
57.RSS RDF
58.Rule Engine
59.Science
60.Scripting
61.Search Engine
62.Security
63.Sevlet Container
64.Source Control
65.Swing Library
66.Template Engine
67.Test Coverage
68.Testing
69.UML
70.Web Crawler
71.Web Framework
72.Web Mail
73.Web Server
74.Web Services
75.Web Services apache cxf 2.2.6
76.Web Services AXIS2
77.Wiki Engine
78.Workflow Engines
79.XML
80.XML UI
Java Source Code / Java Documentation » 6.0 JDK Core » io nio » java.nio 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.