Example usage for java.nio.charset Charset isSupported

List of usage examples for java.nio.charset Charset isSupported

Introduction

In this page you can find the example usage for java.nio.charset Charset isSupported.

Prototype

public static boolean isSupported(String charsetName) 

Source Link

Document

Tells whether the named charset is supported.

Usage

From source file:com.alibaba.antx.util.CharsetUtil.java

public static String detectedSystemCharset() {
    String charset = Charset.defaultCharset().name();

    // unix?LANGcharset
    if (SystemUtils.IS_OS_UNIX) {
        String lang = System.getenv("LANG");
        int index = -1;

        if (!StringUtil.isBlank(lang) && (index = lang.indexOf(".")) >= 0) {
            String langCharset = lang.substring(index + 1);

            if (Charset.isSupported(langCharset)) {
                charset = langCharset;//w  w w  .  j  a va2 s.  c  om
            }
        }
    }

    return charset;
}

From source file:Main.java

/**
 * Safely return whether <charsetName> is supported, without throwing exceptions
 * /*from  w w w  .  j a  v  a2s.  c om*/
 * @param charsetName Name of charset (can be null)
 * @return true if the character set is supported
 */
public static boolean isSupported(String charsetName) {
    try {
        return Charset.isSupported(charsetName);
    } catch (IllegalCharsetNameException e) {
        return false;
    } catch (IllegalArgumentException e) {
        // null, for example
        return false;
    } catch (Exception e) {
        // Unexpected exception, what to do?
        return false;
    }
}

From source file:Main.java

/**
 * Safely return whether <charsetName> is supported, without throwing exceptions
 * /*from  www  . ja  va2  s. c o  m*/
 * @param charsetName Name of charset (can be null)
 * @return true if the character set is supported
 */
public static boolean isSupported(String charsetName) {
    try {
        if (isSupportedICU != null && ((Boolean) isSupportedICU.invoke(null, charsetName)).booleanValue()) {
            return true;
        }
        return Charset.isSupported(charsetName);
    } catch (IllegalCharsetNameException e) {
        return false;
    } catch (IllegalArgumentException e) {
        // null, for example
        return false;
    } catch (Exception e) {
        // Unexpected exception, what to do?
        return false;
    }
}

From source file:Main.java

public static Charset getCharsetFromHttpRequest(final HttpRequestBase request) {
    if (request == null)
        return null;
    String charsetName = null;/*from w  w  w . j  a va 2s.c o m*/
    Header header = request.getFirstHeader("Content-Type");
    if (header != null) {
        for (HeaderElement element : header.getElements()) {
            NameValuePair charsetPair = element.getParameterByName("charset");
            if (charsetPair != null) {
                charsetName = charsetPair.getValue();
                break;
            }
        }
    }

    boolean isSupportedCharset = false;
    if (!TextUtils.isEmpty(charsetName)) {
        try {
            isSupportedCharset = Charset.isSupported(charsetName);
        } catch (Throwable e) {
        }
    }

    return isSupportedCharset ? Charset.forName(charsetName) : null;
}

From source file:org.colombbus.tangara.io.ScriptReader.java

private static final void addCommonCharsetIfSupported(String charsetName) {
     if (Charset.isSupported(charsetName)) {
         Charset cs = Charset.forName(charsetName);
         if (COMMON_CHARSETS.contains(cs) == false) {
             COMMON_CHARSETS.add(cs);/*  w w  w. jav  a 2  s  . c  o m*/
         }
     }
 }

From source file:com.cloud.utils.StringUtils.java

public static boolean isUtf8Supported() {
    return Charset.isSupported(UTF8);
}

From source file:org.ngrinder.common.util.EncodingUtils.java

/**
 * Detect encoding of given data./*from   w w w . ja v a  2 s.  c o m*/
 * 
 * @param data
 *            byte array
 * @param defaultEncoding
 *            the default encoding if no encoding is sure.
 * @return encoding name detected encoding name
 * @throws IOException
 *             occurs when the detection is failed.
 */
public static String detectEncoding(byte[] data, String defaultEncoding) throws IOException {
    CharsetDetector detector = new CharsetDetector();
    detector.setText(data);
    CharsetMatch cm = detector.detect();
    String estimatedEncoding = cm.getName();
    boolean isReliable = Charset.isSupported(estimatedEncoding)
            && cm.getConfidence() >= MINIMAL_CONFIDENCE_LEVEL;
    return isReliable ? estimatedEncoding : defaultEncoding;
}

From source file:ar.com.zauber.commons.web.uri.factory.RelativePathUriFactory.java

/** Creates a new {@link RelativePathUriFactory} */
public RelativePathUriFactory(final UriFactory callback, final String encoding,
        final RequestProvider requestProvider) {
    Validate.notNull(callback);/*from ww w  .ja va 2s . com*/
    Validate.notNull(encoding);
    Validate.notNull(requestProvider);
    Validate.isTrue(Charset.isSupported(encoding), "Encoding no soportado");

    defaultEncoding = encoding;
    this.callback = callback;
    this.requestProvider = requestProvider;
}

From source file:org.structr.mail.DynamicFileDataSource.java

public DynamicFileDataSource(final File fileNode) {

    contentType = fileNode.getContentType();
    fileName = fileNode.getName();// w  w w. j av a2  s  .  co m

    if (contentType != null) {

        final String charset = StringUtils.substringAfterLast(contentType, "charset=").trim().toUpperCase();
        try {
            if (!"".equals(charset) && Charset.isSupported(charset)) {
                encoding = charset;
            }
        } catch (IllegalCharsetNameException ice) {
            logger.warn("Charset is not supported '{}'. Using 'UTF-8'", charset);
        }
    }

    try {
        fileContent = IOUtils.toString(fileNode.getInputStream(), encoding);
    } catch (IOException ex) {
        logger.warn("Unable to open input stream for {}: {}", fileName, ex.getMessage());
        fileContent = "";
    }
}

From source file:com.dongfang.utils.OtherUtils.java

public static String getCharsetFromHttpResponse(final HttpResponse response) {
    if (response == null)
        return null;
    String result = null;/*from ww  w . ja  v  a  2s  .c o  m*/
    Header header = response.getEntity().getContentType();
    if (header != null) {
        for (HeaderElement element : header.getElements()) {
            NameValuePair charsetPair = element.getParameterByName("charset");
            if (charsetPair != null) {
                result = charsetPair.getValue();
                break;
            }
        }
    }
    boolean isSupportedCharset = false;
    if (!TextUtils.isEmpty(result)) {
        try {
            isSupportedCharset = Charset.isSupported(result);
        } catch (Throwable e) {
        }
    }
    return isSupportedCharset ? result : null;
}