From dccc78146a281208874b76e5bb69bb8bc3af608d Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Mon, 14 Sep 2020 14:28:02 +0100 Subject: [PATCH] Expose defaultCharset in StringDecoder Closes gh-25762 --- .../core/codec/StringDecoder.java | 24 +++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/core/codec/StringDecoder.java b/spring-core/src/main/java/org/springframework/core/codec/StringDecoder.java index 67ecfbe077fb..212d23900cad 100644 --- a/spring-core/src/main/java/org/springframework/core/codec/StringDecoder.java +++ b/spring-core/src/main/java/org/springframework/core/codec/StringDecoder.java @@ -72,6 +72,8 @@ public final class StringDecoder extends AbstractDataBufferDecoder { private final boolean stripDelimiter; + private Charset defaultCharset = DEFAULT_CHARSET; + private final ConcurrentMap delimitersCache = new ConcurrentHashMap<>(); @@ -83,6 +85,24 @@ private StringDecoder(List delimiters, boolean stripDelimiter, MimeType. } + /** + * Set the default character set to fall back on if the MimeType does not specify any. + *

By default this is {@code UTF-8}. + * @param defaultCharset the charset to fall back on + * @since 5.2.9 + */ + public void setDefaultCharset(Charset defaultCharset) { + this.defaultCharset = defaultCharset; + } + + /** + * Return the configured {@link #setDefaultCharset(Charset) defaultCharset}. + * @since 5.2.9 + */ + public Charset getDefaultCharset() { + return this.defaultCharset; + } + @Override public boolean canDecode(ResolvableType elementType, @Nullable MimeType mimeType) { return (elementType.resolve() == String.class && super.canDecode(elementType, mimeType)); @@ -136,12 +156,12 @@ public String decode(DataBuffer dataBuffer, ResolvableType elementType, return value; } - private static Charset getCharset(@Nullable MimeType mimeType) { + private Charset getCharset(@Nullable MimeType mimeType) { if (mimeType != null && mimeType.getCharset() != null) { return mimeType.getCharset(); } else { - return DEFAULT_CHARSET; + return getDefaultCharset(); } }