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
30
31
32
33 public class CharsetUtil {
34
35
36
37
38
39 public static final Charset UTF_16 = Charset.forName("UTF-16");
40
41
42
43
44 public static final Charset UTF_16BE = Charset.forName("UTF-16BE");
45
46
47
48
49 public static final Charset UTF_16LE = Charset.forName("UTF-16LE");
50
51
52
53
54 public static final Charset UTF_8 = Charset.forName("UTF-8");
55
56
57
58
59 public static final Charset ISO_8859_1 = Charset.forName("ISO-8859-1");
60
61
62
63
64
65 public static final Charset US_ASCII = Charset.forName("US-ASCII");
66
67 private static final ThreadLocal<Map<Charset, CharsetEncoder>> encoders =
68 new ThreadLocal<Map<Charset,CharsetEncoder>>() {
69 @Override
70 protected Map<Charset, CharsetEncoder> initialValue() {
71 return new IdentityHashMap<Charset, CharsetEncoder>();
72 }
73 };
74
75 private static final ThreadLocal<Map<Charset, CharsetDecoder>> decoders =
76 new ThreadLocal<Map<Charset,CharsetDecoder>>() {
77 @Override
78 protected Map<Charset, CharsetDecoder> initialValue() {
79 return new IdentityHashMap<Charset, CharsetDecoder>();
80 }
81 };
82
83
84
85
86
87 public static CharsetEncoder getEncoder(Charset charset) {
88 if (charset == null) {
89 throw new NullPointerException("charset");
90 }
91
92 Map<Charset, CharsetEncoder> map = encoders.get();
93 CharsetEncoder e = map.get(charset);
94 if (e != null) {
95 e.reset();
96 e.onMalformedInput(CodingErrorAction.REPLACE);
97 e.onUnmappableCharacter(CodingErrorAction.REPLACE);
98 return e;
99 }
100
101 e = charset.newEncoder();
102 e.onMalformedInput(CodingErrorAction.REPLACE);
103 e.onUnmappableCharacter(CodingErrorAction.REPLACE);
104 map.put(charset, e);
105 return e;
106 }
107
108
109
110
111
112 public static CharsetDecoder getDecoder(Charset charset) {
113 if (charset == null) {
114 throw new NullPointerException("charset");
115 }
116
117 Map<Charset, CharsetDecoder> map = decoders.get();
118 CharsetDecoder d = map.get(charset);
119 if (d != null) {
120 d.reset();
121 d.onMalformedInput(CodingErrorAction.REPLACE);
122 d.onUnmappableCharacter(CodingErrorAction.REPLACE);
123 return d;
124 }
125
126 d = charset.newDecoder();
127 d.onMalformedInput(CodingErrorAction.REPLACE);
128 d.onUnmappableCharacter(CodingErrorAction.REPLACE);
129 map.put(charset, d);
130 return d;
131 }
132
133 private CharsetUtil() {
134
135 }
136 }