Decodes a "x-www-form-urlencoded" to a String . @param s the String to decode @return the newly decoded String
(String s)
| 648 | * @return the newly decoded <code>String</code> |
| 649 | */ |
| 650 | static String decode(String s) { |
| 651 | if (s == null) |
| 652 | return null; |
| 653 | if (indexOfAny(s, "+%") == -1) |
| 654 | return s; // the common case |
| 655 | |
| 656 | StringBuilder sb = new StringBuilder(); |
| 657 | for (int i = 0; i < s.length(); i++) { |
| 658 | char c = s.charAt(i); |
| 659 | switch (c) { |
| 660 | case '+': |
| 661 | sb.append(' '); |
| 662 | break; |
| 663 | case '%': |
| 664 | try { |
| 665 | sb.append((char)Integer.parseInt( |
| 666 | s.substring(i+1,i+3),16)); |
| 667 | } catch (NumberFormatException e) { |
| 668 | throw new IllegalArgumentException( |
| 669 | "Illegal URL encoded value: " + |
| 670 | s.substring(i,i+3)); |
| 671 | } |
| 672 | i += 2; |
| 673 | break; |
| 674 | default: |
| 675 | sb.append(c); |
| 676 | break; |
| 677 | } |
| 678 | } |
| 679 | // Undo conversion to external encoding |
| 680 | String result = sb.toString(); |
| 681 | try { |
| 682 | byte[] inputBytes = result.getBytes("8859_1"); |
| 683 | result = new String(inputBytes); |
| 684 | } catch (UnsupportedEncodingException e) { |
| 685 | // The system should always have 8859_1 |
| 686 | } |
| 687 | return result; |
| 688 | } |
| 689 | |
| 690 | /** |
| 691 | * Return the first index of any of the characters in "any" in "s", |