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
66
67
68 @Deprecated
69 public static CharsetEncoder getEncoder(Charset charset) {
70 return encoder(charset);
71 }
72
73
74
75
76
77
78
79
80
81 public static CharsetEncoder encoder(Charset charset, CodingErrorAction malformedInputAction,
82 CodingErrorAction unmappableCharacterAction) {
83 checkNotNull(charset, "charset");
84 CharsetEncoder e = charset.newEncoder();
85 e.onMalformedInput(malformedInputAction).onUnmappableCharacter(unmappableCharacterAction);
86 return e;
87 }
88
89
90
91
92
93
94
95
96 public static CharsetEncoder encoder(Charset charset, CodingErrorAction codingErrorAction) {
97 return encoder(charset, codingErrorAction, codingErrorAction);
98 }
99
100
101
102
103
104
105
106 public static CharsetEncoder encoder(Charset charset) {
107 checkNotNull(charset, "charset");
108
109 Map<Charset, CharsetEncoder> map = InternalThreadLocalMap.get().charsetEncoderCache();
110 CharsetEncoder e = map.get(charset);
111 if (e != null) {
112 e.reset().onMalformedInput(CodingErrorAction.REPLACE).onUnmappableCharacter(CodingErrorAction.REPLACE);
113 return e;
114 }
115
116 e = encoder(charset, CodingErrorAction.REPLACE, CodingErrorAction.REPLACE);
117 map.put(charset, e);
118 return e;
119 }
120
121
122
123
124 @Deprecated
125 public static CharsetDecoder getDecoder(Charset charset) {
126 return decoder(charset);
127 }
128
129
130
131
132
133
134
135
136
137 public static CharsetDecoder decoder(Charset charset, CodingErrorAction malformedInputAction,
138 CodingErrorAction unmappableCharacterAction) {
139 checkNotNull(charset, "charset");
140 CharsetDecoder d = charset.newDecoder();
141 d.onMalformedInput(malformedInputAction).onUnmappableCharacter(unmappableCharacterAction);
142 return d;
143 }
144
145
146
147
148
149
150
151
152 public static CharsetDecoder decoder(Charset charset, CodingErrorAction codingErrorAction) {
153 return decoder(charset, codingErrorAction, codingErrorAction);
154 }
155
156
157
158
159
160
161
162 public static CharsetDecoder decoder(Charset charset) {
163 checkNotNull(charset, "charset");
164
165 Map<Charset, CharsetDecoder> map = InternalThreadLocalMap.get().charsetDecoderCache();
166 CharsetDecoder d = map.get(charset);
167 if (d != null) {
168 d.reset().onMalformedInput(CodingErrorAction.REPLACE).onUnmappableCharacter(CodingErrorAction.REPLACE);
169 return d;
170 }
171
172 d = decoder(charset, CodingErrorAction.REPLACE, CodingErrorAction.REPLACE);
173 map.put(charset, d);
174 return d;
175 }
176
177 private CharsetUtil() {
178
179 }
180 }