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    public static final Charset ISO_8859_1 = StandardCharsets.ISO_8859_1;
101
102    /**
103     * <p>
104     * Seven-bit ASCII, also known as ISO646-US, also known as the Basic Latin block of the Unicode character set.
105     * </p>
106     * <p>
107     * Every implementation of the Java platform is required to support this character encoding.
108     * </p>
109     *
110     * @see <a href="https://docs.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
111     * @deprecated replaced by {@link StandardCharsets} in Java 7
112     */
113    public static final Charset US_ASCII = StandardCharsets.US_ASCII;
114
115    /**
116     * <p>
117     * Sixteen-bit Unicode Transformation Format, The byte order specified by a mandatory initial byte-order mark
118     * (either order accepted on input, big-endian used on output)
119     * </p>
120     * <p>
121     * Every implementation of the Java platform is required to support this character encoding.
122     * </p>
123     *
124     * @see <a href="https://docs.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
125     * @deprecated replaced by {@link StandardCharsets} in Java 7
126     */
127    public static final Charset UTF_16 = StandardCharsets.UTF_16;
128
129    /**
130     * <p>
131     * Sixteen-bit Unicode Transformation Format, big-endian byte order.
132     * </p>
133     * <p>
134     * Every implementation of the Java platform is required to support this character encoding.
135     * </p>
136     *
137     * @see <a href="https://docs.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
138     * @deprecated replaced by {@link StandardCharsets} in Java 7
139     */
140    public static final Charset UTF_16BE = StandardCharsets.UTF_16BE;
141
142    /**
143     * <p>
144     * Sixteen-bit Unicode Transformation Format, little-endian byte order.
145     * </p>
146     * <p>
147     * Every implementation of the Java platform is required to support this character encoding.
148     * </p>
149     *
150     * @see <a href="https://docs.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
151     * @deprecated replaced by {@link StandardCharsets} in Java 7
152     */
153    public static final Charset UTF_16LE = StandardCharsets.UTF_16LE;
154
155    /**
156     * <p>
157     * Eight-bit Unicode Transformation Format.
158     * </p>
159     * <p>
160     * Every implementation of the Java platform is required to support this character encoding.
161     * </p>
162     *
163     * @see <a href="https://docs.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
164     * @deprecated replaced by {@link StandardCharsets} in Java 7
165     */
166    public static final Charset UTF_8 = StandardCharsets.UTF_8;
167}