2
|
1 |
/*
|
5506
|
2 |
* Copyright (c) 2000, 2001, 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
|
|
7 |
* published by the Free Software Foundation.
|
|
8 |
*
|
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
13 |
* accompanied this code).
|
|
14 |
*
|
|
15 |
* You should have received a copy of the GNU General Public License version
|
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
18 |
*
|
5506
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
20 |
* or visit www.oracle.com if you need additional information or have any
|
|
21 |
* questions.
|
2
|
22 |
*/
|
|
23 |
|
|
24 |
/*
|
|
25 |
* @test
|
|
26 |
* @bug 4257115
|
|
27 |
* @summary Test URL encoder and decoder on a string that contains
|
|
28 |
* characters within and beyond the 8859-1 range.
|
|
29 |
*
|
|
30 |
*/
|
|
31 |
|
|
32 |
import java.io.*;
|
|
33 |
import java.net.*;
|
|
34 |
|
|
35 |
public class URLEncodeDecode {
|
|
36 |
|
|
37 |
static char chars[] = {'H', 'e', 'l', 'l', 'o',
|
|
38 |
' ', '+', '%',
|
|
39 |
'-', '_', '.', '!', '~', '*', '\'', '(',
|
|
40 |
')',
|
|
41 |
'@',
|
|
42 |
'\u00ae', '\u0101', '\u10a0'};
|
|
43 |
|
|
44 |
static String str = new String(chars);
|
|
45 |
|
|
46 |
static String correctEncodedUTF8 =
|
|
47 |
"Hello+%2B%25-_.%21%7E*%27%28%29%40%C2%AE%C4%81%E1%82%A0";
|
|
48 |
|
|
49 |
public static void main(String[] args) throws Exception {
|
|
50 |
|
|
51 |
System.out.println("Constructed the string: " + str);
|
|
52 |
System.out.println("The Unicode bytes are: " +
|
|
53 |
getHexBytes(str));
|
|
54 |
System.out.println("");
|
|
55 |
test("UTF-8", correctEncodedUTF8);
|
|
56 |
}
|
|
57 |
|
|
58 |
private static void test(String enc, String correctEncoded)
|
|
59 |
throws Exception{
|
|
60 |
|
|
61 |
String encoded = null;
|
|
62 |
String outStr = null;
|
|
63 |
|
|
64 |
if (enc == null) {
|
|
65 |
encoded = URLEncoder.encode(str);
|
|
66 |
outStr = "default";
|
|
67 |
}
|
|
68 |
else {
|
|
69 |
encoded = URLEncoder.encode(str, enc);
|
|
70 |
outStr = enc;
|
|
71 |
}
|
|
72 |
|
|
73 |
System.out.println("URLEncode it ("
|
|
74 |
+ outStr + ") : " + encoded);
|
|
75 |
System.out.println("The Unicode bytes are: " +
|
|
76 |
getHexBytes(encoded));
|
|
77 |
|
|
78 |
if (encoded.equals(correctEncoded))
|
|
79 |
System.out.println("The encoding is correct!");
|
|
80 |
else {
|
|
81 |
throw new Exception("The encoding is incorrect!" +
|
|
82 |
" It should be " + correctEncoded);
|
|
83 |
}
|
|
84 |
System.out.println("");
|
|
85 |
|
|
86 |
String decoded = null;
|
|
87 |
|
|
88 |
if (enc == null)
|
|
89 |
decoded = URLDecoder.decode(encoded);
|
|
90 |
else
|
|
91 |
decoded = URLDecoder.decode(encoded, enc);
|
|
92 |
|
|
93 |
System.out.println("URLDecode it ("
|
|
94 |
+ outStr + ") : " + decoded);
|
|
95 |
System.out.println("The Unicode bytes are: " +
|
|
96 |
getHexBytes(decoded));
|
|
97 |
|
|
98 |
if (str.equals(decoded))
|
|
99 |
System.out.println("The decoding is correct");
|
|
100 |
else {
|
|
101 |
throw new Exception("The decoded is not equal to the original");
|
|
102 |
}
|
|
103 |
|
|
104 |
}
|
|
105 |
|
|
106 |
private static String getHexBytes(String s) throws Exception {
|
|
107 |
StringBuffer sb = new StringBuffer();
|
|
108 |
for (int i = 0; i < s.length(); i++) {
|
|
109 |
|
|
110 |
int a = s.charAt(i);
|
|
111 |
int b1 = (a >>8) & 0xff;
|
|
112 |
int b2 = (byte)a;
|
|
113 |
int b11 = (b1>>4) & 0x0f;
|
|
114 |
int b12 = b1 & 0x0f;
|
|
115 |
int b21 = (b2 >>4) & 0x0f;
|
|
116 |
int b22 = b2 & 0x0f;
|
|
117 |
|
|
118 |
sb.append(Integer.toHexString(b11));
|
|
119 |
sb.append(Integer.toHexString(b12));
|
|
120 |
sb.append(Integer.toHexString(b21));
|
|
121 |
sb.append(Integer.toHexString(b22));
|
|
122 |
sb.append(' ');
|
|
123 |
}
|
|
124 |
return sb.toString();
|
|
125 |
}
|
|
126 |
|
|
127 |
}
|