United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
JDK-6522294 : If URI scheme is file and URL is not UTF-8 encoded, the ParseUtil.decode throws an Exception

Details
Type:
Bug
Submit Date:
2007-02-07
Status:
Resolved
Updated Date:
2010-07-29
Project Name:
JDK
Resolved Date:
2007-04-17
Component:
core-libs
OS:
generic
Sub-Component:
java.net
CPU:
generic
Priority:
P3
Resolution:
Fixed
Affected Versions:
5.0u11
Fixed Versions:

Related Reports
Relates:

Sub Tasks

Description
By using the sun.net.www.protocol.file.Handler.openConnection() the sun.net.www.ParseUtil.decode() throws an java.lang.IllegalArgumentException if an URL is used which has been encoded with the default encoding of the platform rather than UTF-8 and if the URI scheme is "file".

There is no problem if the URL is UTF-8 encoded and URI scheme is "file".

There is no problem anymore [to be exact 1.5.0_07+ is required, see CR 6274990] when the ParseUtil.decode() is the URL is not UTF-8 encoded, but run through an HttpURLConnection (if the URI scheme is "http").

$ java -showversion JEditorPaneTest
java version "1.6.0"
Java(TM) SE Runtime Environment (build 1.6.0-b105)
Java HotSpot(TM) Server VM (build 1.6.0-b105, mixed mode)

java.lang.IllegalArgumentException
        at sun.net.www.ParseUtil.decode(ParseUtil.java:185)
        at sun.net.www.protocol.file.Handler.openConnection(Handler.java:63)
        at sun.net.www.protocol.file.Handler.openConnection(Handler.java:55)
        at java.net.URL.openConnection(URL.java:945)
        at javax.swing.JEditorPane.getStream(JEditorPane.java:769)
        at javax.swing.JEditorPane.setPage(JEditorPane.java:418)
        at javax.swing.JEditorPane.setPage(JEditorPane.java:922)
        at javax.swing.JEditorPane.<init>(JEditorPane.java:258)
        at JEditorPaneTest.<init>(JEditorPaneTest.java:18)
        at JEditorPaneTest.main(JEditorPaneTest.java:35)

$ java -showversion JEditorPaneTest
java version "1.5.0_11"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_11-b03)
Java HotSpot(TM) Server VM (build 1.5.0_11-b03, mixed mode)

java.lang.IllegalArgumentException
        at sun.net.www.ParseUtil.decode(ParseUtil.java:185)
        at sun.net.www.protocol.file.Handler.openConnection(Handler.java:63)
        at sun.net.www.protocol.file.Handler.openConnection(Handler.java:55)
        at java.net.URL.openConnection(URL.java:943)
        at javax.swing.JEditorPane.getStream(JEditorPane.java:685)
        at javax.swing.JEditorPane.setPage(JEditorPane.java:406)
        at javax.swing.JEditorPane.setPage(JEditorPane.java:790)
        at javax.swing.JEditorPane.<init>(JEditorPane.java:251)
        at JEditorPaneTest.<init>(JEditorPaneTest.java:18)
        at JEditorPaneTest.main(JEditorPaneTest.java:35)

The sourcecode to reproduce the problem is attached to the bug report (see also workaround).

                                    

Comments
EVALUATION

sun.net.www.protocol.file.Handler calls sun.net.www.ParseUtil.decode to decode any percent encoded characters. This fails with IllegalArgumentException as the internal implementation of the file protocol handler expects that any percent encoded characters are UTF-8 encoded.

The http protocol handler does not have this problem as it does not decode the percent encoded characters in the URL. It simply sends a request to server with the URL path as it. It is up to the server to know how to handle this. The file protocol handler on the other hand needs to decode the percent encoded characters as the requested resource will be accessed from the local machine.

The recommended way to manage the encoding and decoding of URLs is to use URI, and to convert between these two classes using toURI() and URI.toURL(), as suggested in the workaround section. The java.net.URI class specifies that any percent encoded characters should use UTF-8.

So the solution is to use the URI class, that being said the exception that is being thrown (IllegalArgumentException) does not clearly identify what the problem is. We could change the internally implementation of the file protocol handler to report a more clear explaination of the problem.

The IllegalArgumentException now contains a more detailed message about the cause of the problem. For compatibility reasons an IAE is still being thrown.
                                     
2007-02-07
WORK AROUND

Don't use localized characters in URLs

OR

use
private static final String url = "file:///m\u00FCnchen.html";
JEditorPane editorPane = new JEditorPane(new URI(url).toASCIIString());
// m%C3%BCnchen.html

rather than
private static final String url = "file:///m%FCnchen.html";
JEditorPane editorPane = new JEditorPane(url);
                                     
2007-02-07



Hardware and Software, Engineered to Work Together