1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.codec.net;
19
20 import java.io.ByteArrayOutputStream;
21 import java.io.UnsupportedEncodingException;
22 import java.util.BitSet;
23
24 import org.apache.commons.codec.BinaryDecoder;
25 import org.apache.commons.codec.BinaryEncoder;
26 import org.apache.commons.codec.CharEncoding;
27 import org.apache.commons.codec.DecoderException;
28 import org.apache.commons.codec.EncoderException;
29 import org.apache.commons.codec.StringDecoder;
30 import org.apache.commons.codec.StringEncoder;
31 import org.apache.commons.codec.binary.StringUtils;
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49 public class URLCodec implements BinaryEncoder, BinaryDecoder, StringEncoder, StringDecoder {
50
51
52
53
54 protected static final byte ESCAPE_CHAR = '%';
55
56
57
58
59
60
61
62 @Deprecated
63 protected static final BitSet WWW_FORM_URL;
64
65 private static final BitSet WWW_FORM_URL_SAFE = new BitSet(256);
66
67
68 static {
69
70 for (int i = 'a'; i <= 'z'; i++) {
71 WWW_FORM_URL_SAFE.set(i);
72 }
73 for (int i = 'A'; i <= 'Z'; i++) {
74 WWW_FORM_URL_SAFE.set(i);
75 }
76
77 for (int i = '0'; i <= '9'; i++) {
78 WWW_FORM_URL_SAFE.set(i);
79 }
80
81 WWW_FORM_URL_SAFE.set('-');
82 WWW_FORM_URL_SAFE.set('_');
83 WWW_FORM_URL_SAFE.set('.');
84 WWW_FORM_URL_SAFE.set('*');
85
86 WWW_FORM_URL_SAFE.set(' ');
87
88
89 WWW_FORM_URL = (BitSet) WWW_FORM_URL_SAFE.clone();
90 }
91
92
93
94
95
96
97
98
99
100
101
102 public static final byte[] decodeUrl(final byte[] bytes) throws DecoderException {
103 if (bytes == null) {
104 return null;
105 }
106 final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
107 for (int i = 0; i < bytes.length; i++) {
108 final int b = bytes[i];
109 if (b == '+') {
110 buffer.write(' ');
111 } else if (b == ESCAPE_CHAR) {
112 try {
113 final int u = Utils.digit16(bytes[++i]);
114 final int l = Utils.digit16(bytes[++i]);
115 buffer.write((char) ((u << 4) + l));
116 } catch (final ArrayIndexOutOfBoundsException e) {
117 throw new DecoderException("Invalid URL encoding: ", e);
118 }
119 } else {
120 buffer.write(b);
121 }
122 }
123 return buffer.toByteArray();
124 }
125
126
127
128
129
130
131
132
133
134
135 public static final byte[] encodeUrl(BitSet urlsafe, final byte[] bytes) {
136 if (bytes == null) {
137 return null;
138 }
139 if (urlsafe == null) {
140 urlsafe = WWW_FORM_URL_SAFE;
141 }
142
143 final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
144 for (final byte c : bytes) {
145 int b = c;
146 if (b < 0) {
147 b = 256 + b;
148 }
149 if (urlsafe.get(b)) {
150 if (b == ' ') {
151 b = '+';
152 }
153 buffer.write(b);
154 } else {
155 buffer.write(ESCAPE_CHAR);
156 final char hex1 = Utils.hexDigit(b >> 4);
157 final char hex2 = Utils.hexDigit(b);
158 buffer.write(hex1);
159 buffer.write(hex2);
160 }
161 }
162 return buffer.toByteArray();
163 }
164
165
166
167
168
169
170 @Deprecated
171 protected volatile String charset;
172
173
174
175
176 public URLCodec() {
177 this(CharEncoding.UTF_8);
178 }
179
180
181
182
183
184
185 public URLCodec(final String charset) {
186 this.charset = charset;
187 }
188
189
190
191
192
193
194
195
196
197
198
199 @Override
200 public byte[] decode(final byte[] bytes) throws DecoderException {
201 return decodeUrl(bytes);
202 }
203
204
205
206
207
208
209
210
211
212
213
214
215 @Override
216 public Object decode(final Object obj) throws DecoderException {
217 if (obj == null) {
218 return null;
219 }
220 if (obj instanceof byte[]) {
221 return decode((byte[]) obj);
222 }
223 if (obj instanceof String) {
224 return decode((String) obj);
225 }
226 throw new DecoderException("Objects of type " + obj.getClass().getName() + " cannot be URL decoded");
227 }
228
229
230
231
232
233
234
235
236
237
238
239
240 @Override
241 public String decode(final String str) throws DecoderException {
242 if (str == null) {
243 return null;
244 }
245 try {
246 return decode(str, getDefaultCharset());
247 } catch (final UnsupportedEncodingException e) {
248 throw new DecoderException(e.getMessage(), e);
249 }
250 }
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266 public String decode(final String str, final String charsetName)
267 throws DecoderException, UnsupportedEncodingException {
268 if (str == null) {
269 return null;
270 }
271 return new String(decode(StringUtils.getBytesUsAscii(str)), charsetName);
272 }
273
274
275
276
277
278
279
280
281 @Override
282 public byte[] encode(final byte[] bytes) {
283 return encodeUrl(WWW_FORM_URL_SAFE, bytes);
284 }
285
286
287
288
289
290
291
292
293
294
295 @Override
296 public Object encode(final Object obj) throws EncoderException {
297 if (obj == null) {
298 return null;
299 }
300 if (obj instanceof byte[]) {
301 return encode((byte[]) obj);
302 }
303 if (obj instanceof String) {
304 return encode((String) obj);
305 }
306 throw new EncoderException("Objects of type " + obj.getClass().getName() + " cannot be URL encoded");
307 }
308
309
310
311
312
313
314
315
316
317
318
319
320 @Override
321 public String encode(final String str) throws EncoderException {
322 if (str == null) {
323 return null;
324 }
325 try {
326 return encode(str, getDefaultCharset());
327 } catch (final UnsupportedEncodingException e) {
328 throw new EncoderException(e.getMessage(), e);
329 }
330 }
331
332
333
334
335
336
337
338
339
340
341
342
343 public String encode(final String str, final String charsetName) throws UnsupportedEncodingException {
344 if (str == null) {
345 return null;
346 }
347 return StringUtils.newStringUsAscii(encode(str.getBytes(charsetName)));
348 }
349
350
351
352
353
354
355 public String getDefaultCharset() {
356 return this.charset;
357 }
358
359
360
361
362
363
364
365
366 @Deprecated
367 public String getEncoding() {
368 return this.charset;
369 }
370
371 }