privatestatic MimeType parseMimeTypeInternal(String mimeType){ int index = mimeType.indexOf(';'); String fullType = (index >= 0 ? mimeType.substring(0, index) : mimeType).trim(); if (fullType.isEmpty()) { thrownew InvalidMimeTypeException(mimeType, "'mimeType' must not be empty"); }
// java.net.HttpURLConnection returns a *; q=.2 Accept header if (MimeType.WILDCARD_TYPE.equals(fullType)) { fullType = "*/*"; } int subIndex = fullType.indexOf('/'); if (subIndex == -1) { thrownew InvalidMimeTypeException(mimeType, "does not contain '/'"); } if (subIndex == fullType.length() - 1) { thrownew InvalidMimeTypeException(mimeType, "does not contain subtype after '/'"); } String type = fullType.substring(0, subIndex); String subtype = fullType.substring(subIndex + 1); if (MimeType.WILDCARD_TYPE.equals(type) && !MimeType.WILDCARD_TYPE.equals(subtype)) { thrownew InvalidMimeTypeException(mimeType, "wildcard type is legal only in '*/*' (all mime types)"); }
Map<String, String> parameters = null; do { int nextIndex = index + 1; boolean quoted = false; while (nextIndex < mimeType.length()) { char ch = mimeType.charAt(nextIndex); if (ch == ';') { if (!quoted) { break; } } elseif (ch == '"') { quoted = !quoted; } nextIndex++; } String parameter = mimeType.substring(index + 1, nextIndex).trim(); if (parameter.length() > 0) { if (parameters == null) { parameters = new LinkedHashMap<>(4); } int eqIndex = parameter.indexOf('='); if (eqIndex >= 0) { String attribute = parameter.substring(0, eqIndex).trim(); String value = parameter.substring(eqIndex + 1).trim(); parameters.put(attribute, value); } } index = nextIndex; } while (index < mimeType.length());