Skip to content

Commit 3ae84d6

Browse files
committed
Consistent support for Charset/StandardCharsets in UriUtils etc
Issue: SPR-15613
1 parent 14161d1 commit 3ae84d6

File tree

15 files changed

+243
-170
lines changed

15 files changed

+243
-170
lines changed

spring-core/src/main/java/org/springframework/core/codec/ResourceEncoder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public boolean canEncode(ResolvableType elementType, @Nullable MimeType mimeType
6868

6969
@Override
7070
protected Flux<DataBuffer> encode(Resource resource, DataBufferFactory dataBufferFactory,
71-
ResolvableType type, MimeType mimeType, @Nullable Map<String, Object> hints) {
71+
ResolvableType type, @Nullable MimeType mimeType, @Nullable Map<String, Object> hints) {
7272

7373
try {
7474
if (resource.isFile()) {

spring-jdbc/src/main/java/org/springframework/jdbc/support/lob/DefaultLobHandler.java

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,7 +21,7 @@
2121
import java.io.InputStreamReader;
2222
import java.io.Reader;
2323
import java.io.StringReader;
24-
import java.io.UnsupportedEncodingException;
24+
import java.nio.charset.StandardCharsets;
2525
import java.sql.Blob;
2626
import java.sql.Clob;
2727
import java.sql.PreparedStatement;
@@ -322,17 +322,12 @@ public void setClobAsAsciiStream(
322322

323323
if (streamAsLob) {
324324
if (asciiStream != null) {
325-
try {
326-
Reader reader = new InputStreamReader(asciiStream, "US-ASCII");
327-
if (contentLength >= 0) {
328-
ps.setClob(paramIndex, reader, contentLength);
329-
}
330-
else {
331-
ps.setClob(paramIndex, reader);
332-
}
325+
Reader reader = new InputStreamReader(asciiStream, StandardCharsets.US_ASCII);
326+
if (contentLength >= 0) {
327+
ps.setClob(paramIndex, reader, contentLength);
333328
}
334-
catch (UnsupportedEncodingException ex) {
335-
throw new SQLException("US-ASCII encoding not supported: " + ex);
329+
else {
330+
ps.setClob(paramIndex, reader);
336331
}
337332
}
338333
else {

spring-jdbc/src/main/java/org/springframework/jdbc/support/lob/PassThroughClob.java

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,7 +23,6 @@
2323
import java.io.OutputStream;
2424
import java.io.Reader;
2525
import java.io.StringReader;
26-
import java.io.UnsupportedEncodingException;
2726
import java.io.Writer;
2827
import java.nio.charset.StandardCharsets;
2928
import java.sql.Clob;
@@ -72,19 +71,14 @@ public long length() throws SQLException {
7271

7372
@Override
7473
public Reader getCharacterStream() throws SQLException {
75-
try {
76-
if (this.content != null) {
77-
return new StringReader(this.content);
78-
}
79-
else if (this.characterStream != null) {
80-
return this.characterStream;
81-
}
82-
else {
83-
return new InputStreamReader(this.asciiStream, "US-ASCII");
84-
}
74+
if (this.content != null) {
75+
return new StringReader(this.content);
8576
}
86-
catch (UnsupportedEncodingException ex) {
87-
throw new SQLException("US-ASCII encoding not supported: " + ex);
77+
else if (this.characterStream != null) {
78+
return this.characterStream;
79+
}
80+
else {
81+
return new InputStreamReader(this.asciiStream, StandardCharsets.US_ASCII);
8882
}
8983
}
9084

@@ -102,9 +96,6 @@ else if (this.characterStream != null) {
10296
return this.asciiStream;
10397
}
10498
}
105-
catch (UnsupportedEncodingException ex) {
106-
throw new SQLException("US-ASCII encoding not supported: " + ex);
107-
}
10899
catch (IOException ex) {
109100
throw new SQLException("Failed to read stream content: " + ex);
110101
}

spring-test/src/main/java/org/springframework/test/web/servlet/request/MockHttpServletRequestBuilder.java

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import java.io.ByteArrayInputStream;
2020
import java.io.IOException;
2121
import java.io.InputStream;
22-
import java.io.UnsupportedEncodingException;
2322
import java.net.URI;
2423
import java.nio.charset.StandardCharsets;
2524
import java.security.Principal;
@@ -698,17 +697,12 @@ private void updatePathRequestProperties(MockHttpServletRequest request, String
698697
}
699698

700699
private void addRequestParams(MockHttpServletRequest request, MultiValueMap<String, String> map) {
701-
try {
702-
for (Entry<String, List<String>> entry : map.entrySet()) {
703-
for (String value : entry.getValue()) {
704-
value = (value != null) ? UriUtils.decode(value, "UTF-8") : null;
705-
request.addParameter(UriUtils.decode(entry.getKey(), "UTF-8"), value);
706-
}
700+
for (Entry<String, List<String>> entry : map.entrySet()) {
701+
for (String value : entry.getValue()) {
702+
value = (value != null ? UriUtils.decode(value, StandardCharsets.UTF_8) : null);
703+
request.addParameter(UriUtils.decode(entry.getKey(), StandardCharsets.UTF_8), value);
707704
}
708705
}
709-
catch (UnsupportedEncodingException ex) {
710-
// shouldn't happen
711-
}
712706
}
713707

714708
private MultiValueMap<String, String> parseFormData(final MediaType mediaType) {

spring-web/src/main/java/org/springframework/http/codec/xml/Jaxb2XmlEncoder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public boolean canEncode(ResolvableType elementType, @Nullable MimeType mimeType
7171

7272
@Override
7373
protected Flux<DataBuffer> encode(Object value, DataBufferFactory dataBufferFactory,
74-
ResolvableType type, MimeType mimeType, @Nullable Map<String, Object> hints) {
74+
ResolvableType type, @Nullable MimeType mimeType, @Nullable Map<String, Object> hints) {
7575
try {
7676
DataBuffer buffer = dataBufferFactory.allocateBuffer(1024);
7777
OutputStream outputStream = buffer.asOutputStream();

spring-web/src/main/java/org/springframework/http/converter/feed/AbstractWireFeedHttpMessageConverter.java

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -66,8 +66,8 @@ protected T readInternal(Class<? extends T> clazz, HttpInputMessage inputMessage
6666

6767
WireFeedInput feedInput = new WireFeedInput();
6868
MediaType contentType = inputMessage.getHeaders().getContentType();
69-
Charset charset =
70-
(contentType != null && contentType.getCharset() != null? contentType.getCharset() : DEFAULT_CHARSET);
69+
Charset charset = (contentType != null && contentType.getCharset() != null ?
70+
contentType.getCharset() : DEFAULT_CHARSET);
7171
try {
7272
Reader reader = new InputStreamReader(inputMessage.getBody(), charset);
7373
return (T) feedInput.build(reader);
@@ -81,20 +81,17 @@ protected T readInternal(Class<? extends T> clazz, HttpInputMessage inputMessage
8181
protected void writeInternal(T wireFeed, HttpOutputMessage outputMessage)
8282
throws IOException, HttpMessageNotWritableException {
8383

84-
String wireFeedEncoding = wireFeed.getEncoding();
85-
if (!StringUtils.hasLength(wireFeedEncoding)) {
86-
wireFeedEncoding = DEFAULT_CHARSET.name();
87-
}
84+
Charset charset = (StringUtils.hasLength(wireFeed.getEncoding()) ?
85+
Charset.forName(wireFeed.getEncoding()) : DEFAULT_CHARSET);
8886
MediaType contentType = outputMessage.getHeaders().getContentType();
8987
if (contentType != null) {
90-
Charset wireFeedCharset = Charset.forName(wireFeedEncoding);
91-
contentType = new MediaType(contentType.getType(), contentType.getSubtype(), wireFeedCharset);
88+
contentType = new MediaType(contentType.getType(), contentType.getSubtype(), charset);
9289
outputMessage.getHeaders().setContentType(contentType);
9390
}
9491

9592
WireFeedOutput feedOutput = new WireFeedOutput();
9693
try {
97-
Writer writer = new OutputStreamWriter(outputMessage.getBody(), wireFeedEncoding);
94+
Writer writer = new OutputStreamWriter(outputMessage.getBody(), charset);
9895
feedOutput.output(wireFeed, writer);
9996
}
10097
catch (FeedException ex) {

spring-web/src/main/java/org/springframework/web/client/RestClientResponseException.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,6 +18,7 @@
1818

1919
import java.io.UnsupportedEncodingException;
2020
import java.nio.charset.Charset;
21+
import java.nio.charset.StandardCharsets;
2122

2223
import org.springframework.http.HttpHeaders;
2324
import org.springframework.lang.Nullable;
@@ -32,7 +33,7 @@ public class RestClientResponseException extends RestClientException {
3233

3334
private static final long serialVersionUID = -8803556342728481792L;
3435

35-
private static final String DEFAULT_CHARSET = "ISO-8859-1";
36+
private static final Charset DEFAULT_CHARSET = StandardCharsets.ISO_8859_1;
3637

3738

3839
private final int rawStatusCode;
@@ -62,7 +63,7 @@ public RestClientResponseException(String message, int statusCode, String status
6263
this.statusText = statusText;
6364
this.responseHeaders = responseHeaders;
6465
this.responseBody = (responseBody != null ? responseBody : new byte[0]);
65-
this.responseCharset = (responseCharset != null ? responseCharset.name() : DEFAULT_CHARSET);
66+
this.responseCharset = (responseCharset != null ? responseCharset.name() : null);
6667
}
6768

6869

@@ -98,6 +99,9 @@ public byte[] getResponseBodyAsByteArray() {
9899
* Return the response body as a string.
99100
*/
100101
public String getResponseBodyAsString() {
102+
if (this.responseCharset == null) {
103+
return new String(this.responseBody, DEFAULT_CHARSET);
104+
}
101105
try {
102106
return new String(this.responseBody, this.responseCharset);
103107
}

0 commit comments

Comments
 (0)