提交 f8f095e4 编写于 作者: M Matt Sicker 提交者: Oleg Nenashev

Replace Charset.forName with StandardCharsets (#4132)

This refactors usage of Charset::forName on common character sets that
now have an equivalent value in StandardCharsets.
上级 fda304cb
...@@ -27,7 +27,6 @@ import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; ...@@ -27,7 +27,6 @@ import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.SequenceInputStream; import java.io.SequenceInputStream;
import java.io.Writer; import java.io.Writer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.security.interfaces.RSAPublicKey; import java.security.interfaces.RSAPublicKey;
import javax.annotation.Nullable; import javax.annotation.Nullable;
...@@ -140,7 +139,7 @@ public final class TcpSlaveAgentListener extends Thread { ...@@ -140,7 +139,7 @@ public final class TcpSlaveAgentListener extends Thread {
@Nullable @Nullable
public String getIdentityPublicKey() { public String getIdentityPublicKey() {
RSAPublicKey key = InstanceIdentityProvider.RSA.getPublicKey(); RSAPublicKey key = InstanceIdentityProvider.RSA.getPublicKey();
return key == null ? null : new String(Base64.encodeBase64(key.getEncoded()), Charset.forName("UTF-8")); return key == null ? null : new String(Base64.encodeBase64(key.getEncoded()), StandardCharsets.UTF_8);
} }
/** /**
......
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
*/ */
package hudson.security.csrf; package hudson.security.csrf;
import java.nio.charset.Charset; import java.nio.charset.StandardCharsets;
import java.security.MessageDigest; import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.util.logging.Level; import java.util.logging.Level;
...@@ -118,8 +118,8 @@ public class DefaultCrumbIssuer extends CrumbIssuer { ...@@ -118,8 +118,8 @@ public class DefaultCrumbIssuer extends CrumbIssuer {
String newCrumb = issueCrumb(request, salt); String newCrumb = issueCrumb(request, salt);
if ((newCrumb != null) && (crumb != null)) { if ((newCrumb != null) && (crumb != null)) {
// String.equals() is not constant-time, but this is // String.equals() is not constant-time, but this is
return MessageDigest.isEqual(newCrumb.getBytes(Charset.forName("US-ASCII")), return MessageDigest.isEqual(newCrumb.getBytes(StandardCharsets.US_ASCII),
crumb.getBytes(Charset.forName("US-ASCII"))); crumb.getBytes(StandardCharsets.US_ASCII));
} }
} }
return false; return false;
......
...@@ -33,7 +33,7 @@ import javax.annotation.Nonnull; ...@@ -33,7 +33,7 @@ import javax.annotation.Nonnull;
import javax.servlet.ServletException; import javax.servlet.ServletException;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.Charset; import java.nio.charset.StandardCharsets;
import java.util.Map; import java.util.Map;
/** /**
...@@ -142,8 +142,6 @@ public class HttpResponses extends org.kohsuke.stapler.HttpResponses { ...@@ -142,8 +142,6 @@ public class HttpResponses extends org.kohsuke.stapler.HttpResponses {
*/ */
static class JSONObjectResponse implements HttpResponse { static class JSONObjectResponse implements HttpResponse {
private static final Charset UTF8 = Charset.forName("UTF-8");
private final JSONObject jsonObject; private final JSONObject jsonObject;
/** /**
...@@ -210,7 +208,7 @@ public class HttpResponses extends org.kohsuke.stapler.HttpResponses { ...@@ -210,7 +208,7 @@ public class HttpResponses extends org.kohsuke.stapler.HttpResponses {
*/ */
@Override @Override
public void generateResponse(StaplerRequest req, StaplerResponse rsp, Object node) throws IOException, ServletException { public void generateResponse(StaplerRequest req, StaplerResponse rsp, Object node) throws IOException, ServletException {
byte[] bytes = jsonObject.toString().getBytes(UTF8); byte[] bytes = jsonObject.toString().getBytes(StandardCharsets.UTF_8);
rsp.setContentType("application/json; charset=UTF-8"); rsp.setContentType("application/json; charset=UTF-8");
rsp.setContentLength(bytes.length); rsp.setContentLength(bytes.length);
rsp.getOutputStream().write(bytes); rsp.getOutputStream().write(bytes);
......
...@@ -30,6 +30,7 @@ import java.nio.charset.CharsetDecoder; ...@@ -30,6 +30,7 @@ import java.nio.charset.CharsetDecoder;
import java.nio.charset.CodingErrorAction; import java.nio.charset.CodingErrorAction;
import java.nio.charset.CoderResult; import java.nio.charset.CoderResult;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.charset.UnsupportedCharsetException; import java.nio.charset.UnsupportedCharsetException;
import java.nio.*; import java.nio.*;
...@@ -127,7 +128,7 @@ public class WriterOutputStream extends OutputStream { ...@@ -127,7 +128,7 @@ public class WriterOutputStream extends OutputStream {
String encoding = System.getProperty("file.encoding"); String encoding = System.getProperty("file.encoding");
return Charset.forName(encoding); return Charset.forName(encoding);
} catch (UnsupportedCharsetException e) { } catch (UnsupportedCharsetException e) {
return Charset.forName("UTF-8"); return StandardCharsets.UTF_8;
} }
} }
} }
...@@ -70,7 +70,7 @@ import java.io.Writer; ...@@ -70,7 +70,7 @@ import java.io.Writer;
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.nio.charset.Charset; import java.nio.charset.StandardCharsets;
import java.util.HashSet; import java.util.HashSet;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
...@@ -308,7 +308,7 @@ public class XStream2 extends XStream { ...@@ -308,7 +308,7 @@ public class XStream2 extends XStream {
* @since 1.504 * @since 1.504
*/ */
public void toXMLUTF8(Object obj, OutputStream out) throws IOException { public void toXMLUTF8(Object obj, OutputStream out) throws IOException {
Writer w = new OutputStreamWriter(out, Charset.forName("UTF-8")); Writer w = new OutputStreamWriter(out, StandardCharsets.UTF_8);
w.write("<?xml version=\"1.1\" encoding=\"UTF-8\"?>\n"); w.write("<?xml version=\"1.1\" encoding=\"UTF-8\"?>\n");
toXML(obj, w); toXML(obj, w);
} }
......
...@@ -83,7 +83,7 @@ public class AtomicFileWriterTest { ...@@ -83,7 +83,7 @@ public class AtomicFileWriterTest {
final Path childFileInSymlinkToDir = Paths.get(zeSymlink.toString(), "childFileInSymlinkToDir"); final Path childFileInSymlinkToDir = Paths.get(zeSymlink.toString(), "childFileInSymlinkToDir");
new AtomicFileWriter(childFileInSymlinkToDir, Charset.forName("UTF-8")); new AtomicFileWriter(childFileInSymlinkToDir, StandardCharsets.UTF_8);
} }
@Test @Test
...@@ -154,7 +154,7 @@ public class AtomicFileWriterTest { ...@@ -154,7 +154,7 @@ public class AtomicFileWriterTest {
assertFalse(parentExistsAndIsAFile.exists()); assertFalse(parentExistsAndIsAFile.exists());
try { try {
new AtomicFileWriter(parentExistsAndIsAFile.toPath(), Charset.forName("UTF-8")); new AtomicFileWriter(parentExistsAndIsAFile.toPath(), StandardCharsets.UTF_8);
fail("Expected a failure"); fail("Expected a failure");
} catch (IOException e) { } catch (IOException e) {
assertThat(e.getMessage(), assertThat(e.getMessage(),
......
...@@ -42,7 +42,6 @@ import org.jvnet.hudson.test.JenkinsRule; ...@@ -42,7 +42,6 @@ import org.jvnet.hudson.test.JenkinsRule;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.security.KeyFactory; import java.security.KeyFactory;
import java.security.interfaces.RSAPrivateKey; import java.security.interfaces.RSAPrivateKey;
import java.security.spec.PKCS8EncodedKeySpec; import java.security.spec.PKCS8EncodedKeySpec;
...@@ -172,7 +171,7 @@ public class UsageStatisticsTest { ...@@ -172,7 +171,7 @@ public class UsageStatisticsTest {
private void compareWithFile(String fileName, Object object) throws IOException { private void compareWithFile(String fileName, Object object) throws IOException {
Class clazz = this.getClass(); Class clazz = this.getClass();
String fileContent = Resources.toString(clazz.getResource(clazz.getSimpleName() + "/" + fileName), Charset.forName("UTF-8")); String fileContent = Resources.toString(clazz.getResource(clazz.getSimpleName() + "/" + fileName), StandardCharsets.UTF_8);
fileContent = fileContent.replace("JVMVENDOR", System.getProperty("java.vendor")); fileContent = fileContent.replace("JVMVENDOR", System.getProperty("java.vendor"));
fileContent = fileContent.replace("JVMNAME", System.getProperty("java.vm.name")); fileContent = fileContent.replace("JVMNAME", System.getProperty("java.vm.name"));
fileContent = fileContent.replace("JVMVERSION", System.getProperty("java.version")); fileContent = fileContent.replace("JVMVERSION", System.getProperty("java.version"));
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册