Java URL Decode decode(String source, String encoding)

Here you can find the source of decode(String source, String encoding)

Description

Decodes the given encoded source String into an URI.

License

Open Source License

Parameter

Parameter Description
source the source string
encoding the encoding

Exception

Parameter Description
UnsupportedEncodingException when the given encoding parameter is not supported

Return

the decoded URI

Declaration

public static String decode(String source, String encoding) throws UnsupportedEncodingException 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.ByteArrayOutputStream;
import java.io.UnsupportedEncodingException;

public class Main {
    /**// w w w  .jav  a  2  s. co  m
     * Decodes the given encoded source String into an URI. Based on the following
     * rules:
     * <ul>
     * <li>Alphanumeric characters {@code "a"} through {@code "z"},
     * {@code "A"} through {@code "Z"}, and {@code "0"} through {@code "9"}
     * stay the same.
     * <li>Special characters {@code "-"}, {@code "_"}, {@code "."}, and
     * {@code "*"} stay the same.
     * <li>All other characters are converted into one or more bytes using the
     * given encoding scheme. Each of the resulting bytes is written as a
     * hexadecimal string in the {@code %xy} format.
     * <li>A sequence "<code>%<i>xy</i></code>" is interpreted as a hexadecimal
     * representation of the character.
     * </ul>
     * 
     * @param source the source string
     * @param encoding the encoding
     * @return the decoded URI
     * @throws UnsupportedEncodingException when the given encoding parameter is not supported
     * @see java.net.URLDecoder#decode(String, String)
     */
    public static String decode(String source, String encoding) throws UnsupportedEncodingException {
        // Assert.notNull(source, "'source' must not be null");
        // Assert.hasLength(encoding, "'encoding' must not be empty");
        int length = source.length();
        ByteArrayOutputStream bos = new ByteArrayOutputStream(length);
        for (int i = 0; i < length; i++) {
            int ch = source.charAt(i);
            if (ch == '%') {
                if ((i + 2) < length) {
                    char hex1 = source.charAt(i + 1);
                    char hex2 = source.charAt(i + 2);
                    int u = Character.digit(hex1, 16);
                    int l = Character.digit(hex2, 16);
                    bos.write((char) ((u << 4) + l));
                    i += 2;
                } else {
                    throw new IllegalArgumentException("Invalid encoded sequence \"" + source.substring(i) + "\"");
                }
            } else {
                bos.write(ch);
            }
        }
        return new String(bos.toByteArray(), encoding);
    }
}

Related

  1. decode(String s)
  2. decode(String s)
  3. decode(String s, boolean formDecode)
  4. decode(String s, String enc)
  5. decode(String source)
  6. decode(String source, String encoding)
  7. decode(String str)
  8. decode(String str)
  9. decode(String value)