FileDocCategorySizeDatePackage
ContentType.javaAPI DocApache Tomcat 6.0.143352Fri Jul 20 04:20:36 BST 2007org.apache.tomcat.util.http

ContentType

public class ContentType extends Object
Usefull methods for Content-Type processing
author
James Duncan Davidson [duncan@eng.sun.com]
author
James Todd [gonzo@eng.sun.com]
author
Jason Hunter [jch@eng.sun.com]
author
Harish Prabandham
author
costin@eng.sun.com

Fields Summary
Constructors Summary
Methods Summary
public static java.lang.StringgetCharsetFromContentType(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.

param
contentType a content type header


        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 booleanhasCharset(java.lang.String type)
Returns true if the given content type contains a charset component, false otherwise.

param
type Content type
return
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;