jdk/src/java.base/share/classes/java/nio/MappedByteBuffer.java
author jjg
Wed, 26 Apr 2017 13:38:21 -0700
changeset 44844 b2b4d98404ba
parent 44369 b5c4e28a7521
permissions -rw-r--r--
8179364: update "<a name=" in java.base module to use id attribute Reviewed-by: darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 18574
diff changeset
     2
 * Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.nio;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
7025
6e002f9a2899 6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents: 6128
diff changeset
    28
import java.io.FileDescriptor;
33674
566777f73c32 8140606: Update library code to use internal Unsafe
chegar
parents: 32143
diff changeset
    29
import jdk.internal.misc.Unsafe;
6128
f95e5fdc1a65 6934977: (bf) MappedByteBuffer.load can SIGBUS if file is truncated
alanb
parents: 5506
diff changeset
    30
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * A direct byte buffer whose content is a memory-mapped region of a file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * <p> Mapped byte buffers are created via the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * java.nio.channels.FileChannel#map FileChannel.map} method.  This class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * extends the {@link ByteBuffer} class with operations that are specific to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * memory-mapped file regions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * <p> A mapped byte buffer and the file mapping that it represents remain
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * valid until the buffer itself is garbage-collected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * <p> The content of a mapped byte buffer can change at any time, for example
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * if the content of the corresponding region of the mapped file is changed by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * this program or another.  Whether or not such changes occur, and when they
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * occur, is operating-system dependent and therefore unspecified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *
44844
b2b4d98404ba 8179364: update "<a name=" in java.base module to use id attribute
jjg
parents: 44369
diff changeset
    48
 * <a id="inaccess"></a><p> All or part of a mapped byte buffer may become
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * inaccessible at any time, for example if the mapped file is truncated.  An
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * attempt to access an inaccessible region of a mapped byte buffer will not
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * change the buffer's content and will cause an unspecified exception to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * thrown either at the time of the access or at some later time.  It is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * therefore strongly recommended that appropriate precautions be taken to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * avoid the manipulation of a mapped file by this program, or by a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * concurrently running program, except to read or write the file's content.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * <p> Mapped byte buffers otherwise behave no differently than ordinary direct
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * byte buffers. </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * @author Mark Reinhold
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * @author JSR-51 Expert Group
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
public abstract class MappedByteBuffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    extends ByteBuffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    // This is a little bit backwards: By rights MappedByteBuffer should be a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    // subclass of DirectByteBuffer, but to keep the spec clear and simple, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    // for optimization purposes, it's easier to do it the other way around.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    // This works because DirectByteBuffer is a package-private class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
7025
6e002f9a2899 6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents: 6128
diff changeset
    75
    // For mapped buffers, a FileDescriptor that may be used for mapping
6e002f9a2899 6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents: 6128
diff changeset
    76
    // operations if valid; null if the buffer is not mapped.
6e002f9a2899 6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents: 6128
diff changeset
    77
    private final FileDescriptor fd;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    // This should only be invoked by the DirectByteBuffer constructors
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    MappedByteBuffer(int mark, int pos, int lim, int cap, // package-private
7025
6e002f9a2899 6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents: 6128
diff changeset
    82
                     FileDescriptor fd)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        super(mark, pos, lim, cap);
7025
6e002f9a2899 6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents: 6128
diff changeset
    85
        this.fd = fd;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    MappedByteBuffer(int mark, int pos, int lim, int cap) { // package-private
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        super(mark, pos, lim, cap);
7025
6e002f9a2899 6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents: 6128
diff changeset
    90
        this.fd = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    private void checkMapped() {
7025
6e002f9a2899 6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents: 6128
diff changeset
    94
        if (fd == null)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            // Can only happen if a luser explicitly casts a direct byte buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
6128
f95e5fdc1a65 6934977: (bf) MappedByteBuffer.load can SIGBUS if file is truncated
alanb
parents: 5506
diff changeset
    99
    // Returns the distance (in bytes) of the buffer from the page aligned address
f95e5fdc1a65 6934977: (bf) MappedByteBuffer.load can SIGBUS if file is truncated
alanb
parents: 5506
diff changeset
   100
    // of the mapping. Computed each time to avoid storing in every direct buffer.
f95e5fdc1a65 6934977: (bf) MappedByteBuffer.load can SIGBUS if file is truncated
alanb
parents: 5506
diff changeset
   101
    private long mappingOffset() {
f95e5fdc1a65 6934977: (bf) MappedByteBuffer.load can SIGBUS if file is truncated
alanb
parents: 5506
diff changeset
   102
        int ps = Bits.pageSize();
f95e5fdc1a65 6934977: (bf) MappedByteBuffer.load can SIGBUS if file is truncated
alanb
parents: 5506
diff changeset
   103
        long offset = address % ps;
f95e5fdc1a65 6934977: (bf) MappedByteBuffer.load can SIGBUS if file is truncated
alanb
parents: 5506
diff changeset
   104
        return (offset >= 0) ? offset : (ps + offset);
f95e5fdc1a65 6934977: (bf) MappedByteBuffer.load can SIGBUS if file is truncated
alanb
parents: 5506
diff changeset
   105
    }
f95e5fdc1a65 6934977: (bf) MappedByteBuffer.load can SIGBUS if file is truncated
alanb
parents: 5506
diff changeset
   106
f95e5fdc1a65 6934977: (bf) MappedByteBuffer.load can SIGBUS if file is truncated
alanb
parents: 5506
diff changeset
   107
    private long mappingAddress(long mappingOffset) {
f95e5fdc1a65 6934977: (bf) MappedByteBuffer.load can SIGBUS if file is truncated
alanb
parents: 5506
diff changeset
   108
        return address - mappingOffset;
f95e5fdc1a65 6934977: (bf) MappedByteBuffer.load can SIGBUS if file is truncated
alanb
parents: 5506
diff changeset
   109
    }
f95e5fdc1a65 6934977: (bf) MappedByteBuffer.load can SIGBUS if file is truncated
alanb
parents: 5506
diff changeset
   110
f95e5fdc1a65 6934977: (bf) MappedByteBuffer.load can SIGBUS if file is truncated
alanb
parents: 5506
diff changeset
   111
    private long mappingLength(long mappingOffset) {
f95e5fdc1a65 6934977: (bf) MappedByteBuffer.load can SIGBUS if file is truncated
alanb
parents: 5506
diff changeset
   112
        return (long)capacity() + mappingOffset;
f95e5fdc1a65 6934977: (bf) MappedByteBuffer.load can SIGBUS if file is truncated
alanb
parents: 5506
diff changeset
   113
    }
f95e5fdc1a65 6934977: (bf) MappedByteBuffer.load can SIGBUS if file is truncated
alanb
parents: 5506
diff changeset
   114
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * Tells whether or not this buffer's content is resident in physical
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * memory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     *
32143
394ab6a6658d 8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents: 27292
diff changeset
   119
     * <p> A return value of {@code true} implies that it is highly likely
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * that all of the data in this buffer is resident in physical memory and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * may therefore be accessed without incurring any virtual-memory page
32143
394ab6a6658d 8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents: 27292
diff changeset
   122
     * faults or I/O operations.  A return value of {@code false} does not
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * necessarily imply that the buffer's content is not resident in physical
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * memory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * <p> The returned value is a hint, rather than a guarantee, because the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * underlying operating system may have paged out some of the buffer's data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * by the time that an invocation of this method returns.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     *
32143
394ab6a6658d 8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents: 27292
diff changeset
   130
     * @return  {@code true} if it is likely that this buffer's content
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     *          is resident in physical memory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    public final boolean isLoaded() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        checkMapped();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        if ((address == 0) || (capacity() == 0))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            return true;
6128
f95e5fdc1a65 6934977: (bf) MappedByteBuffer.load can SIGBUS if file is truncated
alanb
parents: 5506
diff changeset
   137
        long offset = mappingOffset();
f95e5fdc1a65 6934977: (bf) MappedByteBuffer.load can SIGBUS if file is truncated
alanb
parents: 5506
diff changeset
   138
        long length = mappingLength(offset);
f95e5fdc1a65 6934977: (bf) MappedByteBuffer.load can SIGBUS if file is truncated
alanb
parents: 5506
diff changeset
   139
        return isLoaded0(mappingAddress(offset), length, Bits.pageCount(length));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
12696
74ad88cf81e2 7168505: (bf) MappedByteBuffer.load does not load buffer's content into memory
alanb
parents: 7668
diff changeset
   142
    // not used, but a potential target for a store, see load() for details.
74ad88cf81e2 7168505: (bf) MappedByteBuffer.load does not load buffer's content into memory
alanb
parents: 7668
diff changeset
   143
    private static byte unused;
74ad88cf81e2 7168505: (bf) MappedByteBuffer.load does not load buffer's content into memory
alanb
parents: 7668
diff changeset
   144
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * Loads this buffer's content into physical memory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * <p> This method makes a best effort to ensure that, when it returns,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * this buffer's content is resident in physical memory.  Invoking this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * method may cause some number of page faults and I/O operations to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * occur. </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * @return  This buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    public final MappedByteBuffer load() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        checkMapped();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        if ((address == 0) || (capacity() == 0))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            return this;
6128
f95e5fdc1a65 6934977: (bf) MappedByteBuffer.load can SIGBUS if file is truncated
alanb
parents: 5506
diff changeset
   159
        long offset = mappingOffset();
f95e5fdc1a65 6934977: (bf) MappedByteBuffer.load can SIGBUS if file is truncated
alanb
parents: 5506
diff changeset
   160
        long length = mappingLength(offset);
f95e5fdc1a65 6934977: (bf) MappedByteBuffer.load can SIGBUS if file is truncated
alanb
parents: 5506
diff changeset
   161
        load0(mappingAddress(offset), length);
f95e5fdc1a65 6934977: (bf) MappedByteBuffer.load can SIGBUS if file is truncated
alanb
parents: 5506
diff changeset
   162
12696
74ad88cf81e2 7168505: (bf) MappedByteBuffer.load does not load buffer's content into memory
alanb
parents: 7668
diff changeset
   163
        // Read a byte from each page to bring it into memory. A checksum
74ad88cf81e2 7168505: (bf) MappedByteBuffer.load does not load buffer's content into memory
alanb
parents: 7668
diff changeset
   164
        // is computed as we go along to prevent the compiler from otherwise
74ad88cf81e2 7168505: (bf) MappedByteBuffer.load does not load buffer's content into memory
alanb
parents: 7668
diff changeset
   165
        // considering the loop as dead code.
6128
f95e5fdc1a65 6934977: (bf) MappedByteBuffer.load can SIGBUS if file is truncated
alanb
parents: 5506
diff changeset
   166
        Unsafe unsafe = Unsafe.getUnsafe();
f95e5fdc1a65 6934977: (bf) MappedByteBuffer.load can SIGBUS if file is truncated
alanb
parents: 5506
diff changeset
   167
        int ps = Bits.pageSize();
f95e5fdc1a65 6934977: (bf) MappedByteBuffer.load can SIGBUS if file is truncated
alanb
parents: 5506
diff changeset
   168
        int count = Bits.pageCount(length);
f95e5fdc1a65 6934977: (bf) MappedByteBuffer.load can SIGBUS if file is truncated
alanb
parents: 5506
diff changeset
   169
        long a = mappingAddress(offset);
12696
74ad88cf81e2 7168505: (bf) MappedByteBuffer.load does not load buffer's content into memory
alanb
parents: 7668
diff changeset
   170
        byte x = 0;
6128
f95e5fdc1a65 6934977: (bf) MappedByteBuffer.load can SIGBUS if file is truncated
alanb
parents: 5506
diff changeset
   171
        for (int i=0; i<count; i++) {
12696
74ad88cf81e2 7168505: (bf) MappedByteBuffer.load does not load buffer's content into memory
alanb
parents: 7668
diff changeset
   172
            x ^= unsafe.getByte(a);
6128
f95e5fdc1a65 6934977: (bf) MappedByteBuffer.load can SIGBUS if file is truncated
alanb
parents: 5506
diff changeset
   173
            a += ps;
f95e5fdc1a65 6934977: (bf) MappedByteBuffer.load can SIGBUS if file is truncated
alanb
parents: 5506
diff changeset
   174
        }
12696
74ad88cf81e2 7168505: (bf) MappedByteBuffer.load does not load buffer's content into memory
alanb
parents: 7668
diff changeset
   175
        if (unused != 0)
74ad88cf81e2 7168505: (bf) MappedByteBuffer.load does not load buffer's content into memory
alanb
parents: 7668
diff changeset
   176
            unused = x;
6128
f95e5fdc1a65 6934977: (bf) MappedByteBuffer.load can SIGBUS if file is truncated
alanb
parents: 5506
diff changeset
   177
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * Forces any changes made to this buffer's content to be written to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * storage device containing the mapped file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * <p> If the file mapped into this buffer resides on a local storage
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * device then when this method returns it is guaranteed that all changes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * made to the buffer since it was created, or since this method was last
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * invoked, will have been written to that device.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * <p> If the file does not reside on a local device then no such guarantee
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * is made.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * <p> If this buffer was not mapped in read/write mode ({@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * java.nio.channels.FileChannel.MapMode#READ_WRITE}) then invoking this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * method has no effect. </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * @return  This buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    public final MappedByteBuffer force() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        checkMapped();
6128
f95e5fdc1a65 6934977: (bf) MappedByteBuffer.load can SIGBUS if file is truncated
alanb
parents: 5506
diff changeset
   201
        if ((address != 0) && (capacity() != 0)) {
f95e5fdc1a65 6934977: (bf) MappedByteBuffer.load can SIGBUS if file is truncated
alanb
parents: 5506
diff changeset
   202
            long offset = mappingOffset();
7025
6e002f9a2899 6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents: 6128
diff changeset
   203
            force0(fd, mappingAddress(offset), mappingLength(offset));
6128
f95e5fdc1a65 6934977: (bf) MappedByteBuffer.load can SIGBUS if file is truncated
alanb
parents: 5506
diff changeset
   204
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
6128
f95e5fdc1a65 6934977: (bf) MappedByteBuffer.load can SIGBUS if file is truncated
alanb
parents: 5506
diff changeset
   208
    private native boolean isLoaded0(long address, long length, int pageCount);
f95e5fdc1a65 6934977: (bf) MappedByteBuffer.load can SIGBUS if file is truncated
alanb
parents: 5506
diff changeset
   209
    private native void load0(long address, long length);
7025
6e002f9a2899 6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents: 6128
diff changeset
   210
    private native void force0(FileDescriptor fd, long address, long length);
27292
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   211
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   212
    // -- Covariant return type overrides
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   213
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   214
    /**
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   215
     * {@inheritDoc}
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   216
     */
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   217
    @Override
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   218
    public final MappedByteBuffer position(int newPosition) {
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   219
        super.position(newPosition);
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   220
        return this;
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   221
    }
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   222
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   223
    /**
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   224
     * {@inheritDoc}
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   225
     */
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   226
    @Override
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   227
    public final MappedByteBuffer limit(int newLimit) {
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   228
        super.limit(newLimit);
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   229
        return this;
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   230
    }
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   231
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   232
    /**
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   233
     * {@inheritDoc}
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   234
     */
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   235
    @Override
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   236
    public final MappedByteBuffer mark() {
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   237
        super.mark();
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   238
        return this;
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   239
    }
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   240
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   241
    /**
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   242
     * {@inheritDoc}
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   243
     */
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   244
    @Override
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   245
    public final MappedByteBuffer reset() {
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   246
        super.reset();
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   247
        return this;
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   248
    }
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   249
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   250
    /**
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   251
     * {@inheritDoc}
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   252
     */
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   253
    @Override
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   254
    public final MappedByteBuffer clear() {
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   255
        super.clear();
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   256
        return this;
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   257
    }
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   258
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   259
    /**
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   260
     * {@inheritDoc}
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   261
     */
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   262
    @Override
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   263
    public final MappedByteBuffer flip() {
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   264
        super.flip();
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   265
        return this;
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   266
    }
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   267
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   268
    /**
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   269
     * {@inheritDoc}
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   270
     */
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   271
    @Override
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   272
    public final MappedByteBuffer rewind() {
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   273
        super.rewind();
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   274
        return this;
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   275
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
}