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