001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 * 
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 * 
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.io;
018
019import java.nio.charset.Charset;
020import java.nio.charset.UnsupportedCharsetException;
021import java.util.Collections;
022import java.util.SortedMap;
023import java.util.TreeMap;
024
025/**
026 * Charsets required of every implementation of the Java platform.
027 * 
028 * From the Java documentation <a href="http://docs.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html">
029 * Standard charsets</a>:
030 * <p>
031 * <cite>Every implementation of the Java platform is required to support the following character encodings. Consult
032 * the release documentation for your implementation to see if any other encodings are supported. Consult the release
033 * documentation for your implementation to see if any other encodings are supported. </cite>
034 * </p>
035 * 
036 * <ul>
037 * <li><code>US-ASCII</code><br/>
038 * Seven-bit ASCII, a.k.a. ISO646-US, a.k.a. the Basic Latin block of the Unicode character set.</li>
039 * <li><code>ISO-8859-1</code><br/>
040 * ISO Latin Alphabet No. 1, a.k.a. ISO-LATIN-1.</li>
041 * <li><code>UTF-8</code><br/>
042 * Eight-bit Unicode Transformation Format.</li>
043 * <li><code>UTF-16BE</code><br/>
044 * Sixteen-bit Unicode Transformation Format, big-endian byte order.</li>
045 * <li><code>UTF-16LE</code><br/>
046 * Sixteen-bit Unicode Transformation Format, little-endian byte order.</li>
047 * <li><code>UTF-16</code><br/>
048 * Sixteen-bit Unicode Transformation Format, byte order specified by a mandatory initial byte-order mark (either order
049 * accepted on input, big-endian used on output.)</li>
050 * </ul>
051 * 
052 * @see <a href="http://docs.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
053 * @since 2.3
054 * @version $Id: Charsets.java 1415850 2012-11-30 20:51:39Z ggregory $
055 */
056public class Charsets {
057    //
058    // This class should only contain Charset instances for required encodings. This guarantees that it will load
059    // correctly and without delay on all Java platforms.
060    //
061
062    /**
063     * Constructs a sorted map from canonical charset names to charset objects required of every implementation of the
064     * Java platform.
065     * <p>
066     * From the Java documentation <a href="http://docs.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html">
067     * Standard charsets</a>:
068     * </p>
069     * 
070     * @return An immutable, case-insensitive map from canonical charset names to charset objects.
071     * @see Charset#availableCharsets()
072     * @since 2.5
073     */
074    public static SortedMap<String, Charset> requiredCharsets() {
075        // maybe cache?
076        final TreeMap<String, Charset> m = new TreeMap<String, Charset>(String.CASE_INSENSITIVE_ORDER);
077        m.put(ISO_8859_1.name(), ISO_8859_1);
078        m.put(US_ASCII.name(), US_ASCII);
079        m.put(UTF_16.name(), UTF_16);
080        m.put(UTF_16BE.name(), UTF_16BE);
081        m.put(UTF_16LE.name(), UTF_16LE);
082        m.put(UTF_8.name(), UTF_8);
083        return Collections.unmodifiableSortedMap(m);
084    }
085
086    /**
087     * Returns the given Charset or the default Charset if the given Charset is null.
088     * 
089     * @param charset
090     *            A charset or null.
091     * @return the given Charset or the default Charset if the given Charset is null
092     */
093    public static Charset toCharset(final Charset charset) {
094        return charset == null ? Charset.defaultCharset() : charset;
095    }
096
097    /**
098     * Returns a Charset for the named charset. If the name is null, return the default Charset.
099     * 
100     * @param charset
101     *            The name of the requested charset, may be null.
102     * @return a Charset for the named charset
103     * @throws 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. </p>
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     */
118    public static final Charset ISO_8859_1 = Charset.forName("ISO-8859-1");
119
120    /**
121     * <p>
122     * Seven-bit ASCII, also known as ISO646-US, also known as the Basic Latin block of the Unicode character set.
123     * </p>
124     * <p>
125     * Every implementation of the Java platform is required to support this character encoding.
126     * </p>
127     * 
128     * @see <a href="http://docs.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
129     */
130    public static final Charset US_ASCII = Charset.forName("US-ASCII");
131
132    /**
133     * <p>
134     * Sixteen-bit Unicode Transformation Format, The byte order specified by a mandatory initial byte-order mark
135     * (either order accepted on input, big-endian used on output)
136     * </p>
137     * <p>
138     * Every implementation of the Java platform is required to support this character encoding.
139     * </p>
140     * 
141     * @see <a href="http://docs.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
142     */
143    public static final Charset UTF_16 = Charset.forName("UTF-16");
144
145    /**
146     * <p>
147     * Sixteen-bit Unicode Transformation Format, big-endian byte order.
148     * </p>
149     * <p>
150     * Every implementation of the Java platform is required to support this character encoding.
151     * </p>
152     * 
153     * @see <a href="http://docs.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
154     */
155    public static final Charset UTF_16BE = Charset.forName("UTF-16BE");
156
157    /**
158     * <p>
159     * Sixteen-bit Unicode Transformation Format, little-endian byte order.
160     * </p>
161     * <p>
162     * Every implementation of the Java platform is required to support this character encoding.
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_16LE = Charset.forName("UTF-16LE");
168
169    /**
170     * <p>
171     * Eight-bit Unicode Transformation Format.
172     * </p>
173     * <p>
174     * Every implementation of the Java platform is required to support this character encoding.
175     * </p>
176     * 
177     * @see <a href="http://docs.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
178     */
179    public static final Charset UTF_8 = Charset.forName("UTF-8");
180}