Encode a Uri Chunk, ensuring that all reserved characters are also encoded. @param value to encode. @param charset to use. @return an encoded uri chunk.
(String value, Charset charset, boolean allowReserved)
| 148 | * @return an encoded uri chunk. |
| 149 | */ |
| 150 | private static String encodeChunk(String value, Charset charset, boolean allowReserved) { |
| 151 | if (isEncoded(value, charset)) { |
| 152 | return value; |
| 153 | } |
| 154 | |
| 155 | byte[] data = value.getBytes(charset); |
| 156 | try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) { |
| 157 | for (byte b : data) { |
| 158 | if (isUnreserved((char) b)) { |
| 159 | bos.write(b); |
| 160 | } else if (isReserved((char) b) && allowReserved) { |
| 161 | bos.write(b); |
| 162 | } else { |
| 163 | pctEncode(b, bos); |
| 164 | } |
| 165 | } |
| 166 | return new String(bos.toByteArray(), charset); |
| 167 | } catch (IOException ioe) { |
| 168 | throw new IllegalStateException( |
| 169 | "Error occurred during encoding of the uri: " + ioe.getMessage(), ioe); |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Percent Encode the provided byte. |
no test coverage detected