| 573 | } |
| 574 | |
| 575 | private static String _encode(String s) { |
| 576 | int maxBytesPerChar = 10; |
| 577 | StringBuilder out = new StringBuilder(s.length()); |
| 578 | ByteArrayOutputStream buf = new ByteArrayOutputStream(maxBytesPerChar); |
| 579 | OutputStreamWriter writer = new OutputStreamWriter(buf); |
| 580 | |
| 581 | for (int i = 0; i < s.length(); i++) { |
| 582 | int c = (int)s.charAt(i); |
| 583 | if (dontNeedEncoding.get(c)) { |
| 584 | if (c == ' ') { |
| 585 | c = '+'; |
| 586 | } |
| 587 | out.append((char)c); |
| 588 | } else { |
| 589 | // convert to external encoding before hex conversion |
| 590 | try { |
| 591 | writer.write(c); |
| 592 | writer.flush(); |
| 593 | } catch(IOException e) { |
| 594 | buf.reset(); |
| 595 | continue; |
| 596 | } |
| 597 | byte[] ba = buf.toByteArray(); |
| 598 | for (int j = 0; j < ba.length; j++) { |
| 599 | out.append('%'); |
| 600 | char ch = Character.forDigit((ba[j] >> 4) & 0xF, 16); |
| 601 | // converting to use uppercase letter as part of |
| 602 | // the hex value if ch is a letter. |
| 603 | if (Character.isLetter(ch)) { |
| 604 | ch -= caseDiff; |
| 605 | } |
| 606 | out.append(ch); |
| 607 | ch = Character.forDigit(ba[j] & 0xF, 16); |
| 608 | if (Character.isLetter(ch)) { |
| 609 | ch -= caseDiff; |
| 610 | } |
| 611 | out.append(ch); |
| 612 | } |
| 613 | buf.reset(); |
| 614 | } |
| 615 | } |
| 616 | |
| 617 | return out.toString(); |
| 618 | } |
| 619 | |
| 620 | |
| 621 | /** |