Skip to content

Commit dccc781

Browse files
committed
Expose defaultCharset in StringDecoder
Closes gh-25762
1 parent dde79a9 commit dccc781

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

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

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ public final class StringDecoder extends AbstractDataBufferDecoder<String> {
7272

7373
private final boolean stripDelimiter;
7474

75+
private Charset defaultCharset = DEFAULT_CHARSET;
76+
7577
private final ConcurrentMap<Charset, byte[][]> delimitersCache = new ConcurrentHashMap<>();
7678

7779

@@ -83,6 +85,24 @@ private StringDecoder(List<String> delimiters, boolean stripDelimiter, MimeType.
8385
}
8486

8587

88+
/**
89+
* Set the default character set to fall back on if the MimeType does not specify any.
90+
* <p>By default this is {@code UTF-8}.
91+
* @param defaultCharset the charset to fall back on
92+
* @since 5.2.9
93+
*/
94+
public void setDefaultCharset(Charset defaultCharset) {
95+
this.defaultCharset = defaultCharset;
96+
}
97+
98+
/**
99+
* Return the configured {@link #setDefaultCharset(Charset) defaultCharset}.
100+
* @since 5.2.9
101+
*/
102+
public Charset getDefaultCharset() {
103+
return this.defaultCharset;
104+
}
105+
86106
@Override
87107
public boolean canDecode(ResolvableType elementType, @Nullable MimeType mimeType) {
88108
return (elementType.resolve() == String.class && super.canDecode(elementType, mimeType));
@@ -136,12 +156,12 @@ public String decode(DataBuffer dataBuffer, ResolvableType elementType,
136156
return value;
137157
}
138158

139-
private static Charset getCharset(@Nullable MimeType mimeType) {
159+
private Charset getCharset(@Nullable MimeType mimeType) {
140160
if (mimeType != null && mimeType.getCharset() != null) {
141161
return mimeType.getCharset();
142162
}
143163
else {
144-
return DEFAULT_CHARSET;
164+
return getDefaultCharset();
145165
}
146166
}
147167

0 commit comments

Comments
 (0)