Stefan Bodewig | c654e04 | 2012-04-04 04:51:50 +0000 | [diff] [blame] | 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. |
Sebastian Bazley | 2f69e63 | 2013-01-22 17:00:33 +0000 | [diff] [blame] | 16 | * |
Stefan Bodewig | c654e04 | 2012-04-04 04:51:50 +0000 | [diff] [blame] | 17 | */ |
| 18 | |
| 19 | package org.apache.commons.compress.utils; |
| 20 | |
| 21 | import java.nio.charset.Charset; |
Stefan Bodewig | c654e04 | 2012-04-04 04:51:50 +0000 | [diff] [blame] | 22 | |
| 23 | /** |
| 24 | * Charsets required of every implementation of the Java platform. |
Sebastian Bazley | 2f69e63 | 2013-01-22 17:00:33 +0000 | [diff] [blame] | 25 | * |
Stefan Bodewig | c654e04 | 2012-04-04 04:51:50 +0000 | [diff] [blame] | 26 | * 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 Bazley | 2f69e63 | 2013-01-22 17:00:33 +0000 | [diff] [blame] | 33 | * |
Stefan Bodewig | 45e51c2 | 2013-12-22 07:03:43 +0000 | [diff] [blame^] | 34 | * <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 Bazley | 2f69e63 | 2013-01-22 17:00:33 +0000 | [diff] [blame] | 49 | * |
Stefan Bodewig | 45e51c2 | 2013-12-22 07:03:43 +0000 | [diff] [blame^] | 50 | * <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 Bazley | 2f69e63 | 2013-01-22 17:00:33 +0000 | [diff] [blame] | 52 | * |
Stefan Bodewig | c654e04 | 2012-04-04 04:51:50 +0000 | [diff] [blame] | 53 | * @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 | */ |
| 57 | public class Charsets { |
Sebastian Bazley | 2f69e63 | 2013-01-22 17:00:33 +0000 | [diff] [blame] | 58 | |
Stefan Bodewig | c654e04 | 2012-04-04 04:51:50 +0000 | [diff] [blame] | 59 | // |
| 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 Bazley | 2f69e63 | 2013-01-22 17:00:33 +0000 | [diff] [blame] | 63 | |
Stefan Bodewig | c654e04 | 2012-04-04 04:51:50 +0000 | [diff] [blame] | 64 | /** |
Sebastian Bazley | 2f69e63 | 2013-01-22 17:00:33 +0000 | [diff] [blame] | 65 | * Returns the given Charset or the default Charset if the given Charset is null. |
| 66 | * |
Stefan Bodewig | c654e04 | 2012-04-04 04:51:50 +0000 | [diff] [blame] | 67 | * @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 Bazley | 2f69e63 | 2013-01-22 17:00:33 +0000 | [diff] [blame] | 77 | * |
Stefan Bodewig | c654e04 | 2012-04-04 04:51:50 +0000 | [diff] [blame] | 78 | * @param charset |
| 79 | * The name of the requested charset, may be null. |
| 80 | * @return a Charset for the named charset |
Sebastian Bazley | d2af700 | 2013-01-11 01:52:45 +0000 | [diff] [blame] | 81 | * @throws java.nio.charset.UnsupportedCharsetException |
Stefan Bodewig | c654e04 | 2012-04-04 04:51:50 +0000 | [diff] [blame] | 82 | * If the named charset is unavailable |
Sebastian Bazley | d2af700 | 2013-01-11 01:52:45 +0000 | [diff] [blame] | 83 | * @throws java.nio.charset.IllegalCharsetNameException |
| 84 | * If the given charset name is illegal |
Stefan Bodewig | c654e04 | 2012-04-04 04:51:50 +0000 | [diff] [blame] | 85 | */ |
| 86 | public static Charset toCharset(String charset) { |
| 87 | return charset == null ? Charset.defaultCharset() : Charset.forName(charset); |
| 88 | } |
| 89 | |
| 90 | /** |
Stefan Bodewig | 45e51c2 | 2013-12-22 07:03:43 +0000 | [diff] [blame^] | 91 | * CharsetNamesISO Latin Alphabet No. 1, a.k.a. ISO-LATIN-1. |
Stefan Bodewig | c654e04 | 2012-04-04 04:51:50 +0000 | [diff] [blame] | 92 | * <p> |
| 93 | * Every implementation of the Java platform is required to support this character encoding. |
| 94 | * </p> |
Sebastian Bazley | 2f69e63 | 2013-01-22 17:00:33 +0000 | [diff] [blame] | 95 | * |
Stefan Bodewig | c654e04 | 2012-04-04 04:51:50 +0000 | [diff] [blame] | 96 | * @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 Bazley | 2f69e63 | 2013-01-22 17:00:33 +0000 | [diff] [blame] | 107 | * |
Stefan Bodewig | c654e04 | 2012-04-04 04:51:50 +0000 | [diff] [blame] | 108 | * @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 Bazley | 2f69e63 | 2013-01-22 17:00:33 +0000 | [diff] [blame] | 120 | * |
Stefan Bodewig | c654e04 | 2012-04-04 04:51:50 +0000 | [diff] [blame] | 121 | * @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 Bazley | 2f69e63 | 2013-01-22 17:00:33 +0000 | [diff] [blame] | 132 | * |
Stefan Bodewig | c654e04 | 2012-04-04 04:51:50 +0000 | [diff] [blame] | 133 | * @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 Bazley | 2f69e63 | 2013-01-22 17:00:33 +0000 | [diff] [blame] | 144 | * |
Stefan Bodewig | c654e04 | 2012-04-04 04:51:50 +0000 | [diff] [blame] | 145 | * @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 Bazley | 2f69e63 | 2013-01-22 17:00:33 +0000 | [diff] [blame] | 156 | * |
Stefan Bodewig | c654e04 | 2012-04-04 04:51:50 +0000 | [diff] [blame] | 157 | * @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 | } |