author | chegar |
Fri, 18 Oct 2019 21:25:01 +0100 | |
branch | datagramsocketimpl-branch |
changeset 58697 | e3ff12d14d43 |
parent 53018 | 8bf9268df0e2 |
permissions | -rw-r--r-- |
2 | 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 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package java.net; |
|
27 |
||
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 | 33 |
|
34 |
/** |
|
35 |
* Utility class for HTML form decoding. This class contains static methods |
|
36 |
* for decoding a String from the <CODE>application/x-www-form-urlencoded</CODE> |
|
37 |
* MIME format. |
|
38 |
* <p> |
|
39 |
* The conversion process is the reverse of that used by the URLEncoder class. It is assumed |
|
40 |
* that all characters in the encoded string are one of the following: |
|
19069 | 41 |
* "{@code a}" through "{@code z}", |
42 |
* "{@code A}" through "{@code Z}", |
|
43 |
* "{@code 0}" through "{@code 9}", and |
|
44 |
* "{@code -}", "{@code _}", |
|
45 |
* "{@code .}", and "{@code *}". The |
|
46 |
* character "{@code %}" is allowed but is interpreted |
|
2 | 47 |
* as the start of a special escaped sequence. |
48 |
* <p> |
|
49 |
* The following rules are applied in the conversion: |
|
21334 | 50 |
* |
2 | 51 |
* <ul> |
19069 | 52 |
* <li>The alphanumeric characters "{@code a}" through |
53 |
* "{@code z}", "{@code A}" through |
|
54 |
* "{@code Z}" and "{@code 0}" |
|
55 |
* through "{@code 9}" remain the same. |
|
56 |
* <li>The special characters "{@code .}", |
|
57 |
* "{@code -}", "{@code *}", and |
|
58 |
* "{@code _}" remain the same. |
|
59 |
* <li>The plus sign "{@code +}" is converted into a |
|
60 |
* space character " " . |
|
61 |
* <li>A sequence of the form "<i>{@code %xy}</i>" will be |
|
2 | 62 |
* treated as representing a byte where <i>xy</i> is the two-digit |
63 |
* hexadecimal representation of the 8 bits. Then, all substrings |
|
64 |
* that contain one or more of these byte sequences consecutively |
|
65 |
* will be replaced by the character(s) whose encoding would result |
|
66 |
* in those consecutive bytes. |
|
67 |
* The encoding scheme used to decode these characters may be specified, |
|
68 |
* or if unspecified, the default encoding of the platform will be used. |
|
69 |
* </ul> |
|
70 |
* <p> |
|
71 |
* There are two possible ways in which this decoder could deal with |
|
72 |
* illegal strings. It could either leave illegal characters alone or |
|
19069 | 73 |
* it could throw an {@link java.lang.IllegalArgumentException}. |
2 | 74 |
* Which approach the decoder takes is left to the |
75 |
* implementation. |
|
76 |
* |
|
77 |
* @author Mark Chamness |
|
78 |
* @author Michael McCloskey |
|
79 |
* @since 1.2 |
|
80 |
*/ |
|
81 |
||
82 |
public class URLDecoder { |
|
83 |
||
84 |
// The platform default encoding |
|
85 |
static String dfltEncName = URLEncoder.dfltEncName; |
|
86 |
||
87 |
/** |
|
19069 | 88 |
* Decodes a {@code x-www-form-urlencoded} string. |
2 | 89 |
* The platform's default encoding is used to determine what characters |
90 |
* are represented by any consecutive sequences of the form |
|
19069 | 91 |
* "<i>{@code %xy}</i>". |
92 |
* @param s the {@code String} to decode |
|
2 | 93 |
* @deprecated The resulting string may vary depending on the platform's |
94 |
* default encoding. Instead, use the decode(String,String) method |
|
95 |
* to specify the encoding. |
|
19069 | 96 |
* @return the newly decoded {@code String} |
2 | 97 |
*/ |
98 |
@Deprecated |
|
99 |
public static String decode(String s) { |
|
100 |
||
101 |
String str = null; |
|
102 |
||
103 |
try { |
|
104 |
str = decode(s, dfltEncName); |
|
105 |
} catch (UnsupportedEncodingException e) { |
|
106 |
// The system should always have the platform default |
|
107 |
} |
|
108 |
||
109 |
return str; |
|
110 |
} |
|
111 |
||
112 |
/** |
|
37880 | 113 |
* Decodes an {@code application/x-www-form-urlencoded} string using |
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 | 152 |
* what characters are represented by any consecutive sequences of the |
19069 | 153 |
* form "<i>{@code %xy}</i>". |
2 | 154 |
* <p> |
155 |
* <em><strong>Note:</strong> The <a href= |
|
156 |
* "http://www.w3.org/TR/html40/appendix/notes.html#non-ascii-chars"> |
|
157 |
* World Wide Web Consortium Recommendation</a> states that |
|
158 |
* UTF-8 should be used. Not doing so may introduce |
|
21278 | 159 |
* incompatibilities.</em> |
2 | 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 | 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 | 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 | 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 | 175 |
boolean needToChange = false; |
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 | 178 |
int i = 0; |
179 |
||
180 |
char c; |
|
181 |
byte[] bytes = null; |
|
182 |
while (i < numChars) { |
|
183 |
c = s.charAt(i); |
|
184 |
switch (c) { |
|
185 |
case '+': |
|
186 |
sb.append(' '); |
|
187 |
i++; |
|
188 |
needToChange = true; |
|
189 |
break; |
|
190 |
case '%': |
|
191 |
/* |
|
192 |
* Starting with this instance of %, process all |
|
193 |
* consecutive substrings of the form %xy. Each |
|
194 |
* substring %xy will yield a byte. Convert all |
|
195 |
* consecutive bytes obtained this way to whatever |
|
196 |
* character(s) they represent in the provided |
|
197 |
* encoding. |
|
198 |
*/ |
|
199 |
||
200 |
try { |
|
201 |
||
202 |
// (numChars-i)/3 is an upper bound for the number |
|
203 |
// of remaining bytes |
|
204 |
if (bytes == null) |
|
205 |
bytes = new byte[(numChars-i)/3]; |
|
206 |
int pos = 0; |
|
207 |
||
208 |
while ( ((i+2) < numChars) && |
|
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 | 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 | 215 |
bytes[pos++] = (byte) v; |
216 |
i+= 3; |
|
217 |
if (i < numChars) |
|
218 |
c = s.charAt(i); |
|
219 |
} |
|
220 |
||
221 |
// A trailing, incomplete byte encoding such as |
|
222 |
// "%x" will cause an exception to be thrown |
|
223 |
||
224 |
if ((i < numChars) && (c=='%')) |
|
225 |
throw new IllegalArgumentException( |
|
226 |
"URLDecoder: Incomplete trailing escape (%) pattern"); |
|
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 | 229 |
} catch (NumberFormatException e) { |
230 |
throw new IllegalArgumentException( |
|
231 |
"URLDecoder: Illegal hex characters in escape (%) pattern - " |
|
232 |
+ e.getMessage()); |
|
233 |
} |
|
234 |
needToChange = true; |
|
235 |
break; |
|
236 |
default: |
|
237 |
sb.append(c); |
|
238 |
i++; |
|
239 |
break; |
|
240 |
} |
|
241 |
} |
|
242 |
||
243 |
return (needToChange? sb.toString() : s); |
|
244 |
} |
|
245 |
} |