# HG changeset patch # User redestad # Date 1524755644 -7200 # Node ID 770679787db5753f6740e9e8b7e686e2f8a50208 # Parent b99fcb855107633cf9bc7a1dfbc76e4e9dd63a8f 8202324: Avoid loading FileInput-/OutputStream$AltFinalizer Reviewed-by: alanb diff -r b99fcb855107 -r 770679787db5 src/java.base/share/classes/java/io/FileInputStream.java --- a/src/java.base/share/classes/java/io/FileInputStream.java Thu Apr 26 09:04:09 2018 -0400 +++ b/src/java.base/share/classes/java/io/FileInputStream.java Thu Apr 26 17:14:04 2018 +0200 @@ -79,7 +79,7 @@ private volatile boolean closed; - private final AltFinalizer altFinalizer; + private final Object altFinalizer; /** * Creates a FileInputStream by @@ -155,7 +155,7 @@ 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 @@ 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 @@ 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; } diff -r b99fcb855107 -r 770679787db5 src/java.base/share/classes/java/io/FileOutputStream.java --- a/src/java.base/share/classes/java/io/FileOutputStream.java Thu Apr 26 09:04:09 2018 -0400 +++ b/src/java.base/share/classes/java/io/FileOutputStream.java Thu Apr 26 17:14:04 2018 +0200 @@ -95,7 +95,7 @@ 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 @@ 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 @@ 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 @@ 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; }