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.  *      https://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.codec;

  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/6/docs/api/java/nio/charset/Charset.html">Standard
  24.  * charsets</a>:
  25.  * <p>
  26.  * <cite>Every implementation of the Java platform is required to support the following character encodings. Consult the
  27.  * release documentation for your implementation to see if any other encodings are supported. Consult the release
  28.  * documentation for your implementation to see if any other encodings are supported. </cite>
  29.  * </p>
  30.  *
  31.  * <ul>
  32.  * <li>{@code US-ASCII}<p>
  33.  * Seven-bit ASCII, a.k.a. ISO646-US, a.k.a. the Basic Latin block of the Unicode character set.</p></li>
  34.  * <li>{@code ISO-8859-1}<p>
  35.  * ISO Latin Alphabet No. 1, a.k.a. ISO-LATIN-1.</p></li>
  36.  * <li>{@code UTF-8}<p>
  37.  * Eight-bit Unicode Transformation Format.</p></li>
  38.  * <li>{@code UTF-16BE}<p>
  39.  * Sixteen-bit Unicode Transformation Format, big-endian byte order.</p></li>
  40.  * <li>{@code UTF-16LE}<p>
  41.  * Sixteen-bit Unicode Transformation Format, little-endian byte order.</p></li>
  42.  * <li>{@code UTF-16}<p>
  43.  * Sixteen-bit Unicode Transformation Format, byte order specified by a mandatory initial byte-order mark (either order
  44.  * accepted on input, big-endian used on output.)</p></li>
  45.  * </ul>
  46.  *
  47.  * This perhaps would best belong in the Commons Lang project. Even if a similar class is defined in Commons Lang, it is
  48.  * not foreseen that Commons Codec would be made to depend on Commons Lang.
  49.  *
  50.  * <p>
  51.  * This class is immutable and thread-safe.
  52.  * </p>
  53.  *
  54.  * @see <a href="https://docs.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
  55.  * @since 1.7
  56.  */
  57. public class Charsets {

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

  62.     /**
  63.      * CharEncodingISO 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.      * @deprecated Use {@link java.nio.charset.StandardCharsets#ISO_8859_1} instead.
  69.      * @see <a href="https://docs.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
  70.      */
  71.     @Deprecated
  72.     public static final Charset ISO_8859_1 = StandardCharsets.ISO_8859_1;

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

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

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

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

  118.     /**
  119.      * Eight-bit Unicode Transformation Format.
  120.      * <p>
  121.      * Every implementation of the Java platform is required to support this character encoding.
  122.      * </p>
  123.      *
  124.      * @deprecated Use {@link java.nio.charset.StandardCharsets#UTF_8} instead.
  125.      * @see <a href="https://docs.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
  126.      */
  127.     @Deprecated
  128.     public static final Charset UTF_8 = StandardCharsets.UTF_8;

  129.     /**
  130.      * Returns the given Charset or the default Charset if the given Charset is null.
  131.      *
  132.      * @param charset
  133.      *            A charset or null.
  134.      * @return the given Charset or the default Charset if the given Charset is null
  135.      */
  136.     public static Charset toCharset(final Charset charset) {
  137.         return charset == null ? Charset.defaultCharset() : charset;
  138.     }

  139.     /**
  140.      * Returns a Charset for the named charset. If the name is null, return the default Charset.
  141.      *
  142.      * @param charset
  143.      *            The name of the requested charset, may be null.
  144.      * @return a Charset for the named charset
  145.      * @throws java.nio.charset.UnsupportedCharsetException
  146.      *             If the named charset is unavailable
  147.      */
  148.     public static Charset toCharset(final String charset) {
  149.         return charset == null ? Charset.defaultCharset() : Charset.forName(charset);
  150.     }

  151.     /**
  152.      * TODO Make private in 2.0.
  153.      *
  154.      * @deprecated TODO Make private in 2.0.
  155.      */
  156.     @Deprecated
  157.     public Charsets() {
  158.         // empty
  159.     }
  160. }