Skip to content

Commit

Permalink
Expose defaultCharset in StringDecoder
Browse files Browse the repository at this point in the history
Closes gh-25762
  • Loading branch information
rstoyanchev committed Sep 14, 2020
1 parent dde79a9 commit dccc781
Showing 1 changed file with 22 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ public final class StringDecoder extends AbstractDataBufferDecoder<String> {

private final boolean stripDelimiter;

private Charset defaultCharset = DEFAULT_CHARSET;

private final ConcurrentMap<Charset, byte[][]> delimitersCache = new ConcurrentHashMap<>();


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


/**
* Set the default character set to fall back on if the MimeType does not specify any.
* <p>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));
Expand Down Expand Up @@ -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();
}
}

Expand Down

0 comments on commit dccc781

Please sign in to comment.