Skip to content

Commit 1dcf683

Browse files
author
Timothy O'Brien
committed
Bugzilla #21633: Commit'd patch from Michael Becke to URL
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/codec/trunk@130174 13f79535-47bb-0310-9956-ffa450edef68
1 parent c38480d commit 1dcf683

2 files changed

Lines changed: 43 additions & 8 deletions

File tree

src/java/org/apache/commons/codec/net/URLCodec.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
2-
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//codec/src/java/org/apache/commons/codec/net/URLCodec.java,v 1.2 2003/07/25 22:42:46 ggregory Exp $
3-
* $Revision: 1.2 $
4-
* $Date: 2003/07/25 22:42:46 $
2+
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//codec/src/java/org/apache/commons/codec/net/URLCodec.java,v 1.3 2003/07/31 20:09:21 tobrien Exp $
3+
* $Revision: 1.3 $
4+
* $Date: 2003/07/31 20:09:21 $
55
*
66
* ====================================================================
77
*
@@ -87,7 +87,7 @@
8787
* </p>
8888
*
8989
* @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
90-
* @version $Id: URLCodec.java,v 1.2 2003/07/25 22:42:46 ggregory Exp $
90+
* @version $Id: URLCodec.java,v 1.3 2003/07/31 20:09:21 tobrien Exp $
9191
*/
9292

9393
public class URLCodec
@@ -152,7 +152,7 @@ public static final byte[] urlencode(BitSet urlsafe, byte[] pArray)
152152
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
153153
for (int i = 0; i < pArray.length; i++) {
154154
int b = pArray[i];
155-
if (urlsafe.get(b)) {
155+
if (b >= 0 && urlsafe.get(b)) {
156156
if (b == ' ') {
157157
b = '+';
158158
}

src/test/org/apache/commons/codec/net/URLCodecTest.java

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
2-
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//codec/src/test/org/apache/commons/codec/net/URLCodecTest.java,v 1.1 2003/07/11 20:14:37 tobrien Exp $
3-
* $Revision: 1.1 $
4-
* $Date: 2003/07/11 20:14:37 $
2+
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//codec/src/test/org/apache/commons/codec/net/URLCodecTest.java,v 1.2 2003/07/31 20:09:21 tobrien Exp $
3+
* $Revision: 1.2 $
4+
* $Date: 2003/07/31 20:09:21 $
55
*
66
* ====================================================================
77
*
@@ -71,11 +71,46 @@
7171
*/
7272

7373
public class URLCodecTest extends TestCase {
74+
75+
static final int SWISS_GERMAN_STUFF_UNICODE [] = {
76+
0x47, 0x72, 0xFC, 0x65, 0x7A, 0x69, 0x5F, 0x7A, 0xE4, 0x6D, 0xE4
77+
};
78+
79+
static final int RUSSIAN_STUFF_UNICODE [] = {
80+
0x412, 0x441, 0x435, 0x43C, 0x5F, 0x43F, 0x440, 0x438,
81+
0x432, 0x435, 0x442
82+
};
7483

7584
public URLCodecTest(String name) {
7685
super(name);
7786
}
7887

88+
private String constructString(int [] unicodeChars) {
89+
StringBuffer buffer = new StringBuffer();
90+
if (unicodeChars != null) {
91+
for (int i = 0; i < unicodeChars.length; i++) {
92+
buffer.append((char)unicodeChars[i]);
93+
}
94+
}
95+
return buffer.toString();
96+
}
97+
98+
public void testUTF8RoundTrip() throws Exception {
99+
100+
String ru_msg = constructString(RUSSIAN_STUFF_UNICODE);
101+
String ch_msg = constructString(SWISS_GERMAN_STUFF_UNICODE);
102+
103+
URLCodec codec = new URLCodec();
104+
105+
assertEquals(
106+
"%D0%92%D1%81%D0%B5%D0%BC_%D0%BF%D1%80%D0%B8%D0%B2%D0%B5%D1%82",
107+
codec.encode(ru_msg, "UTF-8")
108+
);
109+
assertEquals("Gr%C3%BCezi_z%C3%A4m%C3%A4", codec.encode(ch_msg, "UTF-8"));
110+
111+
assertEquals(ru_msg, codec.decode(codec.encode(ru_msg, "UTF-8"), "UTF-8"));
112+
assertEquals(ch_msg, codec.decode(codec.encode(ch_msg, "UTF-8"), "UTF-8"));
113+
}
79114

80115
public void testBasicEncodeDecode() throws Exception {
81116
URLCodec urlcodec = new URLCodec();

0 commit comments

Comments
 (0)