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