Java URI Decode decodeURI(URI uri)

Here you can find the source of decodeURI(URI uri)

Description

Decode a URI by converting all percent encoded character into a String character.

License

Open Source License

Parameter

Parameter Description
uri The URI to decode.

Return

The specified URI in a String with converted percent encoded characters.

Declaration

public static String decodeURI(URI uri) 

Method Source Code


//package com.java2s;
import java.net.URI;

public class Main {
    /**//from  w  w w. ja  va  2 s .com
     * Decode a URI by converting all percent encoded character into a String
     * character.
     * 
     * @param uri
     *            The URI to decode.
     * @return The specified URI in a String with converted percent encoded
     *         characters.
     */
    public static String decodeURI(URI uri) {
        StringBuffer retVal = new StringBuffer();
        String uriStr = uri.toASCIIString();
        char c;
        for (int i = 0; i < uriStr.length(); ++i) {
            c = uriStr.charAt(i);
            if (c == '%') {
                // We certainly found an encoded character, check for length
                // now ( '%' HEXDIGIT HEXDIGIT)
                if (((uriStr.length() - i) < 2)) {
                    throw new IllegalArgumentException(
                            "The uri " + uriStr + " contain invalid encoded character !");
                }

                // Decode the encoded character
                char decodedChar = (char) Integer.parseInt(uriStr.substring(i + 1, i + 3), 16);
                retVal.append(decodedChar);
                i += 2;
                continue;
            }
            retVal.append(c);
        }
        return retVal.toString();
    }
}

Related

  1. decodeURI(java.net.URI uri)
  2. decodeURI(String s)
  3. decodeURI(String str, boolean fullUri)
  4. decodeUri(String string)
  5. decodeURI(String uri)
  6. decodeURIComponent(String s, String charset)
  7. decodeURItoMap(String URIstring)
  8. uriDecode(String encoded)
  9. uriDecodePath(String path)