JDK-4707784 : URLEncoder / URLDecoder usage change from 1.3 to 1.4
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.net
  • Affected Version: 1.4.1,1.4.2,10
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • Submitted: 2002-06-25
  • Updated: 2017-08-25
  • Resolved: 2017-08-25
Related Reports
Duplicate :  
Description

Name: nt126004			Date: 06/25/2002


FULL PRODUCT VERSION :
java version "1.4.1-beta"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.1-beta-b14)
Java HotSpot(TM) Client VM (build 1.4.1-beta-b14, mixed mode)


FULL OPERATING SYSTEM VERSION :Al


A DESCRIPTION OF THE PROBLEM :
In JDK < 1.4 URLEncoder and URLDecoder only had 1 method each
In JDK >= 1.4 this method has been deprecated and the
suggested replacement has a different number of parameters
and, worse, throws a checked exception.

Since it seems from the documentation that passing anything
but "UTF-8" for the second parameter will lead to unreliable
results it would be much easier for developers upgrading
from 1.3 if there was a default implementation that didn't
throw an exception, something like

<pre>
	public static String defaultEncode (String s) {
		try {
			return java.net.URLEncoder.encode (s, "UTF-8");
		} catch (UnsupportedEncodingException ex) {
			assert false;
			return null;
		}
	}
</pre>

The assert false is justified since the JLS stipulates that
all virtual machines must support UTF-8 hence the exception
should never be thrown.

If we don't have this in the JDK then developers will have
to cut/paste this code everywhere or provide an alternative
implementation of URLEncoder/URLDecoder

REPRODUCIBILITY :
This bug can be reproduced always.
(Review ID: 158428) 
======================================================================

Comments
This is a duplicate of JDK-8178704.
25-08-2017

At this point the best we can do ( which is not bad ) is: URLDecoder.decode(String, Charset) URLEncoder.encode(String, Charset)
25-08-2017

EVALUATION It is unfortunate that we had to leave the behavior of the 1 arg decode method. We originally wanted to change it to do what the submitter suggests in the proposed defaultEncode() method. But this was an incompatibility that people complained about. Perhaps the solution is to add a UTF8encode() method doing what the submitter suggests. Will revisit for tiger. ###@###.### 2002-08-15
15-08-2002