Parse out a charset from a content type header. - Java Network

Java examples for Network:Http

Description

Parse out a charset from a content type header.

Demo Code


//package com.java2s;

import java.nio.charset.Charset;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    private static final Pattern charsetPattern = Pattern
            .compile("(?i)\\bcharset=\\s*\"?([^\\s;\"/]*)/?>");

    /**//from  www .j  a va2  s. com
     * Parse out a charset from a content type header. If the charset is not supported, returns null (so the default
     * will kick in.)
     *
     * @param contentType e.g. "text/html; charset=EUC-JP"
     * @return "EUC-JP", or null if not found. Charset is trimmed and uppercased.
     */
    public static String getCharsetFromContentType(String contentType) {
        if (contentType == null)
            return null;
        Matcher m = charsetPattern.matcher(contentType);
        if (m.find()) {
            String charset = m.group(1).trim();
            if (Charset.isSupported(charset))
                return charset;
            charset = charset.toUpperCase(Locale.ENGLISH);
            if (Charset.isSupported(charset))
                return charset;
        }
        return null;
    }
}

Related Tutorials