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 
  31 import jdk.internal.access.JavaIOFileDescriptorAccess;
  32 import jdk.internal.access.SharedSecrets;
  33 
  34 class FileDispatcherImpl extends FileDispatcher {
  35 
  36     static {
  37         IOUtil.load();
  38         init();
  39     }
  40 
  41     private static final JavaIOFileDescriptorAccess fdAccess =
  42             SharedSecrets.getJavaIOFileDescriptorAccess();
  43 
  44     FileDispatcherImpl() {
  45     }
  46 
  47     int read(FileDescriptor fd, long address, int len) throws IOException {
  48         return read0(fd, address, len);
  49     }
  50 
  51     int pread(FileDescriptor fd, long address, int len, long position)
  52         throws IOException
  53     {
  54         return pread0(fd, address, len, position);
  55     }
  56 
  57     long readv(FileDescriptor fd, long address, int len) throws IOException {
  58         return readv0(fd, address, len);
  59     }
  60 
  61     int write(FileDescriptor fd, long address, int len) throws IOException {
  62         return write0(fd, address, len);
  63     }
  64 
  65     int pwrite(FileDescriptor fd, long address, int len, long position)
  66         throws IOException
  67     {
  68         return pwrite0(fd, address, len, position);
  69     }
  70 
  71     long writev(FileDescriptor fd, long address, int len)
  72         throws IOException
  73     {
  74         return writev0(fd, address, len);
  75     }
  76 
  77     long seek(FileDescriptor fd, long offset) throws IOException {
  78         return seek0(fd, offset);
  79     }
  80 
  81     int force(FileDescriptor fd, boolean metaData) throws IOException {
  82         return force0(fd, metaData);
  83     }
  84 
  85     int truncate(FileDescriptor fd, long size) throws IOException {
  86         return truncate0(fd, size);
  87     }
  88 
  89     long size(FileDescriptor fd) throws IOException {
  90         return size0(fd);
  91     }
  92 
  93     int lock(FileDescriptor fd, boolean blocking, long pos, long size,
  94              boolean shared) throws IOException
  95     {
  96         return lock0(fd, blocking, pos, size, shared);
  97     }
  98 
  99     void release(FileDescriptor fd, long pos, long size) throws IOException {
 100         release0(fd, pos, size);
 101     }
 102 
 103     void close(FileDescriptor fd) throws IOException {
 104         fdAccess.close(fd);
 105     }
 106 
 107     void preClose(FileDescriptor fd) throws IOException {
 108         preClose0(fd);
 109     }
 110 
 111     FileDescriptor duplicateForMapping(FileDescriptor fd) {
 112         // file descriptor not required for mapping operations; okay
 113         // to return invalid file descriptor.
 114         return new FileDescriptor();
 115     }
 116 
 117     boolean canTransferToDirectly(java.nio.channels.SelectableChannel sc) {
 118         return true;
 119     }
 120 
 121     boolean transferToDirectlyNeedsPositionLock() {
 122         return false;
 123     }
 124 
 125     int setDirectIO(FileDescriptor fd, String path) {
 126         int result = -1;
 127         try {
 128             result = setDirect0(fd);
 129         } catch (IOException e) {
 130             throw new UnsupportedOperationException
 131                 ("Error setting up DirectIO", e);
 132         }
 133         return result;
 134     }
 135 
 136     // -- Native methods --
 137 
 138     static native int read0(FileDescriptor fd, long address, int len)
 139         throws IOException;
 140 
 141     static native int pread0(FileDescriptor fd, long address, int len,
 142                              long position) throws IOException;
 143 
 144     static native long readv0(FileDescriptor fd, long address, int len)
 145         throws IOException;
 146 
 147     static native int write0(FileDescriptor fd, long address, int len)
 148         throws IOException;
 149 
 150     static native int pwrite0(FileDescriptor fd, long address, int len,
 151                              long position) throws IOException;
 152 
 153     static native long writev0(FileDescriptor fd, long address, int len)
 154         throws IOException;
 155 
 156     static native int force0(FileDescriptor fd, boolean metaData)
 157         throws IOException;
 158 
 159     static native long seek0(FileDescriptor fd, long offset)
 160         throws IOException;
 161 
 162     static native int truncate0(FileDescriptor fd, long size)
 163         throws IOException;
 164 
 165     static native long size0(FileDescriptor fd) throws IOException;
 166 
 167     static native int lock0(FileDescriptor fd, boolean blocking, long pos,
 168                             long size, boolean shared) throws IOException;
 169 
 170     static native void release0(FileDescriptor fd, long pos, long size)
 171         throws IOException;
 172 
 173     // Shared with SocketDispatcher and DatagramDispatcher but
 174     // NOT used by FileDispatcherImpl
 175     static native void close0(FileDescriptor fd) throws IOException;
 176 
 177     static native void preClose0(FileDescriptor fd) throws IOException;
 178 
 179     static native void closeIntFD(int fd) throws IOException;
 180 
 181     static native int setDirect0(FileDescriptor fd) throws IOException;
 182 
 183     static native void init();
 184 
 185 }