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 org.jboss.netty.util;
17  
18  import java.nio.charset.Charset;
19  import java.nio.charset.CharsetDecoder;
20  import java.nio.charset.CharsetEncoder;
21  import java.nio.charset.CodingErrorAction;
22  import java.util.IdentityHashMap;
23  import java.util.Map;
24  
25  /**
26   * A utility class that provides various common operations and constants
27   * related with {@link Charset} and its relevant classes.
28   */
29  public final class CharsetUtil {
30  
31      /**
32       * 16-bit UTF (UCS Transformation Format) whose byte order is identified by
33       * an optional byte-order mark
34       */
35      public static final Charset UTF_16 = Charset.forName("UTF-16");
36  
37      /**
38       * 16-bit UTF (UCS Transformation Format) whose byte order is big-endian
39       */
40      public static final Charset UTF_16BE = Charset.forName("UTF-16BE");
41  
42      /**
43       * 16-bit UTF (UCS Transformation Format) whose byte order is little-endian
44       */
45      public static final Charset UTF_16LE = Charset.forName("UTF-16LE");
46  
47      /**
48       * 8-bit UTF (UCS Transformation Format)
49       */
50      public static final Charset UTF_8 = Charset.forName("UTF-8");
51  
52      /**
53       * ISO Latin Alphabet No. 1, as known as <tt>ISO-LATIN-1</tt>
54       */
55      public static final Charset ISO_8859_1 = Charset.forName("ISO-8859-1");
56  
57      /**
58       * 7-bit ASCII, as known as ISO646-US or the Basic Latin block of the
59       * Unicode character set
60       */
61      public static final Charset US_ASCII = Charset.forName("US-ASCII");
62  
63      private static final ThreadLocal<Map<Charset, CharsetEncoder>> encoders =
64          new ThreadLocal<Map<Charset, CharsetEncoder>>() {
65              @Override
66              protected Map<Charset, CharsetEncoder> initialValue() {
67                  return new IdentityHashMap<Charset, CharsetEncoder>();
68              }
69          };
70  
71      private static final ThreadLocal<Map<Charset, CharsetDecoder>> decoders =
72          new ThreadLocal<Map<Charset, CharsetDecoder>>() {
73              @Override
74              protected Map<Charset, CharsetDecoder> initialValue() {
75                  return new IdentityHashMap<Charset, CharsetDecoder>();
76              }
77          };
78  
79      /**
80       * Returns a cached thread-local {@link CharsetEncoder} for the specified
81       * <tt>charset</tt>.
82       */
83      public static CharsetEncoder getEncoder(Charset charset) {
84          if (charset == null) {
85              throw new NullPointerException("charset");
86          }
87  
88          Map<Charset, CharsetEncoder> map = encoders.get();
89          CharsetEncoder e = map.get(charset);
90          if (e != null) {
91              e.reset();
92              e.onMalformedInput(CodingErrorAction.REPLACE);
93              e.onUnmappableCharacter(CodingErrorAction.REPLACE);
94              return e;
95          }
96  
97          e = charset.newEncoder();
98          e.onMalformedInput(CodingErrorAction.REPLACE);
99          e.onUnmappableCharacter(CodingErrorAction.REPLACE);
100         map.put(charset, e);
101         return e;
102     }
103 
104     /**
105      * Returns a cached thread-local {@link CharsetDecoder} for the specified
106      * <tt>charset</tt>.
107      */
108     public static CharsetDecoder getDecoder(Charset charset) {
109         if (charset == null) {
110             throw new NullPointerException("charset");
111         }
112 
113         Map<Charset, CharsetDecoder> map = decoders.get();
114         CharsetDecoder d = map.get(charset);
115         if (d != null) {
116             d.reset();
117             d.onMalformedInput(CodingErrorAction.REPLACE);
118             d.onUnmappableCharacter(CodingErrorAction.REPLACE);
119             return d;
120         }
121 
122         d = charset.newDecoder();
123         d.onMalformedInput(CodingErrorAction.REPLACE);
124         d.onUnmappableCharacter(CodingErrorAction.REPLACE);
125         map.put(charset, d);
126         return d;
127     }
128 
129     private CharsetUtil() {
130         // Unused
131     }
132 }