src/java.base/share/classes/java/net/URLDecoder.java
author chegar
Fri, 18 Oct 2019 21:25:01 +0100
branchdatagramsocketimpl-branch
changeset 58697 e3ff12d14d43
parent 53018 8bf9268df0e2
permissions -rw-r--r--
datagramsocketimpl-branch: minor refactoring
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
48252
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47216
diff changeset
     2
 * Copyright (c) 1998, 2017, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.net;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.*;
48252
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47216
diff changeset
    29
import java.nio.charset.Charset;
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47216
diff changeset
    30
import java.nio.charset.IllegalCharsetNameException;
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47216
diff changeset
    31
import java.nio.charset.UnsupportedCharsetException;
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47216
diff changeset
    32
import java.util.Objects;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * Utility class for HTML form decoding. This class contains static methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * for decoding a String from the <CODE>application/x-www-form-urlencoded</CODE>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * MIME format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * The conversion process is the reverse of that used by the URLEncoder class. It is assumed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * that all characters in the encoded string are one of the following:
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 5506
diff changeset
    41
 * &quot;{@code a}&quot; through &quot;{@code z}&quot;,
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 5506
diff changeset
    42
 * &quot;{@code A}&quot; through &quot;{@code Z}&quot;,
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 5506
diff changeset
    43
 * &quot;{@code 0}&quot; through &quot;{@code 9}&quot;, and
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 5506
diff changeset
    44
 * &quot;{@code -}&quot;, &quot;{@code _}&quot;,
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 5506
diff changeset
    45
 * &quot;{@code .}&quot;, and &quot;{@code *}&quot;. The
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 5506
diff changeset
    46
 * character &quot;{@code %}&quot; is allowed but is interpreted
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * as the start of a special escaped sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * The following rules are applied in the conversion:
21334
c60dfce46a77 8026982: javadoc errors in core libs
rriggs
parents: 19069
diff changeset
    50
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * <ul>
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 5506
diff changeset
    52
 * <li>The alphanumeric characters &quot;{@code a}&quot; through
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 5506
diff changeset
    53
 *     &quot;{@code z}&quot;, &quot;{@code A}&quot; through
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 5506
diff changeset
    54
 *     &quot;{@code Z}&quot; and &quot;{@code 0}&quot;
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 5506
diff changeset
    55
 *     through &quot;{@code 9}&quot; remain the same.
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 5506
diff changeset
    56
 * <li>The special characters &quot;{@code .}&quot;,
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 5506
diff changeset
    57
 *     &quot;{@code -}&quot;, &quot;{@code *}&quot;, and
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 5506
diff changeset
    58
 *     &quot;{@code _}&quot; remain the same.
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 5506
diff changeset
    59
 * <li>The plus sign &quot;{@code +}&quot; is converted into a
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 5506
diff changeset
    60
 *     space character &quot; &nbsp; &quot; .
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 5506
diff changeset
    61
 * <li>A sequence of the form "<i>{@code %xy}</i>" will be
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *     treated as representing a byte where <i>xy</i> is the two-digit
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *     hexadecimal representation of the 8 bits. Then, all substrings
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *     that contain one or more of these byte sequences consecutively
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *     will be replaced by the character(s) whose encoding would result
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 *     in those consecutive bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *     The encoding scheme used to decode these characters may be specified,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 *     or if unspecified, the default encoding of the platform will be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * There are two possible ways in which this decoder could deal with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * illegal strings.  It could either leave illegal characters alone or
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 5506
diff changeset
    73
 * it could throw an {@link java.lang.IllegalArgumentException}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * Which approach the decoder takes is left to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * @author  Mark Chamness
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * @author  Michael McCloskey
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * @since   1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
public class URLDecoder {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    // The platform default encoding
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    static String dfltEncName = URLEncoder.dfltEncName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    /**
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 5506
diff changeset
    88
     * Decodes a {@code x-www-form-urlencoded} string.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * The platform's default encoding is used to determine what characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * are represented by any consecutive sequences of the form
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 5506
diff changeset
    91
     * "<i>{@code %xy}</i>".
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 5506
diff changeset
    92
     * @param s the {@code String} to decode
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * @deprecated The resulting string may vary depending on the platform's
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     *          default encoding. Instead, use the decode(String,String) method
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     *          to specify the encoding.
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 5506
diff changeset
    96
     * @return the newly decoded {@code String}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    @Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    public static String decode(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        String str = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            str = decode(s, dfltEncName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        } catch (UnsupportedEncodingException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            // The system should always have the platform default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        return str;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    /**
37880
60ec48925dc6 8156661: Handful of typos in javadoc
igerasim
parents: 26720
diff changeset
   113
     * Decodes an {@code application/x-www-form-urlencoded} string using
60ec48925dc6 8156661: Handful of typos in javadoc
igerasim
parents: 26720
diff changeset
   114
     * a specific encoding scheme.
48252
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47216
diff changeset
   115
     *
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47216
diff changeset
   116
     * <p>
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47216
diff changeset
   117
     * This method behaves the same as {@linkplain decode(String s, Charset charset)}
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47216
diff changeset
   118
     * except that it will {@linkplain java.nio.charset.Charset#forName look up the charset}
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47216
diff changeset
   119
     * using the given encoding name.
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47216
diff changeset
   120
     *
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47216
diff changeset
   121
     * @implNote This implementation will throw an {@link java.lang.IllegalArgumentException}
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47216
diff changeset
   122
     * when illegal strings are encountered.
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47216
diff changeset
   123
     *
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47216
diff changeset
   124
     * @param s the {@code String} to decode
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47216
diff changeset
   125
     * @param enc   The name of a supported
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47216
diff changeset
   126
     *    <a href="../lang/package-summary.html#charenc">character
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47216
diff changeset
   127
     *    encoding</a>.
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47216
diff changeset
   128
     * @return the newly decoded {@code String}
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47216
diff changeset
   129
     * @throws UnsupportedEncodingException
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47216
diff changeset
   130
     *             If character encoding needs to be consulted, but
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47216
diff changeset
   131
     *             named character encoding is not supported
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47216
diff changeset
   132
     * @see URLEncoder#encode(java.lang.String, java.lang.String)
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47216
diff changeset
   133
     * @since 1.4
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47216
diff changeset
   134
     */
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47216
diff changeset
   135
    public static String decode(String s, String enc) throws UnsupportedEncodingException {
53018
8bf9268df0e2 8215281: Use String.isEmpty() when applicable in java.base
redestad
parents: 48252
diff changeset
   136
        if (enc.isEmpty()) {
48252
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47216
diff changeset
   137
            throw new UnsupportedEncodingException ("URLDecoder: empty string enc parameter");
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47216
diff changeset
   138
        }
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47216
diff changeset
   139
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47216
diff changeset
   140
        try {
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47216
diff changeset
   141
            Charset charset = Charset.forName(enc);
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47216
diff changeset
   142
            return decode(s, charset);
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47216
diff changeset
   143
        } catch (IllegalCharsetNameException | UnsupportedCharsetException e) {
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47216
diff changeset
   144
            throw new UnsupportedEncodingException(enc);
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47216
diff changeset
   145
        }
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47216
diff changeset
   146
    }
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47216
diff changeset
   147
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47216
diff changeset
   148
    /**
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47216
diff changeset
   149
     * Decodes an {@code application/x-www-form-urlencoded} string using
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47216
diff changeset
   150
     * a specific {@linkplain java.nio.charset.Charset Charset}.
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47216
diff changeset
   151
     * The supplied charset is used to determine
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * what characters are represented by any consecutive sequences of the
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 5506
diff changeset
   153
     * form "<i>{@code %xy}</i>".
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * <em><strong>Note:</strong> The <a href=
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * "http://www.w3.org/TR/html40/appendix/notes.html#non-ascii-chars">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * World Wide Web Consortium Recommendation</a> states that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * UTF-8 should be used. Not doing so may introduce
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 19069
diff changeset
   159
     * incompatibilities.</em>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     *
48252
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47216
diff changeset
   161
     * @implNote This implementation will throw an {@link java.lang.IllegalArgumentException}
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47216
diff changeset
   162
     * when illegal strings are encountered.
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47216
diff changeset
   163
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 5506
diff changeset
   164
     * @param s the {@code String} to decode
48252
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47216
diff changeset
   165
     * @param charset the given charset
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 5506
diff changeset
   166
     * @return the newly decoded {@code String}
48252
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47216
diff changeset
   167
     * @throws NullPointerException if {@code s} or {@code charset} is {@code null}
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47216
diff changeset
   168
     * @throws IllegalArgumentException if the implementation encounters illegal
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47216
diff changeset
   169
     * characters
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47216
diff changeset
   170
     * @see URLEncoder#encode(java.lang.String, java.nio.charset.Charset)
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47216
diff changeset
   171
     * @since 10
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     */
48252
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47216
diff changeset
   173
    public static String decode(String s, Charset charset) {
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47216
diff changeset
   174
        Objects.requireNonNull(charset, "Charset");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        boolean needToChange = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        int numChars = s.length();
24969
afa6934dd8e8 8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents: 21428
diff changeset
   177
        StringBuilder sb = new StringBuilder(numChars > 500 ? numChars / 2 : numChars);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        char c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        byte[] bytes = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        while (i < numChars) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            c = s.charAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            switch (c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            case '+':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                sb.append(' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                needToChange = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            case '%':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                 * Starting with this instance of %, process all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                 * consecutive substrings of the form %xy. Each
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                 * substring %xy will yield a byte. Convert all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                 * consecutive  bytes obtained this way to whatever
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                 * character(s) they represent in the provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                 * encoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                    // (numChars-i)/3 is an upper bound for the number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                    // of remaining bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                    if (bytes == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                        bytes = new byte[(numChars-i)/3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                    int pos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                    while ( ((i+2) < numChars) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                            (c=='%')) {
26720
6b160d97c51d 8055032: Improve numerical parsing in java.net and sun.net
redestad
parents: 25859
diff changeset
   210
                        int v = Integer.parseInt(s, i + 1, i + 3, 16);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                        if (v < 0)
48252
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47216
diff changeset
   212
                            throw new IllegalArgumentException(
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47216
diff changeset
   213
                                    "URLDecoder: Illegal hex characters in escape "
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47216
diff changeset
   214
                                            + "(%) pattern - negative value");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                        bytes[pos++] = (byte) v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                        i+= 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                        if (i < numChars)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                            c = s.charAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                    // A trailing, incomplete byte encoding such as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                    // "%x" will cause an exception to be thrown
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                    if ((i < numChars) && (c=='%'))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                        throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                         "URLDecoder: Incomplete trailing escape (%) pattern");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
48252
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47216
diff changeset
   228
                    sb.append(new String(bytes, 0, pos, charset));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                } catch (NumberFormatException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                    throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                    "URLDecoder: Illegal hex characters in escape (%) pattern - "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                    + e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                needToChange = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                sb.append(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        return (needToChange? sb.toString() : s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
}