Path: blob/master/src/java.base/windows/classes/sun/nio/ch/FileDispatcherImpl.java
67773 views
/*1* Copyright (c) 2000, 2021, 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}126127boolean canTransferToFromOverlappedMap() {128return true;129}130131int setDirectIO(FileDescriptor fd, String path) {132int result = -1;133String filePath = path.substring(0, path.lastIndexOf(File.separator));134CharBuffer buffer = CharBuffer.allocate(filePath.length());135buffer.put(filePath);136try {137result = setDirect0(fd, buffer);138} catch (IOException e) {139throw new UnsupportedOperationException140("Error setting up DirectIO", e);141}142return result;143}144145static boolean isFastFileTransferRequested() {146String fileTransferProp = GetPropertyAction147.privilegedGetProperty("jdk.nio.enableFastFileTransfer", "false");148return fileTransferProp.isEmpty() ? true : Boolean.parseBoolean(fileTransferProp);149}150151static {152IOUtil.load();153fastFileTransfer = isFastFileTransferRequested();154}155156//-- Native methods157158static native int read0(FileDescriptor fd, long address, int len)159throws IOException;160161static native int pread0(FileDescriptor fd, long address, int len,162long position) throws IOException;163164static native long readv0(FileDescriptor fd, long address, int len)165throws IOException;166167static native int write0(FileDescriptor fd, long address, int len, boolean append)168throws IOException;169170static native int pwrite0(FileDescriptor fd, long address, int len,171long position) throws IOException;172173static native long writev0(FileDescriptor fd, long address, int len, boolean append)174throws IOException;175176static native long seek0(FileDescriptor fd, long offset) throws IOException;177178static native int force0(FileDescriptor fd, boolean metaData)179throws IOException;180181static native int truncate0(FileDescriptor fd, long size)182throws IOException;183184static native long size0(FileDescriptor fd) throws IOException;185186static native int lock0(FileDescriptor fd, boolean blocking, long pos,187long size, boolean shared) throws IOException;188189static native void release0(FileDescriptor fd, long pos, long size)190throws IOException;191192static native void close0(FileDescriptor fd) throws IOException;193194static native long duplicateHandle(long fd) throws IOException;195196static native int setDirect0(FileDescriptor fd, CharBuffer buffer) throws IOException;197}198199200