1 /*
   2  * Copyright (c) 2000, 2009, 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.*;
  29 import sun.misc.SharedSecrets;
  30 import sun.misc.JavaIOFileDescriptorAccess;
  31 
  32 class FileDispatcherImpl extends FileDispatcher
  33 {
  34     static {
  35         Util.load();
  36     }
  37 
  38     /**
  39      * Indicates if the dispatcher should first advance the file position
  40      * to the end of file when writing.
  41      */
  42     private final boolean append;
  43 
  44     FileDispatcherImpl(boolean append) {
  45         this.append = append;
  46     }
  47 
  48     FileDispatcherImpl() {
  49         this(false);
  50     }
  51 
  52     int read(FileDescriptor fd, long address, int len)
  53         throws IOException
  54     {
  55         return read0(fd, address, len);
  56     }
  57 
  58     int pread(FileDescriptor fd, long address, int len,
  59                              long position, Object lock) throws IOException
  60     {
  61         synchronized(lock) {
  62             return pread0(fd, address, len, position);
  63         }
  64     }
  65 
  66     long readv(FileDescriptor fd, long address, int len) throws IOException {
  67         return readv0(fd, address, len);
  68     }
  69 
  70     int write(FileDescriptor fd, long address, int len) throws IOException {
  71         return write0(fd, address, len, append);
  72     }
  73 
  74     int pwrite(FileDescriptor fd, long address, int len,
  75                              long position, Object lock) throws IOException
  76     {
  77         synchronized(lock) {
  78             return pwrite0(fd, address, len, position);
  79         }
  80     }
  81 
  82     long writev(FileDescriptor fd, long address, int len) throws IOException {
  83         return writev0(fd, address, len, append);
  84     }
  85 
  86     int force(FileDescriptor fd, boolean metaData) throws IOException {
  87         return force0(fd, metaData);
  88     }
  89 
  90     int truncate(FileDescriptor fd, long size) throws IOException {
  91         return truncate0(fd, size);
  92     }
  93 
  94     long size(FileDescriptor fd) throws IOException {
  95         return size0(fd);
  96     }
  97 
  98     int lock(FileDescriptor fd, boolean blocking, long pos, long size,
  99              boolean shared) throws IOException
 100     {
 101         return lock0(fd, blocking, pos, size, shared);
 102     }
 103 
 104     void release(FileDescriptor fd, long pos, long size) throws IOException {
 105         release0(fd, pos, size);
 106     }
 107 
 108     void close(FileDescriptor fd) throws IOException {
 109         close0(fd);
 110     }
 111 
 112     FileDescriptor duplicateForMapping(FileDescriptor fd) throws IOException {
 113         // on Windows we need to keep a handle to the file
 114         JavaIOFileDescriptorAccess fdAccess =
 115             SharedSecrets.getJavaIOFileDescriptorAccess();
 116         FileDescriptor result = new FileDescriptor();
 117         long handle = duplicateHandle(fdAccess.getHandle(fd));
 118         fdAccess.setHandle(result, handle);
 119         return result;
 120     }
 121 
 122     //-- Native methods
 123 
 124     static native int read0(FileDescriptor fd, long address, int len)
 125         throws IOException;
 126 
 127     static native int pread0(FileDescriptor fd, long address, int len,
 128                              long position) throws IOException;
 129 
 130     static native long readv0(FileDescriptor fd, long address, int len)
 131         throws IOException;
 132 
 133     static native int write0(FileDescriptor fd, long address, int len, boolean append)
 134         throws IOException;
 135 
 136     static native int pwrite0(FileDescriptor fd, long address, int len,
 137                              long position) throws IOException;
 138 
 139     static native long writev0(FileDescriptor fd, long address, int len, boolean append)
 140         throws IOException;
 141 
 142     static native int force0(FileDescriptor fd, boolean metaData)
 143         throws IOException;
 144 
 145     static native int truncate0(FileDescriptor fd, long size)
 146         throws IOException;
 147 
 148     static native long size0(FileDescriptor fd) throws IOException;
 149 
 150     static native int lock0(FileDescriptor fd, boolean blocking, long pos,
 151                             long size, boolean shared) throws IOException;
 152 
 153     static native void release0(FileDescriptor fd, long pos, long size)
 154         throws IOException;
 155 
 156     static native void close0(FileDescriptor fd) throws IOException;
 157 
 158     static native void closeByHandle(long fd) throws IOException;
 159 
 160     static native long duplicateHandle(long fd) throws IOException;
 161 }