moved similiar code to a utility function to remove code duplication

This commit is contained in:
Jeremy Long
2016-12-04 11:28:53 -05:00
parent 4dd6dedaa4
commit a271d422f6
4 changed files with 32 additions and 78 deletions

View File

@@ -18,7 +18,6 @@
package org.owasp.dependencycheck.analyzer; package org.owasp.dependencycheck.analyzer;
import java.io.BufferedInputStream; import java.io.BufferedInputStream;
import java.io.Closeable;
import java.io.File; import java.io.File;
import java.io.FileFilter; import java.io.FileFilter;
import java.io.FileInputStream; import java.io.FileInputStream;
@@ -434,12 +433,12 @@ public class ArchiveAnalyzer extends AbstractFileTypeAnalyzer {
} finally { } finally {
//overly verbose and not needed... but keeping it anyway due to //overly verbose and not needed... but keeping it anyway due to
//having issue with file handles being left open //having issue with file handles being left open
close(fis); FileUtils.close(fis);
close(in); FileUtils.close(in);
close(zin); FileUtils.close(zin);
close(tin); FileUtils.close(tin);
close(gin); FileUtils.close(gin);
close(bzin); FileUtils.close(bzin);
} }
} }
} }
@@ -521,7 +520,7 @@ public class ArchiveAnalyzer extends AbstractFileTypeAnalyzer {
} catch (Throwable ex) { } catch (Throwable ex) {
throw new ArchiveExtractionException(ex); throw new ArchiveExtractionException(ex);
} finally { } 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()); final String msg = String.format("IO Exception while parsing file '%s'.", file.getName());
throw new AnalysisException(msg, ex); throw new AnalysisException(msg, ex);
} finally { } finally {
close(fos); FileUtils.close(fos);
} }
} }
@@ -577,23 +576,7 @@ public class ArchiveAnalyzer extends AbstractFileTypeAnalyzer {
LOGGER.debug("", ex); LOGGER.debug("", ex);
throw new ArchiveExtractionException(ex); throw new ArchiveExtractionException(ex);
} finally { } finally {
close(out); FileUtils.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);
}
} }
} }

View File

@@ -23,7 +23,6 @@ import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Reader; import java.io.Reader;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.util.ArrayList; import java.util.ArrayList;
@@ -437,42 +436,12 @@ public class JarAnalyzer extends AbstractFileTypeAnalyzer {
LOGGER.warn("An error occurred reading '{}' from '{}'.", path, jar.getName()); LOGGER.warn("An error occurred reading '{}' from '{}'.", path, jar.getName());
LOGGER.error("", ex); LOGGER.error("", ex);
} finally { } finally {
closeStream(fos); FileUtils.close(fos);
closeStream(input); FileUtils.close(input);
} }
return file; 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. * Sets evidence from the pom on the supplied dependency.
* *

View File

@@ -116,7 +116,7 @@ public final class ExtractionUtil {
final String msg = String.format("IO Exception while parsing file '%s'.", file.getName()); final String msg = String.format("IO Exception while parsing file '%s'.", file.getName());
throw new ExtractionException(msg, ex); throw new ExtractionException(msg, ex);
} finally { } finally {
closeStream(fos); FileUtils.close(fos);
} }
} }
} }
@@ -126,7 +126,7 @@ public final class ExtractionUtil {
LOGGER.debug("", ex); LOGGER.debug("", ex);
throw new ExtractionException(msg, ex); throw new ExtractionException(msg, ex);
} finally { } finally {
closeStream(zis); FileUtils.close(zis);
} }
} }
@@ -197,7 +197,7 @@ public final class ExtractionUtil {
} catch (Throwable ex) { } catch (Throwable ex) {
throw new ArchiveExtractionException(ex); throw new ArchiveExtractionException(ex);
} finally { } finally {
closeStream(input); FileUtils.close(input);
} }
} }
@@ -233,22 +233,7 @@ public final class ExtractionUtil {
file.getName()); file.getName());
throw new ExtractionException(msg, ex); throw new ExtractionException(msg, ex);
} finally { } finally {
closeStream(fos); FileUtils.close(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);
} }
} }
} }

View File

@@ -17,6 +17,7 @@
*/ */
package org.owasp.dependencycheck.utils; package org.owasp.dependencycheck.utils;
import java.io.Closeable;
import org.apache.commons.io.FilenameUtils; import org.apache.commons.io.FilenameUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@@ -131,4 +132,20 @@ public final class FileUtils {
return BIT_BUCKET_UNIX; 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);
}
}
}
} }