Commit 5ae061f4 authored by Phillip Webb's avatar Phillip Webb

Merge branch '1.5.x' into 2.0.x

parents 1758d8c1 55d0611b
...@@ -255,6 +255,10 @@ final class JarURLConnection extends java.net.JarURLConnection { ...@@ -255,6 +255,10 @@ final class JarURLConnection extends java.net.JarURLConnection {
static JarURLConnection get(URL url, JarFile jarFile) throws IOException { static JarURLConnection get(URL url, JarFile jarFile) throws IOException {
StringSequence spec = new StringSequence(url.getFile()); StringSequence spec = new StringSequence(url.getFile());
int index = indexOfRootSpec(spec, jarFile.getPathFromRoot()); int index = indexOfRootSpec(spec, jarFile.getPathFromRoot());
if (index == -1) {
return (Boolean.TRUE.equals(useFastExceptions.get()) ? NOT_FOUND_CONNECTION
: new JarURLConnection(url, null, EMPTY_JAR_ENTRY_NAME));
}
int separator; int separator;
while ((separator = spec.indexOf(SEPARATOR, index)) > 0) { while ((separator = spec.indexOf(SEPARATOR, index)) > 0) {
JarEntryName entryName = JarEntryName.get(spec.subSequence(index, separator)); JarEntryName entryName = JarEntryName.get(spec.subSequence(index, separator));
...@@ -275,7 +279,7 @@ final class JarURLConnection extends java.net.JarURLConnection { ...@@ -275,7 +279,7 @@ final class JarURLConnection extends java.net.JarURLConnection {
private static int indexOfRootSpec(StringSequence file, String pathFromRoot) { private static int indexOfRootSpec(StringSequence file, String pathFromRoot) {
int separatorIndex = file.indexOf(SEPARATOR); int separatorIndex = file.indexOf(SEPARATOR);
if (separatorIndex < 0) { if (separatorIndex < 0 || !file.startsWith(pathFromRoot, separatorIndex)) {
return -1; return -1;
} }
return separatorIndex + SEPARATOR.length() + pathFromRoot.length(); return separatorIndex + SEPARATOR.length() + pathFromRoot.length();
......
...@@ -95,6 +95,17 @@ final class StringSequence implements CharSequence { ...@@ -95,6 +95,17 @@ final class StringSequence implements CharSequence {
return this.source.indexOf(str, this.start + fromIndex) - this.start; return this.source.indexOf(str, this.start + fromIndex) - this.start;
} }
public boolean startsWith(CharSequence prefix) {
return startsWith(prefix, 0);
}
public boolean startsWith(CharSequence prefix, int toffset) {
if (length() - prefix.length() - toffset < 0) {
return false;
}
return subSequence(toffset, toffset + prefix.length()).equals(prefix);
}
@Override @Override
public String toString() { public String toString() {
return this.source.substring(this.start, this.end); return this.source.substring(this.start, this.end);
...@@ -117,10 +128,10 @@ final class StringSequence implements CharSequence { ...@@ -117,10 +128,10 @@ final class StringSequence implements CharSequence {
if (this == obj) { if (this == obj) {
return true; return true;
} }
if (obj == null || getClass() != obj.getClass()) { if (obj == null || !CharSequence.class.isInstance(obj)) {
return false; return false;
} }
StringSequence other = (StringSequence) obj; CharSequence other = (CharSequence) obj;
int n = length(); int n = length();
if (n == other.length()) { if (n == other.length()) {
int i = 0; int i = 0;
......
...@@ -18,6 +18,7 @@ package org.springframework.boot.loader.jar; ...@@ -18,6 +18,7 @@ package org.springframework.boot.loader.jar;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException;
import java.net.URL; import java.net.URL;
import org.junit.Before; import org.junit.Before;
...@@ -149,6 +150,15 @@ public class JarURLConnectionTests { ...@@ -149,6 +150,15 @@ public class JarURLConnectionTests {
.hasSameContentAs(new ByteArrayInputStream(new byte[] { 3 })); .hasSameContentAs(new ByteArrayInputStream(new byte[] { 3 }));
} }
@Test(expected = FileNotFoundException.class)
public void connectionToEntryUsingWrongAbsoluteUrlForEntryFromNestedJarFile()
throws Exception {
URL url = new URL("jar:file:" + getAbsolutePath() + "!/w.jar!/3.dat");
JarFile nested = this.jarFile
.getNestedJarFile(this.jarFile.getEntry("nested.jar"));
JarURLConnection.get(url, nested).getInputStream();
}
@Test @Test
public void getContentLengthReturnsLengthOfUnderlyingEntry() throws Exception { public void getContentLengthReturnsLengthOfUnderlyingEntry() throws Exception {
URL url = new URL(new URL("jar", null, -1, URL url = new URL(new URL("jar", null, -1,
......
...@@ -167,4 +167,46 @@ public class StringSequenceTests { ...@@ -167,4 +167,46 @@ public class StringSequenceTests {
assertThat(a).isEqualTo(b).isNotEqualTo(c); assertThat(a).isEqualTo(b).isNotEqualTo(c);
} }
@Test
public void startsWithWhenExactMatch() {
assertThat(new StringSequence("abc").startsWith("abc")).isTrue();
}
@Test
public void startsWithWhenLongerAndStartsWith() {
assertThat(new StringSequence("abcd").startsWith("abc")).isTrue();
}
@Test
public void startsWithWhenLongerAndDoesNotStartWith() {
assertThat(new StringSequence("abcd").startsWith("abx")).isFalse();
}
@Test
public void startsWithWhenShorterAndDoesNotStartWith() {
assertThat(new StringSequence("ab").startsWith("abc")).isFalse();
assertThat(new StringSequence("ab").startsWith("c")).isFalse();
}
@Test
public void startsWithOffsetWhenExactMatch() {
assertThat(new StringSequence("xabc").startsWith("abc", 1)).isTrue();
}
@Test
public void startsWithOffsetWhenLongerAndStartsWith() {
assertThat(new StringSequence("xabcd").startsWith("abc", 1)).isTrue();
}
@Test
public void startsWithOffsetWhenLongerAndDoesNotStartWith() {
assertThat(new StringSequence("xabcd").startsWith("abx", 1)).isFalse();
}
@Test
public void startsWithOffsetWhenShorterAndDoesNotStartWith() {
assertThat(new StringSequence("xab").startsWith("abc", 1)).isFalse();
assertThat(new StringSequence("xab").startsWith("c", 1)).isFalse();
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment