8080225: FileInput/OutputStream/FileChannel cleanup should be improved

Reviewed-by: mchung, plevart, bpb
This commit is contained in:
Roger Riggs 2017-12-01 16:40:08 -05:00
parent b93586c51e
commit f29e21abb1
23 changed files with 1104 additions and 261 deletions

View file

@ -25,6 +25,7 @@
package java.io;
import java.lang.reflect.Method;
import java.nio.channels.FileChannel;
import jdk.internal.misc.SharedSecrets;
import jdk.internal.misc.JavaIOFileDescriptorAccess;
@ -44,6 +45,21 @@ import sun.nio.ch.FileChannelImpl;
* such as image data. For writing streams of characters, consider using
* <code>FileWriter</code>.
*
* @apiNote
* To release resources used by this stream {@link #close} should be called
* directly or by try-with-resources. Subclasses are responsible for the cleanup
* of resources acquired by the subclass.
* Subclasses that override {@link #finalize} in order to perform cleanup
* should be modified to use alternative cleanup mechanisms such as
* {@link java.lang.ref.Cleaner} and remove the overriding {@code finalize} method.
*
* @implSpec
* If this FileOutputStream 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.
*
* @author Arthur van Hoff
* @see java.io.File
* @see java.io.FileDescriptor
@ -80,6 +96,8 @@ class FileOutputStream extends OutputStream
private volatile boolean closed;
private final AltFinalizer altFinalizer;
/**
* Creates a file output stream to write to the file with the
* specified name. A new <code>FileDescriptor</code> object is
@ -218,6 +236,10 @@ class FileOutputStream extends OutputStream
this.path = name;
open(name, append);
altFinalizer = AltFinalizer.get(this);
if (altFinalizer == null) {
fd.registerCleanup(); // open set the fd, register the cleanup
}
}
/**
@ -253,6 +275,7 @@ class FileOutputStream extends OutputStream
}
this.fd = fdObj;
this.path = null;
this.altFinalizer = null;
fd.attach(this);
}
@ -340,6 +363,14 @@ class FileOutputStream extends OutputStream
* <p> If this stream has an associated channel then the channel is closed
* as well.
*
* @apiNote
* Overriding {@link #close} to perform cleanup actions is reliable
* only when called directly or when called by try-with-resources.
* Do not depend on finalization to invoke {@code close};
* finalization is not reliable and is deprecated.
* If cleanup of native resources is needed, other mechanisms such as
* {@linkplain java.lang.ref.Cleaner} should be used.
*
* @exception IOException if an I/O error occurs.
*
* @revised 1.4
@ -429,34 +460,35 @@ class FileOutputStream extends OutputStream
/**
* Cleans up the connection to the file, and ensures that the
* <code>close</code> method of this file output stream is
* {@link #close} method of this file output stream is
* called when there are no more references to this stream.
* 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 FileOutputStream has been subclassed and the {@link #close}
* method has been overridden, the {@link #close} method will be
* called when the FileOutputStream 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.
*
* @deprecated The {@code finalize} method has been deprecated.
* 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")
@Deprecated(since="9", forRemoval = true)
protected void finalize() throws IOException {
if (fd != null) {
if (fd == FileDescriptor.out || fd == FileDescriptor.err) {
flush();
} else {
/* 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()
*/
close();
}
}
}
private static native void initIDs();
@ -465,4 +497,59 @@ class FileOutputStream extends OutputStream
initIDs();
}
/**
* Class to call {@code FileOutputStream.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 FileOutputStream fos;
/*
* Returns a finalizer object if the FOS needs a finalizer; otherwise null.
* If the FOS has a close method; it needs an AltFinalizer.
*/
static AltFinalizer get(FileOutputStream fos) {
Class<?> clazz = fos.getClass();
while (clazz != FileOutputStream.class) {
try {
clazz.getDeclaredMethod("close");
return new AltFinalizer(fos);
} catch (NoSuchMethodException nsme) {
// ignore
}
clazz = clazz.getSuperclass();
}
return null;
}
private AltFinalizer(FileOutputStream fos) {
this.fos = fos;
}
@Override
@SuppressWarnings("deprecation")
protected final void finalize() {
try {
if (fos.fd != null) {
if (fos.fd == FileDescriptor.out || fos.fd == FileDescriptor.err) {
// Subclass may override flush; otherwise it is no-op
fos.flush();
} else {
/* 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()
*/
fos.close();
}
}
} catch (IOException ioe) {
// ignore
}
}
}
}