Merge pull request #5289 from Vladimir Tsanev
* gh-5289: Polish contribution Update JarURLConnection to support jar:file://… URLs
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2014 the original author or authors.
|
||||
* Copyright 2012-2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -59,6 +59,8 @@ class JarURLConnection extends java.net.JarURLConnection {
|
||||
|
||||
private static final JarEntryName EMPTY_JAR_ENTRY_NAME = new JarEntryName("");
|
||||
|
||||
private static final String FILE_COLON_DOUBLE_SLASH = "file://";
|
||||
|
||||
private static ThreadLocal<Boolean> useFastExceptions = new ThreadLocal<Boolean>();
|
||||
|
||||
private final JarFile jarFile;
|
||||
@@ -73,7 +75,8 @@ class JarURLConnection extends java.net.JarURLConnection {
|
||||
// What we pass to super is ultimately ignored
|
||||
super(EMPTY_JAR_URL);
|
||||
this.url = url;
|
||||
String spec = url.getFile().substring(jarFile.getUrl().getFile().length());
|
||||
String spec = getNormalizedFile(url)
|
||||
.substring(jarFile.getUrl().getFile().length());
|
||||
int separator;
|
||||
while ((separator = spec.indexOf(SEPARATOR)) > 0) {
|
||||
jarFile = getNestedJarFile(jarFile, spec.substring(0, separator));
|
||||
@@ -83,6 +86,13 @@ class JarURLConnection extends java.net.JarURLConnection {
|
||||
this.jarEntryName = getJarEntryName(spec);
|
||||
}
|
||||
|
||||
private String getNormalizedFile(URL url) {
|
||||
if (!url.getFile().startsWith(FILE_COLON_DOUBLE_SLASH)) {
|
||||
return url.getFile();
|
||||
}
|
||||
return "file:" + url.getFile().substring(FILE_COLON_DOUBLE_SLASH.length());
|
||||
}
|
||||
|
||||
private JarFile getNestedJarFile(JarFile jarFile, String name) throws IOException {
|
||||
JarEntry jarEntry = jarFile.getJarEntry(name);
|
||||
if (jarEntry == null) {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2015 the original author or authors.
|
||||
* Copyright 2012-2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -321,8 +321,18 @@ public class JarFileTests {
|
||||
|
||||
@Test
|
||||
public void createNonNestedUrlFromString() throws Exception {
|
||||
nonNestedJarFileFromString(
|
||||
"jar:file:" + this.rootJarFile.getAbsolutePath() + "!/2.dat");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createNonNestedUrlFromStringWithDoubleSlash() throws Exception {
|
||||
nonNestedJarFileFromString(
|
||||
"jar:file://" + this.rootJarFile.getAbsolutePath() + "!/2.dat");
|
||||
}
|
||||
|
||||
private void nonNestedJarFileFromString(String spec) throws Exception {
|
||||
JarFile.registerUrlProtocolHandler();
|
||||
String spec = "jar:" + this.rootJarFile.toURI() + "!/2.dat";
|
||||
URL url = new URL(spec);
|
||||
assertThat(url.toString(), equalTo(spec));
|
||||
InputStream inputStream = url.openStream();
|
||||
|
||||
Reference in New Issue
Block a user