blob: 8c1559e406b6d5b58042c06075bdc01af5fe8a25 [file] [log] [blame]
Stefan Bodewigc654e042012-04-04 04:51:50 +00001/*
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.
Sebastian Bazley2f69e632013-01-22 17:00:33 +000016 *
Stefan Bodewigc654e042012-04-04 04:51:50 +000017 */
18
19package org.apache.commons.compress.utils;
20
21import java.nio.charset.Charset;
Stefan Bodewigc654e042012-04-04 04:51:50 +000022
23/**
24 * Charsets required of every implementation of the Java platform.
Sebastian Bazley2f69e632013-01-22 17:00:33 +000025 *
Stefan Bodewigc654e042012-04-04 04:51:50 +000026 * From the Java documentation <a href="http://docs.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html">Standard
27 * charsets</a>:
28 * <p>
29 * <cite>Every implementation of the Java platform is required to support the following character encodings. Consult the
30 * release documentation for your implementation to see if any other encodings are supported. Consult the release
31 * documentation for your implementation to see if any other encodings are supported. </cite>
32 * </p>
Sebastian Bazley2f69e632013-01-22 17:00:33 +000033 *
Stefan Bodewig45e51c22013-12-22 07:03:43 +000034 * <dl>
35 * <dt><code>US-ASCII</code></dt>
36 * <dd>Seven-bit ASCII, a.k.a. ISO646-US, a.k.a. the Basic Latin block of the Unicode character set.</dd>
37 * <dt><code>ISO-8859-1</code></dt>
38 * <dd>ISO Latin Alphabet No. 1, a.k.a. ISO-LATIN-1.</dd>
39 * <dt><code>UTF-8</code></dt>
40 * <dd>Eight-bit Unicode Transformation Format.</dd>
41 * <dt><code>UTF-16BE</code></dt>
42 * <dd>Sixteen-bit Unicode Transformation Format, big-endian byte order.</dd>
43 * <dt><code>UTF-16LE</code></dt>
44 * <dd>Sixteen-bit Unicode Transformation Format, little-endian byte order.</dd>
45 * <dt><code>UTF-16</code></dt>
46 * <dd>Sixteen-bit Unicode Transformation Format, byte order specified by a mandatory initial byte-order mark (either order
47 * accepted on input, big-endian used on output.)</dd>
48 * </dl>
Sebastian Bazley2f69e632013-01-22 17:00:33 +000049 *
Stefan Bodewig45e51c22013-12-22 07:03:43 +000050 * <p>This class best belongs in the Commons Lang or IO project. Even if a similar class is defined in another Commons
51 * component, it is not foreseen that Commons Compress would be made to depend on another Commons component.</p>
Sebastian Bazley2f69e632013-01-22 17:00:33 +000052 *
Stefan Bodewigc654e042012-04-04 04:51:50 +000053 * @see <a href="http://docs.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
54 * @since 1.4
55 * @version $Id$
56 */
57public class Charsets {
Sebastian Bazley2f69e632013-01-22 17:00:33 +000058
Stefan Bodewigc654e042012-04-04 04:51:50 +000059 //
60 // This class should only contain Charset instances for required encodings. This guarantees that it will load correctly and
61 // without delay on all Java platforms.
62 //
Sebastian Bazley2f69e632013-01-22 17:00:33 +000063
Stefan Bodewigc654e042012-04-04 04:51:50 +000064 /**
Sebastian Bazley2f69e632013-01-22 17:00:33 +000065 * Returns the given Charset or the default Charset if the given Charset is null.
66 *
Stefan Bodewigc654e042012-04-04 04:51:50 +000067 * @param charset
68 * A charset or null.
69 * @return the given Charset or the default Charset if the given Charset is null
70 */
71 public static Charset toCharset(Charset charset) {
72 return charset == null ? Charset.defaultCharset() : charset;
73 }
74
75 /**
76 * Returns a Charset for the named charset. If the name is null, return the default Charset.
Sebastian Bazley2f69e632013-01-22 17:00:33 +000077 *
Stefan Bodewigc654e042012-04-04 04:51:50 +000078 * @param charset
79 * The name of the requested charset, may be null.
80 * @return a Charset for the named charset
Sebastian Bazleyd2af7002013-01-11 01:52:45 +000081 * @throws java.nio.charset.UnsupportedCharsetException
Stefan Bodewigc654e042012-04-04 04:51:50 +000082 * If the named charset is unavailable
Sebastian Bazleyd2af7002013-01-11 01:52:45 +000083 * @throws java.nio.charset.IllegalCharsetNameException
84 * If the given charset name is illegal
Stefan Bodewigc654e042012-04-04 04:51:50 +000085 */
86 public static Charset toCharset(String charset) {
87 return charset == null ? Charset.defaultCharset() : Charset.forName(charset);
88 }
89
90 /**
Stefan Bodewig45e51c22013-12-22 07:03:43 +000091 * CharsetNamesISO Latin Alphabet No. 1, a.k.a. ISO-LATIN-1.
Stefan Bodewigc654e042012-04-04 04:51:50 +000092 * <p>
93 * Every implementation of the Java platform is required to support this character encoding.
94 * </p>
Sebastian Bazley2f69e632013-01-22 17:00:33 +000095 *
Stefan Bodewigc654e042012-04-04 04:51:50 +000096 * @see <a href="http://docs.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
97 */
98 public static final Charset ISO_8859_1 = Charset.forName(CharsetNames.ISO_8859_1);
99
100 /**
101 * <p>
102 * Seven-bit ASCII, also known as ISO646-US, also known as the Basic Latin block of the Unicode character set.
103 * </p>
104 * <p>
105 * Every implementation of the Java platform is required to support this character encoding.
106 * </p>
Sebastian Bazley2f69e632013-01-22 17:00:33 +0000107 *
Stefan Bodewigc654e042012-04-04 04:51:50 +0000108 * @see <a href="http://docs.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
109 */
110 public static final Charset US_ASCII = Charset.forName(CharsetNames.US_ASCII);
111
112 /**
113 * <p>
114 * Sixteen-bit Unicode Transformation Format, The byte order specified by a mandatory initial byte-order mark
115 * (either order accepted on input, big-endian used on output)
116 * </p>
117 * <p>
118 * Every implementation of the Java platform is required to support this character encoding.
119 * </p>
Sebastian Bazley2f69e632013-01-22 17:00:33 +0000120 *
Stefan Bodewigc654e042012-04-04 04:51:50 +0000121 * @see <a href="http://docs.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
122 */
123 public static final Charset UTF_16 = Charset.forName(CharsetNames.UTF_16);
124
125 /**
126 * <p>
127 * Sixteen-bit Unicode Transformation Format, big-endian byte order.
128 * </p>
129 * <p>
130 * Every implementation of the Java platform is required to support this character encoding.
131 * </p>
Sebastian Bazley2f69e632013-01-22 17:00:33 +0000132 *
Stefan Bodewigc654e042012-04-04 04:51:50 +0000133 * @see <a href="http://docs.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
134 */
135 public static final Charset UTF_16BE = Charset.forName(CharsetNames.UTF_16BE);
136
137 /**
138 * <p>
139 * Sixteen-bit Unicode Transformation Format, little-endian byte order.
140 * </p>
141 * <p>
142 * Every implementation of the Java platform is required to support this character encoding.
143 * </p>
Sebastian Bazley2f69e632013-01-22 17:00:33 +0000144 *
Stefan Bodewigc654e042012-04-04 04:51:50 +0000145 * @see <a href="http://docs.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
146 */
147 public static final Charset UTF_16LE = Charset.forName(CharsetNames.UTF_16LE);
148
149 /**
150 * <p>
151 * Eight-bit Unicode Transformation Format.
152 * </p>
153 * <p>
154 * Every implementation of the Java platform is required to support this character encoding.
155 * </p>
Sebastian Bazley2f69e632013-01-22 17:00:33 +0000156 *
Stefan Bodewigc654e042012-04-04 04:51:50 +0000157 * @see <a href="http://docs.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
158 */
159 public static final Charset UTF_8 = Charset.forName(CharsetNames.UTF_8);
160}