1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
29
30
31 public final class CharsetUtil {
32
33
34
35
36
37 public static final Charset UTF_16 = Charset.forName("UTF-16");
38
39
40
41
42 public static final Charset UTF_16BE = Charset.forName("UTF-16BE");
43
44
45
46
47 public static final Charset UTF_16LE = Charset.forName("UTF-16LE");
48
49
50
51
52 public static final Charset UTF_8 = Charset.forName("UTF-8");
53
54
55
56
57 public static final Charset ISO_8859_1 = Charset.forName("ISO-8859-1");
58
59
60
61
62
63 public static final Charset US_ASCII = Charset.forName("US-ASCII");
64
65 private static final Charset[] CHARSETS = new Charset[]
66 { UTF_16, UTF_16BE, UTF_16LE, UTF_8, ISO_8859_1, US_ASCII };
67
68 public static Charset[] values() {
69 return CHARSETS;
70 }
71
72
73
74
75 @Deprecated
76 public static CharsetEncoder getEncoder(Charset charset) {
77 return encoder(charset);
78 }
79
80
81
82
83
84
85
86
87
88 public static CharsetEncoder encoder(Charset charset, CodingErrorAction malformedInputAction,
89 CodingErrorAction unmappableCharacterAction) {
90 checkNotNull(charset, "charset");
91 CharsetEncoder e = charset.newEncoder();
92 e.onMalformedInput(malformedInputAction).onUnmappableCharacter(unmappableCharacterAction);
93 return e;
94 }
95
96
97
98
99
100
101
102
103 public static CharsetEncoder encoder(Charset charset, CodingErrorAction codingErrorAction) {
104 return encoder(charset, codingErrorAction, codingErrorAction);
105 }
106
107
108
109
110
111
112
113 public static CharsetEncoder encoder(Charset charset) {
114 checkNotNull(charset, "charset");
115
116 Map<Charset, CharsetEncoder> map = InternalThreadLocalMap.get().charsetEncoderCache();
117 CharsetEncoder e = map.get(charset);
118 if (e != null) {
119 e.reset().onMalformedInput(CodingErrorAction.REPLACE).onUnmappableCharacter(CodingErrorAction.REPLACE);
120 return e;
121 }
122
123 e = encoder(charset, CodingErrorAction.REPLACE, CodingErrorAction.REPLACE);
124 map.put(charset, e);
125 return e;
126 }
127
128
129
130
131 @Deprecated
132 public static CharsetDecoder getDecoder(Charset charset) {
133 return decoder(charset);
134 }
135
136
137
138
139
140
141
142
143
144 public static CharsetDecoder decoder(Charset charset, CodingErrorAction malformedInputAction,
145 CodingErrorAction unmappableCharacterAction) {
146 checkNotNull(charset, "charset");
147 CharsetDecoder d = charset.newDecoder();
148 d.onMalformedInput(malformedInputAction).onUnmappableCharacter(unmappableCharacterAction);
149 return d;
150 }
151
152
153
154
155
156
157
158
159 public static CharsetDecoder decoder(Charset charset, CodingErrorAction codingErrorAction) {
160 return decoder(charset, codingErrorAction, codingErrorAction);
161 }
162
163
164
165
166
167
168
169 public static CharsetDecoder decoder(Charset charset) {
170 checkNotNull(charset, "charset");
171
172 Map<Charset, CharsetDecoder> map = InternalThreadLocalMap.get().charsetDecoderCache();
173 CharsetDecoder d = map.get(charset);
174 if (d != null) {
175 d.reset().onMalformedInput(CodingErrorAction.REPLACE).onUnmappableCharacter(CodingErrorAction.REPLACE);
176 return d;
177 }
178
179 d = decoder(charset, CodingErrorAction.REPLACE, CodingErrorAction.REPLACE);
180 map.put(charset, d);
181 return d;
182 }
183
184 private CharsetUtil() { }
185 }