Source code: com/lutris/http/URLDecoder.java
1 /*
2 * Enhydra Java Application Server Project
3 *
4 * The contents of this file are subject to the Enhydra Public License
5 * Version 1.1 (the "License"); you may not use this file except in
6 * compliance with the License. You may obtain a copy of the License on
7 * the Enhydra web site ( http://www.enhydra.org/ ).
8 *
9 * Software distributed under the License is distributed on an "AS IS"
10 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11 * the License for the specific terms governing rights and limitations
12 * under the License.
13 *
14 * The Initial Developer of the Enhydra Application Server is Lutris
15 * Technologies, Inc. The Enhydra Application Server and portions created
16 * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17 * All Rights Reserved.
18 *
19 * Contributor(s):
20 *
21 * $Id: URLDecoder.java,v 1.7.12.1 2000/10/19 17:58:53 jasona Exp $
22 */
23
24
25
26
27 package com.lutris.http;
28
29 /**
30 * This class contains a utility method for converting a <code>String</code>
31 * from the MIME <code>"x-www-form-urlencoded"</code> into its original
32 * format. This class implements the inverse functionality of
33 * <code>java.net.URLEncoder</code>.
34 *
35 * @see java.net.URLEncoder
36 */
37 public class URLDecoder {
38 /**
39 * Decode a urlencoded string by replacing '+' with space ' ',
40 * and "%xx" to the Latin1 character specified by the hex digits
41 * "xx". The input string is assumed to have been broken up into
42 * either a key or a value pair, so '=', '?', and '&' are not
43 * treated as separators. This method implements the inverse
44 * functionality of <code>java.net.URLEncoder.encode</code>.
45 *
46 * @param s The string to decode.
47 * @return The decoded string.
48 *
49 * @see java.net.URLEncoder#encode
50 */
51 public static
52 String decode(String s)
53 {
54 StringBuffer buf = new StringBuffer();
55 char[] chars = s.toCharArray();
56 int i = 0, len = s.length();
57 while (i < len) {
58 char ch = chars[i];
59 switch (ch) {
60 case '+':
61 buf.append(' ');
62 i++;
63 break;
64 case '%':
65 if (i < (len - 2)) {
66 int hi = Character.digit(chars[i+1],16);
67 int lo = Character.digit(chars[i+2],16);
68 if ((hi < 0) || (lo < 0)) {
69 buf.append(ch);
70 i++;
71 } else {
72 buf.append((char)((hi << 4) + lo));
73 i += 3;
74 }
75 } else {
76 buf.append(ch);
77 i++;
78 }
79 break;
80 default:
81 buf.append(ch);
82 i++;
83 break;
84 }
85 }
86 return new String(buf);
87 }
88 }