1 /*
   2  * Copyright (c) 2000, 2010, 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     //  Added parameter writable for AIX platform port (not used on Windows).
  89     int force(FileDescriptor fd, boolean metaData, boolean writable) throws IOException {
  90         return force0(fd, metaData);
  91     }
  92 
  93     int truncate(FileDescriptor fd, long size) throws IOException {
  94         return truncate0(fd, size);
  95     }
  96 
  97     long size(FileDescriptor fd) throws IOException {
  98         return size0(fd);
  99     }
 100 
 101     int lock(FileDescriptor fd, boolean blocking, long pos, long size,
 102              boolean shared) throws IOException
 103     {
 104         return lock0(fd, blocking, pos, size, shared);
 105     }
 106 
 107     void release(FileDescriptor fd, long pos, long size) throws IOException {
 108         release0(fd, pos, size);
 109     }
 110 
 111     void close(FileDescriptor fd) throws IOException {
 112         close0(fd);
 113     }
 114 
 115     FileDescriptor duplicateForMapping(FileDescriptor fd) throws IOException {
 116         // on Windows we need to keep a handle to the file
 117         JavaIOFileDescriptorAccess fdAccess =
 118             SharedSecrets.getJavaIOFileDescriptorAccess();
 119         FileDescriptor result = new FileDescriptor();
 120         long handle = duplicateHandle(fdAccess.getHandle(fd));
 121         fdAccess.setHandle(result, handle);
 122         return result;
 123     }
 124 
 125     boolean canTransferToDirectly(java.nio.channels.SelectableChannel sc) {
 126         return fastFileTransfer && sc.isBlocking();
 127     }
 128 
 129     boolean transferToDirectlyNeedsPositionLock() {
 130         return true;
 131     }
 132 
 133     static boolean isFastFileTransferRequested() {
 134         String fileTransferProp = java.security.AccessController.doPrivileged(
 135             new PrivilegedAction<String>() {
 136                 @Override
 137                 public String run() {
 138                     return System.getProperty("jdk.nio.enableFastFileTransfer");
 139                 }
 140             });
 141         boolean enable;
 142         if ("".equals(fileTransferProp)) {
 143             enable = true;
 144         } else {
 145             enable = Boolean.parseBoolean(fileTransferProp);
 146         }
 147         return enable;
 148     }
 149 
 150     static {
 151         IOUtil.load();
 152         fastFileTransfer = isFastFileTransferRequested();
 153     }
 154 
 155     //-- Native methods
 156 
 157     static native int read0(FileDescriptor fd, long address, int len)
 158         throws IOException;
 159 
 160     static native int pread0(FileDescriptor fd, long address, int len,
 161                              long position) throws IOException;
 162 
 163     static native long readv0(FileDescriptor fd, long address, int len)
 164         throws IOException;
 165 
 166     static native int write0(FileDescriptor fd, long address, int len, boolean append)
 167         throws IOException;
 168 
 169     static native int pwrite0(FileDescriptor fd, long address, int len,
 170                              long position) throws IOException;
 171 
 172     static native long writev0(FileDescriptor fd, long address, int len, boolean append)
 173         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 void closeByHandle(long fd) throws IOException;
 192 
 193     static native long duplicateHandle(long fd) throws IOException;
 194 }