View Javadoc
1   /*
2    * Copyright 2012 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package io.netty.util;
17  
18  import io.netty.util.internal.InternalThreadLocalMap;
19  import static io.netty.util.internal.ObjectUtil.checkNotNull;
20  
21  import java.nio.charset.Charset;
22  import java.nio.charset.CharsetDecoder;
23  import java.nio.charset.CharsetEncoder;
24  import java.nio.charset.CodingErrorAction;
25  import java.util.Map;
26  
27  /**
28   * A utility class that provides various common operations and constants
29   * related with {@link Charset} and its relevant classes.
30   */
31  public final class CharsetUtil {
32  
33      /**
34       * 16-bit UTF (UCS Transformation Format) whose byte order is identified by
35       * an optional byte-order mark
36       */
37      public static final Charset UTF_16 = Charset.forName("UTF-16");
38  
39      /**
40       * 16-bit UTF (UCS Transformation Format) whose byte order is big-endian
41       */
42      public static final Charset UTF_16BE = Charset.forName("UTF-16BE");
43  
44      /**
45       * 16-bit UTF (UCS Transformation Format) whose byte order is little-endian
46       */
47      public static final Charset UTF_16LE = Charset.forName("UTF-16LE");
48  
49      /**
50       * 8-bit UTF (UCS Transformation Format)
51       */
52      public static final Charset UTF_8 = Charset.forName("UTF-8");
53  
54      /**
55       * ISO Latin Alphabet No. 1, as known as <tt>ISO-LATIN-1</tt>
56       */
57      public static final Charset ISO_8859_1 = Charset.forName("ISO-8859-1");
58  
59      /**
60       * 7-bit ASCII, as known as ISO646-US or the Basic Latin block of the
61       * Unicode character set
62       */
63      public static final Charset US_ASCII = Charset.forName("US-ASCII");
64  
65      private static final Charset[] CHARSETS = new Charset[]
66              { UTF_16, UTF_16BE, UTF_16LE, UTF_8, ISO_8859_1, US_ASCII };
67  
68      public static Charset[] values() { return CHARSETS; }
69  
70      /**
71       * @deprecated Use {@link #encoder(Charset)}.
72       */
73      @Deprecated
74      public static CharsetEncoder getEncoder(Charset charset) {
75          return encoder(charset);
76      }
77  
78      /**
79       * Returns a new {@link CharsetEncoder} for the {@link Charset} with specified error actions.
80       *
81       * @param charset The specified charset
82       * @param malformedInputAction The encoder's action for malformed-input errors
83       * @param unmappableCharacterAction The encoder's action for unmappable-character errors
84       * @return The encoder for the specified {@code charset}
85       */
86      public static CharsetEncoder encoder(Charset charset, CodingErrorAction malformedInputAction,
87                                           CodingErrorAction unmappableCharacterAction) {
88          checkNotNull(charset, "charset");
89          CharsetEncoder e = charset.newEncoder();
90          e.onMalformedInput(malformedInputAction).onUnmappableCharacter(unmappableCharacterAction);
91          return e;
92      }
93  
94      /**
95       * Returns a new {@link CharsetEncoder} for the {@link Charset} with the specified error action.
96       *
97       * @param charset The specified charset
98       * @param codingErrorAction The encoder's action for malformed-input and unmappable-character errors
99       * @return The encoder for the specified {@code charset}
100      */
101     public static CharsetEncoder encoder(Charset charset, CodingErrorAction codingErrorAction) {
102         return encoder(charset, codingErrorAction, codingErrorAction);
103     }
104 
105     /**
106      * Returns a cached thread-local {@link CharsetEncoder} for the specified {@link Charset}.
107      *
108      * @param charset The specified charset
109      * @return The encoder for the specified {@code charset}
110      */
111     public static CharsetEncoder encoder(Charset charset) {
112         checkNotNull(charset, "charset");
113 
114         Map<Charset, CharsetEncoder> map = InternalThreadLocalMap.get().charsetEncoderCache();
115         CharsetEncoder e = map.get(charset);
116         if (e != null) {
117             e.reset().onMalformedInput(CodingErrorAction.REPLACE).onUnmappableCharacter(CodingErrorAction.REPLACE);
118             return e;
119         }
120 
121         e = encoder(charset, CodingErrorAction.REPLACE, CodingErrorAction.REPLACE);
122         map.put(charset, e);
123         return e;
124     }
125 
126     /**
127      * @deprecated Use {@link #decoder(Charset)}.
128      */
129     @Deprecated
130     public static CharsetDecoder getDecoder(Charset charset) {
131         return decoder(charset);
132     }
133 
134     /**
135      * Returns a new {@link CharsetDecoder} for the {@link Charset} with specified error actions.
136      *
137      * @param charset The specified charset
138      * @param malformedInputAction The decoder's action for malformed-input errors
139      * @param unmappableCharacterAction The decoder's action for unmappable-character errors
140      * @return The decoder for the specified {@code charset}
141      */
142     public static CharsetDecoder decoder(Charset charset, CodingErrorAction malformedInputAction,
143                                          CodingErrorAction unmappableCharacterAction) {
144         checkNotNull(charset, "charset");
145         CharsetDecoder d = charset.newDecoder();
146         d.onMalformedInput(malformedInputAction).onUnmappableCharacter(unmappableCharacterAction);
147         return d;
148     }
149 
150     /**
151      * Returns a new {@link CharsetDecoder} for the {@link Charset} with the specified error action.
152      *
153      * @param charset The specified charset
154      * @param codingErrorAction The decoder's action for malformed-input and unmappable-character errors
155      * @return The decoder for the specified {@code charset}
156      */
157     public static CharsetDecoder decoder(Charset charset, CodingErrorAction codingErrorAction) {
158         return decoder(charset, codingErrorAction, codingErrorAction);
159     }
160 
161     /**
162      * Returns a cached thread-local {@link CharsetDecoder} for the specified {@link Charset}.
163      *
164      * @param charset The specified charset
165      * @return The decoder for the specified {@code charset}
166      */
167     public static CharsetDecoder decoder(Charset charset) {
168         checkNotNull(charset, "charset");
169 
170         Map<Charset, CharsetDecoder> map = InternalThreadLocalMap.get().charsetDecoderCache();
171         CharsetDecoder d = map.get(charset);
172         if (d != null) {
173             d.reset().onMalformedInput(CodingErrorAction.REPLACE).onUnmappableCharacter(CodingErrorAction.REPLACE);
174             return d;
175         }
176 
177         d = decoder(charset, CodingErrorAction.REPLACE, CodingErrorAction.REPLACE);
178         map.put(charset, d);
179         return d;
180     }
181 
182     private CharsetUtil() { }
183 }