Skip to content

Commit fb24a15

Browse files
committed
PR: 25995
Obtained from: http://nagoya.apache.org/bugzilla/show_bug.cgi?id=25995 Submitted by: olegk@apache.org (Oleg Kalnichevski) Reviewed by: Gary D. Gregory git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/codec/trunk@130281 13f79535-47bb-0310-9956-ffa450edef68
1 parent cb74b50 commit fb24a15

2 files changed

Lines changed: 20 additions & 11 deletions

File tree

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,18 +87,19 @@
8787
* @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
8888
* @author <a href="mailto:ggregory@seagullsw.com">Gary Gregory</a>
8989
* @since 1.2
90-
* @version $Id: URLCodec.java,v 1.12 2004/01/02 07:01:47 ggregory Exp $
90+
* @version $Id: URLCodec.java,v 1.13 2004/01/09 19:02:47 ggregory Exp $
9191
*/
92-
9392
public class URLCodec
9493
implements BinaryEncoder, BinaryDecoder,
9594
StringEncoder, StringDecoder
9695
{
9796

97+
private final static String US_ASCII = "US-ASCII";
98+
9899
/**
99100
* The <code>String</code> encoding used for decoding and encoding.
100101
*/
101-
protected String encoding = "US-ASCII";
102+
protected String encoding = "UTF-8";
102103

103104
/**
104105
* BitSet of www-form-url safe characters.
@@ -265,7 +266,7 @@ public String encode(String pString, String encoding)
265266
if (pString == null) {
266267
return null;
267268
}
268-
return new String(encode(pString.getBytes(encoding)), this.getEncoding());
269+
return new String(encode(pString.getBytes(encoding)), US_ASCII);
269270
}
270271

271272

@@ -282,7 +283,7 @@ public String encode(String pString) throws EncoderException {
282283
return null;
283284
}
284285
try {
285-
return new String(encode(pString.getBytes()), this.getEncoding());
286+
return encode(pString, this.encoding);
286287
} catch(UnsupportedEncodingException e) {
287288
throw new EncoderException(e.getMessage());
288289
}
@@ -307,7 +308,7 @@ public String decode(String pString, String encoding)
307308
if (pString == null) {
308309
return null;
309310
}
310-
return new String(decode(pString.getBytes(this.getEncoding())), encoding);
311+
return new String(decode(pString.getBytes(US_ASCII)), encoding);
311312
}
312313

313314

@@ -324,13 +325,12 @@ public String decode(String pString) throws DecoderException {
324325
return null;
325326
}
326327
try {
327-
return new String(decode(pString.getBytes(this.getEncoding())));
328+
return decode(pString, this.encoding);
328329
} catch(UnsupportedEncodingException e) {
329330
throw new DecoderException(e.getMessage());
330331
}
331332
}
332333

333-
334334
/**
335335
* Encodes an object into its URL safe form. Unsafe characters are
336336
* escaped.

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@
6767
*
6868
* @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
6969
*/
70-
7170
public class URLCodecTest extends TestCase {
7271

7372
static final int SWISS_GERMAN_STUFF_UNICODE [] = {
@@ -241,12 +240,12 @@ public void testInvalidEncoding() {
241240
URLCodec urlcodec = new URLCodec("NONSENSE");
242241
String plain = "Hello there!";
243242
try {
244-
String encoded = urlcodec.encode(plain);
243+
urlcodec.encode(plain);
245244
fail( "We set the encoding to a bogus NONSENSE vlaue, this shouldn't have worked.");
246245
} catch( EncoderException ee ) {
247246
}
248247
try {
249-
String decoded = urlcodec.decode(plain);
248+
urlcodec.decode(plain);
250249
fail( "We set the encoding to a bogus NONSENSE vlaue, this shouldn't have worked.");
251250
} catch( DecoderException ee ) {
252251
}
@@ -275,4 +274,14 @@ public void testDecodeObjects() throws Exception {
275274
} catch( DecoderException ee ) {
276275
}
277276
}
277+
278+
public void testDefaultEncoding() throws Exception {
279+
String plain = "Hello there!";
280+
URLCodec urlcodec = new URLCodec("UnicodeBig");
281+
urlcodec.encode(plain); // To work around a weird quirk in Java 1.2.2
282+
String encoded1 = urlcodec.encode(plain, "UnicodeBig");
283+
String encoded2 = urlcodec.encode(plain);
284+
assertEquals(encoded1, encoded2);
285+
}
286+
278287
}

0 commit comments

Comments
 (0)