Home | History | Annotate | Download | only in utils
      1 /*
      2  * Licensed to the Apache Software Foundation (ASF) under one or more
      3  * contributor license agreements.  See the NOTICE file distributed with
      4  * this work for additional information regarding copyright ownership.
      5  * The ASF licenses this file to You under the Apache License, Version 2.0
      6  * (the "License"); you may not use this file except in compliance with
      7  * the License.  You may obtain a copy of the License at
      8  *
      9  *   http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  * Unless required by applicable law or agreed to in writing, software
     12  * distributed under the License is distributed on an "AS IS" BASIS,
     13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  * See the License for the specific language governing permissions and
     15  * limitations under the License.
     16  *
     17  */
     18 
     19 package org.apache.commons.compress.utils;
     20 
     21 import java.nio.charset.Charset;
     22 import java.nio.charset.StandardCharsets;
     23 
     24 /**
     25  * Charsets required of every implementation of the Java platform.
     26  *
     27  * From the Java documentation <a href="https://docs.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html">Standard
     28  * charsets</a>:
     29  * <p>
     30  * <cite>Every implementation of the Java platform is required to support the following character encodings. Consult the
     31  * release documentation for your implementation to see if any other encodings are supported. Consult the release
     32  * documentation for your implementation to see if any other encodings are supported. </cite>
     33  * </p>
     34  *
     35  * <dl>
     36  * <dt><code>US-ASCII</code></dt>
     37  * <dd>Seven-bit ASCII, a.k.a. ISO646-US, a.k.a. the Basic Latin block of the Unicode character set.</dd>
     38  * <dt><code>ISO-8859-1</code></dt>
     39  * <dd>ISO Latin Alphabet No. 1, a.k.a. ISO-LATIN-1.</dd>
     40  * <dt><code>UTF-8</code></dt>
     41  * <dd>Eight-bit Unicode Transformation Format.</dd>
     42  * <dt><code>UTF-16BE</code></dt>
     43  * <dd>Sixteen-bit Unicode Transformation Format, big-endian byte order.</dd>
     44  * <dt><code>UTF-16LE</code></dt>
     45  * <dd>Sixteen-bit Unicode Transformation Format, little-endian byte order.</dd>
     46  * <dt><code>UTF-16</code></dt>
     47  * <dd>Sixteen-bit Unicode Transformation Format, byte order specified by a mandatory initial byte-order mark (either order
     48  * accepted on input, big-endian used on output.)</dd>
     49  * </dl>
     50  *
     51  * <p>This class best belongs in the Commons Lang or IO project. Even if a similar class is defined in another Commons
     52  * component, it is not foreseen that Commons Compress would be made to depend on another Commons component.</p>
     53  *
     54  * @see <a href="https://docs.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
     55  * @see StandardCharsets
     56  * @since 1.4
     57  */
     58 public class Charsets {
     59 
     60     //
     61     // This class should only contain Charset instances for required encodings. This guarantees that it will load correctly and
     62     // without delay on all Java platforms.
     63     //
     64 
     65     /**
     66      * Returns the given Charset or the default Charset if the given Charset is null.
     67      *
     68      * @param charset
     69      *            A charset or null.
     70      * @return the given Charset or the default Charset if the given Charset is null
     71      */
     72     public static Charset toCharset(final Charset charset) {
     73         return charset == null ? Charset.defaultCharset() : charset;
     74     }
     75 
     76     /**
     77      * Returns a Charset for the named charset. If the name is null, return the default Charset.
     78      *
     79      * @param charset
     80      *            The name of the requested charset, may be null.
     81      * @return a Charset for the named charset
     82      * @throws java.nio.charset.UnsupportedCharsetException
     83      *             If the named charset is unavailable
     84      * @throws java.nio.charset.IllegalCharsetNameException
     85      *             If the given charset name is illegal
     86      */
     87     public static Charset toCharset(final String charset) {
     88         return charset == null ? Charset.defaultCharset() : Charset.forName(charset);
     89     }
     90 
     91     /**
     92      * CharsetNamesISO Latin Alphabet No. 1, a.k.a. ISO-LATIN-1.
     93      * <p>
     94      * Every implementation of the Java platform is required to support this character encoding.
     95      * </p>
     96      *
     97      * @see <a href="https://docs.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
     98      * @deprecated replaced by {@link StandardCharsets} in Java 7
     99      */
    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 }
    168