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

Apache License

Parameter

Parameter Description
source the source string
encoding the encoding

Exception

Parameter Description
IllegalArgumentException when the given source contains invalid encoded sequences
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;
/*/*from  ww  w  .  ja v  a 2 s.  co  m*/
 * Copyright 2002-2011 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

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

import com.google.common.base.Preconditions;
import com.google.common.base.Strings;

public class Main {
    /**
     * 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>
     *     <li>Special characters {@code "-"}, {@code "_"}, {@code "."}, and {@code "*"} stay the same.</li>
     *     <li>A sequence "<code>%<i>xy</i></code>" is interpreted as a hexadecimal representation of the character.</li>
     * </ul>
     * @param source the source string
     * @param encoding the encoding
     * @return the decoded URI
     * @throws IllegalArgumentException when the given source contains invalid encoded sequences
     * @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 {
        Preconditions.checkNotNull(source, "'source' must not be null");
        if (Strings.isNullOrEmpty(encoding)) {
            throw new IllegalArgumentException("'encoding' must not be empty");
        }
        int length = source.length();
        ByteArrayOutputStream bos = new ByteArrayOutputStream(length);
        boolean changed = false;
        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);
                    if (u == -1 || l == -1) {
                        throw new IllegalArgumentException(
                                "Invalid encoded sequence \"" + source.substring(i) + "\"");
                    }
                    bos.write((char) ((u << 4) + l));
                    i += 2;
                    changed = true;
                } else {
                    throw new IllegalArgumentException("Invalid encoded sequence \"" + source.substring(i) + "\"");
                }
            } else {
                bos.write(ch);
            }
        }
        return changed ? new String(bos.toByteArray(), encoding) : source;
    }
}

Related

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