Java Charset Create getCharsetFromContentTypeString(String contentType)

Here you can find the source of getCharsetFromContentTypeString(String contentType)

Description

get Charset From Content Type String

License

Open Source License

Declaration

public static String getCharsetFromContentTypeString(String contentType) 

Method Source Code

//package com.java2s;

import java.nio.charset.Charset;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    public static String getCharsetFromContentTypeString(String contentType) {
        if (contentType != null) {
            String pattern = "charset=([a-z\\d\\-]*)";
            Matcher matcher = Pattern.compile(pattern, Pattern.CASE_INSENSITIVE).matcher(contentType);
            if (matcher.find()) {
                String charset = matcher.group(1);
                if (Charset.isSupported(charset)) {
                    return charset;
                }/*  w  w  w .  jav  a2s  .  co  m*/
            }
        }

        return null;
    }
}

Related

  1. getCharset(String name)
  2. getCharsetForSortOrder(final int sortOrder)
  3. getCharsetFromContent(URL url)
  4. getCharsetFromContentType(String contentType)
  5. getCharsetFromContentType(String contentType)
  6. getCharsetList(List availableCharsets, Charset actualCharset)
  7. getCharsetName(String location)
  8. getCharsetOrDefault(String charsetName)
  9. getDefaultCharset()