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 */
017package org.apache.commons.codec;
018
019import java.nio.charset.Charset;
020
021/**
022 * Charsets required of every implementation of the Java platform.
023 *
024 * From the Java documentation <a href="http://docs.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html">Standard
025 * charsets</a>:
026 * <p>
027 * <cite>Every implementation of the Java platform is required to support the following character encodings. Consult the
028 * release documentation for your implementation to see if any other encodings are supported. Consult the release
029 * documentation for your implementation to see if any other encodings are supported. </cite>
030 * </p>
031 *
032 * <ul>
033 * <li><code>US-ASCII</code><br>
034 * Seven-bit ASCII, a.k.a. ISO646-US, a.k.a. the Basic Latin block of the Unicode character set.</li>
035 * <li><code>ISO-8859-1</code><br>
036 * ISO Latin Alphabet No. 1, a.k.a. ISO-LATIN-1.</li>
037 * <li><code>UTF-8</code><br>
038 * Eight-bit Unicode Transformation Format.</li>
039 * <li><code>UTF-16BE</code><br>
040 * Sixteen-bit Unicode Transformation Format, big-endian byte order.</li>
041 * <li><code>UTF-16LE</code><br>
042 * Sixteen-bit Unicode Transformation Format, little-endian byte order.</li>
043 * <li><code>UTF-16</code><br>
044 * Sixteen-bit Unicode Transformation Format, byte order specified by a mandatory initial byte-order mark (either order
045 * accepted on input, big-endian used on output.)</li>
046 * </ul>
047 *
048 * This perhaps would best belong in the Commons Lang project. Even if a similar class is defined in Commons Lang, it is
049 * not foreseen that Commons Codec would be made to depend on Commons Lang.
050 *
051 * <p>
052 * This class is immutable and thread-safe.
053 * </p>
054 *
055 * @see <a href="http://docs.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
056 * @since 1.7
057 * @version $Id: Charsets.html 928559 2014-11-10 02:53:54Z ggregory $
058 */
059public class Charsets {
060
061    //
062    // This class should only contain Charset instances for required encodings. This guarantees that it will load
063    // correctly and without delay on all Java platforms.
064    //
065
066    /**
067     * Returns the given Charset or the default Charset if the given Charset is null.
068     *
069     * @param charset
070     *            A charset or null.
071     * @return the given Charset or the default Charset if the given Charset is null
072     */
073    public static Charset toCharset(final Charset charset) {
074        return charset == null ? Charset.defaultCharset() : charset;
075    }
076
077    /**
078     * Returns a Charset for the named charset. If the name is null, return the default Charset.
079     *
080     * @param charset
081     *            The name of the requested charset, may be null.
082     * @return a Charset for the named charset
083     * @throws java.nio.charset.UnsupportedCharsetException
084     *             If the named charset is unavailable
085     */
086    public static Charset toCharset(final String charset) {
087        return charset == null ? Charset.defaultCharset() : Charset.forName(charset);
088    }
089
090    /**
091     * CharEncodingISO Latin Alphabet No. 1, a.k.a. ISO-LATIN-1.
092     * <p>
093     * Every implementation of the Java platform is required to support this character encoding.
094     *
095     * @see <a href="http://docs.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
096     * @deprecated Use Java 7's {@link java.nio.charset.StandardCharsets}
097     */
098    @Deprecated
099    public static final Charset ISO_8859_1 = Charset.forName(CharEncoding.ISO_8859_1);
100
101    /**
102     * Seven-bit ASCII, also known as ISO646-US, also known as the Basic Latin block of the Unicode character set.
103     * <p>
104     * Every implementation of the Java platform is required to support this character encoding.
105     *
106     * @see <a href="http://docs.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
107     * @deprecated Use Java 7's {@link java.nio.charset.StandardCharsets}
108     */
109    @Deprecated
110    public static final Charset US_ASCII = Charset.forName(CharEncoding.US_ASCII);
111
112    /**
113     * Sixteen-bit Unicode Transformation Format, The byte order specified by a mandatory initial byte-order mark
114     * (either order accepted on input, big-endian used on output)
115     * <p>
116     * Every implementation of the Java platform is required to support this character encoding.
117     *
118     * @see <a href="http://docs.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
119     * @deprecated Use Java 7's {@link java.nio.charset.StandardCharsets}
120     */
121    @Deprecated
122    public static final Charset UTF_16 = Charset.forName(CharEncoding.UTF_16);
123
124    /**
125     * Sixteen-bit Unicode Transformation Format, big-endian byte order.
126     * <p>
127     * Every implementation of the Java platform is required to support this character encoding.
128     *
129     * @see <a href="http://docs.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
130     * @deprecated Use Java 7's {@link java.nio.charset.StandardCharsets}
131     */
132    @Deprecated
133    public static final Charset UTF_16BE = Charset.forName(CharEncoding.UTF_16BE);
134
135    /**
136     * Sixteen-bit Unicode Transformation Format, little-endian byte order.
137     * <p>
138     * Every implementation of the Java platform is required to support this character encoding.
139     *
140     * @see <a href="http://docs.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
141     * @deprecated Use Java 7's {@link java.nio.charset.StandardCharsets}
142     */
143    @Deprecated
144    public static final Charset UTF_16LE = Charset.forName(CharEncoding.UTF_16LE);
145
146    /**
147     * Eight-bit Unicode Transformation Format.
148     * <p>
149     * Every implementation of the Java platform is required to support this character encoding.
150     *
151     * @see <a href="http://docs.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
152     * @deprecated Use Java 7's {@link java.nio.charset.StandardCharsets}
153     */
154    @Deprecated
155    public static final Charset UTF_8 = Charset.forName(CharEncoding.UTF_8);
156}