Skip to content

Commit 99be15f

Browse files
committed
Revise encoding steps towards use of JDK Charset and StandardCharsets
Issue: SPR-14492
1 parent 79d30d8 commit 99be15f

File tree

95 files changed

+480
-569
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

95 files changed

+480
-569
lines changed

spring-beans/src/main/java/org/springframework/beans/propertyeditors/PropertiesEditor.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2016 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.
@@ -19,6 +19,7 @@
1919
import java.beans.PropertyEditorSupport;
2020
import java.io.ByteArrayInputStream;
2121
import java.io.IOException;
22+
import java.nio.charset.StandardCharsets;
2223
import java.util.Map;
2324
import java.util.Properties;
2425

@@ -49,7 +50,7 @@ public void setAsText(String text) throws IllegalArgumentException {
4950
if (text != null) {
5051
try {
5152
// Must use the ISO-8859-1 encoding because Properties.load(stream) expects it.
52-
props.load(new ByteArrayInputStream(text.getBytes("ISO-8859-1")));
53+
props.load(new ByteArrayInputStream(text.getBytes(StandardCharsets.ISO_8859_1)));
5354
}
5455
catch (IOException ex) {
5556
// Should never happen.

spring-core/src/main/java/org/springframework/core/convert/support/StringToPropertiesConverter.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2016 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.
@@ -17,6 +17,7 @@
1717
package org.springframework.core.convert.support;
1818

1919
import java.io.ByteArrayInputStream;
20+
import java.nio.charset.StandardCharsets;
2021
import java.util.Properties;
2122

2223
import org.springframework.core.convert.converter.Converter;
@@ -35,7 +36,7 @@ public Properties convert(String source) {
3536
try {
3637
Properties props = new Properties();
3738
// Must use the ISO-8859-1 encoding because Properties.load(stream) expects it.
38-
props.load(new ByteArrayInputStream(source.getBytes("ISO-8859-1")));
39+
props.load(new ByteArrayInputStream(source.getBytes(StandardCharsets.ISO_8859_1)));
3940
return props;
4041
}
4142
catch (Exception ex) {

spring-core/src/main/java/org/springframework/util/Base64Utils.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.springframework.util;
1818

1919
import java.nio.charset.Charset;
20+
import java.nio.charset.StandardCharsets;
2021
import java.util.Base64;
2122

2223
/**
@@ -31,7 +32,7 @@
3132
*/
3233
public abstract class Base64Utils {
3334

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

3637

3738
/**

spring-core/src/test/java/org/springframework/core/convert/support/DefaultConversionServiceTests.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.math.BigDecimal;
2222
import java.math.BigInteger;
2323
import java.nio.charset.Charset;
24+
import java.nio.charset.StandardCharsets;
2425
import java.time.ZoneId;
2526
import java.util.AbstractList;
2627
import java.util.ArrayList;
@@ -277,12 +278,12 @@ public void testStringToLocale() {
277278

278279
@Test
279280
public void testStringToCharset() {
280-
assertEquals(Charset.forName("UTF-8"), conversionService.convert("UTF-8", Charset.class));
281+
assertEquals(StandardCharsets.UTF_8, conversionService.convert("UTF-8", Charset.class));
281282
}
282283

283284
@Test
284285
public void testCharsetToString() {
285-
assertEquals("UTF-8", conversionService.convert(Charset.forName("UTF-8"), String.class));
286+
assertEquals("UTF-8", conversionService.convert(StandardCharsets.UTF_8, String.class));
286287
}
287288

288289
@Test

spring-core/src/test/java/org/springframework/util/MimeTypeTests.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.springframework.util;
1818

1919
import java.nio.charset.Charset;
20+
import java.nio.charset.StandardCharsets;
2021
import java.util.ArrayList;
2122
import java.util.Collections;
2223
import java.util.List;
@@ -27,7 +28,7 @@
2728
import org.springframework.core.convert.ConversionService;
2829
import org.springframework.core.convert.support.DefaultConversionService;
2930

30-
import static java.util.Collections.singletonMap;
31+
import static java.util.Collections.*;
3132
import static org.junit.Assert.*;
3233

3334
/**
@@ -84,7 +85,7 @@ public void parseQuotedCharset() {
8485
MimeType mimeType = MimeType.valueOf(s);
8586
assertEquals("Invalid type", "application", mimeType.getType());
8687
assertEquals("Invalid subtype", "xml", mimeType.getSubtype());
87-
assertEquals("Invalid charset", Charset.forName("UTF-8"), mimeType.getCharset());
88+
assertEquals("Invalid charset", StandardCharsets.UTF_8, mimeType.getCharset());
8889
}
8990

9091
@Test

spring-expression/src/test/java/org/springframework/expression/spel/SpelReproTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import java.lang.reflect.Array;
2020
import java.lang.reflect.Field;
2121
import java.lang.reflect.Method;
22-
import java.nio.charset.Charset;
22+
import java.nio.charset.StandardCharsets;
2323
import java.util.ArrayList;
2424
import java.util.Arrays;
2525
import java.util.Collection;
@@ -2091,7 +2091,7 @@ public void SPR13918() {
20912091

20922092
Expression ex = parser.parseExpression("T(java.nio.charset.Charset).forName(#encoding)");
20932093
Object result = ex.getValue(context);
2094-
assertEquals(Charset.forName("UTF-8"), result);
2094+
assertEquals(StandardCharsets.UTF_8, result);
20952095
}
20962096

20972097

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2016 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.
@@ -25,6 +25,7 @@
2525
import java.io.StringReader;
2626
import java.io.UnsupportedEncodingException;
2727
import java.io.Writer;
28+
import java.nio.charset.StandardCharsets;
2829
import java.sql.Clob;
2930
import java.sql.SQLException;
3031

@@ -91,11 +92,11 @@ else if (this.characterStream != null) {
9192
public InputStream getAsciiStream() throws SQLException {
9293
try {
9394
if (this.content != null) {
94-
return new ByteArrayInputStream(this.content.getBytes("US-ASCII"));
95+
return new ByteArrayInputStream(this.content.getBytes(StandardCharsets.US_ASCII));
9596
}
9697
else if (this.characterStream != null) {
9798
String tempContent = FileCopyUtils.copyToString(this.characterStream);
98-
return new ByteArrayInputStream(tempContent.getBytes("US-ASCII"));
99+
return new ByteArrayInputStream(tempContent.getBytes(StandardCharsets.US_ASCII));
99100
}
100101
else {
101102
return this.asciiStream;

spring-messaging/src/main/java/org/springframework/messaging/converter/MappingJackson2MessageConverter.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.io.Writer;
2323
import java.lang.reflect.Type;
2424
import java.nio.charset.Charset;
25+
import java.nio.charset.StandardCharsets;
2526
import java.util.Arrays;
2627
import java.util.concurrent.atomic.AtomicReference;
2728

@@ -70,7 +71,7 @@ public class MappingJackson2MessageConverter extends AbstractMessageConverter {
7071
* the {@code application/json} MIME type with {@code UTF-8} character set.
7172
*/
7273
public MappingJackson2MessageConverter() {
73-
super(new MimeType("application", "json", Charset.forName("UTF-8")));
74+
super(new MimeType("application", "json", StandardCharsets.UTF_8));
7475
initObjectMapper();
7576
}
7677

spring-messaging/src/main/java/org/springframework/messaging/converter/StringMessageConverter.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.springframework.messaging.converter;
1818

1919
import java.nio.charset.Charset;
20+
import java.nio.charset.StandardCharsets;
2021

2122
import org.springframework.messaging.Message;
2223
import org.springframework.messaging.MessageHeaders;
@@ -35,7 +36,7 @@ public class StringMessageConverter extends AbstractMessageConverter {
3536

3637

3738
public StringMessageConverter() {
38-
this(Charset.forName("UTF-8"));
39+
this(StandardCharsets.UTF_8);
3940
}
4041

4142
public StringMessageConverter(Charset defaultCharset) {

spring-messaging/src/main/java/org/springframework/messaging/simp/stomp/StompDecoder.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.io.ByteArrayOutputStream;
2020
import java.nio.ByteBuffer;
2121
import java.nio.charset.Charset;
22+
import java.nio.charset.StandardCharsets;
2223
import java.util.ArrayList;
2324
import java.util.List;
2425

@@ -47,13 +48,10 @@
4748
*/
4849
public class StompDecoder {
4950

50-
static final Charset UTF8_CHARSET = Charset.forName("UTF-8");
51-
5251
static final byte[] HEARTBEAT_PAYLOAD = new byte[] {'\n'};
5352

5453
private static final Log logger = LogFactory.getLog(StompDecoder.class);
5554

56-
5755
private MessageHeaderInitializer headerInitializer;
5856

5957

@@ -202,7 +200,7 @@ private String readCommand(ByteBuffer buffer) {
202200
while (buffer.remaining() > 0 && !tryConsumeEndOfLine(buffer)) {
203201
command.write(buffer.get());
204202
}
205-
return new String(command.toByteArray(), UTF8_CHARSET);
203+
return new String(command.toByteArray(), StandardCharsets.UTF_8);
206204
}
207205

208206
private void readHeaders(ByteBuffer buffer, StompHeaderAccessor headerAccessor) {
@@ -217,7 +215,7 @@ private void readHeaders(ByteBuffer buffer, StompHeaderAccessor headerAccessor)
217215
headerStream.write(buffer.get());
218216
}
219217
if (headerStream.size() > 0 && headerComplete) {
220-
String header = new String(headerStream.toByteArray(), UTF8_CHARSET);
218+
String header = new String(headerStream.toByteArray(), StandardCharsets.UTF_8);
221219
int colonIndex = header.indexOf(':');
222220
if (colonIndex <= 0) {
223221
if (buffer.remaining() > 0) {

spring-messaging/src/main/java/org/springframework/messaging/simp/stomp/StompEncoder.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.io.ByteArrayOutputStream;
2020
import java.io.DataOutputStream;
2121
import java.io.IOException;
22+
import java.nio.charset.StandardCharsets;
2223
import java.util.Arrays;
2324
import java.util.List;
2425
import java.util.Map;
@@ -81,7 +82,7 @@ public byte[] encode(Map<String, Object> headers, byte[] payload) {
8182
else {
8283
StompCommand command = StompHeaderAccessor.getCommand(headers);
8384
Assert.notNull(command, "Missing STOMP command: " + headers);
84-
output.write(command.toString().getBytes(StompDecoder.UTF8_CHARSET));
85+
output.write(command.toString().getBytes(StandardCharsets.UTF_8));
8586
output.write(LF);
8687
writeHeaders(command, headers, payload, output);
8788
output.write(LF);
@@ -132,15 +133,15 @@ private void writeHeaders(StompCommand command, Map<String, Object> headers, byt
132133
}
133134
if (command.requiresContentLength()) {
134135
int contentLength = payload.length;
135-
output.write("content-length:".getBytes(StompDecoder.UTF8_CHARSET));
136-
output.write(Integer.toString(contentLength).getBytes(StompDecoder.UTF8_CHARSET));
136+
output.write("content-length:".getBytes(StandardCharsets.UTF_8));
137+
output.write(Integer.toString(contentLength).getBytes(StandardCharsets.UTF_8));
137138
output.write(LF);
138139
}
139140
}
140141

141142
private byte[] encodeHeaderString(String input, boolean escape) {
142143
String inputToUse = (escape ? escape(input) : input);
143-
return inputToUse.getBytes(StompDecoder.UTF8_CHARSET);
144+
return inputToUse.getBytes(StandardCharsets.UTF_8);
144145
}
145146

146147
/**

spring-messaging/src/main/java/org/springframework/messaging/simp/stomp/StompHeaderAccessor.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.springframework.messaging.simp.stomp;
1818

1919
import java.nio.charset.Charset;
20+
import java.nio.charset.StandardCharsets;
2021
import java.util.Arrays;
2122
import java.util.Collections;
2223
import java.util.List;
@@ -441,7 +442,7 @@ private String appendPayload(Object payload) {
441442
return contentType;
442443
}
443444
Charset charset = getContentType().getCharset();
444-
charset = (charset != null ? charset : StompDecoder.UTF8_CHARSET);
445+
charset = (charset != null ? charset : StandardCharsets.UTF_8);
445446
return (bytes.length < 80) ?
446447
contentType + " payload=" + new String(bytes, charset) :
447448
contentType + " payload=" + new String(Arrays.copyOf(bytes, 80), charset) + "...(truncated)";

spring-messaging/src/main/java/org/springframework/messaging/support/MessageHeaderAccessor.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.springframework.messaging.support;
1818

1919
import java.nio.charset.Charset;
20+
import java.nio.charset.StandardCharsets;
2021
import java.util.ArrayList;
2122
import java.util.Arrays;
2223
import java.util.HashMap;
@@ -115,7 +116,7 @@
115116
*/
116117
public class MessageHeaderAccessor {
117118

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

120121
private static final MimeType[] READABLE_MIME_TYPES = new MimeType[] {
121122
MimeTypeUtils.APPLICATION_JSON, MimeTypeUtils.APPLICATION_XML,

0 commit comments

Comments
 (0)