Skip to content

Commit 9795883

Browse files
committed
Avoid repeated Charset resolution in MimeType
Closes gh-25808
1 parent 214bc40 commit 9795883

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

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

+15-11
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
/**
@@ -194,11 +199,13 @@ public MimeType(String type, String subtype, @Nullable Map<String, String> param
194199
* Copy-constructor that copies the type, subtype and parameters of the given {@code MimeType},
195200
* skipping checks performed in other constructors.
196201
* @param other the other MimeType
202+
* @since 5.3
197203
*/
198204
protected MimeType(MimeType other) {
199205
this.type = other.type;
200206
this.subtype = other.subtype;
201207
this.parameters = other.parameters;
208+
this.resolvedCharset = other.resolvedCharset;
202209
this.toStringValue = other.toStringValue;
203210
}
204211

@@ -222,8 +229,9 @@ protected void checkParameters(String attribute, String value) {
222229
Assert.hasLength(value, "'value' must not be empty");
223230
checkToken(attribute);
224231
if (PARAM_CHARSET.equals(attribute)) {
225-
value = unquote(value);
226-
Charset.forName(value);
232+
if (this.resolvedCharset == null) {
233+
this.resolvedCharset = Charset.forName(unquote(value));
234+
}
227235
}
228236
else if (!isQuotedString(value)) {
229237
checkToken(value);
@@ -304,8 +312,7 @@ public String getSubtypeSuffix() {
304312
*/
305313
@Nullable
306314
public Charset getCharset() {
307-
String charset = getParameter(PARAM_CHARSET);
308-
return (charset != null ? Charset.forName(unquote(charset)) : null);
315+
return this.resolvedCharset;
309316
}
310317

311318
/**
@@ -393,17 +400,14 @@ else if (getType().equals(other.getType())) {
393400
if (isWildcardSubtype() || other.isWildcardSubtype()) {
394401
String thisSuffix = getSubtypeSuffix();
395402
String otherSuffix = other.getSubtypeSuffix();
396-
if (getSubtype().equals(WILDCARD_TYPE)
397-
|| other.getSubtype().equals(WILDCARD_TYPE)) {
403+
if (getSubtype().equals(WILDCARD_TYPE) || other.getSubtype().equals(WILDCARD_TYPE)) {
398404
return true;
399405
}
400406
else if (isWildcardSubtype() && thisSuffix != null) {
401-
return thisSuffix.equals(other.getSubtype())
402-
|| thisSuffix.equals(otherSuffix);
407+
return (thisSuffix.equals(other.getSubtype()) || thisSuffix.equals(otherSuffix));
403408
}
404409
else if (other.isWildcardSubtype() && otherSuffix != null) {
405-
return this.getSubtype().equals(otherSuffix)
406-
|| otherSuffix.equals(thisSuffix);
410+
return (this.getSubtype().equals(otherSuffix) || otherSuffix.equals(thisSuffix));
407411
}
408412
}
409413
}

0 commit comments

Comments
 (0)