Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -65,20 +65,28 @@ private static String encode(char[] path, int fromIndex) {
result[size++] = ch;
continue;
}
if (ch > 0x07FF) {
size = escape(result, 0xE0 | ((ch >> 12) & 0x0F), size);
size = escape(result, 0x80 | ((ch >> 6) & 0x3F), size);
size = escape(result, 0x80 | ((ch >> 0) & 0x3F), size);
}
else {
size = escape(result, 0xC0 | ((ch >> 6) & 0x1F), size);
size = escape(result, 0x80 | ((ch >> 0) & 0x3F), size);
}
size = encode(ch, result, size);
}
return new String(result, 0, size);
}

private static int escape(char[] result, int ch, int size) {
private static int encode(char ch, char[] result, int size) {
if (ch <= 0x007F) {
size = encodeByteChar(result, ch, size);
return size;
}
if (ch <= 0x07FF) {
size = encodeByteChar(result, 0xC0 | ((ch >> 6) & 0x1F), size);
size = encodeByteChar(result, 0x80 | ((ch >> 0) & 0x3F), size);
return size;
}
size = encodeByteChar(result, 0xE0 | ((ch >> 12) & 0x0F), size);
size = encodeByteChar(result, 0x80 | ((ch >> 6) & 0x3F), size);
size = encodeByteChar(result, 0x80 | ((ch >> 0) & 0x3F), size);
return size;
}

private static int encodeByteChar(char[] result, int ch, int size) {
result[size++] = '%';
result[size++] = HexFormat.of().toHighHexDigit(ch);
result[size++] = HexFormat.of().toLowHexDigit(ch);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,28 @@ void resolveFromNested() throws Exception {
}
}

@Test
void resolveFromNestedWhenNameNeedsEncoding() throws Exception {
ByteArrayOutputStream nestedJarData = new ByteArrayOutputStream();
try (JarOutputStream jarOutputStream = new JarOutputStream(nestedJarData)) {
jarOutputStream.putNextEntry(new JarEntry("dir with space/a#b.txt"));
jarOutputStream.write(42);
jarOutputStream.closeEntry();
}
File jarFile = new File(this.tempDir, "test.jar");
try (JarOutputStream jarOutputStream = new JarOutputStream(new FileOutputStream(jarFile))) {
writeStoredEntry(jarOutputStream, "nested.jar", nestedJarData.toByteArray());
}
URL url = JarUrl.create(jarFile, "nested.jar");
try (JarUrlClassLoader loader = new TestJarUrlClassLoader(url)) {
URL resource = loader.getResource("dir with space/a#b.txt");
assertThat(resource).isNotNull();
try (InputStream input = resource.openConnection().getInputStream()) {
assertThat(input.read()).isEqualTo(42);
}
}
}

@Test
void resolveFromNestedWhenMissingReturnsNull() throws Exception {
File jarFile = new File(this.tempDir, "test.jar");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,21 @@ void encodeWhenBasicString() {
assertThat(UrlEncoder.encode("a/b/C.class")).isEqualTo("a/b/C.class");
}

@Test
void encodeWhenHasSingleByteEncodedCharacters() {
assertThat(UrlEncoder.encode("a b/c#d%e?f.txt")).isEqualTo("a%20b/c%23d%25e%3ff.txt");
}

@Test
void encodeWhenHasDoubleByteEncodedCharacters() {
assertThat(UrlEncoder.encode("\u00e1/b/C.class")).isEqualTo("%c3%a1/b/C.class");
}

@Test
void encodeWhenHasTrippleByteEncodedCharacters() {
assertThat(UrlEncoder.encode("\u6841/b/C.class")).isEqualTo("%e6%a1%81/b/C.class");
}

@Test
void encodeWhenHasMixtureOfEncodedAndUnencodedDoubleByteCharacters() {
assertThat(UrlEncoder.encode("\u00e1/b/\u00c7.class")).isEqualTo("%c3%a1/b/%c3%87.class");
Expand Down