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

CharsetEncodergetEncoder()
get Encoder
final CharsetEncoder encoder;
if (Charset.isSupported(ENCODING)) {
    encoder = Charset.forName(ENCODING).newEncoder();
} else {
    encoder = Charset.defaultCharset().newEncoder();
return encoder;
CharsetEncodergetEncoder(String encoding)
get Encoder
return getCharset(encoding).newEncoder();
CharsetgetEncoding(byte[] htmlData)
get Encoding
Charset charset = null;
String charsetName = getEncodingName(htmlData);
if (charsetName != null) {
    try {
        charset = Charset.forName(charsetName);
    } catch (Exception e) {
        System.err.println("Charset encoding " + charsetName + " not found");
        charset = null;
...
StringgetEncoding(OutputStreamWriter inWriter)
Get the default system encoding using a writer
String encoding = inWriter.getEncoding();
try {
    encoding = Charset.forName(encoding).name();
} catch (Exception e) {
return encoding;
CharsetgetEncoding(String text)
get Encoding
Pattern xml = Pattern.compile("^\\s*<\\?xml\\s+[^>]*\\s*encoding\\s*=\\s*(\"[^\"]*\")",
        Pattern.DOTALL | Pattern.MULTILINE);
Matcher xmlMatcher = xml.matcher(text);
if (xmlMatcher.find())
    return getEncoding(xmlMatcher);
Pattern html = Pattern.compile(
        "^\\s*<(html|HTML).*<(meta|META)\\s+[^>]*\\s*(charset|CHARSET)\\s*=\\s*(\"[^\"]*\"|[^\"\\s]*)",
        Pattern.DOTALL | Pattern.MULTILINE);
...
StringgetEncodingOfXml(File file)
Try to find the encoding of a xml file.
byte[] bs = readFile(file, 150);
String encoding = "utf-8";
boolean findEncoding = false;
Map chars = Charset.availableCharsets();
Set keys = chars.keySet();
Iterator iterator = keys.iterator();
Pattern pattern = Pattern.compile("encoding=\"([^\"]*?)\"");
while (iterator.hasNext()) {
...
CharsetgetEncodingOption(List options)
Extract the encoding flag from the list of javac options.
int i = options.lastIndexOf("-encoding");
return (i >= 0) ? Charset.forName(options.get(i + 1)) : null;
String[]getEncodings()
get Encodings
if (encodings == null) {
    SortedMap<String, Charset> m = Charset.availableCharsets();
    Set<String> keySet = m.keySet();
    encodings = keySet.toArray(new String[keySet.size()]);
return encodings;
String[]getEncodings()
Returns a list of supported character encodings.
List returnValue = new ArrayList();
Map map = Charset.availableCharsets();
Iterator iter = map.keySet().iterator();
returnValue.add(UTF_8_Y);
while (iter.hasNext())
    returnValue.add(iter.next());
return (String[]) returnValue.toArray(new String[returnValue.size()]);