Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/windows/classes/sun/nio/ch/FileDispatcherImpl.java
32288 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 java.security.PrivilegedAction;30import sun.misc.SharedSecrets;31import sun.misc.JavaIOFileDescriptorAccess;3233class FileDispatcherImpl extends FileDispatcher {3435// set to true if fast file transmission (TransmitFile) is enabled36private static final boolean fastFileTransfer;3738/**39* Indicates if the dispatcher should first advance the file position40* to the end of file when writing.41*/42private final boolean append;4344FileDispatcherImpl(boolean append) {45this.append = append;46}4748FileDispatcherImpl() {49this(false);50}5152@Override53boolean needsPositionLock() {54return true;55}5657int read(FileDescriptor fd, long address, int len)58throws IOException59{60return read0(fd, address, len);61}6263int pread(FileDescriptor fd, long address, int len, long position)64throws IOException65{66return pread0(fd, address, len, position);67}6869long readv(FileDescriptor fd, long address, int len) throws IOException {70return readv0(fd, address, len);71}7273int write(FileDescriptor fd, long address, int len) throws IOException {74return write0(fd, address, len, append);75}7677int pwrite(FileDescriptor fd, long address, int len, long position)78throws IOException79{80return pwrite0(fd, address, len, position);81}8283long writev(FileDescriptor fd, long address, int len) throws IOException {84return writev0(fd, address, len, append);85}8687long seek(FileDescriptor fd, long offset) throws IOException {88return seek0(fd, offset);89}9091int force(FileDescriptor fd, boolean metaData) throws IOException {92return force0(fd, metaData);93}9495int truncate(FileDescriptor fd, long size) throws IOException {96return truncate0(fd, size);97}9899long size(FileDescriptor fd) throws IOException {100return size0(fd);101}102103int lock(FileDescriptor fd, boolean blocking, long pos, long size,104boolean shared) throws IOException105{106return lock0(fd, blocking, pos, size, shared);107}108109void release(FileDescriptor fd, long pos, long size) throws IOException {110release0(fd, pos, size);111}112113void close(FileDescriptor fd) throws IOException {114close0(fd);115}116117FileDescriptor duplicateForMapping(FileDescriptor fd) throws IOException {118// on Windows we need to keep a handle to the file119JavaIOFileDescriptorAccess fdAccess =120SharedSecrets.getJavaIOFileDescriptorAccess();121FileDescriptor result = new FileDescriptor();122long handle = duplicateHandle(fdAccess.getHandle(fd));123fdAccess.setHandle(result, handle);124return result;125}126127boolean canTransferToDirectly(java.nio.channels.SelectableChannel sc) {128return fastFileTransfer && sc.isBlocking();129}130131boolean transferToDirectlyNeedsPositionLock() {132return true;133}134135static boolean isFastFileTransferRequested() {136String fileTransferProp = java.security.AccessController.doPrivileged(137new PrivilegedAction<String>() {138@Override139public String run() {140return System.getProperty("jdk.nio.enableFastFileTransfer");141}142});143boolean enable;144if ("".equals(fileTransferProp)) {145enable = true;146} else {147enable = Boolean.parseBoolean(fileTransferProp);148}149return enable;150}151152static {153IOUtil.load();154fastFileTransfer = isFastFileTransferRequested();155}156157//-- Native methods158159static native int read0(FileDescriptor fd, long address, int len)160throws IOException;161162static native int pread0(FileDescriptor fd, long address, int len,163long position) throws IOException;164165static native long readv0(FileDescriptor fd, long address, int len)166throws IOException;167168static native int write0(FileDescriptor fd, long address, int len, boolean append)169throws IOException;170171static native int pwrite0(FileDescriptor fd, long address, int len,172long position) throws IOException;173174static native long writev0(FileDescriptor fd, long address, int len, boolean append)175throws IOException;176177static native long seek0(FileDescriptor fd, long offset) throws IOException;178179static native int force0(FileDescriptor fd, boolean metaData)180throws IOException;181182static native int truncate0(FileDescriptor fd, long size)183throws IOException;184185static native long size0(FileDescriptor fd) throws IOException;186187static native int lock0(FileDescriptor fd, boolean blocking, long pos,188long size, boolean shared) throws IOException;189190static native void release0(FileDescriptor fd, long pos, long size)191throws IOException;192193static native void close0(FileDescriptor fd) throws IOException;194195static native void closeByHandle(long fd) throws IOException;196197static native long duplicateHandle(long fd) throws IOException;198}199200201