Skip to content

Commit 90477b4

Browse files
philwebbsnicoll
authored andcommitted
Defer Charset.availableCharsets() call
Change the `StringHttpMessageConverter` to defer calling Charset.availableCharsets() until absolutely necessary to help improve startup times. Issue: SPR-15502
1 parent 2390748 commit 90477b4

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public class StringHttpMessageConverter extends AbstractHttpMessageConverter<Str
4343
public static final Charset DEFAULT_CHARSET = StandardCharsets.ISO_8859_1;
4444

4545

46-
private final List<Charset> availableCharsets;
46+
private volatile List<Charset> availableCharsets;
4747

4848
private boolean writeAcceptCharset = true;
4949

@@ -62,7 +62,6 @@ public StringHttpMessageConverter() {
6262
*/
6363
public StringHttpMessageConverter(Charset defaultCharset) {
6464
super(defaultCharset, MediaType.TEXT_PLAIN, MediaType.ALL);
65-
this.availableCharsets = new ArrayList<>(Charset.availableCharsets().values());
6665
}
6766

6867

@@ -109,6 +108,10 @@ protected void writeInternal(String str, HttpOutputMessage outputMessage) throws
109108
* @return the list of accepted charsets
110109
*/
111110
protected List<Charset> getAcceptedCharsets() {
111+
if (this.availableCharsets == null) {
112+
this.availableCharsets = new ArrayList<>(
113+
Charset.availableCharsets().values());
114+
}
112115
return this.availableCharsets;
113116
}
114117

0 commit comments

Comments
 (0)