8202324: Avoid loading FileInput-/OutputStream$AltFinalizer

Reviewed-by: alanb
This commit is contained in:
Claes Redestad 2018-04-26 17:14:04 +02:00
parent a7b8407fbc
commit dce2872700
2 changed files with 41 additions and 42 deletions

View File

@ -79,7 +79,7 @@ class FileInputStream extends InputStream
private volatile boolean closed; private volatile boolean closed;
private final AltFinalizer altFinalizer; private final Object altFinalizer;
/** /**
* Creates a <code>FileInputStream</code> by * Creates a <code>FileInputStream</code> by
@ -155,7 +155,7 @@ class FileInputStream extends InputStream
fd.attach(this); fd.attach(this);
path = name; path = name;
open(name); open(name);
altFinalizer = AltFinalizer.get(this); altFinalizer = getFinalizer(this);
if (altFinalizer == null) { if (altFinalizer == null) {
FileCleanable.register(fd); // open set the fd, register the cleanup FileCleanable.register(fd); // open set the fd, register the cleanup
} }
@ -471,6 +471,23 @@ class FileInputStream extends InputStream
protected void finalize() throws IOException { 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. * Class to call {@code FileInputStream.close} when finalized.
* If finalization of the stream is needed, an instance is created * If finalization of the stream is needed, an instance is created
@ -481,25 +498,7 @@ class FileInputStream extends InputStream
static class AltFinalizer { static class AltFinalizer {
private final FileInputStream fis; private final FileInputStream fis;
/* AltFinalizer(FileInputStream fis) {
* Returns a finalizer object if the FIS needs a finalizer; otherwise null.
* If the FIS has a close method; it needs an AltFinalizer.
*/
static AltFinalizer get(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;
}
private AltFinalizer(FileInputStream fis) {
this.fis = fis; this.fis = fis;
} }

View File

@ -95,7 +95,7 @@ class FileOutputStream extends OutputStream
private volatile boolean closed; private volatile boolean closed;
private final AltFinalizer altFinalizer; private final Object altFinalizer;
/** /**
* Creates a file output stream to write to the file with the * Creates a file output stream to write to the file with the
@ -235,7 +235,7 @@ class FileOutputStream extends OutputStream
this.path = name; this.path = name;
open(name, append); open(name, append);
altFinalizer = AltFinalizer.get(this); altFinalizer = getFinalizer(this);
if (altFinalizer == null) { if (altFinalizer == null) {
FileCleanable.register(fd); // open sets the fd, register the cleanup FileCleanable.register(fd); // open sets the fd, register the cleanup
} }
@ -496,6 +496,24 @@ class FileOutputStream extends OutputStream
initIDs(); initIDs();
} }
/*
* Returns a finalizer object if the FOS needs a finalizer; otherwise null.
* If the FOS has a close method; it needs an AltFinalizer.
*/
private static Object getFinalizer(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;
}
/** /**
* Class to call {@code FileOutputStream.close} when finalized. * Class to call {@code FileOutputStream.close} when finalized.
* If finalization of the stream is needed, an instance is created * If finalization of the stream is needed, an instance is created
@ -506,25 +524,7 @@ class FileOutputStream extends OutputStream
static class AltFinalizer { static class AltFinalizer {
private final FileOutputStream fos; private final FileOutputStream fos;
/* AltFinalizer(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; this.fos = fos;
} }