From b7ed6a3eb9e7c49790d6be6b8f8bedd288e911ca Mon Sep 17 00:00:00 2001 From: Basil Crow Date: Mon, 16 Dec 2024 16:58:21 -0800 Subject: [PATCH] Use try-with-resources where possible --- src/main/java/hudson/plugins/ec2/ssh/EC2MacLauncher.java | 8 ++------ src/main/java/hudson/plugins/ec2/ssh/EC2UnixLauncher.java | 8 ++------ 2 files changed, 4 insertions(+), 12 deletions(-) diff --git a/src/main/java/hudson/plugins/ec2/ssh/EC2MacLauncher.java b/src/main/java/hudson/plugins/ec2/ssh/EC2MacLauncher.java index 996245d62..18ffc57f4 100644 --- a/src/main/java/hudson/plugins/ec2/ssh/EC2MacLauncher.java +++ b/src/main/java/hudson/plugins/ec2/ssh/EC2MacLauncher.java @@ -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 diff --git a/src/main/java/hudson/plugins/ec2/ssh/EC2UnixLauncher.java b/src/main/java/hudson/plugins/ec2/ssh/EC2UnixLauncher.java index 04acbd606..ac38cf377 100644 --- a/src/main/java/hudson/plugins/ec2/ssh/EC2UnixLauncher.java +++ b/src/main/java/hudson/plugins/ec2/ssh/EC2UnixLauncher.java @@ -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