SLIDER-1152 Resource leaks found in code
diff --git a/slider-core/src/main/java/org/apache/slider/api/proto/RestTypeMarshalling.java b/slider-core/src/main/java/org/apache/slider/api/proto/RestTypeMarshalling.java
index 766ee83..17fd965 100644
--- a/slider-core/src/main/java/org/apache/slider/api/proto/RestTypeMarshalling.java
+++ b/slider-core/src/main/java/org/apache/slider/api/proto/RestTypeMarshalling.java
@@ -105,13 +105,17 @@
return builder.build();
}
- @SuppressWarnings("IOResourceOpenedButNotSafelyClosed")
private static byte[] getStoreBytes(SecurityStore securityStore)
throws IOException {
- InputStream is = new FileInputStream(securityStore.getFile());
- byte[] storeBytes = IOUtils.toByteArray(is);
- if (is != null) {
- is.close();
+ InputStream is = null;
+ byte[] storeBytes;
+ try {
+ is = new FileInputStream(securityStore.getFile());
+ storeBytes = IOUtils.toByteArray(is);
+ } finally {
+ if (is != null) {
+ is.close();
+ }
}
return storeBytes;
}
diff --git a/slider-core/src/main/java/org/apache/slider/client/SliderClient.java b/slider-core/src/main/java/org/apache/slider/client/SliderClient.java
index 288cff3..f3dcea3 100644
--- a/slider-core/src/main/java/org/apache/slider/client/SliderClient.java
+++ b/slider-core/src/main/java/org/apache/slider/client/SliderClient.java
@@ -1181,10 +1181,17 @@
byte[] keystore = createClusterOperations(clientInfo.name)
.getClientCertificateStore(hostname, "client", password, type.name());
// persist to file
- FileOutputStream storeFileOutputStream = new FileOutputStream(storeFile);
- IOUtils.write(keystore, storeFileOutputStream);
- if (storeFileOutputStream != null) {
- storeFileOutputStream.close();
+ FileOutputStream storeFileOutputStream = null;
+ try {
+ storeFileOutputStream = new FileOutputStream(storeFile);
+ IOUtils.write(keystore, storeFileOutputStream);
+ } catch (Exception e) {
+ log.error("Unable to persist to file {}", storeFile);
+ throw e;
+ } finally {
+ if (storeFileOutputStream != null) {
+ storeFileOutputStream.close();
+ }
}
return EXIT_SUCCESS;
diff --git a/slider-core/src/main/java/org/apache/slider/core/launch/CredentialUtils.java b/slider-core/src/main/java/org/apache/slider/core/launch/CredentialUtils.java
index 255890b..5357cc4 100644
--- a/slider-core/src/main/java/org/apache/slider/core/launch/CredentialUtils.java
+++ b/slider-core/src/main/java/org/apache/slider/core/launch/CredentialUtils.java
@@ -87,8 +87,11 @@
ByteBuffer buffer = null;
if (!credentials.getAllTokens().isEmpty()) {
DataOutputBuffer dob = new DataOutputBuffer();
- credentials.writeTokenStorageToStream(dob);
- dob.close();
+ try {
+ credentials.writeTokenStorageToStream(dob);
+ } finally {
+ dob.close();
+ }
buffer = ByteBuffer.wrap(dob.getData(), 0, dob.getLength());
}
return buffer;