Skip to content

Commit

Permalink
Use try-with-resources where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
basil committed Dec 17, 2024
1 parent f22af57 commit b7ed6a3
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 12 deletions.
8 changes: 2 additions & 6 deletions src/main/java/hudson/plugins/ec2/ssh/EC2MacLauncher.java
Original file line number Diff line number Diff line change
Expand Up @@ -352,14 +352,10 @@ private File createIdentityKeyFile(EC2Computer computer) throws IOException {
File tempFile = Files.createTempFile("ec2_", ".pem").toFile();

try {
FileOutputStream fileOutputStream = new FileOutputStream(tempFile);
OutputStreamWriter writer = new OutputStreamWriter(fileOutputStream, StandardCharsets.UTF_8);
try {
try (FileOutputStream fileOutputStream = new FileOutputStream(tempFile);
OutputStreamWriter writer = new OutputStreamWriter(fileOutputStream, StandardCharsets.UTF_8)) {
writer.write(privateKey);
writer.flush();
} finally {
writer.close();
fileOutputStream.close();
}
FilePath filePath = new FilePath(tempFile);
filePath.chmod(0400); // octal file mask - readonly by owner
Expand Down
8 changes: 2 additions & 6 deletions src/main/java/hudson/plugins/ec2/ssh/EC2UnixLauncher.java
Original file line number Diff line number Diff line change
Expand Up @@ -358,14 +358,10 @@ private File createIdentityKeyFile(EC2Computer computer) throws IOException {
File tempFile = Files.createTempFile("ec2_", ".pem").toFile();

try {
FileOutputStream fileOutputStream = new FileOutputStream(tempFile);
OutputStreamWriter writer = new OutputStreamWriter(fileOutputStream, StandardCharsets.UTF_8);
try {
try (FileOutputStream fileOutputStream = new FileOutputStream(tempFile);
OutputStreamWriter writer = new OutputStreamWriter(fileOutputStream, StandardCharsets.UTF_8)) {
writer.write(privateKey);
writer.flush();
} finally {
writer.close();
fileOutputStream.close();
}
FilePath filePath = new FilePath(tempFile);
filePath.chmod(0400); // octal file mask - readonly by owner
Expand Down

0 comments on commit b7ed6a3

Please sign in to comment.