1 /*
   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.
   4  *
   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
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  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.security.PrivilegedAction;
  31 import sun.misc.SharedSecrets;
  32 import sun.misc.JavaIOFileDescriptorAccess;
  33 
  34 class FileDispatcherImpl extends FileDispatcher {
  35 
  36     // set to true if fast file transmission (TransmitFile) is enabled
  37     private static final boolean fastFileTransfer;
  38 
  39     /**
  40      * Indicates if the dispatcher should first advance the file position
  41      * to the end of file when writing.
  42      */
  43     private final boolean append;
  44 
  45     FileDispatcherImpl(boolean append) {
  46         this.append = append;
  47     }
  48 
  49     FileDispatcherImpl() {
  50         this(false);
  51     }
  52 
  53     @Override
  54     boolean needsPositionLock() {
  55         return true;
  56     }
  57 
  58     int read(FileDescriptor fd, long address, int len)
  59         throws IOException
  60     {
  61         return read0(fd, address, len);
  62     }
  63 
  64     int pread(FileDescriptor fd, long address, int len, long position)
  65         throws IOException
  66     {
  67         return pread0(fd, address, len, position);
  68     }
  69 
  70     long readv(FileDescriptor fd, long address, int len) throws IOException {
  71         return readv0(fd, address, len);
  72     }
  73 
  74     int write(FileDescriptor fd, long address, int len) throws IOException {
  75         return write0(fd, address, len, append);
  76     }
  77 
  78     int pwrite(FileDescriptor fd, long address, int len, long position)
  79         throws IOException
  80     {
  81         return pwrite0(fd, address, len, position);
  82     }
  83 
  84     long writev(FileDescriptor fd, long address, int len) throws IOException {
  85         return writev0(fd, address, len, append);
  86     }
  87 
  88     int force(FileDescriptor fd, boolean metaData) throws IOException {
  89         return force0(fd, metaData);
  90     }
  91 
  92     int truncate(FileDescriptor fd, long size) throws IOException {
  93         return truncate0(fd, size);
  94     }
  95 
  96     int allocate(FileDescriptor fd, long size) throws IOException {
  97         // truncate0() works for extending and truncating file size
  98         return truncate0(fd, size);
  99     }
 100 
 101     long size(FileDescriptor fd) throws IOException {
 102         return size0(fd);
 103     }
 104 
 105     int lock(FileDescriptor fd, boolean blocking, long pos, long size,
 106              boolean shared) throws IOException
 107     {
 108         return lock0(fd, blocking, pos, size, shared);
 109     }
 110 
 111     void release(FileDescriptor fd, long pos, long size) throws IOException {
 112         release0(fd, pos, size);
 113     }
 114 
 115     void close(FileDescriptor fd) throws IOException {
 116         close0(fd);
 117     }
 118 
 119     FileDescriptor duplicateForMapping(FileDescriptor fd) throws IOException {
 120         // on Windows we need to keep a handle to the file
 121         JavaIOFileDescriptorAccess fdAccess =
 122             SharedSecrets.getJavaIOFileDescriptorAccess();
 123         FileDescriptor result = new FileDescriptor();
 124         long handle = duplicateHandle(fdAccess.getHandle(fd));
 125         fdAccess.setHandle(result, handle);
 126         return result;
 127     }
 128 
 129     boolean canTransferToDirectly(java.nio.channels.SelectableChannel sc) {
 130         return fastFileTransfer && sc.isBlocking();
 131     }
 132 
 133     boolean transferToDirectlyNeedsPositionLock() {
 134         return true;
 135     }
 136 
 137     static boolean isFastFileTransferRequested() {
 138         String fileTransferProp = java.security.AccessController.doPrivileged(
 139             new PrivilegedAction<String>() {
 140                 @Override
 141                 public String run() {
 142                     return System.getProperty("jdk.nio.enableFastFileTransfer");
 143                 }
 144             });
 145         boolean enable;
 146         if ("".equals(fileTransferProp)) {
 147             enable = true;
 148         } else {
 149             enable = Boolean.parseBoolean(fileTransferProp);
 150         }
 151         return enable;
 152     }
 153 
 154     static {
 155         IOUtil.load();
 156         fastFileTransfer = isFastFileTransferRequested();
 157     }
 158 
 159     //-- Native methods
 160 
 161     static native int read0(FileDescriptor fd, long address, int len)
 162         throws IOException;
 163 
 164     static native int pread0(FileDescriptor fd, long address, int len,
 165                              long position) throws IOException;
 166 
 167     static native long readv0(FileDescriptor fd, long address, int len)
 168         throws IOException;
 169 
 170     static native int write0(FileDescriptor fd, long address, int len, boolean append)
 171         throws IOException;
 172 
 173     static native int pwrite0(FileDescriptor fd, long address, int len,
 174                              long position) throws IOException;
 175 
 176     static native long writev0(FileDescriptor fd, long address, int len, boolean append)
 177         throws IOException;
 178 
 179     static native int force0(FileDescriptor fd, boolean metaData)
 180         throws IOException;
 181 
 182     static native int truncate0(FileDescriptor fd, long size)
 183         throws IOException;
 184 
 185     static native long size0(FileDescriptor fd) throws IOException;
 186 
 187     static native int lock0(FileDescriptor fd, boolean blocking, long pos,
 188                             long size, boolean shared) throws IOException;
 189 
 190     static native void release0(FileDescriptor fd, long pos, long size)
 191         throws IOException;
 192 
 193     static native void close0(FileDescriptor fd) throws IOException;
 194 
 195     static native void closeByHandle(long fd) throws IOException;
 196 
 197     static native long duplicateHandle(long fd) throws IOException;
 198 }