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.io;

  18. import java.nio.charset.Charset;
  19. import java.nio.charset.StandardCharsets;
  20. import java.nio.charset.UnsupportedCharsetException;
  21. import java.util.Collections;
  22. import java.util.SortedMap;
  23. import java.util.TreeMap;

  24. /**
  25.  * Charsets required of every implementation of the Java platform.
  26.  *
  27.  * From the Java documentation <a href="https://docs.oracle.com/javase/8/docs/api/java/nio/charset/Charset.html">
  28.  * Standard charsets</a>:
  29.  * <p>
  30.  * <cite>Every implementation of the Java platform is required to support the following character encodings. Consult
  31.  * the 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.  * <ul>
  36.  * <li>{@code US-ASCII}<br>
  37.  * Seven-bit ASCII, a.k.a. ISO646-US, a.k.a. the Basic Latin block of the Unicode character set.</li>
  38.  * <li>{@code ISO-8859-1}<br>
  39.  * ISO Latin Alphabet No. 1, a.k.a. ISO-LATIN-1.</li>
  40.  * <li>{@code UTF-8}<br>
  41.  * Eight-bit Unicode Transformation Format.</li>
  42.  * <li>{@code UTF-16BE}<br>
  43.  * Sixteen-bit Unicode Transformation Format, big-endian byte order.</li>
  44.  * <li>{@code UTF-16LE}<br>
  45.  * Sixteen-bit Unicode Transformation Format, little-endian byte order.</li>
  46.  * <li>{@code UTF-16}<br>
  47.  * 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.)</li>
  49.  * </ul>
  50.  *
  51.  * @see <a href="https://docs.oracle.com/javase/8/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
  52.  * @since 2.3
  53.  */
  54. public class Charsets {

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

  59.     private static final SortedMap<String, Charset> STANDARD_CHARSET_MAP;

  60.     static {
  61.         final SortedMap<String, Charset> standardCharsetMap = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
  62.         standardCharsetMap.put(StandardCharsets.ISO_8859_1.name(), StandardCharsets.ISO_8859_1);
  63.         standardCharsetMap.put(StandardCharsets.US_ASCII.name(), StandardCharsets.US_ASCII);
  64.         standardCharsetMap.put(StandardCharsets.UTF_16.name(), StandardCharsets.UTF_16);
  65.         standardCharsetMap.put(StandardCharsets.UTF_16BE.name(), StandardCharsets.UTF_16BE);
  66.         standardCharsetMap.put(StandardCharsets.UTF_16LE.name(), StandardCharsets.UTF_16LE);
  67.         standardCharsetMap.put(StandardCharsets.UTF_8.name(), StandardCharsets.UTF_8);
  68.         STANDARD_CHARSET_MAP = Collections.unmodifiableSortedMap(standardCharsetMap);
  69.     }

  70.     /**
  71.      * CharEncodingISO Latin Alphabet No. 1, a.k.a. ISO-LATIN-1.
  72.      * <p>
  73.      * Every implementation of the Java platform is required to support this character encoding.
  74.      * </p>
  75.      *
  76.      * @see <a href="https://docs.oracle.com/javase/8/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
  77.      * @deprecated Use Java 7's {@link java.nio.charset.StandardCharsets}
  78.      */
  79.     @Deprecated
  80.     public static final Charset ISO_8859_1 = StandardCharsets.ISO_8859_1;

  81.     /**
  82.      * <p>
  83.      * Seven-bit ASCII, also known as ISO646-US, also known as the Basic Latin block of the Unicode character set.
  84.      * </p>
  85.      * <p>
  86.      * Every implementation of the Java platform is required to support this character encoding.
  87.      * </p>
  88.      *
  89.      * @see <a href="https://docs.oracle.com/javase/8/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
  90.      * @deprecated Use Java 7's {@link java.nio.charset.StandardCharsets}
  91.      */
  92.     @Deprecated
  93.     public static final Charset US_ASCII = StandardCharsets.US_ASCII;

  94.     /**
  95.      * <p>
  96.      * Sixteen-bit Unicode Transformation Format, The byte order specified by a mandatory initial byte-order mark
  97.      * (either order accepted on input, big-endian used on output)
  98.      * </p>
  99.      * <p>
  100.      * Every implementation of the Java platform is required to support this character encoding.
  101.      * </p>
  102.      *
  103.      * @see <a href="https://docs.oracle.com/javase/8/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
  104.      * @deprecated Use Java 7's {@link java.nio.charset.StandardCharsets}
  105.      */
  106.     @Deprecated
  107.     public static final Charset UTF_16 = StandardCharsets.UTF_16;

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

  121.     /**
  122.      * <p>
  123.      * Sixteen-bit Unicode Transformation Format, little-endian byte order.
  124.      * </p>
  125.      * <p>
  126.      * Every implementation of the Java platform is required to support this character encoding.
  127.      * </p>
  128.      *
  129.      * @see <a href="https://docs.oracle.com/javase/8/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
  130.      * @deprecated Use Java 7's {@link java.nio.charset.StandardCharsets}
  131.      */
  132.     @Deprecated
  133.     public static final Charset UTF_16LE = StandardCharsets.UTF_16LE;

  134.     /**
  135.      * <p>
  136.      * Eight-bit Unicode Transformation Format.
  137.      * </p>
  138.      * <p>
  139.      * Every implementation of the Java platform is required to support this character encoding.
  140.      * </p>
  141.      *
  142.      * @see <a href="https://docs.oracle.com/javase/8/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
  143.      * @deprecated Use Java 7's {@link java.nio.charset.StandardCharsets}
  144.      */
  145.     @Deprecated
  146.     public static final Charset UTF_8 = StandardCharsets.UTF_8;

  147.     /**
  148.      * Constructs a sorted map from canonical charset names to charset objects required of every implementation of the
  149.      * Java platform.
  150.      * <p>
  151.      * From the Java documentation <a href="https://docs.oracle.com/javase/8/docs/api/java/nio/charset/Charset.html">
  152.      * Standard charsets</a>:
  153.      * </p>
  154.      *
  155.      * @return An immutable, case-insensitive map from canonical charset names to charset objects.
  156.      * @see Charset#availableCharsets()
  157.      * @since 2.5
  158.      */
  159.     public static SortedMap<String, Charset> requiredCharsets() {
  160.         return STANDARD_CHARSET_MAP;
  161.     }

  162.     /**
  163.      * Returns the given Charset or the {@link Charset#defaultCharset() default Charset} if the given Charset is null.
  164.      *
  165.      * @param charset
  166.      *            A charset or null.
  167.      * @return the given Charset or the default Charset if the given Charset is null
  168.      * @see Charset#defaultCharset()
  169.      */
  170.     public static Charset toCharset(final Charset charset) {
  171.         return charset == null ? Charset.defaultCharset() : charset;
  172.     }

  173.     /**
  174.      * Returns the given charset if non-null, otherwise return defaultCharset.
  175.      *
  176.      * @param charset The charset to test, may be null.
  177.      * @param defaultCharset The charset to return if charset is null, may be null.
  178.      * @return a Charset.
  179.      * @since 2.12.0
  180.      */
  181.     public static Charset toCharset(final Charset charset, final Charset defaultCharset) {
  182.         return charset == null ? defaultCharset : charset;
  183.     }

  184.     /**
  185.      * Returns a Charset for the named charset. If the name is null, return the {@link Charset#defaultCharset() default Charset}.
  186.      *
  187.      * @param charsetName The name of the requested charset, may be null.
  188.      * @return a Charset for the named charset.
  189.      * @throws UnsupportedCharsetException If the named charset is unavailable (unchecked exception).
  190.      * @see Charset#defaultCharset()
  191.      */
  192.     public static Charset toCharset(final String charsetName) throws UnsupportedCharsetException {
  193.         return toCharset(charsetName, Charset.defaultCharset());
  194.     }

  195.     /**
  196.      * Returns a Charset for the named charset. If the name is null, return the given default Charset.
  197.      *
  198.      * @param charsetName The name of the requested charset, may be null.
  199.      * @param defaultCharset The name charset to return if charsetName is null, may be null.
  200.      * @return a Charset for the named charset.
  201.      * @throws UnsupportedCharsetException If the named charset is unavailable (unchecked exception).
  202.      * @since 2.12.0
  203.      */
  204.     public static Charset toCharset(final String charsetName, final Charset defaultCharset) throws UnsupportedCharsetException {
  205.         return charsetName == null ? defaultCharset : Charset.forName(charsetName);
  206.     }

  207.     /**
  208.      * Construct a new instance.
  209.      *
  210.      * @deprecated Will be private in 4.0
  211.      */
  212.     @Deprecated
  213.     public Charsets() {
  214.         // empty
  215.     }
  216. }