Decodes the string argument which is assumed to be encoded in the x-www-form-urlencoded MIME content type using the UTF-8 encoding scheme. '%' and two following hex digit characters are converted to the equivalent byte value. All other characters are passed through unmodified. e.g. "
(String s)
| 182 | * @return java.lang.String The decoded version. |
| 183 | */ |
| 184 | static String decode(String s) throws UnsupportedEncodingException { |
| 185 | |
| 186 | StringBuilder result = new StringBuilder(); |
| 187 | ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 188 | for (int i = 0; i < s.length();) { |
| 189 | char c = s.charAt(i); |
| 190 | if (c == '%') { |
| 191 | out.reset(); |
| 192 | do { |
| 193 | if (i + 2 >= s.length()) { |
| 194 | throw new IllegalArgumentException(Messages.getString( |
| 195 | "luni.80", i)); //$NON-NLS-1$ |
| 196 | } |
| 197 | int d1 = Character.digit(s.charAt(i + 1), 16); |
| 198 | int d2 = Character.digit(s.charAt(i + 2), 16); |
| 199 | if (d1 == -1 || d2 == -1) { |
| 200 | throw new IllegalArgumentException(Messages.getString( |
| 201 | "luni.81", s.substring(i, i + 3), //$NON-NLS-1$ |
| 202 | String.valueOf(i))); |
| 203 | } |
| 204 | out.write((byte) ((d1 << 4) + d2)); |
| 205 | i += 3; |
| 206 | } while (i < s.length() && s.charAt(i) == '%'); |
| 207 | result.append(out.toString(encoding)); |
| 208 | continue; |
| 209 | } |
| 210 | result.append(c); |
| 211 | i++; |
| 212 | } |
| 213 | return result.toString(); |
| 214 | } |
| 215 | |
| 216 | } |