Improve the performance of JarURLConnection

This commit improves the performance of JarURLConnection. There are two
main changes:

Firstly, the way in which the spec is determined has been changed so
that it’s no longer necessary to create an absolute file. Instead,
the JarFile’s pathFromRoot is used to extract the spec from the URL
relative to the JarFile.

Secondly, the number of temporary Objects that are created has been
reduced, for example by tracking an index as we process a String
rather than creating a new substring for each iteration.

See gh-6215
This commit is contained in:
Andy Wilkinson
2016-06-24 16:14:06 +01:00
parent 9788a7bdda
commit 0d207d438a
4 changed files with 45 additions and 38 deletions

View File

@@ -102,6 +102,28 @@ public class JarURLConnectionTests {
.hasSameContentAs(new ByteArrayInputStream(new byte[] { 3 }));
}
@Test
public void connectionToEntryUsingAbsoluteUrlForEntryFromNestedJarFile()
throws Exception {
URL absoluteUrl = new URL(
"jar:file:" + getAbsolutePath() + "!/nested.jar!/3.dat");
assertThat(new JarURLConnection(absoluteUrl,
this.jarFile.getNestedJarFile(this.jarFile.getEntry("nested.jar")))
.getInputStream()).hasSameContentAs(
new ByteArrayInputStream(new byte[] { 3 }));
}
@Test
public void connectionToEntryUsingRelativeUrlForEntryFromNestedJarFile()
throws Exception {
URL absoluteUrl = new URL(
"jar:file:" + getRelativePath() + "!/nested.jar!/3.dat");
assertThat(new JarURLConnection(absoluteUrl,
this.jarFile.getNestedJarFile(this.jarFile.getEntry("nested.jar")))
.getInputStream()).hasSameContentAs(
new ByteArrayInputStream(new byte[] { 3 }));
}
private String getAbsolutePath() {
return this.rootJarFile.getAbsolutePath().replace('\\', '/');
}