@@ -49,6 +49,10 @@ public abstract class MimeTypeUtils {
49
49
50
50
private static Charset US_ASCII = Charset .forName ("US-ASCII" );
51
51
52
+ /**
53
+ * Comparator used by {@link #sortBySpecificity(List)}.
54
+ */
55
+ public static final Comparator <MimeType > SPECIFICITY_COMPARATOR = new SpecificityComparator <MimeType >();
52
56
53
57
/**
54
58
* Public constant mime type that includes all media ranges (i.e. "*/*").
@@ -219,12 +223,13 @@ public static MimeType parseMimeType(String mimeType) {
219
223
if (!StringUtils .hasLength (mimeType )) {
220
224
throw new InvalidMimeTypeException (mimeType , "'mimeType' must not be empty" );
221
225
}
222
- String [] parts = StringUtils .tokenizeToStringArray (mimeType , ";" );
223
- if (parts .length == 0 ) {
226
+
227
+ int index = mimeType .indexOf (';' );
228
+ String fullType = (index >= 0 ? mimeType .substring (0 , index ) : mimeType ).trim ();
229
+ if (fullType .length () == 0 ) {
224
230
throw new InvalidMimeTypeException (mimeType , "'mimeType' must not be empty" );
225
231
}
226
232
227
- String fullType = parts [0 ].trim ();
228
233
// java.net.HttpURLConnection returns a *; q=.2 Accept header
229
234
if (MimeType .WILDCARD_TYPE .equals (fullType )) {
230
235
fullType = "*/*" ;
@@ -243,18 +248,36 @@ public static MimeType parseMimeType(String mimeType) {
243
248
}
244
249
245
250
Map <String , String > parameters = null ;
246
- if (parts .length > 1 ) {
247
- parameters = new LinkedHashMap <String , String >(parts .length - 1 );
248
- for (int i = 1 ; i < parts .length ; i ++) {
249
- String parameter = parts [i ];
251
+ do {
252
+ int nextIndex = index + 1 ;
253
+ boolean quoted = false ;
254
+ while (nextIndex < mimeType .length ()) {
255
+ char ch = mimeType .charAt (nextIndex );
256
+ if (ch == ';' ) {
257
+ if (!quoted ) {
258
+ break ;
259
+ }
260
+ }
261
+ else if (ch == '"' ) {
262
+ quoted = !quoted ;
263
+ }
264
+ nextIndex ++;
265
+ }
266
+ String parameter = mimeType .substring (index + 1 , nextIndex ).trim ();
267
+ if (parameter .length () > 0 ) {
268
+ if (parameters == null ) {
269
+ parameters = new LinkedHashMap <String , String >(4 );
270
+ }
250
271
int eqIndex = parameter .indexOf ('=' );
251
- if (eqIndex != - 1 ) {
272
+ if (eqIndex >= 0 ) {
252
273
String attribute = parameter .substring (0 , eqIndex );
253
274
String value = parameter .substring (eqIndex + 1 , parameter .length ());
254
275
parameters .put (attribute , value );
255
276
}
256
277
}
278
+ index = nextIndex ;
257
279
}
280
+ while (index < mimeType .length ());
258
281
259
282
try {
260
283
return new MimeType (type , subtype , parameters );
@@ -353,11 +376,4 @@ public static String generateMultipartBoundaryString() {
353
376
return new String (generateMultipartBoundary (), US_ASCII );
354
377
}
355
378
356
-
357
-
358
- /**
359
- * Comparator used by {@link #sortBySpecificity(List)}.
360
- */
361
- public static final Comparator <MimeType > SPECIFICITY_COMPARATOR = new SpecificityComparator <MimeType >();
362
-
363
379
}
0 commit comments