URL Encode string, using a specified encoding. @param s string to be encoded @param start the beginning index, inclusive @param end the ending index, exclusive @return A new CharChunk contained the URL encoded string @throws IOException If an I/O error occurs
(String s, int start, int end)
| 83 | * @throws IOException If an I/O error occurs |
| 84 | */ |
| 85 | public CharChunk encodeURL(String s, int start, int end) throws IOException { |
| 86 | if (c2b == null) { |
| 87 | bb = new ByteChunk(8); // small enough. |
| 88 | cb = new CharChunk(2); // small enough. |
| 89 | output = new CharChunk(64); // small enough. |
| 90 | c2b = new C2BConverter(StandardCharsets.UTF_8); |
| 91 | } else { |
| 92 | bb.recycle(); |
| 93 | cb.recycle(); |
| 94 | output.recycle(); |
| 95 | } |
| 96 | |
| 97 | for (int i = start; i < end; i++) { |
| 98 | char c = s.charAt(i); |
| 99 | if (safeChars.get(c)) { |
| 100 | output.append(c); |
| 101 | } else { |
| 102 | cb.append(c); |
| 103 | c2b.convert(cb, bb); |
| 104 | |
| 105 | // "surrogate" - UTF is _not_ 16 bit, but 21 !!!! |
| 106 | // ( while UCS is 31 ). Amazing... |
| 107 | if (c >= 0xD800 && c <= 0xDBFF) { |
| 108 | if ((i + 1) < end) { |
| 109 | char d = s.charAt(i + 1); |
| 110 | if (d >= 0xDC00 && d <= 0xDFFF) { |
| 111 | cb.append(d); |
| 112 | c2b.convert(cb, bb); |
| 113 | i++; |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | urlEncode(output, bb); |
| 119 | cb.recycle(); |
| 120 | bb.recycle(); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | return output; |
| 125 | } |
| 126 | |
| 127 | private void urlEncode(CharChunk out, ByteChunk bb) throws IOException { |
| 128 | byte[] bytes = bb.getBuffer(); |