Java Utililty Methods URI Encode

List of utility methods to do URI Encode

Description

The list of methods to do URI Encode are organized into topic(s).

Method

Stringencode(String str, boolean fullUri)
Code taken from rhino-1.7R1/src/org/mozilla/javascript/NativeGlobal.java and slightly adapted.
byte[] utf8buf = null;
StringBuilder sb = null;
for (int k = 0, length = str.length(); k != length; ++k) {
    char c = str.charAt(k);
    if (encodeUnescaped(c, fullUri)) {
        if (sb != null) {
            sb.append(c);
    } else {
        if (sb == null) {
            sb = new StringBuilder(length + 3);
            sb.append(str);
            sb.setLength(k);
            utf8buf = new byte[6];
        if (0xDC00 <= c && c <= 0xDFFF) {
            throw new URISyntaxException(str, c + " outside of valid range");
        int value;
        if (c < 0xD800 || 0xDBFF < c) {
            value = c;
        } else {
            k++;
            if (k == length) {
                throw new URISyntaxException(str, "out of chars");
            char c2 = str.charAt(k);
            if (!(0xDC00 <= c2 && c2 <= 0xDFFF)) {
                throw new URISyntaxException(str, "outside of valid range");
            value = ((c - 0xD800) << 10) + (c2 - 0xDC00) + 0x10000;
        int L = oneUcs4ToUtf8Char(utf8buf, value);
        assert utf8buf != null;
        for (int j = 0; j < L; j++) {
            int d = 0xff & utf8buf[j];
            sb.append('%');
            sb.append(toHexChar(d >>> 4));
            sb.append(toHexChar(d & 0xf));
return (sb == null) ? str : sb.toString();
Stringencode(String uri)
encode
StringBuilder out = new StringBuilder();
for (int i = 0; i < uri.length(); i++) {
    char c = uri.charAt(i);
    if (c >= 'a' && c <= 'z') {
        out.append(c);
    } else if (c >= '0' && c <= '9') {
        out.append(c);
    } else if (c >= 'A' && c <= 'Z') {
...
StringencodeForURI(String uriPart)
encode For URI
String result = urlEncodeUtf8(uriPart);
result = result.replaceAll("\\+", "%20");
result = result.replaceAll("%2D", "-");
result = result.replaceAll("%5F", "_");
result = result.replaceAll("%2E", ".");
result = result.replaceAll("%7E", "~");
result = result.replaceAll("\\*", "%2A");
return result;
...
StringencodeIfNeeded(String uri)
encode If Needed
String decodedUri = URLDecoder.decode(uri, "UTF-8");
if (uri.compareTo(decodedUri) == 0)
    return URLEncoder.encode(uri, "UTF-8");
else
    return uri;
StringencodeUri(final String uri)
URI encodes a path to comply with Subversion specification (according to rfc2396).
try {
    return new URI(null, null, uri, null).toASCIIString();
} catch (URISyntaxException e) {
    throw new IllegalArgumentException(e);
URIencodeUri(final URI uri)
encode Uri
String uriString;
try {
    uriString = URLEncoder.encode(uri.toString(), DEFAULT_ENCODING);
} catch (final UnsupportedEncodingException e) {
    return null;
return URI.create(uriString);
StringencodeURI(String s)
Encodes the passed String as UTF-8 using an algorithm that's compatible with JavaScript's encodeURIComponent function.
String result;
try {
    result = URLEncoder.encode(s, "UTF-8").replaceAll("\\+", "%20").replaceAll("\\%21", "!")
            .replaceAll("\\%27", "'").replaceAll("\\%28", "(").replaceAll("\\%29", ")")
            .replaceAll("\\%7E", "~");
catch (Exception e) {
    result = s;
...
StringencodeUri(String string)
Encodes the given UTF-8 string so that it's safe for use within an URI.
if (string == null) {
    return null;
try {
    return URLEncoder.encode(string, "UTF-8").replace("+", "%20");
} catch (UnsupportedEncodingException ex) {
    throw new IllegalStateException(ex);
StringencodeURI(String uri)
encode URI
String tmp = null;
tmp = uri.replaceAll(" ", "%20");
tmp = tmp.replaceAll("<", "%3C");
tmp = tmp.replaceAll(">", "%3E");
tmp = tmp.replaceAll("'", "%27");
tmp = tmp.replaceAll("\\x28", "%28"); 
tmp = tmp.replaceAll("\\x29", "%29"); 
tmp = tmp.replaceAll("\\x22", "%22"); 
...
StringencodeUri(String uri)
URL-encodes everything between "/"-characters.
String newUri = "";
StringTokenizer st = new StringTokenizer(uri, "/ ", true);
while (st.hasMoreTokens()) {
    String tok = st.nextToken();
    if (tok.equals("/")) {
        newUri += "/";
    } else if (tok.equals(" ")) {
        newUri += "%20";
...