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