0N/A/*
2362N/A * Copyright (c) 1998, 2006, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage java.net;
0N/A
0N/Aimport java.io.*;
0N/A
0N/A/**
0N/A * Utility class for HTML form decoding. This class contains static methods
0N/A * for decoding a String from the <CODE>application/x-www-form-urlencoded</CODE>
0N/A * MIME format.
0N/A * <p>
0N/A * The conversion process is the reverse of that used by the URLEncoder class. It is assumed
0N/A * that all characters in the encoded string are one of the following:
0N/A * &quot;<code>a</code>&quot; through &quot;<code>z</code>&quot;,
0N/A * &quot;<code>A</code>&quot; through &quot;<code>Z</code>&quot;,
0N/A * &quot;<code>0</code>&quot; through &quot;<code>9</code>&quot;, and
0N/A * &quot;<code>-</code>&quot;, &quot;<code>_</code>&quot;,
0N/A * &quot;<code>.</code>&quot;, and &quot;<code>*</code>&quot;. The
0N/A * character &quot;<code>%</code>&quot; is allowed but is interpreted
0N/A * as the start of a special escaped sequence.
0N/A * <p>
0N/A * The following rules are applied in the conversion:
0N/A * <p>
0N/A * <ul>
0N/A * <li>The alphanumeric characters &quot;<code>a</code>&quot; through
0N/A * &quot;<code>z</code>&quot;, &quot;<code>A</code>&quot; through
0N/A * &quot;<code>Z</code>&quot; and &quot;<code>0</code>&quot;
0N/A * through &quot;<code>9</code>&quot; remain the same.
0N/A * <li>The special characters &quot;<code>.</code>&quot;,
0N/A * &quot;<code>-</code>&quot;, &quot;<code>*</code>&quot;, and
0N/A * &quot;<code>_</code>&quot; remain the same.
0N/A * <li>The plus sign &quot;<code>+</code>&quot; is converted into a
0N/A * space character &quot;<code>&nbsp;</code>&quot; .
0N/A * <li>A sequence of the form "<code>%<i>xy</i></code>" will be
0N/A * treated as representing a byte where <i>xy</i> is the two-digit
0N/A * hexadecimal representation of the 8 bits. Then, all substrings
0N/A * that contain one or more of these byte sequences consecutively
0N/A * will be replaced by the character(s) whose encoding would result
0N/A * in those consecutive bytes.
0N/A * The encoding scheme used to decode these characters may be specified,
0N/A * or if unspecified, the default encoding of the platform will be used.
0N/A * </ul>
0N/A * <p>
0N/A * There are two possible ways in which this decoder could deal with
0N/A * illegal strings. It could either leave illegal characters alone or
0N/A * it could throw an <tt>{@link java.lang.IllegalArgumentException}</tt>.
0N/A * Which approach the decoder takes is left to the
0N/A * implementation.
0N/A *
0N/A * @author Mark Chamness
0N/A * @author Michael McCloskey
0N/A * @since 1.2
0N/A */
0N/A
0N/Apublic class URLDecoder {
0N/A
0N/A // The platform default encoding
0N/A static String dfltEncName = URLEncoder.dfltEncName;
0N/A
0N/A /**
0N/A * Decodes a <code>x-www-form-urlencoded</code> string.
0N/A * The platform's default encoding is used to determine what characters
0N/A * are represented by any consecutive sequences of the form
0N/A * "<code>%<i>xy</i></code>".
0N/A * @param s the <code>String</code> to decode
0N/A * @deprecated The resulting string may vary depending on the platform's
0N/A * default encoding. Instead, use the decode(String,String) method
0N/A * to specify the encoding.
0N/A * @return the newly decoded <code>String</code>
0N/A */
0N/A @Deprecated
0N/A public static String decode(String s) {
0N/A
0N/A String str = null;
0N/A
0N/A try {
0N/A str = decode(s, dfltEncName);
0N/A } catch (UnsupportedEncodingException e) {
0N/A // The system should always have the platform default
0N/A }
0N/A
0N/A return str;
0N/A }
0N/A
0N/A /**
0N/A * Decodes a <code>application/x-www-form-urlencoded</code> string using a specific
0N/A * encoding scheme.
0N/A * The supplied encoding is used to determine
0N/A * what characters are represented by any consecutive sequences of the
0N/A * form "<code>%<i>xy</i></code>".
0N/A * <p>
0N/A * <em><strong>Note:</strong> The <a href=
0N/A * "http://www.w3.org/TR/html40/appendix/notes.html#non-ascii-chars">
0N/A * World Wide Web Consortium Recommendation</a> states that
0N/A * UTF-8 should be used. Not doing so may introduce
0N/A * incompatibilites.</em>
0N/A *
0N/A * @param s the <code>String</code> to decode
0N/A * @param enc The name of a supported
0N/A * <a href="../lang/package-summary.html#charenc">character
0N/A * encoding</a>.
0N/A * @return the newly decoded <code>String</code>
0N/A * @exception UnsupportedEncodingException
0N/A * If character encoding needs to be consulted, but
0N/A * named character encoding is not supported
0N/A * @see URLEncoder#encode(java.lang.String, java.lang.String)
0N/A * @since 1.4
0N/A */
0N/A public static String decode(String s, String enc)
0N/A throws UnsupportedEncodingException{
0N/A
0N/A boolean needToChange = false;
0N/A int numChars = s.length();
0N/A StringBuffer sb = new StringBuffer(numChars > 500 ? numChars / 2 : numChars);
0N/A int i = 0;
0N/A
0N/A if (enc.length() == 0) {
0N/A throw new UnsupportedEncodingException ("URLDecoder: empty string enc parameter");
0N/A }
0N/A
0N/A char c;
0N/A byte[] bytes = null;
0N/A while (i < numChars) {
0N/A c = s.charAt(i);
0N/A switch (c) {
0N/A case '+':
0N/A sb.append(' ');
0N/A i++;
0N/A needToChange = true;
0N/A break;
0N/A case '%':
0N/A /*
0N/A * Starting with this instance of %, process all
0N/A * consecutive substrings of the form %xy. Each
0N/A * substring %xy will yield a byte. Convert all
0N/A * consecutive bytes obtained this way to whatever
0N/A * character(s) they represent in the provided
0N/A * encoding.
0N/A */
0N/A
0N/A try {
0N/A
0N/A // (numChars-i)/3 is an upper bound for the number
0N/A // of remaining bytes
0N/A if (bytes == null)
0N/A bytes = new byte[(numChars-i)/3];
0N/A int pos = 0;
0N/A
0N/A while ( ((i+2) < numChars) &&
0N/A (c=='%')) {
0N/A int v = Integer.parseInt(s.substring(i+1,i+3),16);
0N/A if (v < 0)
0N/A throw new IllegalArgumentException("URLDecoder: Illegal hex characters in escape (%) pattern - negative value");
0N/A bytes[pos++] = (byte) v;
0N/A i+= 3;
0N/A if (i < numChars)
0N/A c = s.charAt(i);
0N/A }
0N/A
0N/A // A trailing, incomplete byte encoding such as
0N/A // "%x" will cause an exception to be thrown
0N/A
0N/A if ((i < numChars) && (c=='%'))
0N/A throw new IllegalArgumentException(
0N/A "URLDecoder: Incomplete trailing escape (%) pattern");
0N/A
0N/A sb.append(new String(bytes, 0, pos, enc));
0N/A } catch (NumberFormatException e) {
0N/A throw new IllegalArgumentException(
0N/A "URLDecoder: Illegal hex characters in escape (%) pattern - "
0N/A + e.getMessage());
0N/A }
0N/A needToChange = true;
0N/A break;
0N/A default:
0N/A sb.append(c);
0N/A i++;
0N/A break;
0N/A }
0N/A }
0N/A
0N/A return (needToChange? sb.toString() : s);
0N/A }
0N/A}