This is the mail archive of the
java-patches@gcc.gnu.org
mailing list for the Java project.
URLClassLoader fix for getCanonicalFileURL fix
- From: Mark Wielaard <mark at klomp dot org>
- To: java-patches at gcc dot gnu dot org
- Date: 22 Dec 2002 22:15:24 +0100
- Subject: URLClassLoader fix for getCanonicalFileURL fix
- Organization:
Hi,
The last change to URLClassLoader which introduced the
getCanonicalFileURL() method contained a subtle bug. It works OK for
file URLs that point to real files, but not always for directories.
Since we later use this URL to construct a relative URL for a found
resource we must make sure that the file part ends with a separator if
it points to a directory.
2002-12-21 Mark Wielaard <mark@klomp.org>
* java/net/URLClassLoader.java (getCanonicalFileURL): Make sure that
if the original file part of the URL ended with a separator that the
canonical version also ends with a separator.
Mauve now contains tests for this case which pass with this patch.
It also makes Eclipse run much better.
OK for branch and mainline?
Cheers,
Mark
Index: java/net/URLClassLoader.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/net/URLClassLoader.java,v
retrieving revision 1.12
diff -u -r1.12 URLClassLoader.java
--- java/net/URLClassLoader.java 11 Dec 2002 02:13:51 -0000 1.12
+++ java/net/URLClassLoader.java 22 Dec 2002 21:05:14 -0000
@@ -271,14 +271,28 @@
abstract InputStream getInputStream() throws IOException;
}
+ /**
+ * Returns the given URL with a canonicalized file path name when it
+ * is has the file protocol. Otherwise (or when the file part of the
+ * URL couldn't be canonicalized) it returns the original String.
+ * It makes sure that if the original file part ended with a file
+ * separator that the new file part also ends with a separator.
+ */
static URL getCanonicalFileURL(URL url)
{
if ("file".equals(url.getProtocol()))
{
try
{
- File f = new File(url.getFile()).getCanonicalFile();
- url = new URL("file", "", f.toString());
+ String f = url.getFile();
+ File file = new File(f).getCanonicalFile();
+ String cf = file.toString();
+ String sep = File.separator;
+ if (f.endsWith(sep) && !cf.endsWith(sep))
+ {
+ cf += "/";
+ }
+ url = new URL("file", "", cf);
}
catch (IOException ignore)
{