Charsets.java

  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. package org.apache.commons.compress.utils;

  18. import java.nio.charset.Charset;
  19. import java.nio.charset.StandardCharsets;

  20. /**
  21.  * Charsets required of every implementation of the Java platform.
  22.  *
  23.  * From the Java documentation <a href="https://docs.oracle.com/javase/8/docs/api/java/nio/charset/Charset.html">Standard charsets</a>:
  24.  * <p>
  25.  * <cite>Every implementation of the Java platform is required to support the following character encodings. Consult the release documentation for your
  26.  * implementation to see if any other encodings are supported. Consult the release documentation for your implementation to see if any other encodings are
  27.  * supported. </cite>
  28.  * </p>
  29.  *
  30.  * <dl>
  31.  * <dt>{@code US-ASCII}</dt>
  32.  * <dd>Seven-bit ASCII, a.k.a. ISO646-US, a.k.a. the Basic Latin block of the Unicode character set.</dd>
  33.  * <dt>{@code ISO-8859-1}</dt>
  34.  * <dd>ISO Latin Alphabet No. 1, a.k.a. ISO-LATIN-1.</dd>
  35.  * <dt>{@code UTF-8}</dt>
  36.  * <dd>Eight-bit Unicode Transformation Format.</dd>
  37.  * <dt>{@code UTF-16BE}</dt>
  38.  * <dd>Sixteen-bit Unicode Transformation Format, big-endian byte order.</dd>
  39.  * <dt>{@code UTF-16LE}</dt>
  40.  * <dd>Sixteen-bit Unicode Transformation Format, little-endian byte order.</dd>
  41.  * <dt>{@code UTF-16}</dt>
  42.  * <dd>Sixteen-bit Unicode Transformation Format, byte order specified by a mandatory initial byte-order mark (either order accepted on input, big-endian used
  43.  * on output.)</dd>
  44.  * </dl>
  45.  *
  46.  * <p>
  47.  * This class best belongs in the Commons Lang or IO project. Even if a similar class is defined in another Commons component, it is not foreseen that Commons
  48.  * Compress would be made to depend on another Commons component.
  49.  * </p>
  50.  *
  51.  * @see <a href="https://docs.oracle.com/javase/8/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
  52.  * @see StandardCharsets
  53.  * @since 1.4
  54.  * @deprecated Use {@link org.apache.commons.io.Charsets}.
  55.  */
  56. @Deprecated
  57. public class Charsets {

  58.     //
  59.     // This class should only contain Charset instances for required encodings. This guarantees that it will load correctly and
  60.     // without delay on all Java platforms.
  61.     //

  62.     /**
  63.      * CharsetNamesISO Latin Alphabet No. 1, a.k.a. ISO-LATIN-1.
  64.      * <p>
  65.      * Every implementation of the Java platform is required to support this character encoding.
  66.      * </p>
  67.      *
  68.      * @see <a href="https://docs.oracle.com/javase/8/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
  69.      * @deprecated replaced by {@link StandardCharsets} in Java 7
  70.      */
  71.     @Deprecated
  72.     public static final Charset ISO_8859_1 = StandardCharsets.ISO_8859_1;

  73.     /**
  74.      * <p>
  75.      * Seven-bit ASCII, also known as ISO646-US, also known as the Basic Latin block of the Unicode character set.
  76.      * </p>
  77.      * <p>
  78.      * Every implementation of the Java platform is required to support this character encoding.
  79.      * </p>
  80.      *
  81.      * @see <a href="https://docs.oracle.com/javase/8/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
  82.      * @deprecated replaced by {@link StandardCharsets} in Java 7
  83.      */
  84.     @Deprecated
  85.     public static final Charset US_ASCII = StandardCharsets.US_ASCII;

  86.     /**
  87.      * <p>
  88.      * Sixteen-bit Unicode Transformation Format, The byte order specified by a mandatory initial byte-order mark (either order accepted on input, big-endian
  89.      * used on output)
  90.      * </p>
  91.      * <p>
  92.      * Every implementation of the Java platform is required to support this character encoding.
  93.      * </p>
  94.      *
  95.      * @see <a href="https://docs.oracle.com/javase/8/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
  96.      * @deprecated replaced by {@link StandardCharsets} in Java 7
  97.      */
  98.     @Deprecated
  99.     public static final Charset UTF_16 = StandardCharsets.UTF_16;

  100.     /**
  101.      * <p>
  102.      * Sixteen-bit Unicode Transformation Format, big-endian byte order.
  103.      * </p>
  104.      * <p>
  105.      * Every implementation of the Java platform is required to support this character encoding.
  106.      * </p>
  107.      *
  108.      * @see <a href="https://docs.oracle.com/javase/8/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
  109.      * @deprecated replaced by {@link StandardCharsets} in Java 7
  110.      */
  111.     @Deprecated
  112.     public static final Charset UTF_16BE = StandardCharsets.UTF_16BE;

  113.     /**
  114.      * <p>
  115.      * Sixteen-bit Unicode Transformation Format, little-endian byte order.
  116.      * </p>
  117.      * <p>
  118.      * Every implementation of the Java platform is required to support this character encoding.
  119.      * </p>
  120.      *
  121.      * @see <a href="https://docs.oracle.com/javase/8/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
  122.      * @deprecated replaced by {@link StandardCharsets} in Java 7
  123.      */
  124.     @Deprecated
  125.     public static final Charset UTF_16LE = StandardCharsets.UTF_16LE;

  126.     /**
  127.      * <p>
  128.      * Eight-bit Unicode Transformation Format.
  129.      * </p>
  130.      * <p>
  131.      * Every implementation of the Java platform is required to support this character encoding.
  132.      * </p>
  133.      *
  134.      * @see <a href="https://docs.oracle.com/javase/8/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
  135.      * @deprecated replaced by {@link StandardCharsets} in Java 7
  136.      */
  137.     @Deprecated
  138.     public static final Charset UTF_8 = StandardCharsets.UTF_8;

  139.     /**
  140.      * Returns the given Charset or the default Charset if the given Charset is null.
  141.      *
  142.      * @param charset A charset or null.
  143.      * @return the given Charset or the default Charset if the given Charset is null
  144.      */
  145.     public static Charset toCharset(final Charset charset) {
  146.         return charset == null ? Charset.defaultCharset() : charset;
  147.     }

  148.     /**
  149.      * Returns a Charset for the named charset. If the name is null, return the default Charset.
  150.      *
  151.      * @param charset The name of the requested charset, may be null.
  152.      * @return a Charset for the named charset
  153.      * @throws java.nio.charset.UnsupportedCharsetException If the named charset is unavailable
  154.      * @throws java.nio.charset.IllegalCharsetNameException If the given charset name is illegal
  155.      */
  156.     public static Charset toCharset(final String charset) {
  157.         return charset == null ? Charset.defaultCharset() : Charset.forName(charset);
  158.     }
  159. }