1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
27
28
29 public final class CharsetUtil {
30
31
32
33
34
35 public static final Charset UTF_16 = Charset.forName("UTF-16");
36
37
38
39
40 public static final Charset UTF_16BE = Charset.forName("UTF-16BE");
41
42
43
44
45 public static final Charset UTF_16LE = Charset.forName("UTF-16LE");
46
47
48
49
50 public static final Charset UTF_8 = Charset.forName("UTF-8");
51
52
53
54
55 public static final Charset ISO_8859_1 = Charset.forName("ISO-8859-1");
56
57
58
59
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
81
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
106
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
131 }
132 }