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    *      https://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.codec;
18  
19  import java.nio.charset.Charset;
20  import java.nio.charset.StandardCharsets;
21  
22  /**
23   * Charsets required of every implementation of the Java platform.
24   *
25   * From the Java documentation <a href="https://docs.oracle.com/javase/8/docs/api/java/nio/charset/Charset.html">Standard
26   * charsets</a>:
27   * <p>
28   * <cite>Every implementation of the Java platform is required to support the following character encodings. Consult the
29   * release documentation for your implementation to see if any other encodings are supported. Consult the release
30   * documentation for your implementation to see if any other encodings are supported. </cite>
31   * </p>
32   *
33   * <ul>
34   * <li>{@code US-ASCII}
35   * <p>
36   * Seven-bit ASCII, a.k.a. ISO646-US, a.k.a. the Basic Latin block of the Unicode character set.
37   * </p>
38   * </li>
39   * <li>{@code ISO-8859-1}
40   * <p>
41   * ISO Latin Alphabet No. 1, a.k.a. ISO-LATIN-1.
42   * </p>
43   * </li>
44   * <li>{@code UTF-8}
45   * <p>
46   * Eight-bit Unicode Transformation Format.
47   * </p>
48   * </li>
49   * <li>{@code UTF-16BE}
50   * <p>
51   * Sixteen-bit Unicode Transformation Format, big-endian byte order.
52   * </p>
53   * </li>
54   * <li>{@code UTF-16LE}
55   * <p>
56   * Sixteen-bit Unicode Transformation Format, little-endian byte order.
57   * </p>
58   * </li>
59   * <li>{@code UTF-16}
60   * <p>
61   * Sixteen-bit Unicode Transformation Format, byte order specified by a mandatory initial byte-order mark (either order
62   * accepted on input, big-endian used on output.)
63   * </p>
64   * </li>
65   * </ul>
66   *
67   * This perhaps would best belong in the Commons Lang project. Even if a similar class is defined in Commons Lang, it is
68   * not foreseen that Commons Codec would be made to depend on Commons Lang.
69   *
70   * <p>
71   * This class is immutable and thread-safe.
72   * </p>
73   *
74   * @see <a href="https://docs.oracle.com/javase/8/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
75   * @since 1.7
76   */
77  public class Charsets {
78  
79      //
80      // This class should only contain Charset instances for required encodings. This guarantees that it will load
81      // correctly and without delay on all Java platforms.
82      //
83  
84      /**
85       * CharEncodingISO Latin Alphabet No. 1, a.k.a. ISO-LATIN-1.
86       * <p>
87       * Every implementation of the Java platform is required to support this character encoding.
88       * </p>
89       *
90       * @see <a href="https://docs.oracle.com/javase/8/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
91       * @deprecated Use {@link java.nio.charset.StandardCharsets#ISO_8859_1} instead.
92       */
93      @Deprecated
94      public static final Charset ISO_8859_1 = StandardCharsets.ISO_8859_1;
95  
96      /**
97       * Seven-bit ASCII, also known as ISO646-US, also known as the Basic Latin block of the Unicode character set.
98       * <p>
99       * Every implementation of the Java platform is required to support this character encoding.
100      * </p>
101      *
102      * @see <a href="https://docs.oracle.com/javase/8/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
103      * @deprecated Use {@link java.nio.charset.StandardCharsets#US_ASCII} instead.
104      */
105     @Deprecated
106     public static final Charset US_ASCII = StandardCharsets.US_ASCII;
107 
108     /**
109      * Sixteen-bit Unicode Transformation Format, The byte order specified by a mandatory initial byte-order mark
110      * (either order accepted on input, big-endian used on output)
111      * <p>
112      * Every implementation of the Java platform is required to support this character encoding.
113      * </p>
114      *
115      * @see <a href="https://docs.oracle.com/javase/8/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
116      * @deprecated Use {@link java.nio.charset.StandardCharsets#UTF_16} instead.
117      */
118     @Deprecated
119     public static final Charset UTF_16 = StandardCharsets.UTF_16;
120 
121     /**
122      * Sixteen-bit Unicode Transformation Format, big-endian byte order.
123      * <p>
124      * Every implementation of the Java platform is required to support this character encoding.
125      * </p>
126      *
127      * @see <a href="https://docs.oracle.com/javase/8/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
128      * @deprecated Use {@link java.nio.charset.StandardCharsets#UTF_16BE} instead.
129      */
130     @Deprecated
131     public static final Charset UTF_16BE = StandardCharsets.UTF_16BE;
132 
133     /**
134      * Sixteen-bit Unicode Transformation Format, little-endian byte order.
135      * <p>
136      * Every implementation of the Java platform is required to support this character encoding.
137      * </p>
138      *
139      * @see <a href="https://docs.oracle.com/javase/8/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
140      * @deprecated Use {@link java.nio.charset.StandardCharsets#UTF_16LE} instead.
141      */
142     @Deprecated
143     public static final Charset UTF_16LE = StandardCharsets.UTF_16LE;
144 
145     /**
146      * Eight-bit Unicode Transformation Format.
147      * <p>
148      * Every implementation of the Java platform is required to support this character encoding.
149      * </p>
150      *
151      * @see <a href="https://docs.oracle.com/javase/8/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
152      * @deprecated Use {@link java.nio.charset.StandardCharsets#UTF_8} instead.
153      */
154     @Deprecated
155     public static final Charset UTF_8 = StandardCharsets.UTF_8;
156 
157     /**
158      * Returns the given Charset or the default Charset if the given Charset is null.
159      *
160      * @param charset
161      *            A charset or null.
162      * @return The given Charset or the default Charset if the given Charset is null.
163      */
164     public static Charset toCharset(final Charset charset) {
165         return charset == null ? Charset.defaultCharset() : charset;
166     }
167 
168     /**
169      * Returns a Charset for the named charset. If the name is null, return the default Charset.
170      *
171      * @param charset The name of the requested charset, may be null.
172      * @return A Charset for the named charset.
173      * @throws java.nio.charset.UnsupportedCharsetException Thrown if the named charset is unavailable.
174      */
175     public static Charset toCharset(final String charset) {
176         return charset == null ? Charset.defaultCharset() : Charset.forName(charset);
177     }
178 
179     /**
180      * TODO Make private in 2.0.
181      *
182      * @deprecated TODO Make private in 2.0.
183      */
184     @Deprecated
185     public Charsets() {
186         // empty
187     }
188 }