< prev index next >

src/java.base/share/classes/sun/nio/ch/FileChannelImpl.java

Print this page




  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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.nio.ch;
  27 
  28 import java.io.FileDescriptor;
  29 import java.io.IOException;

  30 import java.nio.ByteBuffer;
  31 import java.nio.MappedByteBuffer;
  32 import java.nio.channels.ClosedByInterruptException;
  33 import java.nio.channels.ClosedChannelException;
  34 import java.nio.channels.FileChannel;
  35 import java.nio.channels.FileLock;
  36 import java.nio.channels.FileLockInterruptionException;
  37 import java.nio.channels.NonReadableChannelException;
  38 import java.nio.channels.NonWritableChannelException;
  39 import java.nio.channels.OverlappingFileLockException;
  40 import java.nio.channels.ReadableByteChannel;
  41 import java.nio.channels.SelectableChannel;
  42 import java.nio.channels.WritableByteChannel;
  43 import java.security.AccessController;
  44 import java.util.ArrayList;
  45 import java.util.List;
  46 
  47 import jdk.internal.misc.JavaIOFileDescriptorAccess;
  48 import jdk.internal.misc.JavaNioAccess;
  49 import jdk.internal.misc.SharedSecrets;
  50 import jdk.internal.ref.Cleaner;
  51 import sun.security.action.GetPropertyAction;
  52 
  53 public class FileChannelImpl
  54     extends FileChannel
  55 {
  56     // Memory allocation size for mapping buffers
  57     private static final long allocationGranularity;
  58 
  59     // Access to FileDispatcher internals
  60     private static final JavaIOFileDescriptorAccess fdAccess =
  61         SharedSecrets.getJavaIOFileDescriptorAccess();
  62 
  63     // Used to make native read and write calls
  64     private final FileDispatcher nd;
  65 
  66     // File descriptor
  67     private final FileDescriptor fd;
  68 
  69     // File access mode (immutable)
  70     private final boolean writable;


 828             address = 0;
 829 
 830             // if this mapping has a valid file descriptor then we close it
 831             if (fd.valid()) {
 832                 try {
 833                     nd.close(fd);
 834                 } catch (IOException ignore) {
 835                     // nothing we can do
 836                 }
 837             }
 838 
 839             synchronized (Unmapper.class) {
 840                 count--;
 841                 totalSize -= size;
 842                 totalCapacity -= cap;
 843             }
 844         }
 845     }
 846 
 847     private static void unmap(MappedByteBuffer bb) {
 848         Cleaner cl = ((DirectBuffer)bb).cleaner();
 849         if (cl != null)
 850             cl.clean();
 851     }
 852 
 853     private static final int MAP_RO = 0;
 854     private static final int MAP_RW = 1;
 855     private static final int MAP_PV = 2;
 856 
 857     public MappedByteBuffer map(MapMode mode, long position, long size)
 858         throws IOException
 859     {
 860         ensureOpen();
 861         if (mode == null)
 862             throw new NullPointerException("Mode is null");
 863         if (position < 0L)
 864             throw new IllegalArgumentException("Negative position");
 865         if (size < 0L)
 866             throw new IllegalArgumentException("Negative size");
 867         if (position + size < 0)
 868             throw new IllegalArgumentException("Position + size overflow");




  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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.nio.ch;
  27 
  28 import java.io.FileDescriptor;
  29 import java.io.IOException;
  30 import java.lang.ref.Cleaner;
  31 import java.nio.ByteBuffer;
  32 import java.nio.MappedByteBuffer;
  33 import java.nio.channels.ClosedByInterruptException;
  34 import java.nio.channels.ClosedChannelException;
  35 import java.nio.channels.FileChannel;
  36 import java.nio.channels.FileLock;
  37 import java.nio.channels.FileLockInterruptionException;
  38 import java.nio.channels.NonReadableChannelException;
  39 import java.nio.channels.NonWritableChannelException;
  40 import java.nio.channels.OverlappingFileLockException;
  41 import java.nio.channels.ReadableByteChannel;
  42 import java.nio.channels.SelectableChannel;
  43 import java.nio.channels.WritableByteChannel;
  44 import java.security.AccessController;
  45 import java.util.ArrayList;
  46 import java.util.List;
  47 
  48 import jdk.internal.misc.JavaIOFileDescriptorAccess;
  49 import jdk.internal.misc.JavaNioAccess;
  50 import jdk.internal.misc.SharedSecrets;

  51 import sun.security.action.GetPropertyAction;
  52 
  53 public class FileChannelImpl
  54     extends FileChannel
  55 {
  56     // Memory allocation size for mapping buffers
  57     private static final long allocationGranularity;
  58 
  59     // Access to FileDispatcher internals
  60     private static final JavaIOFileDescriptorAccess fdAccess =
  61         SharedSecrets.getJavaIOFileDescriptorAccess();
  62 
  63     // Used to make native read and write calls
  64     private final FileDispatcher nd;
  65 
  66     // File descriptor
  67     private final FileDescriptor fd;
  68 
  69     // File access mode (immutable)
  70     private final boolean writable;


 828             address = 0;
 829 
 830             // if this mapping has a valid file descriptor then we close it
 831             if (fd.valid()) {
 832                 try {
 833                     nd.close(fd);
 834                 } catch (IOException ignore) {
 835                     // nothing we can do
 836                 }
 837             }
 838 
 839             synchronized (Unmapper.class) {
 840                 count--;
 841                 totalSize -= size;
 842                 totalCapacity -= cap;
 843             }
 844         }
 845     }
 846 
 847     private static void unmap(MappedByteBuffer bb) {
 848         Cleaner.Cleanable cl = ((DirectBuffer)bb).cleaner();
 849         if (cl != null)
 850             cl.clean();
 851     }
 852 
 853     private static final int MAP_RO = 0;
 854     private static final int MAP_RW = 1;
 855     private static final int MAP_PV = 2;
 856 
 857     public MappedByteBuffer map(MapMode mode, long position, long size)
 858         throws IOException
 859     {
 860         ensureOpen();
 861         if (mode == null)
 862             throw new NullPointerException("Mode is null");
 863         if (position < 0L)
 864             throw new IllegalArgumentException("Negative position");
 865         if (size < 0L)
 866             throw new IllegalArgumentException("Negative size");
 867         if (position + size < 0)
 868             throw new IllegalArgumentException("Position + size overflow");


< prev index next >