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