Encodes the value, preserving all reserved characters.. Values that are already pct-encoded are ignored. @param value inspect. @param charset to use. @return a new String with the reserved characters preserved.
(
String value, Charset charset, boolean allowReservedCharacters)
| 109 | * @return a new String with the reserved characters preserved. |
| 110 | */ |
| 111 | public static String encodeInternal( |
| 112 | String value, Charset charset, boolean allowReservedCharacters) { |
| 113 | /* value is encoded, we need to split it up and skip the parts that are already encoded */ |
| 114 | Matcher matcher = PCT_ENCODED_PATTERN.matcher(value); |
| 115 | |
| 116 | if (!matcher.find()) { |
| 117 | return encodeChunk(value, charset, true); |
| 118 | } |
| 119 | |
| 120 | int length = value.length(); |
| 121 | StringBuilder encoded = new StringBuilder(length + 8); |
| 122 | int index = 0; |
| 123 | do { |
| 124 | /* split out the value before the encoded value */ |
| 125 | String before = value.substring(index, matcher.start()); |
| 126 | |
| 127 | /* encode it */ |
| 128 | encoded.append(encodeChunk(before, charset, allowReservedCharacters)); |
| 129 | |
| 130 | /* append the encoded value */ |
| 131 | encoded.append(matcher.group()); |
| 132 | |
| 133 | /* update the string search index */ |
| 134 | index = matcher.end(); |
| 135 | } while (matcher.find()); |
| 136 | |
| 137 | /* append the rest of the string */ |
| 138 | String tail = value.substring(index, length); |
| 139 | encoded.append(encodeChunk(tail, charset, allowReservedCharacters)); |
| 140 | return encoded.toString(); |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Encode a Uri Chunk, ensuring that all reserved characters are also encoded. |
no test coverage detected