Java Utililty Methods Charset Create

List of utility methods to do Charset Create

Description

The list of methods to do Charset Create are organized into topic(s).

Method

CharsetgetCharset(String enc)
Returns the Java Charset that should be used for the provided encoding
Charset charset = encodingToCharsetCache.get(enc);
if (charset == null) {
    charset = Charset.forName(enc);
    encodingToCharsetCache.put(enc, charset);
return charset;
CharsetgetCharset(String encoding)
get Charset
return ((null == encoding || "".equals(encoding.trim())) ? Charset.forName(DEF_ENCODING)
        : Charset.forName(encoding));
CharsetgetCharset(String encoding)
get Charset
Charset charset = (Charset) charsets.get(encoding);
if (charset == null) {
    charset = Charset.forName(encoding);
    charsets.put(encoding, charset);
return charset;
CharsetgetCharset(String name)
Gets a charset, throwing the java.io exception and not the java.nio exception if an error occurs.
try {
    return Charset.forName(name);
} catch (IllegalCharsetNameException e) {
    throw new UnsupportedEncodingException("Charset " + name + " not found.");
} catch (UnsupportedCharsetException e) {
    throw new UnsupportedEncodingException("Charset " + name + " not found.");
CharsetgetCharsetForSortOrder(final int sortOrder)
Retrieves the CharsetInfo instance asociated with the specified sort order.
String name = sortMap[sortOrder];
return Charset.forName(name);
StringgetCharsetFromContent(URL url)
get Charset From Content
InputStream stream = url.openStream();
byte chunk[] = new byte[2048];
int bytesRead = stream.read(chunk);
if (bytesRead > 0) {
    String startContent = new String(chunk);
    String pattern = "\\<meta\\s*http-equiv=[\\\"\\']content-type[\\\"\\']\\s*content\\s*=\\s*[\"']text/html\\s*;\\s*charset=([a-z\\d\\-]*)[\\\"\\'\\>]";
    Matcher matcher = Pattern.compile(pattern, Pattern.CASE_INSENSITIVE).matcher(startContent);
    if (matcher.find()) {
...
StringgetCharsetFromContentType(String contentType)
Parse out a charset from a content type header.
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);
...
StringgetCharsetFromContentType(String contentType)
get Charset From Content Type
if (contentType == null) {
    return null;
Matcher m = charsetPattern.matcher(contentType);
if (m.find()) {
    String charset = m.group(1).trim();
    charset = charset.replace("charset=", "");
    if (charset.isEmpty())
...
StringgetCharsetFromContentTypeString(String contentType)
get Charset From Content Type String
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;
return null;
Charset[]getCharsetList(List availableCharsets, Charset actualCharset)
Returns the supplied List as an array, and guarantees that the specified actual Charset is contained.
if (actualCharset == null || availableCharsets.contains(actualCharset)) {
    return (Charset[]) availableCharsets.toArray(new Charset[availableCharsets.size()]);
} else {
    List<Charset> resultList = new ArrayList<Charset>();
    resultList.addAll(availableCharsets);
    resultList.add(actualCharset);
    return (Charset[]) resultList.toArray(new Charset[resultList.size()]);