FileDispatcherImpl.java 6.3 KB
Newer Older
D
duke 已提交
1
/*
2
 * Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
D
duke 已提交
3 4 5 6
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
7
 * published by the Free Software Foundation.  Oracle designates this
D
duke 已提交
8
 * particular file as subject to the "Classpath" exception as provided
9
 * by Oracle in the LICENSE file that accompanied this code.
D
duke 已提交
10 11 12 13 14 15 16 17 18 19 20
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
21 22 23
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
D
duke 已提交
24 25 26 27
 */

package sun.nio.ch;

28 29 30
import java.io.FileDescriptor;
import java.io.IOException;
import java.security.PrivilegedAction;
31 32
import sun.misc.SharedSecrets;
import sun.misc.JavaIOFileDescriptorAccess;
D
duke 已提交
33

34 35 36 37
class FileDispatcherImpl extends FileDispatcher {

    // set to true if fast file transmission (TransmitFile) is enabled
    private static final boolean fastFileTransfer;
D
duke 已提交
38

39 40 41 42 43 44 45 46 47 48 49 50 51 52
    /**
     * Indicates if the dispatcher should first advance the file position
     * to the end of file when writing.
     */
    private final boolean append;

    FileDispatcherImpl(boolean append) {
        this.append = append;
    }

    FileDispatcherImpl() {
        this(false);
    }

53 54 55 56 57
    @Override
    boolean needsPositionLock() {
        return true;
    }

D
duke 已提交
58 59 60 61 62 63
    int read(FileDescriptor fd, long address, int len)
        throws IOException
    {
        return read0(fd, address, len);
    }

64 65
    int pread(FileDescriptor fd, long address, int len, long position)
        throws IOException
D
duke 已提交
66
    {
67
        return pread0(fd, address, len, position);
D
duke 已提交
68 69 70 71 72 73 74
    }

    long readv(FileDescriptor fd, long address, int len) throws IOException {
        return readv0(fd, address, len);
    }

    int write(FileDescriptor fd, long address, int len) throws IOException {
75
        return write0(fd, address, len, append);
D
duke 已提交
76 77
    }

78 79
    int pwrite(FileDescriptor fd, long address, int len, long position)
        throws IOException
D
duke 已提交
80
    {
81
        return pwrite0(fd, address, len, position);
D
duke 已提交
82 83 84
    }

    long writev(FileDescriptor fd, long address, int len) throws IOException {
85
        return writev0(fd, address, len, append);
D
duke 已提交
86 87
    }

88 89 90 91 92 93 94 95
    int force(FileDescriptor fd, boolean metaData) throws IOException {
        return force0(fd, metaData);
    }

    int truncate(FileDescriptor fd, long size) throws IOException {
        return truncate0(fd, size);
    }

96 97 98 99 100
    int allocate(FileDescriptor fd, long size) throws IOException {
        // truncate0() works for extending and truncating file size
        return truncate0(fd, size);
    }

101 102 103 104 105 106 107 108 109 110 111 112 113 114
    long size(FileDescriptor fd) throws IOException {
        return size0(fd);
    }

    int lock(FileDescriptor fd, boolean blocking, long pos, long size,
             boolean shared) throws IOException
    {
        return lock0(fd, blocking, pos, size, shared);
    }

    void release(FileDescriptor fd, long pos, long size) throws IOException {
        release0(fd, pos, size);
    }

D
duke 已提交
115 116 117 118
    void close(FileDescriptor fd) throws IOException {
        close0(fd);
    }

119 120 121 122 123 124 125 126 127 128
    FileDescriptor duplicateForMapping(FileDescriptor fd) throws IOException {
        // on Windows we need to keep a handle to the file
        JavaIOFileDescriptorAccess fdAccess =
            SharedSecrets.getJavaIOFileDescriptorAccess();
        FileDescriptor result = new FileDescriptor();
        long handle = duplicateHandle(fdAccess.getHandle(fd));
        fdAccess.setHandle(result, handle);
        return result;
    }

129 130 131 132 133 134 135 136 137 138 139 140 141
    boolean canTransferToDirectly(java.nio.channels.SelectableChannel sc) {
        return fastFileTransfer && sc.isBlocking();
    }

    boolean transferToDirectlyNeedsPositionLock() {
        return true;
    }

    static boolean isFastFileTransferRequested() {
        String fileTransferProp = java.security.AccessController.doPrivileged(
            new PrivilegedAction<String>() {
                @Override
                public String run() {
142
                    return System.getProperty("jdk.nio.enableFastFileTransfer");
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
                }
            });
        boolean enable;
        if ("".equals(fileTransferProp)) {
            enable = true;
        } else {
            enable = Boolean.parseBoolean(fileTransferProp);
        }
        return enable;
    }

    static {
        IOUtil.load();
        fastFileTransfer = isFastFileTransferRequested();
    }

D
duke 已提交
159 160 161 162 163 164 165 166 167 168 169
    //-- Native methods

    static native int read0(FileDescriptor fd, long address, int len)
        throws IOException;

    static native int pread0(FileDescriptor fd, long address, int len,
                             long position) throws IOException;

    static native long readv0(FileDescriptor fd, long address, int len)
        throws IOException;

170
    static native int write0(FileDescriptor fd, long address, int len, boolean append)
D
duke 已提交
171 172 173 174 175
        throws IOException;

    static native int pwrite0(FileDescriptor fd, long address, int len,
                             long position) throws IOException;

176
    static native long writev0(FileDescriptor fd, long address, int len, boolean append)
D
duke 已提交
177 178
        throws IOException;

179 180 181 182 183 184 185 186 187 188 189 190 191 192
    static native int force0(FileDescriptor fd, boolean metaData)
        throws IOException;

    static native int truncate0(FileDescriptor fd, long size)
        throws IOException;

    static native long size0(FileDescriptor fd) throws IOException;

    static native int lock0(FileDescriptor fd, boolean blocking, long pos,
                            long size, boolean shared) throws IOException;

    static native void release0(FileDescriptor fd, long pos, long size)
        throws IOException;

D
duke 已提交
193 194 195 196
    static native void close0(FileDescriptor fd) throws IOException;

    static native void closeByHandle(long fd) throws IOException;

197
    static native long duplicateHandle(long fd) throws IOException;
D
duke 已提交
198
}