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 jdk.internal.access.SharedSecrets;
  31 import jdk.internal.access.JavaIOFileDescriptorAccess;
  32 import sun.security.action.GetPropertyAction;
  33 import java.io.File;
  34 import java.nio.CharBuffer;
  35 
  36 class FileDispatcherImpl extends FileDispatcher {
  37 
  38     private static final JavaIOFileDescriptorAccess fdAccess =
  39         SharedSecrets.getJavaIOFileDescriptorAccess();
  40 
  41     // set to true if fast file transmission (TransmitFile) is enabled
  42     private static final boolean fastFileTransfer;
  43 
  44     FileDispatcherImpl() { }
  45 
  46     @Override
  47     boolean needsPositionLock() {
  48         return true;
  49     }
  50 
  51     int read(FileDescriptor fd, long address, int len)
  52         throws IOException
  53     {
  54         return read0(fd, address, len);
  55     }
  56 
  57     int pread(FileDescriptor fd, long address, int len, long position)
  58         throws IOException
  59     {
  60         return pread0(fd, address, len, position);
  61     }
  62 
  63     long readv(FileDescriptor fd, long address, int len) throws IOException {
  64         return readv0(fd, address, len);
  65     }
  66 
  67     int write(FileDescriptor fd, long address, int len) throws IOException {
  68         return write0(fd, address, len, fdAccess.getAppend(fd));
  69     }
  70 
  71     int pwrite(FileDescriptor fd, long address, int len, long position)
  72         throws IOException
  73     {
  74         return pwrite0(fd, address, len, position);
  75     }
  76 
  77     long writev(FileDescriptor fd, long address, int len) throws IOException {
  78         return writev0(fd, address, len, fdAccess.getAppend(fd));
  79     }
  80 
  81     long seek(FileDescriptor fd, long offset) throws IOException {
  82         return seek0(fd, offset);
  83     }
  84 
  85     int force(FileDescriptor fd, boolean metaData) throws IOException {
  86         return force0(fd, metaData);
  87     }
  88 
  89     int truncate(FileDescriptor fd, long size) throws IOException {
  90         return truncate0(fd, size);
  91     }
  92 
  93     long size(FileDescriptor fd) throws IOException {
  94         return size0(fd);
  95     }
  96 
  97     int lock(FileDescriptor fd, boolean blocking, long pos, long size,
  98              boolean shared) throws IOException
  99     {
 100         return lock0(fd, blocking, pos, size, shared);
 101     }
 102 
 103     void release(FileDescriptor fd, long pos, long size) throws IOException {
 104         release0(fd, pos, size);
 105     }
 106 
 107     void close(FileDescriptor fd) throws IOException {
 108         fdAccess.close(fd);
 109     }
 110 
 111     FileDescriptor duplicateForMapping(FileDescriptor fd) throws IOException {
 112         // on Windows we need to keep a handle to the file
 113         FileDescriptor result = new FileDescriptor();
 114         long handle = duplicateHandle(fdAccess.getHandle(fd));
 115         fdAccess.setHandle(result, handle);
 116         fdAccess.registerCleanup(result);
 117         return result;
 118     }
 119 
 120     boolean canTransferToDirectly(java.nio.channels.SelectableChannel sc) {
 121         return fastFileTransfer && sc.isBlocking();
 122     }
 123 
 124     boolean transferToDirectlyNeedsPositionLock() {
 125         return true;
 126     }
 127 
 128     int setDirectIO(FileDescriptor fd, String path) {
 129         int result = -1;
 130         String filePath = path.substring(0, path.lastIndexOf(File.separator));
 131         CharBuffer buffer = CharBuffer.allocate(filePath.length());
 132         buffer.put(filePath);
 133         try {
 134             result = setDirect0(fd, buffer);
 135         } catch (IOException e) {
 136             throw new UnsupportedOperationException
 137                 ("Error setting up DirectIO", e);
 138         }
 139         return result;
 140     }
 141 
 142     static boolean isFastFileTransferRequested() {
 143         String fileTransferProp = GetPropertyAction
 144                 .privilegedGetProperty("jdk.nio.enableFastFileTransfer", "false");
 145         return fileTransferProp.isEmpty() ? true : Boolean.parseBoolean(fileTransferProp);
 146     }
 147 
 148     static {
 149         IOUtil.load();
 150         fastFileTransfer = isFastFileTransferRequested();
 151     }
 152 
 153     //-- Native methods
 154 
 155     static native int read0(FileDescriptor fd, long address, int len)
 156         throws IOException;
 157 
 158     static native int pread0(FileDescriptor fd, long address, int len,
 159                              long position) throws IOException;
 160 
 161     static native long readv0(FileDescriptor fd, long address, int len)
 162         throws IOException;
 163 
 164     static native int write0(FileDescriptor fd, long address, int len, boolean append)
 165         throws IOException;
 166 
 167     static native int pwrite0(FileDescriptor fd, long address, int len,
 168                              long position) throws IOException;
 169 
 170     static native long writev0(FileDescriptor fd, long address, int len, boolean append)
 171         throws IOException;
 172 
 173     static native long seek0(FileDescriptor fd, long offset) throws IOException;
 174 
 175     static native int force0(FileDescriptor fd, boolean metaData)
 176         throws IOException;
 177 
 178     static native int truncate0(FileDescriptor fd, long size)
 179         throws IOException;
 180 
 181     static native long size0(FileDescriptor fd) throws IOException;
 182 
 183     static native int lock0(FileDescriptor fd, boolean blocking, long pos,
 184                             long size, boolean shared) throws IOException;
 185 
 186     static native void release0(FileDescriptor fd, long pos, long size)
 187         throws IOException;
 188 
 189     static native void close0(FileDescriptor fd) throws IOException;
 190 
 191     static native long duplicateHandle(long fd) throws IOException;
 192 
 193     static native int setDirect0(FileDescriptor fd, CharBuffer buffer) throws IOException;
 194 }