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