CharEncoding.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.lang3;

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

  21. /**
  22.  * Character encoding names required of every implementation of the Java platform.
  23.  *
  24.  * <p>According to <a href="https://docs.oracle.com/javase/8/docs/api/java/nio/charset/Charset.html">JRE character
  25.  * encoding names</a>:</p>
  26.  *
  27.  * <p><cite>Every implementation of the Java platform is required to support the following character encodings.
  28.  * Consult the release documentation for your implementation to see if any other encodings are supported.
  29.  * </cite></p>
  30.  *
  31.  * @see <a href="https://docs.oracle.com/javase/8/docs/technotes/guides/intl/encoding.doc.html">JRE character encoding names</a>
  32.  * @since 2.1
  33.  * @deprecated Java 7 introduced {@link java.nio.charset.StandardCharsets}, which defines these constants as
  34.  * {@link Charset} objects. Use {@link Charset#name()} to get the string values provided in this class.
  35.  * This class will be removed in a future release.
  36.  */
  37. @Deprecated
  38. public class CharEncoding {

  39.     /**
  40.      * ISO Latin Alphabet #1, also known as ISO-LATIN-1.
  41.      *
  42.      * <p>Every implementation of the Java platform is required to support this character encoding.</p>
  43.      */
  44.     public static final String ISO_8859_1 = StandardCharsets.ISO_8859_1.name();

  45.     /**
  46.      * Seven-bit ASCII, also known as ISO646-US, also known as the Basic Latin block
  47.      * of the Unicode character set.
  48.      *
  49.      * <p>Every implementation of the Java platform is required to support this character encoding.</p>
  50.      */
  51.     public static final String US_ASCII = StandardCharsets.US_ASCII.name();

  52.     /**
  53.      * Sixteen-bit Unicode Transformation Format, byte order specified by a mandatory initial
  54.      * byte-order mark (either order accepted on input, big-endian used on output).
  55.      *
  56.      * <p>Every implementation of the Java platform is required to support this character encoding.</p>
  57.      */
  58.     public static final String UTF_16 = StandardCharsets.UTF_16.name();

  59.     /**
  60.      * Sixteen-bit Unicode Transformation Format, big-endian byte order.
  61.      *
  62.      * <p>Every implementation of the Java platform is required to support this character encoding.</p>
  63.      */
  64.     public static final String UTF_16BE = StandardCharsets.UTF_16BE.name();

  65.     /**
  66.      * Sixteen-bit Unicode Transformation Format, little-endian byte order.
  67.      *
  68.      * <p>Every implementation of the Java platform is required to support this character encoding.</p>
  69.      */
  70.     public static final String UTF_16LE = StandardCharsets.UTF_16LE.name();

  71.     /**
  72.      * Eight-bit Unicode Transformation Format.
  73.      *
  74.      * <p>Every implementation of the Java platform is required to support this character encoding.</p>
  75.      */
  76.     public static final String UTF_8 = StandardCharsets.UTF_8.name();

  77.     /**
  78.      * Returns whether the named charset is supported.
  79.      *
  80.      * <p>This is similar to <a
  81.      * href="https://docs.oracle.com/javase/8/docs/api/java/nio/charset/Charset.html#isSupported%28java.lang.String%29">
  82.      * java.nio.charset.Charset.isSupported(String)</a> but handles more formats</p>
  83.      *
  84.      * @param name  the name of the requested charset; may be either a canonical name or an alias, null returns false
  85.      * @return {@code true} if the charset is available in the current Java virtual machine
  86.      * @deprecated Please use {@link Charset#isSupported(String)} instead, although be aware that {@code null}
  87.      * values are not accepted by that method and an {@link IllegalCharsetNameException} may be thrown.
  88.      */
  89.     @Deprecated
  90.     public static boolean isSupported(final String name) {
  91.         if (name == null) {
  92.             return false;
  93.         }
  94.         try {
  95.             return Charset.isSupported(name);
  96.         } catch (final IllegalCharsetNameException ex) {
  97.             return false;
  98.         }
  99.     }

  100.     /**
  101.      * Constructs a new instance.
  102.      *
  103.      * @deprecated Will be removed in 4.0.0.
  104.      */
  105.     @Deprecated
  106.     public CharEncoding() {
  107.         // empty
  108.     }
  109. }