Java String Decode by Charset decodeURL(@Nullable String str, Charset charSet)

Here you can find the source of decodeURL(@Nullable String str, Charset charSet)

Description

Decodes a application/x-www-form-urlencoded string using a specific encoding scheme.

License

Open Source License

Parameter

Parameter Description
str the <code>String</code> to decode
enc The name of a supported <a href="../lang/package-summary.html#charenc">character encoding</a>.

Return

the newly decoded String

Declaration

@Nullable
private static String decodeURL(@Nullable String str, Charset charSet) 

Method Source Code

//package com.java2s;
/*/*from w w  w  . j a v a 2s. c o  m*/
 * Copyright (C) 2014 Christian P. Lerch <christian.p.lerch[at]gmail.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.nio.charset.Charset;

import javax.annotation.Nullable;

public class Main {
    /**
     * Decodes a <code>application/x-www-form-urlencoded</code> string using a specific
     * encoding scheme.
     * The supplied encoding is used to determine
     * what characters are represented by any consecutive sequences of the
     * form "<code>%<i>xy</i></code>".
     * <p>
     * <em><strong>Note:</strong> The <a href=
     * "http://www.w3.org/TR/html40/appendix/notes.html#non-ascii-chars">
     * World Wide Web Consortium Recommendation</a> states that
     * UTF-8 should be used. Not doing so may introduce
     * incompatibilites.</em>
     *
     * @param str the <code>String</code> to decode
     * @param enc   The name of a supported
     *    <a href="../lang/package-summary.html#charenc">character
     *    encoding</a>.
     * @return the newly decoded <code>String</code>
     * @exception  UnsupportedEncodingException
     *             If character encoding needs to be consulted, but
     *             named character encoding is not supported
     * @see URLEncoder#encode(java.lang.String, java.lang.String)
     * @since 1.0
     */
    @Nullable
    private static String decodeURL(@Nullable String str, Charset charSet) {

        boolean needToChange = false;
        int numChars = str.length();
        StringBuilder sb = new StringBuilder(numChars > 500 ? numChars / 2 : numChars);
        int i = 0;

        char c;
        byte[] bytes = null;
        while (i < numChars) {
            c = str.charAt(i);
            switch (c) {
            case '+':
                sb.append(' ');
                i++;
                needToChange = true;
                break;
            case '%':
                /*
                 * Starting with this instance of %, process all
                 * consecutive substrings of the form %xy. Each
                 * substring %xy will yield a byte. Convert all
                 * consecutive  bytes obtained this way to whatever
                 * character(s) they represent in the provided
                 * encoding.
                 */
                try {
                    // (numChars-i)/3 is an upper bound for the number
                    // of remaining bytes
                    if (bytes == null) {
                        bytes = new byte[(numChars - i) / 3];
                    }
                    int pos = 0;

                    while (((i + 2) < numChars) && (c == '%')) {
                        int v = Integer.parseInt(str.substring(i + 1, i + 3), 16);
                        if (v < 0) {
                            throw new IllegalArgumentException(
                                    "URLDecoder: Illegal hex characters in escape (%) pattern - negative value");
                        }
                        bytes[pos++] = (byte) v;
                        i += 3;
                        if (i < numChars) {
                            c = str.charAt(i);
                        }
                    }
                    // A trailing, incomplete byte encoding such as
                    // "%x" will cause an exception to be thrown
                    if ((i < numChars) && (c == '%')) {
                        throw new IllegalArgumentException("URLDecoder: Incomplete trailing escape (%) pattern");
                    }
                    sb.append(new String(bytes, 0, pos, charSet));
                } catch (NumberFormatException e) {
                    throw new IllegalArgumentException(
                            "URLDecoder: Illegal hex characters in escape (%) pattern - " + e.getMessage());
                }
                needToChange = true;
                break;
            default:
                sb.append(c);
                i++;
                break;
            }
        }
        return (needToChange ? sb.toString() : str);
    }
}

Related

  1. decode(String value, Charset charset)
  2. decode(String value, Charset charset)
  3. decodeCharset(String value, String charset)
  4. decodeComponent(final String s, final Charset charset)
  5. decodeFormFields(final String content, final Charset charset)
  6. decodeWithDefaultCharSet(String urlToDecode)
  7. getCharsetDecoder(String charsetName)
  8. getDecoder(Charset charset)
  9. getDecoder(Charset charset)