Skip to content

Commit

Permalink
Merge branch '2.0.x'
Browse files Browse the repository at this point in the history
  • Loading branch information
philwebb committed May 31, 2018
2 parents 9ec9a74 + bdd541b commit cf27917
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,10 @@ static void setUseFastExceptions(boolean useFastExceptions) {
static JarURLConnection get(URL url, JarFile jarFile) throws IOException {
StringSequence spec = new StringSequence(url.getFile());
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;
while ((separator = spec.indexOf(SEPARATOR, index)) > 0) {
JarEntryName entryName = JarEntryName.get(spec.subSequence(index, separator));
Expand All @@ -275,7 +279,7 @@ static JarURLConnection get(URL url, JarFile jarFile) throws IOException {

private static int indexOfRootSpec(StringSequence file, String pathFromRoot) {
int separatorIndex = file.indexOf(SEPARATOR);
if (separatorIndex < 0) {
if (separatorIndex < 0 || !file.startsWith(pathFromRoot, separatorIndex)) {
return -1;
}
return separatorIndex + SEPARATOR.length() + pathFromRoot.length();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,17 @@ public int indexOf(String str, int fromIndex) {
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
public String toString() {
return this.source.substring(this.start, this.end);
Expand All @@ -117,10 +128,10 @@ public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
if (obj == null || !CharSequence.class.isInstance(obj)) {
return false;
}
StringSequence other = (StringSequence) obj;
CharSequence other = (CharSequence) obj;
int n = length();
if (n == other.length()) {
int i = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.net.URL;

import org.junit.Before;
Expand Down Expand Up @@ -149,6 +150,16 @@ public void connectionToEntryWithEncodedSpaceNestedEntry() throws Exception {
.hasSameContentAs(new ByteArrayInputStream(new byte[] { 3 }));
}

@Test
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"));
this.thrown.expect(FileNotFoundException.class);
JarURLConnection.get(url, nested).getInputStream();
}

@Test
public void getContentLengthReturnsLengthOfUnderlyingEntry() throws Exception {
URL url = new URL(new URL("jar", null, -1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,4 +167,46 @@ public void equalsWhenSameContentShouldMatch() {
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();
}

}

0 comments on commit cf27917

Please sign in to comment.