Skip to content

Commit

Permalink
Use StandardCharsets where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
dreis2211 authored and snicoll committed Nov 16, 2017
1 parent bcab23e commit 1e4941e
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.springframework.boot.autoconfigure.context;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

/**
* Configuration properties for Message Source.
Expand All @@ -37,7 +38,7 @@ public class MessageSourceProperties {
/**
* Message bundles encoding.
*/
private Charset encoding = Charset.forName("UTF-8");
private Charset encoding = StandardCharsets.UTF_8;

/**
* Loaded resource bundle files cache expiration, in seconds. When set to -1, bundles
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.springframework.boot.autoconfigure.http;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Locale;
import java.util.Map;

Expand All @@ -32,7 +33,7 @@
@ConfigurationProperties(prefix = "spring.http.encoding")
public class HttpEncodingProperties {

public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");
public static final Charset DEFAULT_CHARSET = StandardCharsets.UTF_8;

/**
* Charset of HTTP requests and responses. Added to the "Content-Type" header if not
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.springframework.boot.autoconfigure.mail;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

Expand All @@ -33,7 +34,7 @@
@ConfigurationProperties(prefix = "spring.mail")
public class MailProperties {

private static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");
private static final Charset DEFAULT_CHARSET = StandardCharsets.UTF_8;

/**
* SMTP server host.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.springframework.boot.autoconfigure.template;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.LinkedHashMap;
import java.util.Map;

Expand All @@ -36,7 +37,7 @@ public abstract class AbstractViewResolverProperties {

private static final MimeType DEFAULT_CONTENT_TYPE = MimeType.valueOf("text/html");

private static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");
private static final Charset DEFAULT_CHARSET = StandardCharsets.UTF_8;

/**
* Enable MVC view resolution for this technology.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.springframework.boot.autoconfigure.thymeleaf;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.List;

import org.springframework.boot.context.properties.ConfigurationProperties;
Expand All @@ -34,7 +35,7 @@
@ConfigurationProperties(prefix = "spring.thymeleaf")
public class ThymeleafProperties {

private static final Charset DEFAULT_ENCODING = Charset.forName("UTF-8");
private static final Charset DEFAULT_ENCODING = StandardCharsets.UTF_8;

public static final String DEFAULT_PREFIX = "classpath:/templates/";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package org.springframework.boot.autoconfigure.template;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

import org.junit.Test;

Expand Down Expand Up @@ -47,23 +47,23 @@ public void customContentTypeDefaultCharset() {
@Test
public void defaultContentTypeCustomCharset() {
ViewResolverProperties properties = new ViewResolverProperties();
properties.setCharset(Charset.forName("UTF-16"));
properties.setCharset(StandardCharsets.UTF_16);
assertThat(properties.getContentType()).hasToString("text/html;charset=UTF-16");
}

@Test
public void customContentTypeCustomCharset() {
ViewResolverProperties properties = new ViewResolverProperties();
properties.setContentType(MimeTypeUtils.parseMimeType("text/plain"));
properties.setCharset(Charset.forName("UTF-16"));
properties.setCharset(StandardCharsets.UTF_16);
assertThat(properties.getContentType()).hasToString("text/plain;charset=UTF-16");
}

@Test
public void customContentTypeWithPropertyAndCustomCharset() {
ViewResolverProperties properties = new ViewResolverProperties();
properties.setContentType(MimeTypeUtils.parseMimeType("text/plain;foo=bar"));
properties.setCharset(Charset.forName("UTF-16"));
properties.setCharset(StandardCharsets.UTF_16);
assertThat(properties.getContentType())
.hasToString("text/plain;charset=UTF-16;foo=bar");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ private static class FieldCollector implements TreeVisitor {
Map<String, Object> values = new HashMap<>();
values.put("Boolean.TRUE", true);
values.put("Boolean.FALSE", false);
values.put("StandardCharsets.ISO_8859_1", "ISO-8859-1");
values.put("StandardCharsets.UTF_8", "UTF-8");
values.put("StandardCharsets.UTF_16", "UTF-16");
values.put("StandardCharsets.US_ASCII", "US-ASCII");
wellKnownStaticFinals = Collections.unmodifiableMap(values);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.springframework.boot.configurationsample.fieldvalues;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

import org.springframework.boot.configurationsample.ConfigurationProperties;
import org.springframework.util.MimeType;
Expand All @@ -41,7 +42,7 @@ public class FieldValues {

private static final Integer INTEGER_OBJ_CONST = 4;

private static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");
private static final Charset DEFAULT_CHARSET = StandardCharsets.UTF_8;

private static final MimeType DEFAULT_MIME_TYPE = MimeType.valueOf("text/plain");

Expand Down Expand Up @@ -77,7 +78,7 @@ public class FieldValues {

private Integer integerObjectConst = INTEGER_OBJ_CONST;

private Charset charset = Charset.forName("US-ASCII");
private Charset charset = StandardCharsets.US_ASCII;

private Charset charsetConst = DEFAULT_CHARSET;

Expand Down

0 comments on commit 1e4941e

Please sign in to comment.