001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package org.apache.commons.lang3;
019
020import java.nio.charset.Charset;
021import java.nio.charset.IllegalCharsetNameException;
022
023/**
024 * <p>Character encoding names required of every implementation of the Java platform.</p>
025 *
026 * <p>According to <a href="http://docs.oracle.com/javase/1.3/docs/api/java/lang/package-summary.html#charenc">JRE character
027 * encoding names</a>:</p>
028 *
029 * <p><cite>Every implementation of the Java platform is required to support the following character encodings.
030 * Consult the release documentation for your implementation to see if any other encodings are supported.
031 * </cite></p>
032 *
033 * @see <a href="http://docs.oracle.com/javase/6/docs/technotes/guides/intl/encoding.doc.html">JRE character encoding names</a>
034 * @since 2.1
035 */
036public class CharEncoding {
037
038    /**
039     * <p>ISO Latin Alphabet #1, also known as ISO-LATIN-1.</p>
040     *
041     * <p>Every implementation of the Java platform is required to support this character encoding.</p>
042     */
043    public static final String ISO_8859_1 = "ISO-8859-1";
044
045    /**
046     * <p>Seven-bit ASCII, also known as ISO646-US, also known as the Basic Latin block
047     * of the Unicode character set.</p>
048     *
049     * <p>Every implementation of the Java platform is required to support this character encoding.</p>
050     */
051    public static final String US_ASCII = "US-ASCII";
052
053    /**
054     * <p>Sixteen-bit Unicode Transformation Format, byte order specified by a mandatory initial
055     * byte-order mark (either order accepted on input, big-endian used on output).</p>
056     *
057     * <p>Every implementation of the Java platform is required to support this character encoding.</p>
058     */
059    public static final String UTF_16 = "UTF-16";
060
061    /**
062     * <p>Sixteen-bit Unicode Transformation Format, big-endian byte order.</p>
063     *
064     * <p>Every implementation of the Java platform is required to support this character encoding.</p>
065     */
066    public static final String UTF_16BE = "UTF-16BE";
067
068    /**
069     * <p>Sixteen-bit Unicode Transformation Format, little-endian byte order.</p>
070     *
071     * <p>Every implementation of the Java platform is required to support this character encoding.</p>
072     */
073    public static final String UTF_16LE = "UTF-16LE";
074
075    /**
076     * <p>Eight-bit Unicode Transformation Format.</p>
077     *
078     * <p>Every implementation of the Java platform is required to support this character encoding.</p>
079     */
080    public static final String UTF_8 = "UTF-8";
081
082    /**
083     * <p>Returns whether the named charset is supported.</p>
084     *
085     * <p>This is similar to <a
086     * href="http://docs.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html#isSupported%28java.lang.String%29">
087     * java.nio.charset.Charset.isSupported(String)</a> but handles more formats</p>
088     *
089     * @param name  the name of the requested charset; may be either a canonical name or an alias, null returns false
090     * @return {@code true} if the charset is available in the current Java virtual machine
091     */
092    public static boolean isSupported(final String name) {
093        if (name == null) {
094            return false;
095        }
096        try {
097            return Charset.isSupported(name);
098        } catch (final IllegalCharsetNameException ex) {
099            return false;
100        }
101    }
102
103}