From 7da06384d617e8e8438420c22266b9f253e91d22 Mon Sep 17 00:00:00 2001 From: Basil Crow Date: Mon, 16 Dec 2024 17:01:57 -0800 Subject: [PATCH] Use Charset constant where possible --- src/main/java/hudson/plugins/ec2/EC2PrivateKey.java | 3 +-- src/main/java/hudson/plugins/ec2/ssh/EC2MacLauncher.java | 2 +- src/main/java/hudson/plugins/ec2/ssh/EC2UnixLauncher.java | 2 +- src/main/java/hudson/plugins/ec2/win/EC2WindowsLauncher.java | 2 +- .../java/hudson/plugins/ec2/ssh/HostKeyVerifierImplTest.java | 5 +++-- 5 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/main/java/hudson/plugins/ec2/EC2PrivateKey.java b/src/main/java/hudson/plugins/ec2/EC2PrivateKey.java index 496414905..30f708bb9 100644 --- a/src/main/java/hudson/plugins/ec2/EC2PrivateKey.java +++ b/src/main/java/hudson/plugins/ec2/EC2PrivateKey.java @@ -33,7 +33,6 @@ import java.io.BufferedReader; import java.io.IOException; import java.io.StringReader; -import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Paths; @@ -147,7 +146,7 @@ public String decryptWindowsPassword(String encodedPassword) throws AmazonClient PEMEncodable.decode(privateKey.getPlainText()).toPrivateKey()); byte[] cipherText = Base64.getDecoder().decode(StringUtils.deleteWhitespace(encodedPassword)); byte[] plainText = cipher.doFinal(cipherText); - return new String(plainText, Charset.forName("ASCII")); + return new String(plainText, StandardCharsets.US_ASCII); } catch (Exception e) { throw new AmazonClientException("Unable to decode password:\n" + e.toString()); } diff --git a/src/main/java/hudson/plugins/ec2/ssh/EC2MacLauncher.java b/src/main/java/hudson/plugins/ec2/ssh/EC2MacLauncher.java index d45c7d9b9..c26805e23 100644 --- a/src/main/java/hudson/plugins/ec2/ssh/EC2MacLauncher.java +++ b/src/main/java/hudson/plugins/ec2/ssh/EC2MacLauncher.java @@ -196,7 +196,7 @@ protected void launchScript(EC2Computer computer, TaskListener listener) && !initScript.trim().isEmpty() && conn.exec("test -e ~/.hudson-run-init", logger) != 0) { logInfo(computer, listener, "Executing init script"); - scp.put(initScript.getBytes("UTF-8"), "init.sh", tmpDir, "0700"); + scp.put(initScript.getBytes(StandardCharsets.UTF_8), "init.sh", tmpDir, "0700"); Session sess = conn.openSession(); sess.requestDumbPTY(); // so that the remote side bundles stdout // and stderr diff --git a/src/main/java/hudson/plugins/ec2/ssh/EC2UnixLauncher.java b/src/main/java/hudson/plugins/ec2/ssh/EC2UnixLauncher.java index 792be2975..c81803fa5 100644 --- a/src/main/java/hudson/plugins/ec2/ssh/EC2UnixLauncher.java +++ b/src/main/java/hudson/plugins/ec2/ssh/EC2UnixLauncher.java @@ -207,7 +207,7 @@ protected void launchScript(EC2Computer computer, TaskListener listener) && !initScript.trim().isEmpty() && conn.exec("test -e ~/.hudson-run-init", logger) != 0) { logInfo(computer, listener, "Executing init script"); - scp.put(initScript.getBytes("UTF-8"), "init.sh", tmpDir, "0700"); + scp.put(initScript.getBytes(StandardCharsets.UTF_8), "init.sh", tmpDir, "0700"); Session sess = conn.openSession(); sess.requestDumbPTY(); // so that the remote side bundles stdout // and stderr diff --git a/src/main/java/hudson/plugins/ec2/win/EC2WindowsLauncher.java b/src/main/java/hudson/plugins/ec2/win/EC2WindowsLauncher.java index 640719cac..508ddfa5b 100644 --- a/src/main/java/hudson/plugins/ec2/win/EC2WindowsLauncher.java +++ b/src/main/java/hudson/plugins/ec2/win/EC2WindowsLauncher.java @@ -63,7 +63,7 @@ protected void launchScript(EC2Computer computer, TaskListener listener) if (initScript != null && !initScript.trim().isEmpty() && !connection.exists(tmpDir + ".jenkins-init")) { logger.println("Executing init script"); try (OutputStream init = connection.putFile(tmpDir + "init.bat")) { - init.write(initScript.getBytes("utf-8")); + init.write(initScript.getBytes(StandardCharsets.UTF_8)); } WindowsProcess initProcess = connection.execute("cmd /c " + tmpDir + "init.bat"); diff --git a/src/test/java/hudson/plugins/ec2/ssh/HostKeyVerifierImplTest.java b/src/test/java/hudson/plugins/ec2/ssh/HostKeyVerifierImplTest.java index 97d935d69..bc8c4bf9d 100644 --- a/src/test/java/hudson/plugins/ec2/ssh/HostKeyVerifierImplTest.java +++ b/src/test/java/hudson/plugins/ec2/ssh/HostKeyVerifierImplTest.java @@ -3,6 +3,7 @@ import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; +import java.nio.charset.StandardCharsets; import org.junit.Test; public class HostKeyVerifierImplTest { @@ -13,12 +14,12 @@ public class HostKeyVerifierImplTest { @Test public void testVerifyFail() throws Exception { HostKeyVerifierImpl impl = new HostKeyVerifierImpl(""); - assertFalse(impl.verifyServerHostKey("", 0, null, key.getBytes("UTF-8"))); + assertFalse(impl.verifyServerHostKey("", 0, null, key.getBytes(StandardCharsets.UTF_8))); } @Test public void testVerifyTrue() throws Exception { HostKeyVerifierImpl impl = new HostKeyVerifierImpl(fp); - assertTrue(impl.verifyServerHostKey("", 0, null, key.getBytes("UTF-8"))); + assertTrue(impl.verifyServerHostKey("", 0, null, key.getBytes(StandardCharsets.UTF_8))); } }