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