From bd0dcfb172780d7b45c677e83ffd38f6b2fe9762 Mon Sep 17 00:00:00 2001 From: Johnny Lim Date: Fri, 10 Nov 2017 05:43:06 +0900 Subject: [PATCH] Utilize StandardCharsets Closes gh-10972 --- .../actuate/autoconfigure/cloudfoundry/Token.java | 6 ++---- .../reactive/ReactiveTokenValidatorTests.java | 6 ++---- .../cloudfoundry/servlet/TokenValidatorTests.java | 6 ++---- ...rseyAutoConfigurationServletContainerTests.java | 4 ++-- .../autoconfigure/web/ServerPropertiesTests.java | 4 ++-- .../HttpEncodingAutoConfigurationTests.java | 6 +++--- .../boot/cli/command/init/InitializrService.java | 5 ++--- .../install/GroovyGrabDependencyResolver.java | 3 ++- .../init/InitializrServiceMetadataTests.java | 6 +++--- .../init/ProjectGenerationRequestTests.java | 6 +++--- .../classloader/RestartClassLoaderTests.java | 6 ++---- .../data/redis/DataRedisTestIntegrationTests.java | 3 ++- .../autoconfigure/data/redis/ExampleService.java | 3 ++- .../springframework/boot/test/json/JsonLoader.java | 3 ++- ...ConfigurationMetadataRepositoryJsonBuilder.java | 14 +++++--------- .../configurationmetadata/JsonReaderTests.java | 5 +++-- .../metadata/JsonMarshaller.java | 8 +++----- .../boot/loader/tools/DefaultLaunchScript.java | 8 +++----- .../boot/loader/jar/AsciiBytes.java | 12 +++++------- .../org/springframework/boot/ResourceBanner.java | 3 ++- .../boot/env/OriginTrackedPropertiesLoader.java | 3 ++- .../logback/DefaultLogbackConfiguration.java | 6 ++---- .../tomcat/TomcatServletWebServerFactory.java | 3 ++- .../tomcat/TomcatServletWebServerFactoryTests.java | 7 ++++--- .../ServletWebServerMvcIntegrationTests.java | 4 ++-- .../AbstractServletWebServerFactoryTests.java | 9 +++++---- .../sample/jetty/SampleJettyApplicationTests.java | 4 ++-- .../tomcat/SampleTomcatApplicationTests.java | 3 ++- .../undertow/SampleUndertowApplicationTests.java | 3 ++- .../sample/HelloWebSecurityApplicationTests.java | 3 ++- 30 files changed, 77 insertions(+), 85 deletions(-) diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/cloudfoundry/Token.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/cloudfoundry/Token.java index 3011ddbfab..905db882ea 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/cloudfoundry/Token.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/cloudfoundry/Token.java @@ -16,7 +16,7 @@ package org.springframework.boot.actuate.autoconfigure.cloudfoundry; -import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.util.List; import java.util.Map; @@ -33,8 +33,6 @@ import org.springframework.util.StringUtils; */ public class Token { - private static final Charset UTF_8 = Charset.forName("UTF-8"); - private final String encoded; private final String signature; @@ -63,7 +61,7 @@ public class Token { private Map parseJson(String base64) { try { byte[] bytes = Base64Utils.decodeFromUrlSafeString(base64); - return JsonParserFactory.getJsonParser().parseMap(new String(bytes, UTF_8)); + return JsonParserFactory.getJsonParser().parseMap(new String(bytes, StandardCharsets.UTF_8)); } catch (RuntimeException ex) { throw new CloudFoundryAuthorizationException(Reason.INVALID_TOKEN, diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/cloudfoundry/reactive/ReactiveTokenValidatorTests.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/cloudfoundry/reactive/ReactiveTokenValidatorTests.java index 7cd9ac4159..d484976c23 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/cloudfoundry/reactive/ReactiveTokenValidatorTests.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/cloudfoundry/reactive/ReactiveTokenValidatorTests.java @@ -18,7 +18,7 @@ package org.springframework.boot.actuate.autoconfigure.cloudfoundry.reactive; import java.io.ByteArrayOutputStream; import java.io.IOException; -import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.security.KeyFactory; import java.security.NoSuchAlgorithmException; import java.security.PrivateKey; @@ -54,8 +54,6 @@ public class ReactiveTokenValidatorTests { private static final byte[] DOT = ".".getBytes(); - private static final Charset UTF_8 = Charset.forName("UTF-8"); - @Mock private ReactiveCloudFoundrySecurityService securityService; @@ -226,7 +224,7 @@ public class ReactiveTokenValidatorTests { byte[] crypto = signature.sign(); byte[] token = dotConcat(Base64Utils.encodeUrlSafe(header), Base64Utils.encodeUrlSafe(claims), Base64Utils.encodeUrlSafe(crypto)); - return new String(token, UTF_8); + return new String(token, StandardCharsets.UTF_8); } private PrivateKey getPrivateKey() diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/cloudfoundry/servlet/TokenValidatorTests.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/cloudfoundry/servlet/TokenValidatorTests.java index a8f77238bb..a4361ae4e6 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/cloudfoundry/servlet/TokenValidatorTests.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/cloudfoundry/servlet/TokenValidatorTests.java @@ -18,7 +18,7 @@ package org.springframework.boot.actuate.autoconfigure.cloudfoundry.servlet; import java.io.ByteArrayOutputStream; import java.io.IOException; -import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.security.KeyFactory; import java.security.NoSuchAlgorithmException; import java.security.PrivateKey; @@ -56,8 +56,6 @@ public class TokenValidatorTests { private static final byte[] DOT = ".".getBytes(); - private static final Charset UTF_8 = Charset.forName("UTF-8"); - @Rule public ExpectedException thrown = ExpectedException.none(); @@ -216,7 +214,7 @@ public class TokenValidatorTests { byte[] crypto = signature.sign(); byte[] token = dotConcat(Base64Utils.encodeUrlSafe(header), Base64Utils.encodeUrlSafe(claims), Base64Utils.encodeUrlSafe(crypto)); - return new String(token, UTF_8); + return new String(token, StandardCharsets.UTF_8); } private PrivateKey getPrivateKey() diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jersey/JerseyAutoConfigurationServletContainerTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jersey/JerseyAutoConfigurationServletContainerTests.java index 0909b1fe1a..f4f70b16dc 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jersey/JerseyAutoConfigurationServletContainerTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jersey/JerseyAutoConfigurationServletContainerTests.java @@ -16,7 +16,7 @@ package org.springframework.boot.autoconfigure.jersey; -import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import javax.ws.rs.GET; import javax.ws.rs.Path; @@ -105,7 +105,7 @@ public class JerseyAutoConfigurationServletContainerTests { jerseyServlet.setServlet(new ServletContainer()); jerseyServlet.setOverridable(false); context.addChild(jerseyServlet); - String pattern = UDecoder.URLDecode("/*", Charset.forName("UTF-8")); + String pattern = UDecoder.URLDecode("/*", StandardCharsets.UTF_8); context.addServletMappingDecoded(pattern, servletName); } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ServerPropertiesTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ServerPropertiesTests.java index 95c67693e0..3dd8ef2d26 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ServerPropertiesTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ServerPropertiesTests.java @@ -17,7 +17,7 @@ package org.springframework.boot.autoconfigure.web; import java.net.InetAddress; -import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.util.Collections; import java.util.HashMap; import java.util.Map; @@ -141,7 +141,7 @@ public class ServerPropertiesTests { public void testCustomizeUriEncoding() throws Exception { bind("server.tomcat.uri-encoding", "US-ASCII"); assertThat(this.properties.getTomcat().getUriEncoding()) - .isEqualTo(Charset.forName("US-ASCII")); + .isEqualTo(StandardCharsets.US_ASCII); } @Test diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/HttpEncodingAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/HttpEncodingAutoConfigurationTests.java index eb814fb204..16d29ff34d 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/HttpEncodingAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/HttpEncodingAutoConfigurationTests.java @@ -16,7 +16,7 @@ package org.springframework.boot.autoconfigure.web.servlet; -import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.List; import java.util.Locale; @@ -160,10 +160,10 @@ public class HttpEncodingAutoConfigurationTests { .getLocaleCharsetMappings().size()).isEqualTo(2); assertThat(this.context.getBean(MockServletWebServerFactory.class) .getLocaleCharsetMappings().get(Locale.ENGLISH)) - .isEqualTo(Charset.forName("UTF-8")); + .isEqualTo(StandardCharsets.UTF_8); assertThat(this.context.getBean(MockServletWebServerFactory.class) .getLocaleCharsetMappings().get(Locale.FRANCE)) - .isEqualTo(Charset.forName("UTF-8")); + .isEqualTo(StandardCharsets.UTF_8); } @SuppressWarnings({ "unchecked", "rawtypes" }) diff --git a/spring-boot-project/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/init/InitializrService.java b/spring-boot-project/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/init/InitializrService.java index 1a7e54dbd5..66b15e0063 100644 --- a/spring-boot-project/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/init/InitializrService.java +++ b/spring-boot-project/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/init/InitializrService.java @@ -19,6 +19,7 @@ package org.springframework.boot.cli.command.init; import java.io.IOException; import java.net.URI; import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import org.apache.http.Header; import org.apache.http.HttpEntity; @@ -47,8 +48,6 @@ class InitializrService { private static final String FILENAME_HEADER_PREFIX = "filename=\""; - private static final Charset UTF_8 = Charset.forName("UTF-8"); - /** * Accept header to use to retrieve the json meta-data. */ @@ -241,7 +240,7 @@ class InitializrService { private String getContent(HttpEntity entity) throws IOException { ContentType contentType = ContentType.getOrDefault(entity); Charset charset = contentType.getCharset(); - charset = (charset != null ? charset : UTF_8); + charset = (charset != null ? charset : StandardCharsets.UTF_8); byte[] content = FileCopyUtils.copyToByteArray(entity.getContent()); return new String(content, charset); } diff --git a/spring-boot-project/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/install/GroovyGrabDependencyResolver.java b/spring-boot-project/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/install/GroovyGrabDependencyResolver.java index 561ae16b00..2cd3ebcceb 100644 --- a/spring-boot-project/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/install/GroovyGrabDependencyResolver.java +++ b/spring-boot-project/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/install/GroovyGrabDependencyResolver.java @@ -22,6 +22,7 @@ import java.io.IOException; import java.io.OutputStreamWriter; import java.net.URISyntaxException; import java.net.URL; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -70,7 +71,7 @@ class GroovyGrabDependencyResolver implements DependencyResolver { File file = File.createTempFile("SpringCLIDependency", ".groovy"); file.deleteOnExit(); try (OutputStreamWriter stream = new OutputStreamWriter( - new FileOutputStream(file), "UTF-8")) { + new FileOutputStream(file), StandardCharsets.UTF_8)) { for (String artifactIdentifier : artifactIdentifiers) { stream.write("@Grab('" + artifactIdentifier + "')"); } diff --git a/spring-boot-project/spring-boot-cli/src/test/java/org/springframework/boot/cli/command/init/InitializrServiceMetadataTests.java b/spring-boot-project/spring-boot-cli/src/test/java/org/springframework/boot/cli/command/init/InitializrServiceMetadataTests.java index 8f496abee0..56ce8f8ce5 100644 --- a/spring-boot-project/spring-boot-cli/src/test/java/org/springframework/boot/cli/command/init/InitializrServiceMetadataTests.java +++ b/spring-boot-project/spring-boot-cli/src/test/java/org/springframework/boot/cli/command/init/InitializrServiceMetadataTests.java @@ -18,7 +18,7 @@ package org.springframework.boot.cli.command.init; import java.io.IOException; import java.io.InputStream; -import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import org.json.JSONException; import org.json.JSONObject; @@ -31,7 +31,7 @@ import org.springframework.util.StreamUtils; import static org.assertj.core.api.Assertions.assertThat; /** - * Tests for {@link InitializrServiceMetadata} + * Tests for {@link InitializrServiceMetadata}. * * @author Stephane Nicoll */ @@ -94,7 +94,7 @@ public class InitializrServiceMetadataTests { "metadata/service-metadata-" + version + ".json"); try (InputStream stream = resource.getInputStream()) { return new JSONObject( - StreamUtils.copyToString(stream, Charset.forName("UTF-8"))); + StreamUtils.copyToString(stream, StandardCharsets.UTF_8)); } } diff --git a/spring-boot-project/spring-boot-cli/src/test/java/org/springframework/boot/cli/command/init/ProjectGenerationRequestTests.java b/spring-boot-project/spring-boot-cli/src/test/java/org/springframework/boot/cli/command/init/ProjectGenerationRequestTests.java index ce854ae0ed..3dd2b296f5 100644 --- a/spring-boot-project/spring-boot-cli/src/test/java/org/springframework/boot/cli/command/init/ProjectGenerationRequestTests.java +++ b/spring-boot-project/spring-boot-cli/src/test/java/org/springframework/boot/cli/command/init/ProjectGenerationRequestTests.java @@ -19,7 +19,7 @@ package org.springframework.boot.cli.command.init; import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; -import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.util.Collections; import java.util.Map; @@ -36,7 +36,7 @@ import org.springframework.util.StreamUtils; import static org.assertj.core.api.Assertions.assertThat; /** - * Tests for {@link ProjectGenerationRequest} + * Tests for {@link ProjectGenerationRequest}. * * @author Stephane Nicoll * @author EddĂș MelĂ©ndez @@ -253,7 +253,7 @@ public class ProjectGenerationRequestTests { Resource resource = new ClassPathResource( "metadata/service-metadata-" + version + ".json"); String content = StreamUtils.copyToString(resource.getInputStream(), - Charset.forName("UTF-8")); + StandardCharsets.UTF_8); JSONObject json = new JSONObject(content); return new InitializrServiceMetadata(json); } diff --git a/spring-boot-project/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/restart/classloader/RestartClassLoaderTests.java b/spring-boot-project/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/restart/classloader/RestartClassLoaderTests.java index 1060f03951..501eadd05c 100644 --- a/spring-boot-project/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/restart/classloader/RestartClassLoaderTests.java +++ b/spring-boot-project/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/restart/classloader/RestartClassLoaderTests.java @@ -22,7 +22,7 @@ import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.net.URLClassLoader; -import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Enumeration; import java.util.List; @@ -54,8 +54,6 @@ public class RestartClassLoaderTests { private static final String PACKAGE_PATH = PACKAGE.replace('.', '/'); - private static final Charset UTF_8 = Charset.forName("UTF-8"); - @Rule public ExpectedException thrown = ExpectedException.none(); @@ -89,7 +87,7 @@ public class RestartClassLoaderTests { StreamUtils.copy(getClass().getResourceAsStream("Sample.class"), jarOutputStream); jarOutputStream.closeEntry(); jarOutputStream.putNextEntry(new ZipEntry(PACKAGE_PATH + "/Sample.txt")); - StreamUtils.copy("fromchild", UTF_8, jarOutputStream); + StreamUtils.copy("fromchild", StandardCharsets.UTF_8, jarOutputStream); jarOutputStream.closeEntry(); jarOutputStream.close(); return file; diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/redis/DataRedisTestIntegrationTests.java b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/redis/DataRedisTestIntegrationTests.java index e60ca1f171..da15a32b2d 100644 --- a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/redis/DataRedisTestIntegrationTests.java +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/redis/DataRedisTestIntegrationTests.java @@ -17,6 +17,7 @@ package org.springframework.boot.test.autoconfigure.data.redis; import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import org.junit.Rule; import org.junit.Test; @@ -57,7 +58,7 @@ public class DataRedisTestIntegrationTests { @Autowired private ApplicationContext applicationContext; - private static final Charset CHARSET = Charset.forName("UTF-8"); + private static final Charset CHARSET = StandardCharsets.UTF_8; @Test public void testRepository() { diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/redis/ExampleService.java b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/redis/ExampleService.java index a3b29170ce..875b6dee0e 100644 --- a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/redis/ExampleService.java +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/redis/ExampleService.java @@ -17,6 +17,7 @@ package org.springframework.boot.test.autoconfigure.data.redis; import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import org.springframework.data.redis.connection.RedisConnection; import org.springframework.data.redis.core.RedisOperations; @@ -30,7 +31,7 @@ import org.springframework.stereotype.Service; @Service public class ExampleService { - private static final Charset CHARSET = Charset.forName("UTF-8"); + private static final Charset CHARSET = StandardCharsets.UTF_8; private RedisOperations operations; diff --git a/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/json/JsonLoader.java b/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/json/JsonLoader.java index e32bba4be1..ee51a6d3d9 100644 --- a/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/json/JsonLoader.java +++ b/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/json/JsonLoader.java @@ -23,6 +23,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; @@ -42,7 +43,7 @@ class JsonLoader { JsonLoader(Class resourceLoadClass, Charset charset) { this.resourceLoadClass = resourceLoadClass; - this.charset = charset == null ? Charset.forName("UTF-8") : charset; + this.charset = charset == null ? StandardCharsets.UTF_8 : charset; } Class getResourceLoadClass() { diff --git a/spring-boot-project/spring-boot-tools/spring-boot-configuration-metadata/src/main/java/org/springframework/boot/configurationmetadata/ConfigurationMetadataRepositoryJsonBuilder.java b/spring-boot-project/spring-boot-tools/spring-boot-configuration-metadata/src/main/java/org/springframework/boot/configurationmetadata/ConfigurationMetadataRepositoryJsonBuilder.java index 045776768b..9d19d0c7c8 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-configuration-metadata/src/main/java/org/springframework/boot/configurationmetadata/ConfigurationMetadataRepositoryJsonBuilder.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-configuration-metadata/src/main/java/org/springframework/boot/configurationmetadata/ConfigurationMetadataRepositoryJsonBuilder.java @@ -19,6 +19,7 @@ package org.springframework.boot.configurationmetadata; import java.io.IOException; import java.io.InputStream; import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -32,12 +33,7 @@ import java.util.Map; */ public final class ConfigurationMetadataRepositoryJsonBuilder { - /** - * UTF-8 Charset. - */ - public static final Charset UTF_8 = Charset.forName("UTF-8"); - - private Charset defaultCharset = UTF_8; + private Charset defaultCharset = StandardCharsets.UTF_8; private final JsonReader reader = new JsonReader(); @@ -159,7 +155,7 @@ public final class ConfigurationMetadataRepositoryJsonBuilder { } /** - * Create a new builder instance using {@link #UTF_8} as the default charset and the + * Create a new builder instance using {@link StandardCharsets#UTF_8} as the default charset and the * specified json resource. * @param inputStreams the source input streams * @return a new {@link ConfigurationMetadataRepositoryJsonBuilder} instance. @@ -175,11 +171,11 @@ public final class ConfigurationMetadataRepositoryJsonBuilder { } /** - * Create a new builder instance using {@link #UTF_8} as the default charset. + * Create a new builder instance using {@link StandardCharsets#UTF_8} as the default charset. * @return a new {@link ConfigurationMetadataRepositoryJsonBuilder} instance. */ public static ConfigurationMetadataRepositoryJsonBuilder create() { - return create(UTF_8); + return create(StandardCharsets.UTF_8); } /** diff --git a/spring-boot-project/spring-boot-tools/spring-boot-configuration-metadata/src/test/java/org/springframework/boot/configurationmetadata/JsonReaderTests.java b/spring-boot-project/spring-boot-tools/spring-boot-configuration-metadata/src/test/java/org/springframework/boot/configurationmetadata/JsonReaderTests.java index 9d855943f8..fbcc3c5c04 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-configuration-metadata/src/test/java/org/springframework/boot/configurationmetadata/JsonReaderTests.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-configuration-metadata/src/test/java/org/springframework/boot/configurationmetadata/JsonReaderTests.java @@ -18,6 +18,7 @@ package org.springframework.boot.configurationmetadata; import java.io.IOException; import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.util.List; import org.hamcrest.CoreMatchers; @@ -27,13 +28,13 @@ import org.junit.Test; import static org.assertj.core.api.Assertions.assertThat; /** - * Tests for {@link JsonReader} + * Tests for {@link JsonReader}. * * @author Stephane Nicoll */ public class JsonReaderTests extends AbstractConfigurationMetadataTests { - private static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8"); + private static final Charset DEFAULT_CHARSET = StandardCharsets.UTF_8; private final JsonReader reader = new JsonReader(); diff --git a/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/metadata/JsonMarshaller.java b/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/metadata/JsonMarshaller.java index 3739b484ac..389e55b65e 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/metadata/JsonMarshaller.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/metadata/JsonMarshaller.java @@ -20,7 +20,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; -import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; @@ -41,8 +41,6 @@ import org.springframework.boot.configurationprocessor.metadata.ItemMetadata.Ite */ public class JsonMarshaller { - private static final Charset UTF_8 = Charset.forName("UTF-8"); - private static final int BUFFER_SIZE = 4098; public void write(ConfigurationMetadata metadata, OutputStream outputStream) @@ -53,7 +51,7 @@ public class JsonMarshaller { object.put("groups", converter.toJsonArray(metadata, ItemType.GROUP)); object.put("properties", converter.toJsonArray(metadata, ItemType.PROPERTY)); object.put("hints", converter.toJsonArray(metadata.getHints())); - outputStream.write(object.toString(2).getBytes(UTF_8)); + outputStream.write(object.toString(2).getBytes(StandardCharsets.UTF_8)); } catch (Exception ex) { if (ex instanceof IOException) { @@ -170,7 +168,7 @@ public class JsonMarshaller { private String toString(InputStream inputStream) throws IOException { StringBuilder out = new StringBuilder(); - InputStreamReader reader = new InputStreamReader(inputStream, UTF_8); + InputStreamReader reader = new InputStreamReader(inputStream, StandardCharsets.UTF_8); char[] buffer = new char[BUFFER_SIZE]; int bytesRead; while ((bytesRead = reader.read(buffer)) != -1) { diff --git a/spring-boot-project/spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/DefaultLaunchScript.java b/spring-boot-project/spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/DefaultLaunchScript.java index fc64fd84ca..4b1a389e92 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/DefaultLaunchScript.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/DefaultLaunchScript.java @@ -22,7 +22,7 @@ import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; -import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.Collections; import java.util.HashSet; @@ -42,8 +42,6 @@ import java.util.regex.Pattern; */ public class DefaultLaunchScript implements LaunchScript { - private static final Charset UTF_8 = Charset.forName("UTF-8"); - private static final int BUFFER_SIZE = 4096; private static final Pattern PLACEHOLDER_PATTERN = Pattern @@ -76,7 +74,7 @@ public class DefaultLaunchScript implements LaunchScript { try { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); copy(inputStream, outputStream); - return new String(outputStream.toByteArray(), UTF_8); + return new String(outputStream.toByteArray(), StandardCharsets.UTF_8); } finally { inputStream.close(); @@ -129,7 +127,7 @@ public class DefaultLaunchScript implements LaunchScript { @Override public byte[] toByteArray() { - return this.content.getBytes(UTF_8); + return this.content.getBytes(StandardCharsets.UTF_8); } } diff --git a/spring-boot-project/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar/AsciiBytes.java b/spring-boot-project/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar/AsciiBytes.java index d6b2ab42b2..10b86d8093 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar/AsciiBytes.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar/AsciiBytes.java @@ -16,7 +16,7 @@ package org.springframework.boot.loader.jar; -import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; /** * Simple wrapper around a byte array that represents an ASCII. Used for performance @@ -27,8 +27,6 @@ import java.nio.charset.Charset; */ final class AsciiBytes { - private static final Charset UTF_8 = Charset.forName("UTF-8"); - private final byte[] bytes; private final int offset; @@ -44,7 +42,7 @@ final class AsciiBytes { * @param string the source string */ AsciiBytes(String string) { - this(string.getBytes(UTF_8)); + this(string.getBytes(StandardCharsets.UTF_8)); this.string = string; } @@ -124,7 +122,7 @@ final class AsciiBytes { if (string == null || string.isEmpty()) { return this; } - return append(string.getBytes(UTF_8)); + return append(string.getBytes(StandardCharsets.UTF_8)); } public AsciiBytes append(AsciiBytes asciiBytes) { @@ -147,7 +145,7 @@ final class AsciiBytes { @Override public String toString() { if (this.string == null) { - this.string = new String(this.bytes, this.offset, this.length, UTF_8); + this.string = new String(this.bytes, this.offset, this.length, StandardCharsets.UTF_8); } return this.string; } @@ -215,7 +213,7 @@ final class AsciiBytes { } static String toString(byte[] bytes) { - return new String(bytes, UTF_8); + return new String(bytes, StandardCharsets.UTF_8); } public static int hashCode(String string) { diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/ResourceBanner.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/ResourceBanner.java index 1373a75d3b..992ae560b9 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/ResourceBanner.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/ResourceBanner.java @@ -18,6 +18,7 @@ package org.springframework.boot; import java.io.PrintStream; import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; @@ -62,7 +63,7 @@ public class ResourceBanner implements Banner { try { String banner = StreamUtils.copyToString(this.resource.getInputStream(), environment.getProperty("banner.charset", Charset.class, - Charset.forName("UTF-8"))); + StandardCharsets.UTF_8)); for (PropertyResolver resolver : getPropertyResolvers(environment, sourceClass)) { diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/env/OriginTrackedPropertiesLoader.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/env/OriginTrackedPropertiesLoader.java index b18a5f8646..801569f084 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/env/OriginTrackedPropertiesLoader.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/env/OriginTrackedPropertiesLoader.java @@ -20,6 +20,7 @@ import java.io.Closeable; import java.io.IOException; import java.io.InputStreamReader; import java.io.LineNumberReader; +import java.nio.charset.StandardCharsets; import java.util.LinkedHashMap; import java.util.Map; @@ -155,7 +156,7 @@ class OriginTrackedPropertiesLoader { CharacterReader(Resource resource) throws IOException { this.reader = new LineNumberReader( - new InputStreamReader(resource.getInputStream(), "8859_1")); + new InputStreamReader(resource.getInputStream(), StandardCharsets.ISO_8859_1)); } @Override diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/DefaultLogbackConfiguration.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/DefaultLogbackConfiguration.java index 54338b4499..18f8c3ac4d 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/DefaultLogbackConfiguration.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/DefaultLogbackConfiguration.java @@ -17,7 +17,7 @@ package org.springframework.boot.logging.logback; import java.lang.reflect.Method; -import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import ch.qos.logback.classic.Level; import ch.qos.logback.classic.encoder.PatternLayoutEncoder; @@ -60,8 +60,6 @@ class DefaultLogbackConfiguration { private static final String MAX_FILE_SIZE = "10MB"; - private static final Charset UTF8 = Charset.forName("UTF-8"); - private final PropertyResolver patterns; private final LogFile logFile; @@ -125,7 +123,7 @@ class DefaultLogbackConfiguration { String logPattern = this.patterns.getProperty("logging.pattern.console", CONSOLE_LOG_PATTERN); encoder.setPattern(OptionHelper.substVars(logPattern, config.getContext())); - encoder.setCharset(UTF8); + encoder.setCharset(StandardCharsets.UTF_8); config.start(encoder); appender.setEncoder(encoder); config.appender("CONSOLE", appender); diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/tomcat/TomcatServletWebServerFactory.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/tomcat/TomcatServletWebServerFactory.java index 01a29d5d05..c9f8afeaff 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/tomcat/TomcatServletWebServerFactory.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/tomcat/TomcatServletWebServerFactory.java @@ -21,6 +21,7 @@ import java.io.InputStream; import java.lang.reflect.Method; import java.net.URL; import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -97,7 +98,7 @@ import org.springframework.util.StringUtils; public class TomcatServletWebServerFactory extends AbstractServletWebServerFactory implements ResourceLoaderAware { - private static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8"); + private static final Charset DEFAULT_CHARSET = StandardCharsets.UTF_8; private static final Set> NO_CLASSES = Collections.emptySet(); diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/embedded/tomcat/TomcatServletWebServerFactoryTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/embedded/tomcat/TomcatServletWebServerFactoryTests.java index daaea47834..4864bada86 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/embedded/tomcat/TomcatServletWebServerFactoryTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/embedded/tomcat/TomcatServletWebServerFactoryTests.java @@ -19,6 +19,7 @@ package org.springframework.boot.web.embedded.tomcat; import java.io.File; import java.io.IOException; import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.Locale; import java.util.Map; @@ -248,7 +249,7 @@ public class TomcatServletWebServerFactoryTests @Test public void uriEncoding() throws Exception { TomcatServletWebServerFactory factory = getFactory(); - factory.setUriEncoding(Charset.forName("US-ASCII")); + factory.setUriEncoding(StandardCharsets.US_ASCII); Tomcat tomcat = getTomcat(factory); Connector connector = ((TomcatWebServer) this.webServer).getServiceConnectors() .get(tomcat.getService())[0]; @@ -364,8 +365,8 @@ public class TomcatServletWebServerFactoryTests TomcatServletWebServerFactory factory = getFactory(); this.webServer = factory.getWebServer(); // override defaults, see org.apache.catalina.util.CharsetMapperDefault.properties - assertThat(getCharset(Locale.ENGLISH).toString()).isEqualTo("UTF-8"); - assertThat(getCharset(Locale.FRENCH).toString()).isEqualTo("UTF-8"); + assertThat(getCharset(Locale.ENGLISH)).isEqualTo(StandardCharsets.UTF_8); + assertThat(getCharset(Locale.FRENCH)).isEqualTo(StandardCharsets.UTF_8); } @Test diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/servlet/context/ServletWebServerMvcIntegrationTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/servlet/context/ServletWebServerMvcIntegrationTests.java index c4a0f03d15..e1a3256868 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/servlet/context/ServletWebServerMvcIntegrationTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/servlet/context/ServletWebServerMvcIntegrationTests.java @@ -17,7 +17,7 @@ package org.springframework.boot.web.servlet.context; import java.net.URI; -import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import org.junit.After; import org.junit.Test; @@ -103,7 +103,7 @@ public class ServletWebServerMvcIntegrationTests { HttpMethod.GET); try (ClientHttpResponse response = request.execute()) { String actual = StreamUtils.copyToString(response.getBody(), - Charset.forName("UTF-8")); + StandardCharsets.UTF_8); assertThat(actual).isEqualTo("Hello World"); } } diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/servlet/server/AbstractServletWebServerFactoryTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/servlet/server/AbstractServletWebServerFactoryTests.java index 8765e8fd6f..f84262e505 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/servlet/server/AbstractServletWebServerFactoryTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/servlet/server/AbstractServletWebServerFactoryTests.java @@ -29,6 +29,7 @@ import java.net.URI; import java.net.URISyntaxException; import java.net.URL; import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.security.KeyStore; import java.security.KeyStoreException; import java.security.NoSuchAlgorithmException; @@ -911,10 +912,10 @@ public abstract class AbstractServletWebServerFactoryTests { public void localeCharsetMappingsAreConfigured() throws Exception { AbstractServletWebServerFactory factory = getFactory(); Map mappings = new HashMap<>(); - mappings.put(Locale.GERMAN, Charset.forName("UTF-8")); + mappings.put(Locale.GERMAN, StandardCharsets.UTF_8); factory.setLocaleCharsetMappings(mappings); this.webServer = factory.getWebServer(); - assertThat(getCharset(Locale.GERMAN).toString()).isEqualTo("UTF-8"); + assertThat(getCharset(Locale.GERMAN)).isEqualTo(StandardCharsets.UTF_8); assertThat(getCharset(Locale.ITALIAN)).isNull(); } @@ -1063,7 +1064,7 @@ public abstract class AbstractServletWebServerFactoryTests { protected String getResponse(String url, HttpMethod method, String... headers) throws IOException, URISyntaxException { try (ClientHttpResponse response = getClientResponse(url, method, headers)) { - return StreamUtils.copyToString(response.getBody(), Charset.forName("UTF-8")); + return StreamUtils.copyToString(response.getBody(), StandardCharsets.UTF_8); } } @@ -1078,7 +1079,7 @@ public abstract class AbstractServletWebServerFactoryTests { throws IOException, URISyntaxException { try (ClientHttpResponse response = getClientResponse(url, method, requestFactory, headers)) { - return StreamUtils.copyToString(response.getBody(), Charset.forName("UTF-8")); + return StreamUtils.copyToString(response.getBody(), StandardCharsets.UTF_8); } } diff --git a/spring-boot-samples/spring-boot-sample-jetty/src/test/java/sample/jetty/SampleJettyApplicationTests.java b/spring-boot-samples/spring-boot-sample-jetty/src/test/java/sample/jetty/SampleJettyApplicationTests.java index d3ea7a18fd..12ade4317d 100644 --- a/spring-boot-samples/spring-boot-sample-jetty/src/test/java/sample/jetty/SampleJettyApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-jetty/src/test/java/sample/jetty/SampleJettyApplicationTests.java @@ -17,7 +17,7 @@ package sample.jetty; import java.io.ByteArrayInputStream; -import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.util.zip.GZIPInputStream; import org.junit.Test; @@ -69,7 +69,7 @@ public class SampleJettyApplicationTests { assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); try (GZIPInputStream inflater = new GZIPInputStream( new ByteArrayInputStream(entity.getBody()))) { - assertThat(StreamUtils.copyToString(inflater, Charset.forName("UTF-8"))) + assertThat(StreamUtils.copyToString(inflater, StandardCharsets.UTF_8)) .isEqualTo("Hello World"); } } diff --git a/spring-boot-samples/spring-boot-sample-tomcat/src/test/java/sample/tomcat/SampleTomcatApplicationTests.java b/spring-boot-samples/spring-boot-sample-tomcat/src/test/java/sample/tomcat/SampleTomcatApplicationTests.java index 42520d1328..03ec8ad680 100644 --- a/spring-boot-samples/spring-boot-sample-tomcat/src/test/java/sample/tomcat/SampleTomcatApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-tomcat/src/test/java/sample/tomcat/SampleTomcatApplicationTests.java @@ -18,6 +18,7 @@ package sample.tomcat; import java.io.ByteArrayInputStream; import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.util.zip.GZIPInputStream; import org.apache.coyote.AbstractProtocol; @@ -77,7 +78,7 @@ public class SampleTomcatApplicationTests { assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); try (GZIPInputStream inflater = new GZIPInputStream( new ByteArrayInputStream(entity.getBody()))) { - assertThat(StreamUtils.copyToString(inflater, Charset.forName("UTF-8"))) + assertThat(StreamUtils.copyToString(inflater, StandardCharsets.UTF_8)) .isEqualTo("Hello World"); } } diff --git a/spring-boot-samples/spring-boot-sample-undertow/src/test/java/sample/undertow/SampleUndertowApplicationTests.java b/spring-boot-samples/spring-boot-sample-undertow/src/test/java/sample/undertow/SampleUndertowApplicationTests.java index 35704151a3..33df9fe118 100644 --- a/spring-boot-samples/spring-boot-sample-undertow/src/test/java/sample/undertow/SampleUndertowApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-undertow/src/test/java/sample/undertow/SampleUndertowApplicationTests.java @@ -18,6 +18,7 @@ package sample.undertow; import java.io.ByteArrayInputStream; import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.util.zip.GZIPInputStream; import org.junit.Test; @@ -73,7 +74,7 @@ public class SampleUndertowApplicationTests { try (GZIPInputStream inflater = new GZIPInputStream( new ByteArrayInputStream(entity.getBody()))) { - assertThat(StreamUtils.copyToString(inflater, Charset.forName("UTF-8"))) + assertThat(StreamUtils.copyToString(inflater, StandardCharsets.UTF_8)) .isEqualTo("Hello World"); } } diff --git a/spring-boot-tests/spring-boot-integration-tests/spring-boot-security-tests/spring-boot-security-test-web-helloworld/src/test/java/sample/HelloWebSecurityApplicationTests.java b/spring-boot-tests/spring-boot-integration-tests/spring-boot-security-tests/spring-boot-security-test-web-helloworld/src/test/java/sample/HelloWebSecurityApplicationTests.java index d3e2572c81..616073a65a 100644 --- a/spring-boot-tests/spring-boot-integration-tests/spring-boot-security-tests/spring-boot-security-test-web-helloworld/src/test/java/sample/HelloWebSecurityApplicationTests.java +++ b/spring-boot-tests/spring-boot-integration-tests/spring-boot-security-tests/spring-boot-security-test-web-helloworld/src/test/java/sample/HelloWebSecurityApplicationTests.java @@ -16,6 +16,7 @@ package sample; +import java.nio.charset.StandardCharsets; import java.util.Base64; import javax.servlet.http.HttpServletResponse; @@ -67,7 +68,7 @@ public class HelloWebSecurityApplicationTests { public void userAuthenticates() throws Exception { this.request.addHeader("Accept", "application/json"); this.request.addHeader("Authorization", "Basic " + new String( - Base64.getEncoder().encode("user:password".getBytes("UTF-8")))); + Base64.getEncoder().encode("user:password".getBytes(StandardCharsets.UTF_8)))); this.springSecurityFilterChain.doFilter(this.request, this.response, this.chain);