< prev index next >

src/java.base/share/classes/java/io/FileInputStream.java

Print this page
rev 52044 : 8192939: Remove Finalize methods from FileInputStream and FileOutputStream

*** 1,7 **** /* ! * Copyright (c) 1994, 2017, Oracle and/or its affiliates. All rights reserved. * 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 * published by the Free Software Foundation. Oracle designates this --- 1,7 ---- /* ! * Copyright (c) 1994, 2018, Oracle and/or its affiliates. All rights reserved. * 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 * published by the Free Software Foundation. Oracle designates this
*** 77,88 **** private final Object closeLock = new Object(); private volatile boolean closed; - private final Object altFinalizer; - /** * Creates a <code>FileInputStream</code> by * opening a connection to an actual file, * the file named by the path name <code>name</code> * in the file system. A new <code>FileDescriptor</code> --- 77,86 ----
*** 153,167 **** } fd = new FileDescriptor(); fd.attach(this); path = name; open(name); - altFinalizer = getFinalizer(this); - if (altFinalizer == null) { FileCleanable.register(fd); // open set the fd, register the cleanup } - } /** * Creates a <code>FileInputStream</code> by using the file descriptor * <code>fdObj</code>, which represents an existing connection to an * actual file in the file system. --- 151,162 ----
*** 193,203 **** if (security != null) { security.checkRead(fdObj); } fd = fdObj; path = null; - altFinalizer = null; /* * FileDescriptor is being shared by streams. * Register this stream with FileDescriptor tracker. */ --- 188,197 ----
*** 436,522 **** private static native void initIDs(); static { initIDs(); } - - /** - * Ensures that the {@link #close} method of this file input stream is - * called when there are no more references to it. - * The {@link #finalize} method does not call {@link #close} directly. - * - * @apiNote - * To release resources used by this stream {@link #close} should be called - * directly or by try-with-resources. - * - * @implSpec - * If this FileInputStream has been subclassed and the {@link #close} - * method has been overridden, the {@link #close} method will be - * called when the FileInputStream is unreachable. - * Otherwise, it is implementation specific how the resource cleanup described in - * {@link #close} is performed. - * - * @deprecated The {@code finalize} method has been deprecated and will be removed. - * Subclasses that override {@code finalize} in order to perform cleanup - * should be modified to use alternative cleanup mechanisms and - * to remove the overriding {@code finalize} method. - * When overriding the {@code finalize} method, its implementation must explicitly - * ensure that {@code super.finalize()} is invoked as described in {@link Object#finalize}. - * See the specification for {@link Object#finalize()} for further - * information about migration options. - * - * @exception IOException if an I/O error occurs. - * @see java.io.FileInputStream#close() - */ - @Deprecated(since="9", forRemoval = true) - protected void finalize() throws IOException { - } - - /* - * Returns a finalizer object if the FIS needs a finalizer; otherwise null. - * If the FIS has a close method; it needs an AltFinalizer. - */ - private static Object getFinalizer(FileInputStream fis) { - Class<?> clazz = fis.getClass(); - while (clazz != FileInputStream.class) { - try { - clazz.getDeclaredMethod("close"); - return new AltFinalizer(fis); - } catch (NoSuchMethodException nsme) { - // ignore - } - clazz = clazz.getSuperclass(); - } - return null; - } - /** - * Class to call {@code FileInputStream.close} when finalized. - * If finalization of the stream is needed, an instance is created - * in its constructor(s). When the set of instances - * related to the stream is unreachable, the AltFinalizer performs - * the needed call to the stream's {@code close} method. - */ - static class AltFinalizer { - private final FileInputStream fis; - - AltFinalizer(FileInputStream fis) { - this.fis = fis; - } - - @Override - @SuppressWarnings("deprecation") - protected final void finalize() { - try { - if ((fis.fd != null) && (fis.fd != FileDescriptor.in)) { - /* if fd is shared, the references in FileDescriptor - * will ensure that finalizer is only called when - * safe to do so. All references using the fd have - * become unreachable. We can call close() - */ - fis.close(); - } - } catch (IOException ioe) { - // ignore - } - } - } } --- 430,435 ----
< prev index next >