mirror of
https://github.com/ysoftdevs/DependencyCheck.git
synced 2026-01-18 09:37:38 +01:00
updated to use try with resouces
This commit is contained in:
@@ -65,11 +65,8 @@ public final class Checksum {
|
||||
*/
|
||||
public static byte[] getChecksum(String algorithm, File file) throws NoSuchAlgorithmException, IOException {
|
||||
final MessageDigest md = MessageDigest.getInstance(algorithm);
|
||||
FileInputStream fis = null;
|
||||
FileChannel ch = null;
|
||||
try {
|
||||
fis = new FileInputStream(file);
|
||||
ch = fis.getChannel();
|
||||
try (FileInputStream fis = new FileInputStream(file);
|
||||
FileChannel ch = fis.getChannel()) {
|
||||
final ByteBuffer buf = ByteBuffer.allocateDirect(8192);
|
||||
int b = ch.read(buf);
|
||||
while (b != -1 && b != 0) {
|
||||
@@ -81,21 +78,6 @@ public final class Checksum {
|
||||
b = ch.read(buf);
|
||||
}
|
||||
return md.digest();
|
||||
} finally {
|
||||
if (ch != null) {
|
||||
try {
|
||||
ch.close();
|
||||
} catch (IOException ex) {
|
||||
LOGGER.trace("Error closing channel '{}'.", file.getName(), ex);
|
||||
}
|
||||
}
|
||||
if (fis != null) {
|
||||
try {
|
||||
fis.close();
|
||||
} catch (IOException ex) {
|
||||
LOGGER.trace("Error closing file '{}'.", file.getName(), ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -433,10 +433,8 @@ public final class Settings {
|
||||
* @param propertiesFilePath the path to the base properties file to load
|
||||
*/
|
||||
private Settings(String propertiesFilePath) {
|
||||
InputStream in = null;
|
||||
props = new Properties();
|
||||
try {
|
||||
in = this.getClass().getClassLoader().getResourceAsStream(propertiesFilePath);
|
||||
try (InputStream in = this.getClass().getClassLoader().getResourceAsStream(propertiesFilePath)) {
|
||||
props.load(in);
|
||||
} catch (NullPointerException ex) {
|
||||
LOGGER.error("Did not find settings file '{}'.", propertiesFilePath);
|
||||
@@ -444,14 +442,6 @@ public final class Settings {
|
||||
} catch (IOException ex) {
|
||||
LOGGER.error("Unable to load settings from '{}'.", propertiesFilePath);
|
||||
LOGGER.debug("", ex);
|
||||
} finally {
|
||||
if (in != null) {
|
||||
try {
|
||||
in.close();
|
||||
} catch (IOException ex) {
|
||||
LOGGER.trace("", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
logProperties("Properties loaded", props);
|
||||
}
|
||||
@@ -644,18 +634,8 @@ public final class Settings {
|
||||
* the properties
|
||||
*/
|
||||
public static void mergeProperties(File filePath) throws FileNotFoundException, IOException {
|
||||
FileInputStream fis = null;
|
||||
try {
|
||||
fis = new FileInputStream(filePath);
|
||||
try (FileInputStream fis = new FileInputStream(filePath)) {
|
||||
mergeProperties(fis);
|
||||
} finally {
|
||||
if (fis != null) {
|
||||
try {
|
||||
fis.close();
|
||||
} catch (IOException ex) {
|
||||
LOGGER.trace("close error", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -672,18 +652,8 @@ public final class Settings {
|
||||
* the properties
|
||||
*/
|
||||
public static void mergeProperties(String filePath) throws FileNotFoundException, IOException {
|
||||
FileInputStream fis = null;
|
||||
try {
|
||||
fis = new FileInputStream(filePath);
|
||||
try (FileInputStream fis = new FileInputStream(filePath)) {
|
||||
mergeProperties(fis);
|
||||
} finally {
|
||||
if (fis != null) {
|
||||
try {
|
||||
fis.close();
|
||||
} catch (IOException ex) {
|
||||
LOGGER.trace("close error", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -977,6 +947,7 @@ public final class Settings {
|
||||
if (path != null && (path.exists() || path.mkdirs())) {
|
||||
return path;
|
||||
}
|
||||
throw new IOException(String.format("Unable to create the data directory '%s'", path.getAbsolutePath()));
|
||||
throw new IOException(String.format("Unable to create the data directory '%s'",
|
||||
(path == null) ? "unknown" : path.getAbsolutePath()));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -39,12 +39,10 @@ public class ExpectedObjectInputStreamTest {
|
||||
*/
|
||||
@Test
|
||||
public void testResolveClass() {
|
||||
ObjectOutputStream out = null;
|
||||
try {
|
||||
List<SimplePojo> data = new ArrayList<>();
|
||||
data.add(new SimplePojo());
|
||||
ByteArrayOutputStream mem = new ByteArrayOutputStream();
|
||||
out = new ObjectOutputStream(new BufferedOutputStream(mem));
|
||||
List<SimplePojo> data = new ArrayList<>();
|
||||
data.add(new SimplePojo());
|
||||
try (ByteArrayOutputStream mem = new ByteArrayOutputStream();
|
||||
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(mem))) {
|
||||
out.writeObject(data);
|
||||
out.flush();
|
||||
byte[] buf = mem.toByteArray();
|
||||
@@ -54,14 +52,6 @@ public class ExpectedObjectInputStreamTest {
|
||||
instance.readObject();
|
||||
} catch (IOException | ClassNotFoundException ex) {
|
||||
fail(ex.getMessage());
|
||||
} finally {
|
||||
try {
|
||||
if (out != null) {
|
||||
out.close();
|
||||
}
|
||||
} catch (IOException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user