Path: blob/master/src/java.base/windows/classes/sun/nio/ch/FileDispatcherImpl.java
41139 views
/*1* Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.nio.ch;2627import java.io.FileDescriptor;28import java.io.IOException;29import jdk.internal.access.SharedSecrets;30import jdk.internal.access.JavaIOFileDescriptorAccess;31import sun.security.action.GetPropertyAction;32import java.io.File;33import java.nio.CharBuffer;3435class FileDispatcherImpl extends FileDispatcher {3637private static final JavaIOFileDescriptorAccess fdAccess =38SharedSecrets.getJavaIOFileDescriptorAccess();3940// set to true if fast file transmission (TransmitFile) is enabled41private static final boolean fastFileTransfer;4243FileDispatcherImpl() { }4445@Override46boolean needsPositionLock() {47return true;48}4950int read(FileDescriptor fd, long address, int len)51throws IOException52{53return read0(fd, address, len);54}5556int pread(FileDescriptor fd, long address, int len, long position)57throws IOException58{59return pread0(fd, address, len, position);60}6162long readv(FileDescriptor fd, long address, int len) throws IOException {63return readv0(fd, address, len);64}6566int write(FileDescriptor fd, long address, int len) throws IOException {67return write0(fd, address, len, fdAccess.getAppend(fd));68}6970int pwrite(FileDescriptor fd, long address, int len, long position)71throws IOException72{73return pwrite0(fd, address, len, position);74}7576long writev(FileDescriptor fd, long address, int len) throws IOException {77return writev0(fd, address, len, fdAccess.getAppend(fd));78}7980long seek(FileDescriptor fd, long offset) throws IOException {81return seek0(fd, offset);82}8384int force(FileDescriptor fd, boolean metaData) throws IOException {85return force0(fd, metaData);86}8788int truncate(FileDescriptor fd, long size) throws IOException {89return truncate0(fd, size);90}9192long size(FileDescriptor fd) throws IOException {93return size0(fd);94}9596int lock(FileDescriptor fd, boolean blocking, long pos, long size,97boolean shared) throws IOException98{99return lock0(fd, blocking, pos, size, shared);100}101102void release(FileDescriptor fd, long pos, long size) throws IOException {103release0(fd, pos, size);104}105106void close(FileDescriptor fd) throws IOException {107fdAccess.close(fd);108}109110FileDescriptor duplicateForMapping(FileDescriptor fd) throws IOException {111// on Windows we need to keep a handle to the file112FileDescriptor result = new FileDescriptor();113long handle = duplicateHandle(fdAccess.getHandle(fd));114fdAccess.setHandle(result, handle);115fdAccess.registerCleanup(result);116return result;117}118119boolean canTransferToDirectly(java.nio.channels.SelectableChannel sc) {120return fastFileTransfer && sc.isBlocking();121}122123boolean transferToDirectlyNeedsPositionLock() {124return true;125}126127int setDirectIO(FileDescriptor fd, String path) {128int result = -1;129String filePath = path.substring(0, path.lastIndexOf(File.separator));130CharBuffer buffer = CharBuffer.allocate(filePath.length());131buffer.put(filePath);132try {133result = setDirect0(fd, buffer);134} catch (IOException e) {135throw new UnsupportedOperationException136("Error setting up DirectIO", e);137}138return result;139}140141static boolean isFastFileTransferRequested() {142String fileTransferProp = GetPropertyAction143.privilegedGetProperty("jdk.nio.enableFastFileTransfer", "false");144return fileTransferProp.isEmpty() ? true : Boolean.parseBoolean(fileTransferProp);145}146147static {148IOUtil.load();149fastFileTransfer = isFastFileTransferRequested();150}151152//-- Native methods153154static native int read0(FileDescriptor fd, long address, int len)155throws IOException;156157static native int pread0(FileDescriptor fd, long address, int len,158long position) throws IOException;159160static native long readv0(FileDescriptor fd, long address, int len)161throws IOException;162163static native int write0(FileDescriptor fd, long address, int len, boolean append)164throws IOException;165166static native int pwrite0(FileDescriptor fd, long address, int len,167long position) throws IOException;168169static native long writev0(FileDescriptor fd, long address, int len, boolean append)170throws IOException;171172static native long seek0(FileDescriptor fd, long offset) throws IOException;173174static native int force0(FileDescriptor fd, boolean metaData)175throws IOException;176177static native int truncate0(FileDescriptor fd, long size)178throws IOException;179180static native long size0(FileDescriptor fd) throws IOException;181182static native int lock0(FileDescriptor fd, boolean blocking, long pos,183long size, boolean shared) throws IOException;184185static native void release0(FileDescriptor fd, long pos, long size)186throws IOException;187188static native void close0(FileDescriptor fd) throws IOException;189190static native long duplicateHandle(long fd) throws IOException;191192static native int setDirect0(FileDescriptor fd, CharBuffer buffer) throws IOException;193}194195196