    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one
    003 * or more contributor license agreements.  See the NOTICE file
    004 * distributed with this work for additional information
    005 * regarding copyright ownership.  The ASF licenses this file
    006 * to you under the Apache License, Version 2.0 (the
    007 * "License"); you may not use this file except in compliance
    008 * with the License.  You may obtain a copy of the License at
    009 *
    010 *   https://www.apache.org/licenses/LICENSE-2.0
    011 *
    012 * Unless required by applicable law or agreed to in writing,
    013 * software distributed under the License is distributed on an
    014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    015 * KIND, either express or implied.  See the License for the
    016 * specific language governing permissions and limitations
    017 * under the License.
    018 */
    019
    020package org.apache.commons.compress.utils;
    021
    022import java.nio.charset.Charset;
    023import java.nio.charset.StandardCharsets;
    024
    025/**
    026 * Charsets required of every implementation of the Java platform.
    027 *
    028 * From the Java documentation <a href="https://docs.oracle.com/javase/8/docs/api/java/nio/charset/Charset.html">Standard charsets</a>:
    029 * <p>
    030 * <cite>Every implementation of the Java platform is required to support the following character encodings. Consult the release documentation for your
    031 * implementation to see if any other encodings are supported. Consult the release documentation for your implementation to see if any other encodings are
    032 * supported. </cite>
    033 * </p>
    034 *
    035 * <dl>
    036 * <dt>{@code US-ASCII}</dt>
    037 * <dd>Seven-bit ASCII, a.k.a. ISO646-US, a.k.a. the Basic Latin block of the Unicode character set.</dd>
    038 * <dt>{@code ISO-8859-1}</dt>
    039 * <dd>ISO Latin Alphabet No. 1, a.k.a. ISO-LATIN-1.</dd>
    040 * <dt>{@code UTF-8}</dt>
    041 * <dd>Eight-bit Unicode Transformation Format.</dd>
    042 * <dt>{@code UTF-16BE}</dt>
    043 * <dd>Sixteen-bit Unicode Transformation Format, big-endian byte order.</dd>
    044 * <dt>{@code UTF-16LE}</dt>
    045 * <dd>Sixteen-bit Unicode Transformation Format, little-endian byte order.</dd>
    046 * <dt>{@code UTF-16}</dt>
    047 * <dd>Sixteen-bit Unicode Transformation Format, byte order specified by a mandatory initial byte-order mark (either order accepted on input, big-endian used
    048 * on output.)</dd>
    049 * </dl>
    050 *
    051 * @see <a href="https://docs.oracle.com/javase/8/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
    052 * @see StandardCharsets
    053 * @since 1.4
    054 * @deprecated Use {@link org.apache.commons.io.Charsets}.
    055 */
    056@Deprecated
    057public class Charsets {
    058
    059    //
    060    // This class should only contain Charset instances for required encodings. This guarantees that it will load correctly and
    061    // without delay on all Java platforms.
    062    //
    063
    064    /**
    065     * ISO Latin Alphabet No. 1, a.k.a. ISO-LATIN-1.
    066     * <p>
    067     * Every implementation of the Java platform is required to support this character encoding.
    068     * </p>
    069     *
    070     * @see <a href="https://docs.oracle.com/javase/8/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
    071     * @deprecated replaced by {@link StandardCharsets} in Java 7
    072     */
    073    @Deprecated
    074    public static final Charset ISO_8859_1 = StandardCharsets.ISO_8859_1;
    075
    076    /**
    077     * <p>
    078     * Seven-bit ASCII, also known as ISO646-US, also known as the Basic Latin block of the Unicode character set.
    079     * </p>
    080     * <p>
    081     * Every implementation of the Java platform is required to support this character encoding.
    082     * </p>
    083     *
    084     * @see <a href="https://docs.oracle.com/javase/8/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
    085     * @deprecated replaced by {@link StandardCharsets} in Java 7
    086     */
    087    @Deprecated
    088    public static final Charset US_ASCII = StandardCharsets.US_ASCII;
    089
    090    /**
    091     * <p>
    092     * Sixteen-bit Unicode Transformation Format, The byte order specified by a mandatory initial byte-order mark (either order accepted on input, big-endian
    093     * used on output)
    094     * </p>
    095     * <p>
    096     * Every implementation of the Java platform is required to support this character encoding.
    097     * </p>
    098     *
    099     * @see <a href="https://docs.oracle.com/javase/8/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
    100     * @deprecated replaced by {@link StandardCharsets} in Java 7
    101     */
    102    @Deprecated
    103    public static final Charset UTF_16 = StandardCharsets.UTF_16;
    104
    105    /**
    106     * <p>
    107     * Sixteen-bit Unicode Transformation Format, big-endian byte order.
    108     * </p>
    109     * <p>
    110     * Every implementation of the Java platform is required to support this character encoding.
    111     * </p>
    112     *
    113     * @see <a href="https://docs.oracle.com/javase/8/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
    114     * @deprecated replaced by {@link StandardCharsets} in Java 7
    115     */
    116    @Deprecated
    117    public static final Charset UTF_16BE = StandardCharsets.UTF_16BE;
    118
    119    /**
    120     * <p>
    121     * Sixteen-bit Unicode Transformation Format, little-endian byte order.
    122     * </p>
    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 replaced by {@link StandardCharsets} in Java 7
    129     */
    130    @Deprecated
    131    public static final Charset UTF_16LE = StandardCharsets.UTF_16LE;
    132
    133    /**
    134     * <p>
    135     * Eight-bit Unicode Transformation Format.
    136     * </p>
    137     * <p>
    138     * Every implementation of the Java platform is required to support this character encoding.
    139     * </p>
    140     *
    141     * @see <a href="https://docs.oracle.com/javase/8/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
    142     * @deprecated replaced by {@link StandardCharsets} in Java 7
    143     */
    144    @Deprecated
    145    public static final Charset UTF_8 = StandardCharsets.UTF_8;
    146
    147    /**
    148     * Returns the given Charset or the default Charset if the given Charset is null.
    149     *
    150     * @param charset A charset or null.
    151     * @return the given Charset or the default Charset if the given Charset is null
    152     */
    153    public static Charset toCharset(final Charset charset) {
    154        return charset == null ? Charset.defaultCharset() : charset;
    155    }
    156
    157    /**
    158     * Returns a Charset for the named charset. If the name is null, return the default Charset.
    159     *
    160     * @param charset The name of the requested charset, may be null.
    161     * @return a Charset for the named charset
    162     * @throws java.nio.charset.UnsupportedCharsetException If the named charset is unavailable
    163     * @throws java.nio.charset.IllegalCharsetNameException If the given charset name is illegal
    164     */
    165    public static Charset toCharset(final String charset) {
    166        return charset == null ? Charset.defaultCharset() : Charset.forName(charset);
    167    }
    168
    169    /**
    170     * Constructs a new instance.
    171     */
    172    public Charsets() {
    173        // empty
    174    }
    175}