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.apache.commons.io;
18  
19  import java.nio.charset.Charset;
20  import java.nio.charset.UnsupportedCharsetException;
21  import java.util.Collections;
22  import java.util.SortedMap;
23  import java.util.TreeMap;
24  
25  /**
26   * Charsets required of every implementation of the Java platform.
27   * 
28   * From the Java documentation <a href="http://docs.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html">
29   * Standard charsets</a>:
30   * <p>
31   * <cite>Every implementation of the Java platform is required to support the following character encodings. Consult
32   * the release documentation for your implementation to see if any other encodings are supported. Consult the release
33   * documentation for your implementation to see if any other encodings are supported. </cite>
34   * </p>
35   * 
36   * <ul>
37   * <li><code>US-ASCII</code><br>
38   * Seven-bit ASCII, a.k.a. ISO646-US, a.k.a. the Basic Latin block of the Unicode character set.</li>
39   * <li><code>ISO-8859-1</code><br>
40   * ISO Latin Alphabet No. 1, a.k.a. ISO-LATIN-1.</li>
41   * <li><code>UTF-8</code><br>
42   * Eight-bit Unicode Transformation Format.</li>
43   * <li><code>UTF-16BE</code><br>
44   * Sixteen-bit Unicode Transformation Format, big-endian byte order.</li>
45   * <li><code>UTF-16LE</code><br>
46   * Sixteen-bit Unicode Transformation Format, little-endian byte order.</li>
47   * <li><code>UTF-16</code><br>
48   * Sixteen-bit Unicode Transformation Format, byte order specified by a mandatory initial byte-order mark (either order
49   * accepted on input, big-endian used on output.)</li>
50   * </ul>
51   * 
52   * @see <a href="http://docs.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
53   * @since 2.3
54   * @version $Id: Charsets.java 1563227 2014-01-31 19:45:30Z ggregory $
55   */
56  public class Charsets {
57      //
58      // This class should only contain Charset instances for required encodings. This guarantees that it will load
59      // correctly and without delay on all Java platforms.
60      //
61  
62      /**
63       * Constructs a sorted map from canonical charset names to charset objects required of every implementation of the
64       * Java platform.
65       * <p>
66       * From the Java documentation <a href="http://docs.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html">
67       * Standard charsets</a>:
68       * </p>
69       * 
70       * @return An immutable, case-insensitive map from canonical charset names to charset objects.
71       * @see Charset#availableCharsets()
72       * @since 2.5
73       */
74      public static SortedMap<String, Charset> requiredCharsets() {
75          // maybe cache?
76          // TODO Re-implement on Java 7 to use java.nio.charset.StandardCharsets
77          final TreeMap<String, Charset> m = new TreeMap<String, Charset>(String.CASE_INSENSITIVE_ORDER);
78          m.put(ISO_8859_1.name(), ISO_8859_1);
79          m.put(US_ASCII.name(), US_ASCII);
80          m.put(UTF_16.name(), UTF_16);
81          m.put(UTF_16BE.name(), UTF_16BE);
82          m.put(UTF_16LE.name(), UTF_16LE);
83          m.put(UTF_8.name(), UTF_8);
84          return Collections.unmodifiableSortedMap(m);
85      }
86  
87      /**
88       * Returns the given Charset or the default Charset if the given Charset is null.
89       * 
90       * @param charset
91       *            A charset or null.
92       * @return the given Charset or the default Charset if the given Charset is null
93       */
94      public static Charset toCharset(final Charset charset) {
95          return charset == null ? Charset.defaultCharset() : charset;
96      }
97  
98      /**
99       * Returns a Charset for the named charset. If the name is null, return the default Charset.
100      * 
101      * @param charset
102      *            The name of the requested charset, may be null.
103      * @return a Charset for the named charset
104      * @throws UnsupportedCharsetException
105      *             If the named charset is unavailable
106      */
107     public static Charset toCharset(final String charset) {
108         return charset == null ? Charset.defaultCharset() : Charset.forName(charset);
109     }
110 
111     /**
112      * CharEncodingISO Latin Alphabet No. 1, a.k.a. ISO-LATIN-1.
113      * <p>
114      * Every implementation of the Java platform is required to support this character encoding.
115      * </p>
116      * 
117      * @see <a href="http://docs.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
118      * @deprecated Use Java 7's {@link java.nio.charset.StandardCharsets}
119      */
120     @Deprecated
121     public static final Charset ISO_8859_1 = Charset.forName("ISO-8859-1");
122 
123     /**
124      * <p>
125      * Seven-bit ASCII, also known as ISO646-US, also known as the Basic Latin block of the Unicode character set.
126      * </p>
127      * <p>
128      * Every implementation of the Java platform is required to support this character encoding.
129      * </p>
130      * 
131      * @see <a href="http://docs.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
132      * @deprecated Use Java 7's {@link java.nio.charset.StandardCharsets}
133      */
134     @Deprecated
135     public static final Charset US_ASCII = Charset.forName("US-ASCII");
136 
137     /**
138      * <p>
139      * Sixteen-bit Unicode Transformation Format, The byte order specified by a mandatory initial byte-order mark
140      * (either order accepted on input, big-endian used on output)
141      * </p>
142      * <p>
143      * Every implementation of the Java platform is required to support this character encoding.
144      * </p>
145      * 
146      * @see <a href="http://docs.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
147      * @deprecated Use Java 7's {@link java.nio.charset.StandardCharsets}
148      */
149     @Deprecated
150     public static final Charset UTF_16 = Charset.forName("UTF-16");
151 
152     /**
153      * <p>
154      * Sixteen-bit Unicode Transformation Format, big-endian byte order.
155      * </p>
156      * <p>
157      * Every implementation of the Java platform is required to support this character encoding.
158      * </p>
159      * 
160      * @see <a href="http://docs.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
161      * @deprecated Use Java 7's {@link java.nio.charset.StandardCharsets}
162      */
163     @Deprecated
164     public static final Charset UTF_16BE = Charset.forName("UTF-16BE");
165 
166     /**
167      * <p>
168      * Sixteen-bit Unicode Transformation Format, little-endian byte order.
169      * </p>
170      * <p>
171      * Every implementation of the Java platform is required to support this character encoding.
172      * </p>
173      * 
174      * @see <a href="http://docs.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
175      * @deprecated Use Java 7's {@link java.nio.charset.StandardCharsets}
176      */
177     @Deprecated
178     public static final Charset UTF_16LE = Charset.forName("UTF-16LE");
179 
180     /**
181      * <p>
182      * Eight-bit Unicode Transformation Format.
183      * </p>
184      * <p>
185      * Every implementation of the Java platform is required to support this character encoding.
186      * </p>
187      * 
188      * @see <a href="http://docs.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
189      * @deprecated Use Java 7's {@link java.nio.charset.StandardCharsets}
190      */
191     @Deprecated
192     public static final Charset UTF_8 = Charset.forName("UTF-8");
193 }