Utility method to encode a string, as per RFC 2396 section 2. Set asQueryValue=false to avoid encoding ampersands. @param source @return @see http://www.ietf.org/rfc/rfc2396.txt
(String source)
| 214 | * @see <a href="http://www.ietf.org/rfc/rfc2396.txt">http://www.ietf.org/rfc/rfc2396.txt</a> |
| 215 | */ |
| 216 | public static String encodeString(String source) { // , boolean |
| 217 | // asQueryValue) { |
| 218 | if (source == null) { |
| 219 | return source; |
| 220 | } |
| 221 | int i = firstIllegalCharacter(source); |
| 222 | // most strings not encoded, so prevent extra objects and work. |
| 223 | if (i == -1) { |
| 224 | return source; |
| 225 | } |
| 226 | StringBuffer encoded = new StringBuffer(); |
| 227 | encoded.append(source.substring(0, i)); |
| 228 | byte bytes[] = toBytes(source); |
| 229 | for (; i < bytes.length; i++) { |
| 230 | int ch = bytes[i]; |
| 231 | // if (ch == URI.QUERY_SEPARATOR && asQueryValue) { |
| 232 | // encoded.append(ENCODED_AMPERSAND); |
| 233 | // } else |
| 234 | if (isLegal(ch)) { |
| 235 | encoded.append((char) ch); |
| 236 | } else { |
| 237 | encoded.append(QUOTE_MARKER + Integer.toHexString((byte) ch & 0xff).toUpperCase()); |
| 238 | } |
| 239 | } |
| 240 | return encoded.toString(); |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * Internal use only, for sources known to always be valid. |
no test coverage detected