MimeTypes { public static String getContentTypeWithoutCharset(String value) { int end = value.length(); int state = 0; int start = 0; boolean quote = false; int i = 0; StringBuilder builder = null; for (; i < end; i++) { char b = value.charAt(i); if ('"' == b) { quote = !quote; switch (state) { case 11: builder.append(b); break; case 10: break; case 9: builder = new StringBuilder(); builder.append(value, 0, start + 1); state = 10; break; default: start = i; state = 0; } continue; } if (quote) { if (builder != null && state != 10) builder.append(b); continue; } switch (state) { case 0: if (';' == b) state = 1; else if (' ' != b) start = i; break; case 1: if ('c' == b) state = 2; else if (' ' != b) state = 0; break; case 2: if ('h' == b) state = 3; else state = 0; break; case 3: if ('a' == b) state = 4; else state = 0; break; case 4: if ('r' == b) state = 5; else state = 0; break; case 5: if ('s' == b) state = 6; else state = 0; break; case 6: if ('e' == b) state = 7; else state = 0; break; case 7: if ('t' == b) state = 8; else state = 0; break; case 8: if ('=' == b) state = 9; else if (' ' != b) state = 0; break; case 9: if (' ' == b) break; builder = new StringBuilder(); builder.append(value, 0, start + 1); state = 10; break; case 10: if (';' == b) { builder.append(b); state = 11; } break; case 11: if (' ' != b) builder.append(b); } } if (builder == null) return value; return builder.toString(); } MimeTypes(); static String getDefaultMimeByExtension(String filename); static Set<String> getKnownMimeTypes(); static String getCharsetFromContentType(String value); static Map<String, String> getInferredEncodings(); static Map<String, String> getAssumedEncodings(); @Deprecated static String inferCharsetFromContentType(String contentType); static String getCharsetInferredFromContentType(String contentType); static String getCharsetAssumedFromContentType(String contentType); static String getContentTypeWithoutCharset(String value); static String getContentTypeMIMEType(String contentType); static List<String> getAcceptMIMETypes(String accept); static List<AcceptMIMEType> parseAcceptMIMETypes(String accept); synchronized Map<String, String> getMimeMap(); void setMimeMap(Map<String, String> mimeMap); String getMimeByExtension(String filename); void addMimeMapping(String extension, String type);  }
@Test void testContentTypeWithoutCharset() { assertEquals("foo/bar;some=else", MimeTypes.getContentTypeWithoutCharset("foo/bar;charset=abc;some=else")); assertEquals("foo/bar", MimeTypes.getContentTypeWithoutCharset("foo/bar;charset=abc")); assertEquals("foo/bar", MimeTypes.getContentTypeWithoutCharset("foo/bar ; charset = abc")); assertEquals("foo/bar;some=else", MimeTypes.getContentTypeWithoutCharset("foo/bar ; charset = abc ; some=else")); assertEquals("foo/bar;other=param;some=else", MimeTypes.getContentTypeWithoutCharset("foo/bar;other=param;charset=abc;some=else")); assertEquals("foo/bar;other=param", MimeTypes.getContentTypeWithoutCharset("foo/bar;other=param;charset=abc")); assertEquals("foo/bar ; other = param", MimeTypes.getContentTypeWithoutCharset("foo/bar ; other = param ; charset = abc")); assertEquals("foo/bar ; other = param;some=else", MimeTypes.getContentTypeWithoutCharset("foo/bar ; other = param ; charset = abc ; some=else")); assertEquals("foo/bar ; other = param", MimeTypes.getContentTypeWithoutCharset("foo/bar ; other = param ; charset = abc")); assertEquals("foo/bar ; other = param;some=else", MimeTypes.getContentTypeWithoutCharset("foo/bar ; other = param ; charset = \"abc\" ; some=else")); assertEquals("foo/bar", MimeTypes.getContentTypeWithoutCharset("foo/bar")); assertEquals("foo/bar", MimeTypes.getContentTypeWithoutCharset("foo/bar;charset=uTf8")); assertEquals("foo/bar;other=\"charset=abc\"", MimeTypes.getContentTypeWithoutCharset("foo/bar;other=\"charset=abc\";charset=uTf8")); assertEquals("text/html", MimeTypes.getContentTypeWithoutCharset("text/html;charset=utf-8")); }