Charsets.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one
  3.  * or more contributor license agreements.  See the NOTICE file
  4.  * distributed with this work for additional information
  5.  * regarding copyright ownership.  The ASF licenses this file
  6.  * to you under the Apache License, Version 2.0 (the
  7.  * "License"); you may not use this file except in compliance
  8.  * with the License.  You may obtain a copy of the License at
  9.  *
  10.  *   https://www.apache.org/licenses/LICENSE-2.0
  11.  *
  12.  * Unless required by applicable law or agreed to in writing,
  13.  * software distributed under the License is distributed on an
  14.  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15.  * KIND, either express or implied.  See the License for the
  16.  * specific language governing permissions and limitations
  17.  * under the License.
  18.  */

  19. package org.apache.commons.compress.utils;

  20. import java.nio.charset.Charset;
  21. import java.nio.charset.StandardCharsets;

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

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

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

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

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

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

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

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

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

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

  156.     /**
  157.      * Constructs a new instance.
  158.      */
  159.     public Charsets() {
  160.         // empty
  161.     }
  162. }