1
1
/*
2
- * Copyright 2002-2019 the original author or authors.
2
+ * Copyright 2002-2020 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
@@ -103,6 +103,9 @@ public class MimeType implements Comparable<MimeType>, Serializable {
103
103
104
104
private final Map <String , String > parameters ;
105
105
106
+ @ Nullable
107
+ private Charset resolvedCharset ;
108
+
106
109
@ Nullable
107
110
private volatile String toStringValue ;
108
111
@@ -138,6 +141,7 @@ public MimeType(String type, String subtype) {
138
141
*/
139
142
public MimeType (String type , String subtype , Charset charset ) {
140
143
this (type , subtype , Collections .singletonMap (PARAM_CHARSET , charset .name ()));
144
+ this .resolvedCharset = charset ;
141
145
}
142
146
143
147
/**
@@ -150,6 +154,7 @@ public MimeType(String type, String subtype, Charset charset) {
150
154
*/
151
155
public MimeType (MimeType other , Charset charset ) {
152
156
this (other .getType (), other .getSubtype (), addCharsetParameter (charset , other .getParameters ()));
157
+ this .resolvedCharset = charset ;
153
158
}
154
159
155
160
/**
@@ -194,11 +199,13 @@ public MimeType(String type, String subtype, @Nullable Map<String, String> param
194
199
* Copy-constructor that copies the type, subtype and parameters of the given {@code MimeType},
195
200
* skipping checks performed in other constructors.
196
201
* @param other the other MimeType
202
+ * @since 5.3
197
203
*/
198
204
protected MimeType (MimeType other ) {
199
205
this .type = other .type ;
200
206
this .subtype = other .subtype ;
201
207
this .parameters = other .parameters ;
208
+ this .resolvedCharset = other .resolvedCharset ;
202
209
this .toStringValue = other .toStringValue ;
203
210
}
204
211
@@ -222,8 +229,9 @@ protected void checkParameters(String attribute, String value) {
222
229
Assert .hasLength (value , "'value' must not be empty" );
223
230
checkToken (attribute );
224
231
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
+ }
227
235
}
228
236
else if (!isQuotedString (value )) {
229
237
checkToken (value );
@@ -304,8 +312,7 @@ public String getSubtypeSuffix() {
304
312
*/
305
313
@ Nullable
306
314
public Charset getCharset () {
307
- String charset = getParameter (PARAM_CHARSET );
308
- return (charset != null ? Charset .forName (unquote (charset )) : null );
315
+ return this .resolvedCharset ;
309
316
}
310
317
311
318
/**
@@ -393,17 +400,14 @@ else if (getType().equals(other.getType())) {
393
400
if (isWildcardSubtype () || other .isWildcardSubtype ()) {
394
401
String thisSuffix = getSubtypeSuffix ();
395
402
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 )) {
398
404
return true ;
399
405
}
400
406
else if (isWildcardSubtype () && thisSuffix != null ) {
401
- return thisSuffix .equals (other .getSubtype ())
402
- || thisSuffix .equals (otherSuffix );
407
+ return (thisSuffix .equals (other .getSubtype ()) || thisSuffix .equals (otherSuffix ));
403
408
}
404
409
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 ));
407
411
}
408
412
}
409
413
}
0 commit comments