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

  18. import java.nio.charset.Charset;

  19. /**
  20.  * Charsets required of every implementation of the Java platform.
  21.  *
  22.  * From the Java documentation <a href="http://docs.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html">Standard
  23.  * charsets</a>:
  24.  * <p>
  25.  * <cite>Every implementation of the Java platform is required to support the following character encodings. Consult the
  26.  * release documentation for your implementation to see if any other encodings are supported. Consult the release
  27.  * documentation for your implementation to see if any other encodings are supported. </cite>
  28.  * </p>
  29.  *
  30.  * <ul>
  31.  * <li><code>US-ASCII</code><br>
  32.  * Seven-bit ASCII, a.k.a. ISO646-US, a.k.a. the Basic Latin block of the Unicode character set.</li>
  33.  * <li><code>ISO-8859-1</code><br>
  34.  * ISO Latin Alphabet No. 1, a.k.a. ISO-LATIN-1.</li>
  35.  * <li><code>UTF-8</code><br>
  36.  * Eight-bit Unicode Transformation Format.</li>
  37.  * <li><code>UTF-16BE</code><br>
  38.  * Sixteen-bit Unicode Transformation Format, big-endian byte order.</li>
  39.  * <li><code>UTF-16LE</code><br>
  40.  * Sixteen-bit Unicode Transformation Format, little-endian byte order.</li>
  41.  * <li><code>UTF-16</code><br>
  42.  * Sixteen-bit Unicode Transformation Format, byte order specified by a mandatory initial byte-order mark (either order
  43.  * accepted on input, big-endian used on output.)</li>
  44.  * </ul>
  45.  *
  46.  * This perhaps would best belong in the Commons Lang project. Even if a similar class is defined in Commons Lang, it is
  47.  * not foreseen that Commons Codec would be made to depend on Commons Lang.
  48.  *
  49.  * <p>
  50.  * This class is immutable and thread-safe.
  51.  * </p>
  52.  *
  53.  * @see <a href="http://docs.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
  54.  * @since 1.7
  55.  * @version $Id: CharEncoding.java 1173287 2011-09-20 18:16:19Z ggregory $
  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.      * Returns the given Charset or the default Charset if the given Charset is null.
  64.      *
  65.      * @param charset
  66.      *            A charset or null.
  67.      * @return the given Charset or the default Charset if the given Charset is null
  68.      */
  69.     public static Charset toCharset(final Charset charset) {
  70.         return charset == null ? Charset.defaultCharset() : charset;
  71.     }

  72.     /**
  73.      * Returns a Charset for the named charset. If the name is null, return the default Charset.
  74.      *
  75.      * @param charset
  76.      *            The name of the requested charset, may be null.
  77.      * @return a Charset for the named charset
  78.      * @throws java.nio.charset.UnsupportedCharsetException
  79.      *             If the named charset is unavailable
  80.      */
  81.     public static Charset toCharset(final String charset) {
  82.         return charset == null ? Charset.defaultCharset() : Charset.forName(charset);
  83.     }

  84.     /**
  85.      * CharEncodingISO Latin Alphabet No. 1, a.k.a. ISO-LATIN-1.
  86.      * <p>
  87.      * Every implementation of the Java platform is required to support this character encoding.
  88.      * </p>
  89.      * <p>
  90.      * On Java 7 or later, use {@link java.nio.charset.StandardCharsets#ISO_8859_1} instead.
  91.      * </p>
  92.      *
  93.      * @see <a href="http://docs.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
  94.      */
  95.     public static final Charset ISO_8859_1 = Charset.forName(CharEncoding.ISO_8859_1);

  96.     /**
  97.      * Seven-bit ASCII, also known as ISO646-US, also known as the Basic Latin block of the Unicode character set.
  98.      * <p>
  99.      * Every implementation of the Java platform is required to support this character encoding.
  100.      * </p>
  101.      * <p>
  102.      * On Java 7 or later, use {@link java.nio.charset.StandardCharsets#ISO_8859_1} instead.
  103.      * </p>
  104.      *
  105.      * @see <a href="http://docs.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
  106.      */
  107.     public static final Charset US_ASCII = Charset.forName(CharEncoding.US_ASCII);

  108.     /**
  109.      * Sixteen-bit Unicode Transformation Format, The byte order specified by a mandatory initial byte-order mark
  110.      * (either order accepted on input, big-endian used on output)
  111.      * <p>
  112.      * Every implementation of the Java platform is required to support this character encoding.
  113.      * </p>
  114.      * <p>
  115.      * On Java 7 or later, use {@link java.nio.charset.StandardCharsets#ISO_8859_1} instead.
  116.      * </p>
  117.      *
  118.      * @see <a href="http://docs.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
  119.      */
  120.     public static final Charset UTF_16 = Charset.forName(CharEncoding.UTF_16);

  121.     /**
  122.      * Sixteen-bit Unicode Transformation Format, big-endian byte order.
  123.      * <p>
  124.      * Every implementation of the Java platform is required to support this character encoding.
  125.      * </p>
  126.      * <p>
  127.      * On Java 7 or later, use {@link java.nio.charset.StandardCharsets#ISO_8859_1} instead.
  128.      * </p>
  129.      *
  130.      * @see <a href="http://docs.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
  131.      */
  132.     public static final Charset UTF_16BE = Charset.forName(CharEncoding.UTF_16BE);

  133.     /**
  134.      * Sixteen-bit Unicode Transformation Format, little-endian byte order.
  135.      * <p>
  136.      * Every implementation of the Java platform is required to support this character encoding.
  137.      * </p>
  138.      * <p>
  139.      * On Java 7 or later, use {@link java.nio.charset.StandardCharsets#ISO_8859_1} instead.
  140.      * </p>
  141.      *
  142.      * @see <a href="http://docs.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
  143.      */
  144.     public static final Charset UTF_16LE = Charset.forName(CharEncoding.UTF_16LE);

  145.     /**
  146.      * Eight-bit Unicode Transformation Format.
  147.      * <p>
  148.      * Every implementation of the Java platform is required to support this character encoding.
  149.      * </p>
  150.      * <p>
  151.      * On Java 7 or later, use {@link java.nio.charset.StandardCharsets#ISO_8859_1} instead.
  152.      * </p>
  153.      *
  154.      * @see <a href="http://docs.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
  155.      */
  156.     public static final Charset UTF_8 = Charset.forName(CharEncoding.UTF_8);
  157. }