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  package org.miloss.fgsms.common.codec;
18  
19  import java.nio.charset.Charset;
20  
21  /**
22   * Charsets required of every implementation of the Java platform.
23   *
24   * From the Java documentation <a href="http://docs.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html">Standard
25   * charsets</a>:
26   * <p>
27   * <cite>Every implementation of the Java platform is required to support the following character encodings. Consult the
28   * release documentation for your implementation to see if any other encodings are supported. Consult the release
29   * documentation for your implementation to see if any other encodings are supported. </cite>
30   * </p>
31   *
32   * <ul>
33   * <li><code>US-ASCII</code><br>
34   * Seven-bit ASCII, a.k.a. ISO646-US, a.k.a. the Basic Latin block of the Unicode character set.</li>
35   * <li><code>ISO-8859-1</code><br>
36   * ISO Latin Alphabet No. 1, a.k.a. ISO-LATIN-1.</li>
37   * <li><code>UTF-8</code><br>
38   * Eight-bit Unicode Transformation Format.</li>
39   * <li><code>UTF-16BE</code><br>
40   * Sixteen-bit Unicode Transformation Format, big-endian byte order.</li>
41   * <li><code>UTF-16LE</code><br>
42   * Sixteen-bit Unicode Transformation Format, little-endian byte order.</li>
43   * <li><code>UTF-16</code><br>
44   * Sixteen-bit Unicode Transformation Format, byte order specified by a mandatory initial byte-order mark (either order
45   * accepted on input, big-endian used on output.)</li>
46   * </ul>
47   *
48   * This perhaps would best belong in the Commons Lang project. Even if a similar class is defined in Commons Lang, it is
49   * not foreseen that Commons Codec would be made to depend on Commons Lang.
50   *
51   * <p>
52   * This class is immutable and thread-safe.
53   * </p>
54   *
55   * @see <a href="http://docs.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
56   * @since 1.7
57   * @version $Id: CharEncoding.java 1173287 2011-09-20 18:16:19Z ggregory $
58   */
59  public class Charsets {
60  
61      //
62      // This class should only contain Charset instances for required encodings. This guarantees that it will load
63      // correctly and without delay on all Java platforms.
64      //
65  
66      /**
67       * Returns the given Charset or the default Charset if the given Charset is null.
68       *
69       * @param charset
70       *            A charset or null.
71       * @return the given Charset or the default Charset if the given Charset is null
72       */
73      public static Charset toCharset(final Charset charset) {
74          return charset == null ? Charset.defaultCharset() : charset;
75      }
76  
77      /**
78       * Returns a Charset for the named charset. If the name is null, return the default Charset.
79       *
80       * @param charset
81       *            The name of the requested charset, may be null.
82       * @return a Charset for the named charset
83       * @throws java.nio.charset.UnsupportedCharsetException
84       *             If the named charset is unavailable
85       */
86      public static Charset toCharset(final String charset) {
87          return charset == null ? Charset.defaultCharset() : Charset.forName(charset);
88      }
89  
90      /**
91       * CharEncodingISO Latin Alphabet No. 1, a.k.a. ISO-LATIN-1.
92       * <p>
93       * Every implementation of the Java platform is required to support this character encoding.
94       * </p>
95       * <p>
96       * On Java 7 or later, use {@link java.nio.charset.StandardCharsets#ISO_8859_1} instead.
97       * </p>
98       *
99       * @see <a href="http://docs.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
100      */
101     public static final Charset ISO_8859_1 = Charset.forName(CharEncoding.ISO_8859_1);
102 
103     /**
104      * Seven-bit ASCII, also known as ISO646-US, also known as the Basic Latin block of the Unicode character set.
105      * <p>
106      * Every implementation of the Java platform is required to support this character encoding.
107      * </p>
108      * <p>
109      * On Java 7 or later, use {@link java.nio.charset.StandardCharsets#ISO_8859_1} instead.
110      * </p>
111      *
112      * @see <a href="http://docs.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
113      */
114     public static final Charset US_ASCII = Charset.forName(CharEncoding.US_ASCII);
115 
116     /**
117      * Sixteen-bit Unicode Transformation Format, The byte order specified by a mandatory initial byte-order mark
118      * (either order accepted on input, big-endian used on output)
119      * <p>
120      * Every implementation of the Java platform is required to support this character encoding.
121      * </p>
122      * <p>
123      * On Java 7 or later, use {@link java.nio.charset.StandardCharsets#ISO_8859_1} instead.
124      * </p>
125      *
126      * @see <a href="http://docs.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
127      */
128     public static final Charset UTF_16 = Charset.forName(CharEncoding.UTF_16);
129 
130     /**
131      * Sixteen-bit Unicode Transformation Format, big-endian byte order.
132      * <p>
133      * Every implementation of the Java platform is required to support this character encoding.
134      * </p>
135      * <p>
136      * On Java 7 or later, use {@link java.nio.charset.StandardCharsets#ISO_8859_1} instead.
137      * </p>
138      *
139      * @see <a href="http://docs.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
140      */
141     public static final Charset UTF_16BE = Charset.forName(CharEncoding.UTF_16BE);
142 
143     /**
144      * Sixteen-bit Unicode Transformation Format, little-endian byte order.
145      * <p>
146      * Every implementation of the Java platform is required to support this character encoding.
147      * </p>
148      * <p>
149      * On Java 7 or later, use {@link java.nio.charset.StandardCharsets#ISO_8859_1} instead.
150      * </p>
151      *
152      * @see <a href="http://docs.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
153      */
154     public static final Charset UTF_16LE = Charset.forName(CharEncoding.UTF_16LE);
155 
156     /**
157      * Eight-bit Unicode Transformation Format.
158      * <p>
159      * Every implementation of the Java platform is required to support this character encoding.
160      * </p>
161      * <p>
162      * On Java 7 or later, use {@link java.nio.charset.StandardCharsets#ISO_8859_1} instead.
163      * </p>
164      *
165      * @see <a href="http://docs.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
166      */
167     public static final Charset UTF_8 = Charset.forName(CharEncoding.UTF_8);
168 }