View Javadoc
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  
18  package org.apache.commons.lang3;
19  
20  import java.nio.charset.Charset;
21  import java.nio.charset.IllegalCharsetNameException;
22  
23  /**
24   * <p>Character encoding names required of every implementation of the Java platform.</p>
25   *
26   * <p>According to <a href="http://docs.oracle.com/javase/1.3/docs/api/java/lang/package-summary.html#charenc">JRE character
27   * encoding names</a>:</p>
28   *
29   * <p><cite>Every implementation of the Java platform is required to support the following character encodings.
30   * Consult the release documentation for your implementation to see if any other encodings are supported.
31   * </cite></p>
32   *
33   * @see <a href="http://download.oracle.com/javase/1.3/docs/guide/intl/encoding.doc.html">JRE character encoding names</a>
34   * @since 2.1
35   * @version $Id: CharEncoding.java 1459653 2013-03-22 07:37:03Z bayard $
36   */
37  public class CharEncoding {
38  
39      /**
40       * <p>ISO Latin Alphabet #1, also known as ISO-LATIN-1.</p>
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 = "ISO-8859-1";
45  
46      /**
47       * <p>Seven-bit ASCII, also known as ISO646-US, also known as the Basic Latin block
48       * of the Unicode character set.</p>
49       *
50       * <p>Every implementation of the Java platform is required to support this character encoding.</p>
51       */
52      public static final String US_ASCII = "US-ASCII";
53  
54      /**
55       * <p>Sixteen-bit Unicode Transformation Format, byte order specified by a mandatory initial
56       * byte-order mark (either order accepted on input, big-endian used on output).</p>
57       *
58       * <p>Every implementation of the Java platform is required to support this character encoding.</p>
59       */
60      public static final String UTF_16 = "UTF-16";
61  
62      /**
63       * <p>Sixteen-bit Unicode Transformation Format, big-endian byte order.</p>
64       *
65       * <p>Every implementation of the Java platform is required to support this character encoding.</p>
66       */
67      public static final String UTF_16BE = "UTF-16BE";
68  
69      /**
70       * <p>Sixteen-bit Unicode Transformation Format, little-endian byte order.</p>
71       *
72       * <p>Every implementation of the Java platform is required to support this character encoding.</p>
73       */
74      public static final String UTF_16LE = "UTF-16LE";
75  
76      /**
77       * <p>Eight-bit Unicode Transformation Format.</p>
78       *
79       * <p>Every implementation of the Java platform is required to support this character encoding.</p>
80       */
81      public static final String UTF_8 = "UTF-8";
82  
83      //-----------------------------------------------------------------------
84      /**
85       * <p>Returns whether the named charset is supported.</p>
86       *
87       * <p>This is similar to <a
88       * href="http://download.oracle.com/javase/1.4.2/docs/api/java/nio/charset/Charset.html#isSupported%28java.lang.String%29">
89       * java.nio.charset.Charset.isSupported(String)</a> but handles more formats</p>
90       *
91       * @param name  the name of the requested charset; may be either a canonical name or an alias, null returns false
92       * @return {@code true} if the charset is available in the current Java virtual machine
93       */
94      public static boolean isSupported(final String name) {
95          if (name == null) {
96              return false;
97          }
98          try {
99              return Charset.isSupported(name);
100         } catch (final IllegalCharsetNameException ex) {
101             return false;
102         }
103     }
104 
105 }