Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use whole separator to delimit JAR file paths #337

Merged
merged 1 commit into from
Aug 27, 2024
Merged
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 @@ -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