1 7 8 package java.net; 9 10 import java.io.*; 11 12 60 61 public class URLDecoder { 62 63 static String dfltEncName = URLEncoder.dfltEncName; 65 66 77 @Deprecated 78 public static String decode(String s) { 79 80 String str = null; 81 82 try { 83 str = decode(s, dfltEncName); 84 } catch (UnsupportedEncodingException e) { 85 } 87 88 return str; 89 } 90 91 115 public static String decode(String s, String enc) 116 throws UnsupportedEncodingException{ 117 118 boolean needToChange = false; 119 int numChars = s.length(); 120 StringBuffer sb = new StringBuffer (numChars > 500 ? numChars / 2 : numChars); 121 int i = 0; 122 123 if (enc.length() == 0) { 124 throw new UnsupportedEncodingException ("URLDecoder: empty string enc parameter"); 125 } 126 127 char c; 128 byte[] bytes = null; 129 while (i < numChars) { 130 c = s.charAt(i); 131 switch (c) { 132 case '+': 133 sb.append(' '); 134 i++; 135 needToChange = true; 136 break; 137 case '%': 138 146 147 try { 148 149 if (bytes == null) 152 bytes = new byte[(numChars-i)/3]; 153 int pos = 0; 154 155 while ( ((i+2) < numChars) && 156 (c=='%')) { 157 bytes[pos++] = 158 (byte)Integer.parseInt(s.substring(i+1,i+3),16); 159 i+= 3; 160 if (i < numChars) 161 c = s.charAt(i); 162 } 163 164 167 if ((i < numChars) && (c=='%')) 168 throw new IllegalArgumentException ( 169 "URLDecoder: Incomplete trailing escape (%) pattern"); 170 171 sb.append(new String (bytes, 0, pos, enc)); 172 } catch (NumberFormatException e) { 173 throw new IllegalArgumentException ( 174 "URLDecoder: Illegal hex characters in escape (%) pattern - " 175 + e.getMessage()); 176 } 177 needToChange = true; 178 break; 179 default: 180 sb.append(c); 181 i++; 182 break; 183 } 184 } 185 186 return (needToChange? sb.toString() : s); 187 } 188 } 189 | Popular Tags |