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      /**
66       * @deprecated Use {@link #encoder(Charset)}.
67       */
68      @Deprecated
69      public static CharsetEncoder getEncoder(Charset charset) {
70          return encoder(charset);
71      }
72  
73      /**
74       * Returns a new {@link CharsetEncoder} for the {@link Charset} with specified error actions.
75       *
76       * @param charset The specified charset
77       * @param malformedInputAction The encoder's action for malformed-input errors
78       * @param unmappableCharacterAction The encoder's action for unmappable-character errors
79       * @return The encoder for the specified {@code charset}
80       */
81      public static CharsetEncoder encoder(Charset charset, CodingErrorAction malformedInputAction,
82                                           CodingErrorAction unmappableCharacterAction) {
83          checkNotNull(charset, "charset");
84          CharsetEncoder e = charset.newEncoder();
85          e.onMalformedInput(malformedInputAction).onUnmappableCharacter(unmappableCharacterAction);
86          return e;
87      }
88  
89      /**
90       * Returns a new {@link CharsetEncoder} for the {@link Charset} with the specified error action.
91       *
92       * @param charset The specified charset
93       * @param codingErrorAction The encoder's action for malformed-input and unmappable-character errors
94       * @return The encoder for the specified {@code charset}
95       */
96      public static CharsetEncoder encoder(Charset charset, CodingErrorAction codingErrorAction) {
97          return encoder(charset, codingErrorAction, codingErrorAction);
98      }
99  
100     /**
101      * Returns a cached thread-local {@link CharsetEncoder} for the specified {@link Charset}.
102      *
103      * @param charset The specified charset
104      * @return The encoder for the specified {@code charset}
105      */
106     public static CharsetEncoder encoder(Charset charset) {
107         checkNotNull(charset, "charset");
108 
109         Map<Charset, CharsetEncoder> map = InternalThreadLocalMap.get().charsetEncoderCache();
110         CharsetEncoder e = map.get(charset);
111         if (e != null) {
112             e.reset().onMalformedInput(CodingErrorAction.REPLACE).onUnmappableCharacter(CodingErrorAction.REPLACE);
113             return e;
114         }
115 
116         e = encoder(charset, CodingErrorAction.REPLACE, CodingErrorAction.REPLACE);
117         map.put(charset, e);
118         return e;
119     }
120 
121     /**
122      * @deprecated Use {@link #decoder(Charset)}.
123      */
124     @Deprecated
125     public static CharsetDecoder getDecoder(Charset charset) {
126         return decoder(charset);
127     }
128 
129     /**
130      * Returns a new {@link CharsetDecoder} for the {@link Charset} with specified error actions.
131      *
132      * @param charset The specified charset
133      * @param malformedInputAction The decoder's action for malformed-input errors
134      * @param unmappableCharacterAction The decoder's action for unmappable-character errors
135      * @return The decoder for the specified {@code charset}
136      */
137     public static CharsetDecoder decoder(Charset charset, CodingErrorAction malformedInputAction,
138                                          CodingErrorAction unmappableCharacterAction) {
139         checkNotNull(charset, "charset");
140         CharsetDecoder d = charset.newDecoder();
141         d.onMalformedInput(malformedInputAction).onUnmappableCharacter(unmappableCharacterAction);
142         return d;
143     }
144 
145     /**
146      * Returns a new {@link CharsetDecoder} for the {@link Charset} with the specified error action.
147      *
148      * @param charset The specified charset
149      * @param codingErrorAction The decoder's action for malformed-input and unmappable-character errors
150      * @return The decoder for the specified {@code charset}
151      */
152     public static CharsetDecoder decoder(Charset charset, CodingErrorAction codingErrorAction) {
153         return decoder(charset, codingErrorAction, codingErrorAction);
154     }
155 
156     /**
157      * Returns a cached thread-local {@link CharsetDecoder} for the specified {@link Charset}.
158      *
159      * @param charset The specified charset
160      * @return The decoder for the specified {@code charset}
161      */
162     public static CharsetDecoder decoder(Charset charset) {
163         checkNotNull(charset, "charset");
164 
165         Map<Charset, CharsetDecoder> map = InternalThreadLocalMap.get().charsetDecoderCache();
166         CharsetDecoder d = map.get(charset);
167         if (d != null) {
168             d.reset().onMalformedInput(CodingErrorAction.REPLACE).onUnmappableCharacter(CodingErrorAction.REPLACE);
169             return d;
170         }
171 
172         d = decoder(charset, CodingErrorAction.REPLACE, CodingErrorAction.REPLACE);
173         map.put(charset, d);
174         return d;
175     }
176 
177     private CharsetUtil() {
178         // Unused
179     }
180 }