src/java.base/share/classes/java/nio/MappedByteBuffer.java
changeset 48847 da4b1106787e
parent 47216 71c04702a3d5
child 49210 7c795d301dbf
equal deleted inserted replaced
48846:fe434d568439 48847:da4b1106787e
     1 /*
     1 /*
     2  * Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
    88     MappedByteBuffer(int mark, int pos, int lim, int cap) { // package-private
    88     MappedByteBuffer(int mark, int pos, int lim, int cap) { // package-private
    89         super(mark, pos, lim, cap);
    89         super(mark, pos, lim, cap);
    90         this.fd = null;
    90         this.fd = null;
    91     }
    91     }
    92 
    92 
    93     private void checkMapped() {
       
    94         if (fd == null)
       
    95             // Can only happen if a luser explicitly casts a direct byte buffer
       
    96             throw new UnsupportedOperationException();
       
    97     }
       
    98 
       
    99     // Returns the distance (in bytes) of the buffer from the page aligned address
    93     // Returns the distance (in bytes) of the buffer from the page aligned address
   100     // of the mapping. Computed each time to avoid storing in every direct buffer.
    94     // of the mapping. Computed each time to avoid storing in every direct buffer.
   101     private long mappingOffset() {
    95     private long mappingOffset() {
   102         int ps = Bits.pageSize();
    96         int ps = Bits.pageSize();
   103         long offset = address % ps;
    97         long offset = address % ps;
   129      *
   123      *
   130      * @return  {@code true} if it is likely that this buffer's content
   124      * @return  {@code true} if it is likely that this buffer's content
   131      *          is resident in physical memory
   125      *          is resident in physical memory
   132      */
   126      */
   133     public final boolean isLoaded() {
   127     public final boolean isLoaded() {
   134         checkMapped();
   128         if (fd == null) {
       
   129             return true;
       
   130         }
   135         if ((address == 0) || (capacity() == 0))
   131         if ((address == 0) || (capacity() == 0))
   136             return true;
   132             return true;
   137         long offset = mappingOffset();
   133         long offset = mappingOffset();
   138         long length = mappingLength(offset);
   134         long length = mappingLength(offset);
   139         return isLoaded0(mappingAddress(offset), length, Bits.pageCount(length));
   135         return isLoaded0(mappingAddress(offset), length, Bits.pageCount(length));
   151      * occur. </p>
   147      * occur. </p>
   152      *
   148      *
   153      * @return  This buffer
   149      * @return  This buffer
   154      */
   150      */
   155     public final MappedByteBuffer load() {
   151     public final MappedByteBuffer load() {
   156         checkMapped();
   152         if (fd == null) {
       
   153             return this;
       
   154         }
   157         if ((address == 0) || (capacity() == 0))
   155         if ((address == 0) || (capacity() == 0))
   158             return this;
   156             return this;
   159         long offset = mappingOffset();
   157         long offset = mappingOffset();
   160         long length = mappingLength(offset);
   158         long length = mappingLength(offset);
   161         load0(mappingAddress(offset), length);
   159         load0(mappingAddress(offset), length);
   195      * method has no effect. </p>
   193      * method has no effect. </p>
   196      *
   194      *
   197      * @return  This buffer
   195      * @return  This buffer
   198      */
   196      */
   199     public final MappedByteBuffer force() {
   197     public final MappedByteBuffer force() {
   200         checkMapped();
   198         if (fd == null) {
       
   199             return this;
       
   200         }
   201         if ((address != 0) && (capacity() != 0)) {
   201         if ((address != 0) && (capacity() != 0)) {
   202             long offset = mappingOffset();
   202             long offset = mappingOffset();
   203             force0(fd, mappingAddress(offset), mappingLength(offset));
   203             force0(fd, mappingAddress(offset), mappingLength(offset));
   204         }
   204         }
   205         return this;
   205         return this;