Path: blob/master/src/java.base/share/classes/java/io/FileCleanable.java
41152 views
/*1* Copyright (c) 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 java.io;2627import jdk.internal.access.JavaIOFileDescriptorAccess;28import jdk.internal.access.SharedSecrets;29import jdk.internal.ref.CleanerFactory;30import jdk.internal.ref.PhantomCleanable;3132import java.lang.ref.Cleaner;3334/**35* Cleanable for a FileDescriptor when it becomes phantom reachable.36* For regular fds on Unix and regular handles on Windows37* register a cleanup if fd != -1 or handle != -1.38* <p>39* Subclassed from {@code PhantomCleanable} so that {@code clear} can be40* called to disable the cleanup when the handle is closed by any means other41* than calling {@link FileDescriptor#close}.42* Otherwise, it might incorrectly close the handle after it has been reused.43*/44final class FileCleanable extends PhantomCleanable<FileDescriptor> {4546// Access to FileDescriptor private fields;47// avoids making fd and handle package private48private static final JavaIOFileDescriptorAccess fdAccess =49SharedSecrets.getJavaIOFileDescriptorAccess();5051/*52* Raw close of the file fd and/or handle.53* Used only for last chance cleanup.54*/55private static native void cleanupClose0(int fd, long handle) throws IOException;5657// The raw fd to close58private final int fd;5960// The handle to close61private final long handle;6263/**64* Register a Cleanable with the FileDescriptor65* if the FileDescriptor is non-null and valid.66* @implNote67* A exception (OutOfMemoryException) will leave the FileDescriptor68* having allocated resources and leak the fd/handle.69*70* @param fdo the FileDescriptor; may be null71*/72static void register(FileDescriptor fdo) {73if (fdo != null && fdo.valid()) {74int fd = fdAccess.get(fdo);75long handle = fdAccess.getHandle(fdo);76fdo.registerCleanup(new FileCleanable(fdo, CleanerFactory.cleaner(), fd, handle));77}78}7980/**81* Unregister a Cleanable from the FileDescriptor.82* @param fdo the FileDescriptor; may be null83*/84static void unregister(FileDescriptor fdo) {85if (fdo != null) {86fdo.unregisterCleanup();87}88}8990/**91* Constructor for a phantom cleanable reference.92*93* @param obj the object to monitor94* @param cleaner the cleaner95* @param fd file descriptor to close96* @param handle handle to close97*/98private FileCleanable(FileDescriptor obj, Cleaner cleaner, int fd, long handle) {99super(obj, cleaner);100this.fd = fd;101this.handle = handle;102}103104/**105* Close the native handle or fd.106*/107@Override108protected void performCleanup() {109try {110cleanupClose0(fd, handle);111} catch (IOException ioe) {112throw new UncheckedIOException("close", ioe);113}114}115}116117118