    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/6/docs/api/java/nio/charset/Charset.html">Standard
    027 * charsets</a>:
    028 * <p>
    029 * <cite>Every implementation of the Java platform is required to support the following character encodings. Consult the
    030 * release documentation for your implementation to see if any other encodings are supported. Consult the release
    031 * documentation for your implementation to see if any other encodings are supported. </cite>
    032 * </p>
    033 *
    034 * <dl>
    035 * <dt>{@code US-ASCII}</dt>
    036 * <dd>Seven-bit ASCII, a.k.a. ISO646-US, a.k.a. the Basic Latin block of the Unicode character set.</dd>
    037 * <dt>{@code ISO-8859-1}</dt>
    038 * <dd>ISO Latin Alphabet No. 1, a.k.a. ISO-LATIN-1.</dd>
    039 * <dt>{@code UTF-8}</dt>
    040 * <dd>Eight-bit Unicode Transformation Format.</dd>
    041 * <dt>{@code UTF-16BE}</dt>
    042 * <dd>Sixteen-bit Unicode Transformation Format, big-endian byte order.</dd>
    043 * <dt>{@code UTF-16LE}</dt>
    044 * <dd>Sixteen-bit Unicode Transformation Format, little-endian byte order.</dd>
    045 * <dt>{@code UTF-16}</dt>
    046 * <dd>Sixteen-bit Unicode Transformation Format, byte order specified by a mandatory initial byte-order mark (either order
    047 * accepted on input, big-endian used on output.)</dd>
    048 * </dl>
    049 *
    050 * <p>This class best belongs in the Commons Lang or IO project. Even if a similar class is defined in another Commons
    051 * component, it is not foreseen that Commons Compress would be made to depend on another Commons component.</p>
    052 *
    053 * @see <a href="https://docs.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
    054 * @see StandardCharsets
    055 * @since 1.4
    056 */
    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     * CharsetNamesISO 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/6/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/6/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
    093     * (either order accepted on input, big-endian 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/6/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/6/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/6/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/6/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
    151     *            A charset or null.
    152     * @return the given Charset or the default Charset if the given Charset is null
    153     */
    154    public static Charset toCharset(final Charset charset) {
    155        return charset == null ? Charset.defaultCharset() : charset;
    156    }
    157
    158    /**
    159     * Returns a Charset for the named charset. If the name is null, return the default Charset.
    160     *
    161     * @param charset
    162     *            The name of the requested charset, may be null.
    163     * @return a Charset for the named charset
    164     * @throws java.nio.charset.UnsupportedCharsetException
    165     *             If the named charset is unavailable
    166     * @throws java.nio.charset.IllegalCharsetNameException
    167     *             If the given charset name is illegal
    168     */
    169    public static Charset toCharset(final String charset) {
    170        return charset == null ? Charset.defaultCharset() : Charset.forName(charset);
    171    }
    172}