Java Utililty Methods URI Decode

List of utility methods to do URI Decode

Description

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

Method

Stringdecode(String encodedURI)
decode
if (encodedURI.endsWith("%2E")) {
    encodedURI = encodedURI.substring(0, encodedURI.length() - 3) + ".";
try {
    return URLDecoder.decode(encodedURI, "UTF-8");
} catch (UnsupportedEncodingException wontHappen) {
    throw new RuntimeException(wontHappen);
Stringdecode(String uri)
decode uri by URLDecoder
String newUri = uri;
try {
    newUri = URLDecoder.decode(uri, "UTF-8");
} catch (UnsupportedEncodingException e) {
return newUri;
StringdecodeToUtf8(String uri)
Decodes uri replacing '+' with ' ' and percent encoded characters with their utf equivalents.
try {
    return URLDecoder.decode(uri, "UTF-8");
} catch (UnsupportedEncodingException e) {
    throw new RuntimeException(e);
} catch (IllegalArgumentException iae) {
    throw new URISyntaxException(uri, "invalid url");
URIdecodeUnreserved(URI address)
Decodes any percent-encoded URI-unreserved characters in the URI address.
String addressStr = address.toString();
String decoded = decodeUnreserved(addressStr);
return decoded == addressStr ? address : URI.create(decoded);
StringdecodeURI(CharSequence uriCharSequence, String charset)
Decodes an encoded URI sequence according to the given charset This is able to decode URL-encoded characters but also leaves existing Non-ASCII characters intact (unlike Common HttpClients URIUtil)
if (uriCharSequence == null) {
    return null;
String uri = uriCharSequence.toString();
int oi = 0; 
ByteArrayOutputStream out = new ByteArrayOutputStream();
for (int i = 0; i < uri.length(); i++) {
    char c = uri.charAt(i);
...
StringdecodeURI(java.net.URI uri)
decode URI
return decodeURI(uri.toString());
StringdecodeURI(String s)
Decodes the passed UTF-8 String using an algorithm that's compatible with JavaScript's decodeURIComponent function.
if (s == null) {
    return null;
String result;
try {
    result = URLDecoder.decode(s, "UTF-8");
catch (Exception e) {
...
StringdecodeURI(String str, boolean fullUri)
decode URI
char[] buf = null;
int bufTop = 0;
for (int k = 0, length = str.length(); k != length;) {
    char C = str.charAt(k);
    if (C != '%') {
        if (buf != null) {
            buf[bufTop++] = C;
        ++k;
    } else {
        if (buf == null) {
            buf = new char[length];
            str.getChars(0, k, buf, 0);
            bufTop = k;
        int start = k;
        if (k + 3 > length) {
            throw new URISyntaxException(str, "Illegal URI format");
        int B = unHex(str.charAt(k + 1), str.charAt(k + 2));
        if (B < 0) {
            throw new URISyntaxException(str, "Illegal URI format");
        k += 3;
        if ((B & 0x80) == 0) {
            C = (char) B;
        } else {
            int utf8Tail, ucs4Char, minUcs4Char;
            if ((B & 0xC0) == 0x80) {
                throw new URISyntaxException(str, "Illegal URI format");
            } else if ((B & 0x20) == 0) {
                utf8Tail = 1;
                ucs4Char = B & 0x1F;
                minUcs4Char = 0x80;
            } else if ((B & 0x10) == 0) {
                utf8Tail = 2;
                ucs4Char = B & 0x0F;
                minUcs4Char = 0x800;
            } else if ((B & 0x08) == 0) {
                utf8Tail = 3;
                ucs4Char = B & 0x07;
                minUcs4Char = 0x10000;
            } else if ((B & 0x04) == 0) {
                utf8Tail = 4;
                ucs4Char = B & 0x03;
                minUcs4Char = 0x200000;
            } else if ((B & 0x02) == 0) {
                utf8Tail = 5;
                ucs4Char = B & 0x01;
                minUcs4Char = 0x4000000;
            } else {
                throw new URISyntaxException(str, "Illegal URI format");
            if (k + 3 * utf8Tail > length) {
                throw new URISyntaxException(str, "Illegal URI format");
            for (int j = 0; j != utf8Tail; j++) {
                if (str.charAt(k) != '%') {
                    throw new URISyntaxException(str, "Illegal URI format");
                B = unHex(str.charAt(k + 1), str.charAt(k + 2));
                if (B < 0 || (B & 0xC0) != 0x80) {
                    throw new URISyntaxException(str, "Illegal URI format");
                ucs4Char = (ucs4Char << 6) | (B & 0x3F);
                k += 3;
            if (ucs4Char < minUcs4Char || ucs4Char == 0xFFFE || ucs4Char == 0xFFFF) {
                ucs4Char = 0xFFFD;
            if (ucs4Char >= 0x10000) {
                ucs4Char -= 0x10000;
                if (ucs4Char > 0xFFFFF) {
                    throw new URISyntaxException(str, "Illegal URI format");
                char H = (char) ((ucs4Char >>> 10) + 0xD800);
                C = (char) ((ucs4Char & 0x3FF) + 0xDC00);
                buf[bufTop++] = H;
            } else {
                C = (char) ucs4Char;
        if (fullUri && URI_DECODE_RESERVED.indexOf(C) >= 0) {
            for (int x = start; x != k; x++) {
                buf[bufTop++] = str.charAt(x);
        } else {
            buf[bufTop++] = C;
return (buf == null) ? str : new String(buf, 0, bufTop);
StringdecodeUri(String string)
Decodes the given URI-encoded, UTF-8 string .
if (string == null) {
    return null;
try {
    return URLDecoder.decode(string, "UTF-8");
} catch (UnsupportedEncodingException ex) {
    throw new IllegalStateException(ex);
StringdecodeURI(String uri)
URLDecoder.decode wrapping
if (uri == null) {
    return null;
return URLDecoder.decode(uri, "UTF-8");