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;
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);
}
}

View File

@@ -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.
*

View File

@@ -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);
}
}
}

View File

@@ -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);
}
}
}
}