diff --git a/dependency-check-core/src/main/java/org/owasp/dependencycheck/analyzer/ArchiveAnalyzer.java b/dependency-check-core/src/main/java/org/owasp/dependencycheck/analyzer/ArchiveAnalyzer.java index 2a675353b..ed40d75e8 100644 --- a/dependency-check-core/src/main/java/org/owasp/dependencycheck/analyzer/ArchiveAnalyzer.java +++ b/dependency-check-core/src/main/java/org/owasp/dependencycheck/analyzer/ArchiveAnalyzer.java @@ -18,7 +18,6 @@ package org.owasp.dependencycheck.analyzer; import java.io.BufferedInputStream; -import java.io.Closeable; import java.io.File; import java.io.FileFilter; import java.io.FileInputStream; @@ -434,12 +433,12 @@ public class ArchiveAnalyzer extends AbstractFileTypeAnalyzer { } finally { //overly verbose and not needed... but keeping it anyway due to //having issue with file handles being left open - close(fis); - close(in); - close(zin); - close(tin); - close(gin); - close(bzin); + FileUtils.close(fis); + FileUtils.close(in); + FileUtils.close(zin); + FileUtils.close(tin); + FileUtils.close(gin); + FileUtils.close(bzin); } } } @@ -521,7 +520,7 @@ public class ArchiveAnalyzer extends AbstractFileTypeAnalyzer { } catch (Throwable ex) { throw new ArchiveExtractionException(ex); } finally { - close(input); + FileUtils.close(input); } } @@ -552,7 +551,7 @@ public class ArchiveAnalyzer extends AbstractFileTypeAnalyzer { final String msg = String.format("IO Exception while parsing file '%s'.", file.getName()); throw new AnalysisException(msg, ex); } finally { - close(fos); + FileUtils.close(fos); } } @@ -577,23 +576,7 @@ public class ArchiveAnalyzer extends AbstractFileTypeAnalyzer { LOGGER.debug("", ex); throw new ArchiveExtractionException(ex); } finally { - close(out); - } - } - - /** - * Close the given {@link Closeable} instance, ignoring nulls, and logging - * any thrown {@link IOException}. - * - * @param closeable to be closed - */ - private static void close(Closeable closeable) { - if (null != closeable) { - try { - closeable.close(); - } catch (IOException ex) { - LOGGER.trace("", ex); - } + FileUtils.close(out); } } diff --git a/dependency-check-core/src/main/java/org/owasp/dependencycheck/analyzer/JarAnalyzer.java b/dependency-check-core/src/main/java/org/owasp/dependencycheck/analyzer/JarAnalyzer.java index f7da35e7d..353f8dade 100644 --- a/dependency-check-core/src/main/java/org/owasp/dependencycheck/analyzer/JarAnalyzer.java +++ b/dependency-check-core/src/main/java/org/owasp/dependencycheck/analyzer/JarAnalyzer.java @@ -23,7 +23,6 @@ import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; -import java.io.OutputStream; import java.io.Reader; import java.io.UnsupportedEncodingException; import java.util.ArrayList; @@ -437,42 +436,12 @@ public class JarAnalyzer extends AbstractFileTypeAnalyzer { LOGGER.warn("An error occurred reading '{}' from '{}'.", path, jar.getName()); LOGGER.error("", ex); } finally { - closeStream(fos); - closeStream(input); + FileUtils.close(fos); + FileUtils.close(input); } return file; } - /** - * Silently closes an input stream ignoring errors. - * - * @param stream an input stream to close - */ - private void closeStream(InputStream stream) { - if (stream != null) { - try { - stream.close(); - } catch (IOException ex) { - LOGGER.trace("", ex); - } - } - } - - /** - * Silently closes an output stream ignoring errors. - * - * @param stream an output stream to close - */ - private void closeStream(OutputStream stream) { - if (stream != null) { - try { - stream.close(); - } catch (IOException ex) { - LOGGER.trace("", ex); - } - } - } - /** * Sets evidence from the pom on the supplied dependency. * diff --git a/dependency-check-core/src/main/java/org/owasp/dependencycheck/utils/ExtractionUtil.java b/dependency-check-core/src/main/java/org/owasp/dependencycheck/utils/ExtractionUtil.java index e4b56ae20..5ed10f2f9 100644 --- a/dependency-check-core/src/main/java/org/owasp/dependencycheck/utils/ExtractionUtil.java +++ b/dependency-check-core/src/main/java/org/owasp/dependencycheck/utils/ExtractionUtil.java @@ -116,7 +116,7 @@ public final class ExtractionUtil { final String msg = String.format("IO Exception while parsing file '%s'.", file.getName()); throw new ExtractionException(msg, ex); } finally { - closeStream(fos); + FileUtils.close(fos); } } } @@ -126,7 +126,7 @@ public final class ExtractionUtil { LOGGER.debug("", ex); throw new ExtractionException(msg, ex); } finally { - closeStream(zis); + FileUtils.close(zis); } } @@ -197,7 +197,7 @@ public final class ExtractionUtil { } catch (Throwable ex) { throw new ArchiveExtractionException(ex); } finally { - closeStream(input); + FileUtils.close(input); } } @@ -233,22 +233,7 @@ public final class ExtractionUtil { file.getName()); throw new ExtractionException(msg, ex); } finally { - closeStream(fos); - } - } - } - - /** - * Closes the stream. - * - * @param stream the stream to close - */ - private static void closeStream(Closeable stream) { - if (stream != null) { - try { - stream.close(); - } catch (IOException ex) { - LOGGER.trace("", ex); + FileUtils.close(fos); } } } diff --git a/dependency-check-utils/src/main/java/org/owasp/dependencycheck/utils/FileUtils.java b/dependency-check-utils/src/main/java/org/owasp/dependencycheck/utils/FileUtils.java index e0c62e4e1..1ba099aca 100644 --- a/dependency-check-utils/src/main/java/org/owasp/dependencycheck/utils/FileUtils.java +++ b/dependency-check-utils/src/main/java/org/owasp/dependencycheck/utils/FileUtils.java @@ -17,6 +17,7 @@ */ package org.owasp.dependencycheck.utils; +import java.io.Closeable; import org.apache.commons.io.FilenameUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -131,4 +132,20 @@ public final class FileUtils { return BIT_BUCKET_UNIX; } } + + /** + * Close the given {@link Closeable} instance, ignoring nulls, and logging + * any thrown {@link IOException}. + * + * @param closeable to be closed + */ + public static void close(Closeable closeable) { + if (null != closeable) { + try { + closeable.close(); + } catch (IOException ex) { + LOGGER.trace("", ex); + } + } + } }