Skip to content

Commit

Permalink
Use whole separator to delimit JAR file paths
Browse files Browse the repository at this point in the history
  • Loading branch information
dmlloyd committed Aug 27, 2024
1 parent 34ab5a0 commit 9b7b21c
Showing 1 changed file with 5 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public static void consumeAsPath(URL url, Consumer<Path> consumer) {
public static <R> R processAsPath(URL url, Function<Path, R> function) {
if (JAR.equals(url.getProtocol())) {
final String file = url.getFile();
final int exclam = file.indexOf('!');
final int exclam = file.indexOf("!/");
try {
URL fileUrl;
String subPath;
Expand All @@ -147,6 +147,7 @@ public static <R> R processAsPath(URL url, Function<Path, R> function) {
if (!fileUrl.getProtocol().equals("file")) {
throw new IllegalArgumentException("Sub-URL of JAR URL is expected to have a scheme of `file`");
}

return processAsJarPath(toLocalPath(fileUrl), subPath, function);
} catch (MalformedURLException e) {
throw new RuntimeException("Failed to create a URL for '" + file.substring(0, exclam) + "'", e);
Expand All @@ -165,19 +166,16 @@ private static <R> R processAsJarPath(Path jarPath, String path, Function<Path,
Path localPath = jarFs.getPath("/");
int start = 0;
for (;;) {
int idx = path.indexOf('!', start);
int idx = path.indexOf("!/", start);
if (idx == -1) {
return function.apply(localPath.resolve(path));
} else {
// could be nested JAR?
Path subPath = localPath.resolve(path.substring(0, idx));
if (Files.isDirectory(subPath)) {
// no, it's a plain directory and the `!` is superfluous
// no, it's a plain directory and the `!/` is superfluous
localPath = subPath;
start = idx + 1;
if (start + 1 < path.length() && path.charAt(start + 1) == '/') {
start++;
}
start = idx + 2;
} else {
// yes, it's a nested JAR file
return processAsJarPath(subPath, path.substring(idx + 1), function);
Expand Down

0 comments on commit 9b7b21c

Please sign in to comment.