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;
021
022/**
023 * Charsets required of every implementation of the Java platform.
024 * 
025 * From the Java documentation <a href="http://docs.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html">
026 * Standard charsets</a>:
027 * <p>
028 * <cite>Every implementation of the Java platform is required to support the following character encodings. Consult the
029 * release documentation for your implementation to see if any other encodings are supported. Consult the release
030 * documentation for your implementation to see if any other encodings are supported. </cite>
031 * </p>
032 * 
033 * <ul>
034 * <li><code>US-ASCII</code><br/>
035 * Seven-bit ASCII, a.k.a. ISO646-US, a.k.a. the Basic Latin block of the Unicode character set.</li>
036 * <li><code>ISO-8859-1</code><br/>
037 * ISO Latin Alphabet No. 1, a.k.a. ISO-LATIN-1.</li>
038 * <li><code>UTF-8</code><br/>
039 * Eight-bit Unicode Transformation Format.</li>
040 * <li><code>UTF-16BE</code><br/>
041 * Sixteen-bit Unicode Transformation Format, big-endian byte order.</li>
042 * <li><code>UTF-16LE</code><br/>
043 * Sixteen-bit Unicode Transformation Format, little-endian byte order.</li>
044 * <li><code>UTF-16</code><br/>
045 * Sixteen-bit Unicode Transformation Format, byte order specified by a mandatory initial byte-order mark (either order
046 * accepted on input, big-endian used on output.)</li>
047 * </ul>
048 * 
049 * @see <a href="http://docs.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
050 * @since 2.3
051 * @version $Id: Charsets.java 1311751 2012-04-10 14:26:21Z ggregory $
052 */
053public class Charsets {
054    //
055    // This class should only contain Charset instances for required encodings. This guarantees that it will load
056    // correctly and without delay on all Java platforms.
057    //
058
059    /**
060     * Returns the given Charset or the default Charset if the given Charset is null.
061     * 
062     * @param charset
063     *            A charset or null.
064     * @return the given Charset or the default Charset if the given Charset is null
065     */
066    public static Charset toCharset(Charset charset) {
067        return charset == null ? Charset.defaultCharset() : charset;
068    }
069
070    /**
071     * Returns a Charset for the named charset. If the name is null, return the default Charset.
072     * 
073     * @param charset
074     *            The name of the requested charset, may be null.
075     * @return a Charset for the named charset
076     * @throws UnsupportedCharsetException
077     *             If the named charset is unavailable
078     */
079    public static Charset toCharset(String charset) {
080        return charset == null ? Charset.defaultCharset() : Charset.forName(charset);
081    }
082
083    /**
084     * CharEncodingISO Latin Alphabet No. 1, a.k.a. ISO-LATIN-1. </p>
085     * <p>
086     * Every implementation of the Java platform is required to support this character encoding.
087     * </p>
088     * 
089     * @see <a href="http://docs.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
090     */
091    public static final Charset ISO_8859_1 = Charset.forName("ISO-8859-1");
092
093    /**
094     * <p>
095     * Seven-bit ASCII, also known as ISO646-US, also known as the Basic Latin block of the Unicode character set.
096     * </p>
097     * <p>
098     * Every implementation of the Java platform is required to support this character encoding.
099     * </p>
100     * 
101     * @see <a href="http://docs.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
102     */
103    public static final Charset US_ASCII = Charset.forName("US-ASCII");
104
105    /**
106     * <p>
107     * Sixteen-bit Unicode Transformation Format, The byte order specified by a mandatory initial byte-order mark
108     * (either order accepted on input, big-endian used on output)
109     * </p>
110     * <p>
111     * Every implementation of the Java platform is required to support this character encoding.
112     * </p>
113     * 
114     * @see <a href="http://docs.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
115     */
116    public static final Charset UTF_16 = Charset.forName("UTF-16");
117
118    /**
119     * <p>
120     * Sixteen-bit Unicode Transformation Format, big-endian byte order.
121     * </p>
122     * <p>
123     * Every implementation of the Java platform is required to support this character encoding.
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_16BE = Charset.forName("UTF-16BE");
129
130    /**
131     * <p>
132     * Sixteen-bit Unicode Transformation Format, little-endian byte order.
133     * </p>
134     * <p>
135     * Every implementation of the Java platform is required to support this character encoding.
136     * </p>
137     * 
138     * @see <a href="http://docs.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
139     */
140    public static final Charset UTF_16LE = Charset.forName("UTF-16LE");
141
142    /**
143     * <p>
144     * Eight-bit Unicode Transformation Format.
145     * </p>
146     * <p>
147     * Every implementation of the Java platform is required to support this character encoding.
148     * </p>
149     * 
150     * @see <a href="http://docs.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
151     */
152    public static final Charset UTF_8 = Charset.forName("UTF-8");
153}