public static Map<String, String> getCharsetHeaderCharsets(String header) { if (header == null) return null; int semicolonIndex = header.indexOf(";"); final String charsets; if (semicolonIndex == -1) charsets = header.trim(); else charsets = header.substring(0, semicolonIndex).trim(); final StringTokenizer st = new StringTokenizer(charsets, ","); final Map<String, String> charsetsMap = new HashMap<String, String>(); while (st.hasMoreTokens()) { charsetsMap.put(st.nextToken(), ""); } return charsetsMap; }
public static Map<String, String> getContentTypeParameters(String contentType) { if (contentType == null) return null; // Check whether there may be parameters final int semicolonIndex = contentType.indexOf(";"); if (semicolonIndex == -1) return null; // Tokenize final StringTokenizer st = new StringTokenizer(contentType, ";"); if (!st.hasMoreTokens()) return null; // should not happen as there should be at least the content type st.nextToken(); // No parameters if (!st.hasMoreTokens()) return null; // Parse parameters final Map<String, String> parameters = new HashMap<String, String>(); while (st.hasMoreTokens()) { final String parameter = st.nextToken().trim(); final int equalIndex = parameter.indexOf('='); if (equalIndex == -1) continue; final String name = parameter.substring(0, equalIndex).trim(); final String value = parameter.substring(equalIndex + 1).trim(); parameters.put(name, value); } return parameters; }