8202324: Avoid loading FileInput-/OutputStream$AltFinalizer
Reviewed-by: alanb
This commit is contained in:
parent
a7b8407fbc
commit
dce2872700
@ -79,7 +79,7 @@ class FileInputStream extends InputStream
|
||||
|
||||
private volatile boolean closed;
|
||||
|
||||
private final AltFinalizer altFinalizer;
|
||||
private final Object altFinalizer;
|
||||
|
||||
/**
|
||||
* Creates a <code>FileInputStream</code> by
|
||||
@ -155,7 +155,7 @@ class FileInputStream extends InputStream
|
||||
fd.attach(this);
|
||||
path = name;
|
||||
open(name);
|
||||
altFinalizer = AltFinalizer.get(this);
|
||||
altFinalizer = getFinalizer(this);
|
||||
if (altFinalizer == null) {
|
||||
FileCleanable.register(fd); // open set the fd, register the cleanup
|
||||
}
|
||||
@ -471,6 +471,23 @@ class FileInputStream extends InputStream
|
||||
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
|
||||
@ -481,25 +498,7 @@ class FileInputStream extends InputStream
|
||||
static class AltFinalizer {
|
||||
private final 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) {
|
||||
AltFinalizer(FileInputStream fis) {
|
||||
this.fis = fis;
|
||||
}
|
||||
|
||||
|
@ -95,7 +95,7 @@ class FileOutputStream extends OutputStream
|
||||
|
||||
private volatile boolean closed;
|
||||
|
||||
private final AltFinalizer altFinalizer;
|
||||
private final Object altFinalizer;
|
||||
|
||||
/**
|
||||
* Creates a file output stream to write to the file with the
|
||||
@ -235,7 +235,7 @@ class FileOutputStream extends OutputStream
|
||||
this.path = name;
|
||||
|
||||
open(name, append);
|
||||
altFinalizer = AltFinalizer.get(this);
|
||||
altFinalizer = getFinalizer(this);
|
||||
if (altFinalizer == null) {
|
||||
FileCleanable.register(fd); // open sets the fd, register the cleanup
|
||||
}
|
||||
@ -496,6 +496,24 @@ class FileOutputStream extends OutputStream
|
||||
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.
|
||||
* If finalization of the stream is needed, an instance is created
|
||||
@ -506,25 +524,7 @@ class FileOutputStream extends OutputStream
|
||||
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) {
|
||||
AltFinalizer(FileOutputStream fos) {
|
||||
this.fos = fos;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user