[patch] File.toURI creates bogus URI
Robin Green
greenrd@greenrd.org
Mon Dec 6 13:54:00 GMT 2004
Hi list,
Please apply the attached 1-line patch, which fixes this issue:
The way Sun recommends to turn a File object into a URL is to do
file.toURI().toURL() so that proper URL escaping is done. However,
the URL produced in this way in libgcj doesn't work if you want to
open a connection to it, or pass it to a URLClassLoader.
2004-12-06 Robin Green <greenrd@greenrd.org>
* java/io/File.java
(toURI): Generate a URI which we understand
I hope this email is correct - I'm not a committer so I don't know
whether to put my name at the top of the changelog entry.
Test case also attached.
--
Robin
-------------- next part --------------
--- java/io/File.java.orig 2004-12-05 22:06:50.256675952 +0000
+++ java/io/File.java 2004-12-05 22:56:57.640484120 +0000
@@ -936,7 +936,7 @@
try
{
- return new URI("file", "", abspath.replace(separatorChar, '/'));
+ return new URI("file", abspath.replace(separatorChar, '/'), null);
}
catch (URISyntaxException use)
{
-------------- next part --------------
import java.io.*;
import java.net.*;
public class FileURLTest
{
public static void main(String args []) throws Exception
{
File f = File.createTempFile("test", null);
// This was to test that the fix works without having to recompile gcj
//test(new FixedFile(f));
// Test the real implementation
test(f);
}
public static void test(File f) throws Exception
{
f.deleteOnExit();
System.out.println("-------------------------------------");
System.out.println(f.getClass() + ": " + f.toString());
System.out.println();
System.out.println("File.toURL creates a URL which CAN be opened:");
URL url = f.toURL();
System.out.println(url);
test(url);
System.out.println();
System.out.println("File.toURI may create a URI which CAN'T be opened - if so an exception will be thrown:");
URI uri = f.toURI();
System.out.println(uri);
System.out.println("Fragment: " + uri.getFragment());
test(uri.toURL());
}
private static void test(URL url) throws IOException {
System.out.println("Anchor: " + url.getRef());
System.out.println("Stream: " + url.openStream());
}
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
URL: <http://gcc.gnu.org/pipermail/java-patches/attachments/20041206/667f165a/attachment.sig>
More information about the Java-patches
mailing list