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