updated to use IOUtils to copy between streams

This commit is contained in:
Jeremy Long
2017-03-12 13:21:59 -04:00
parent 69c6dd40a1
commit 626f6c3de2

View File

@@ -24,6 +24,7 @@ import java.io.FileInputStream;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.util.zip.ZipEntry; import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream; import java.util.zip.ZipInputStream;
import org.apache.commons.compress.utils.IOUtils;
import org.junit.AfterClass; import org.junit.AfterClass;
import org.junit.Before; import org.junit.Before;
import org.owasp.dependencycheck.data.nvdcve.CveDB; import org.owasp.dependencycheck.data.nvdcve.CveDB;
@@ -58,12 +59,10 @@ public abstract class BaseDBTestCase extends BaseTest {
} }
public static void ensureDBExists() throws Exception { public static void ensureDBExists() throws Exception {
File f = new File("./target/data/dc.h2.db"); File f = new File("./target/data/dc.h2.db");
if (f.exists() && f.isFile() && f.length() < 71680) { if (f.exists() && f.isFile() && f.length() < 71680) {
f.delete(); f.delete();
} }
File dataPath = Settings.getDataDirectory(); File dataPath = Settings.getDataDirectory();
String fileName = Settings.getString(Settings.KEYS.DB_FILE_NAME); String fileName = Settings.getString(Settings.KEYS.DB_FILE_NAME);
LOGGER.trace("DB file name {}", fileName); LOGGER.trace("DB file name {}", fileName);
@@ -72,12 +71,9 @@ public abstract class BaseDBTestCase extends BaseTest {
if (!dataPath.exists() || !dataFile.exists()) { if (!dataPath.exists() || !dataFile.exists()) {
LOGGER.trace("Extracting database to {}", dataPath.toString()); LOGGER.trace("Extracting database to {}", dataPath.toString());
dataPath.mkdirs(); dataPath.mkdirs();
FileInputStream fis = null;
ZipInputStream zin = null;
try {
File path = new File(BaseDBTestCase.class.getClassLoader().getResource("data.zip").toURI().getPath()); File path = new File(BaseDBTestCase.class.getClassLoader().getResource("data.zip").toURI().getPath());
fis = new FileInputStream(path); try (FileInputStream fis = new FileInputStream(path);
zin = new ZipInputStream(new BufferedInputStream(fis)); ZipInputStream zin = new ZipInputStream(new BufferedInputStream(fis))) {
ZipEntry entry; ZipEntry entry;
while ((entry = zin.getNextEntry()) != null) { while ((entry = zin.getNextEntry()) != null) {
if (entry.isDirectory()) { if (entry.isDirectory()) {
@@ -85,52 +81,14 @@ public abstract class BaseDBTestCase extends BaseTest {
d.mkdir(); d.mkdir();
continue; continue;
} }
FileOutputStream fos = null;
BufferedOutputStream dest = null;
try {
File o = new File(dataPath, entry.getName()); File o = new File(dataPath, entry.getName());
o.createNewFile(); o.createNewFile();
fos = new FileOutputStream(o, false); try (FileOutputStream fos = new FileOutputStream(o, false);
dest = new BufferedOutputStream(fos, BUFFER_SIZE); BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER_SIZE)) {
byte data[] = new byte[BUFFER_SIZE]; IOUtils.copy(zin, dest);
int count;
while ((count = zin.read(data, 0, BUFFER_SIZE)) != -1) {
dest.write(data, 0, count);
}
} catch (Throwable ex) { } catch (Throwable ex) {
LOGGER.error("", ex); LOGGER.error("", ex);
} finally { }
try {
if (dest != null) {
dest.flush();
dest.close();
}
} catch (Throwable ex) {
LOGGER.trace("", ex);
}
try {
if (fos != null) {
fos.close();
}
} catch (Throwable ex) {
LOGGER.trace("", ex);
}
}
}
} finally {
try {
if (zin != null) {
zin.close();
}
} catch (Throwable ex) {
LOGGER.trace("", ex);
}
try {
if (fis != null) {
fis.close();
}
} catch (Throwable ex) {
LOGGER.trace("", ex);
} }
} }
} }