Protect against malformed URLs on Windows

Update JarFile to correctly create system independent URLs to prevent
potential URISyntaxExceptions when running on Windows.

Fixes gh-836
pull/965/head
Phillip Webb 11 years ago
parent 845a86d548
commit 6a644e2e23

@ -365,11 +365,6 @@ public class JarFile extends java.util.jar.JarFile implements Iterable<JarEntryD
throw new IllegalArgumentException("ZipEntry must be contained in this file");
}
@Override
public String getName() {
return this.name;
}
@Override
public int size() {
return (int) this.size;
@ -380,11 +375,6 @@ public class JarFile extends java.util.jar.JarFile implements Iterable<JarEntryD
this.rootFile.close();
}
@Override
public String toString() {
return getName();
}
/**
* Return a URL that can be used to access this JAR file. NOTE: the specified URL
* cannot be serialized and or cloned.
@ -393,7 +383,26 @@ public class JarFile extends java.util.jar.JarFile implements Iterable<JarEntryD
*/
public URL getUrl() throws MalformedURLException {
Handler handler = new Handler(this);
return new URL("jar", "", -1, "file:" + getName() + "!/", handler);
String file = "file:" + getName(PathForm.SYSTEM_INDEPENDENT) + "!/";
return new URL("jar", "", -1, file, handler);
}
@Override
public String toString() {
return getName();
}
@Override
public String getName() {
return getName(PathForm.SYSTEM_DEPENDENT);
}
private String getName(PathForm pathForm) {
if (pathForm == PathForm.SYSTEM_INDEPENDENT && File.separatorChar != '/') {
return this.name.replace(File.separatorChar, '/');
}
return this.name;
}
/**
@ -420,4 +429,20 @@ public class JarFile extends java.util.jar.JarFile implements Iterable<JarEntryD
// Ignore
}
}
/**
* Different forms that paths can be returned.
*/
private static enum PathForm {
/**
* Use system dependent paths (i.e. include backslashes on Windows)
*/
SYSTEM_DEPENDENT,
/**
* Use system independent paths (i.e. replace backslashes on Windows)
*/
SYSTEM_INDEPENDENT
}
}

@ -92,7 +92,6 @@ public class JarFileTests {
assertThat(urlClassLoader.getResource("special/\u00EB.dat"), notNullValue());
assertThat(urlClassLoader.getResource("d/9.dat"), notNullValue());
jarFile.close();
urlClassLoader.close();
}
@Test
@ -135,7 +134,6 @@ public class JarFileTests {
URLClassLoader urlClassLoader = new URLClassLoader(
new URL[] { this.jarFile.getUrl() });
assertThat(urlClassLoader.getResource("special/\u00EB.dat"), notNullValue());
urlClassLoader.close();
}
@Test

Loading…
Cancel
Save