Skip to content

Commit

Permalink
Fix issue when opening the same jar file multiple times.
Browse files Browse the repository at this point in the history
  • Loading branch information
LexManos committed Nov 3, 2023
1 parent c279804 commit 1d11630
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,13 @@ void testEmptyJar() throws Exception {
}
}
}

@Test // Test opening the same file multiple times.
void testSameJar() throws Exception {
var path = Paths.get("src", "test", "resources", "empty.zip");
var jar1 = SecureJar.from(path);
assertNotNull(jar1);
var jar2 = SecureJar.from(path);
assertNotNull(jar2);
}
}
11 changes: 9 additions & 2 deletions src/main/java/cpw/mods/jarhandling/impl/Jar.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.FileSystem;
import java.nio.file.FileSystemAlreadyExistsException;
import java.nio.file.FileSystemNotFoundException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
Expand Down Expand Up @@ -185,9 +187,14 @@ private Path newFileSystem(BiPredicate<String, String> filter, Path[] paths) {
return paths[0];
// We have to manually open the jar files up via a URI instead of a Path
// because the ZipFileSystem implementation only caches the FileSystems
// when accessed that way.
// when accessed that way. But we can only open it once or else it throws
// a FileSystemAlreadyExistsException. So, exceptions as codeflow, yay!
var uri = new URI("jar:" + paths[0].toUri());
fs = FileSystems.newFileSystem(uri, Map.of(), null);
try {
fs = FileSystems.newFileSystem(uri, Map.of(), null);
} catch (FileSystemAlreadyExistsException e) {
fs = FileSystems.getFileSystem(uri);
}
} else {
var map = new HashMap<String, Object>();
if (filter != null)
Expand Down

0 comments on commit 1d11630

Please sign in to comment.