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
/**
@@ -197,7 +202,7 @@ public MimeType(String type, String subtype, @Nullable Map<String, String> param
197
202
* @see <a href="https://tools.ietf.org/html/rfc2616#section-2.2">HTTP 1.1, section 2.2</a>
198
203
*/
199
204
private void checkToken (String token ) {
200
- for (int i = 0 ; i < token .length (); i ++ ) {
205
+ for (int i = 0 ; i < token .length (); i ++) {
201
206
char ch = token .charAt (i );
202
207
if (!TOKEN .get (ch )) {
203
208
throw new IllegalArgumentException ("Invalid token character '" + ch + "' in token \" " + token + "\" " );
@@ -210,8 +215,9 @@ protected void checkParameters(String attribute, String value) {
210
215
Assert .hasLength (value , "'value' must not be empty" );
211
216
checkToken (attribute );
212
217
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
+ }
215
221
}
216
222
else if (!isQuotedString (value )) {
217
223
checkToken (value );
@@ -279,8 +285,7 @@ public String getSubtype() {
279
285
*/
280
286
@ Nullable
281
287
public Charset getCharset () {
282
- String charset = getParameter (PARAM_CHARSET );
283
- return (charset != null ? Charset .forName (unquote (charset )) : null );
288
+ return this .resolvedCharset ;
284
289
}
285
290
286
291
/**
0 commit comments