ContentTypepublic class ContentType extends Object Usefull methods for Content-Type processing |
Methods Summary |
---|
public static java.lang.String | getCharsetFromContentType(java.lang.String contentType)Parse the character encoding from the specified content type header.
If the content type is null, or there is no explicit character encoding,
null is returned.
if (contentType == null)
return (null);
int start = contentType.indexOf("charset=");
if (start < 0)
return (null);
String encoding = contentType.substring(start + 8);
int end = encoding.indexOf(';");
if (end >= 0)
encoding = encoding.substring(0, end);
encoding = encoding.trim();
if ((encoding.length() > 2) && (encoding.startsWith("\""))
&& (encoding.endsWith("\"")))
encoding = encoding.substring(1, encoding.length() - 1);
return (encoding.trim());
| public static boolean | hasCharset(java.lang.String type)Returns true if the given content type contains a charset component,
false otherwise.
boolean hasCharset = false;
int len = type.length();
int index = type.indexOf(';");
while (index != -1) {
index++;
while (index < len && Character.isSpace(type.charAt(index))) {
index++;
}
if (index+8 < len
&& type.charAt(index) == 'c"
&& type.charAt(index+1) == 'h"
&& type.charAt(index+2) == 'a"
&& type.charAt(index+3) == 'r"
&& type.charAt(index+4) == 's"
&& type.charAt(index+5) == 'e"
&& type.charAt(index+6) == 't"
&& type.charAt(index+7) == '=") {
hasCharset = true;
break;
}
index = type.indexOf(';", index);
}
return hasCharset;
|
|