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