Skip to content

Commit c044008

Browse files
committed
Avoid repeated Charset resolution in MimeType
Closes gh-25808
1 parent bf00db3 commit c044008

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 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.
@@ -103,6 +103,9 @@ public class MimeType implements Comparable<MimeType>, Serializable {
103103

104104
private final Map<String, String> parameters;
105105

106+
@Nullable
107+
private Charset resolvedCharset;
108+
106109
@Nullable
107110
private volatile String toStringValue;
108111

@@ -138,6 +141,7 @@ public MimeType(String type, String subtype) {
138141
*/
139142
public MimeType(String type, String subtype, Charset charset) {
140143
this(type, subtype, Collections.singletonMap(PARAM_CHARSET, charset.name()));
144+
this.resolvedCharset = charset;
141145
}
142146

143147
/**
@@ -150,6 +154,7 @@ public MimeType(String type, String subtype, Charset charset) {
150154
*/
151155
public MimeType(MimeType other, Charset charset) {
152156
this(other.getType(), other.getSubtype(), addCharsetParameter(charset, other.getParameters()));
157+
this.resolvedCharset = charset;
153158
}
154159

155160
/**
@@ -197,7 +202,7 @@ public MimeType(String type, String subtype, @Nullable Map<String, String> param
197202
* @see <a href="https://tools.ietf.org/html/rfc2616#section-2.2">HTTP 1.1, section 2.2</a>
198203
*/
199204
private void checkToken(String token) {
200-
for (int i = 0; i < token.length(); i++ ) {
205+
for (int i = 0; i < token.length(); i++) {
201206
char ch = token.charAt(i);
202207
if (!TOKEN.get(ch)) {
203208
throw new IllegalArgumentException("Invalid token character '" + ch + "' in token \"" + token + "\"");
@@ -210,8 +215,9 @@ protected void checkParameters(String attribute, String value) {
210215
Assert.hasLength(value, "'value' must not be empty");
211216
checkToken(attribute);
212217
if (PARAM_CHARSET.equals(attribute)) {
213-
value = unquote(value);
214-
Charset.forName(value);
218+
if (this.resolvedCharset == null) {
219+
this.resolvedCharset = Charset.forName(unquote(value));
220+
}
215221
}
216222
else if (!isQuotedString(value)) {
217223
checkToken(value);
@@ -279,8 +285,7 @@ public String getSubtype() {
279285
*/
280286
@Nullable
281287
public Charset getCharset() {
282-
String charset = getParameter(PARAM_CHARSET);
283-
return (charset != null ? Charset.forName(unquote(charset)) : null);
288+
return this.resolvedCharset;
284289
}
285290

286291
/**

0 commit comments

Comments
 (0)