Skip to content

Commit

Permalink
Prevent automatic closing of NativeImageResourceFileSystem
Browse files Browse the repository at this point in the history
  • Loading branch information
linghengqian committed Feb 29, 2024
1 parent bd2af0f commit 3a62733
Showing 1 changed file with 13 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,8 @@
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
Expand Down Expand Up @@ -122,7 +120,7 @@ public static Stream<String> read(final ClassLoader classLoader, final String di
if (JAR_URL_PROTOCOLS.contains(directoryUrl.getProtocol())) {
return readDirectoryInJar(directory, directoryUrl);
} else {
return readDirectoryInFileSystem(directoryUrl).stream();
return readDirectoryInFileSystem(directoryUrl);
}
});
}
Expand Down Expand Up @@ -164,27 +162,31 @@ private static JarFile getJarFile(final URL url) {
* so additional determination is required.
*
* @param directoryUrl directory url
* @return list of resource name
* @return stream of resource name
*/
@SneakyThrows({IOException.class, URISyntaxException.class})
private static List<String> readDirectoryInFileSystem(final URL directoryUrl) {
private static Stream<String> readDirectoryInFileSystem(final URL directoryUrl) {
if ("resource".equals(directoryUrl.getProtocol())) {
try (FileSystem ignored = FileSystems.getFileSystem(directoryUrl.toURI())) {
return loadFromDirectory(directoryUrl);
} catch (FileSystemNotFoundException exception) {
try (FileSystem ignored = FileSystems.newFileSystem(directoryUrl.toURI(), Collections.emptyMap())) {
return loadFromDirectory(directoryUrl);
}
FileSystem nativeImageResourceFileSystem = FileSystems.newFileSystem(directoryUrl.toURI(), Collections.emptyMap());
return loadFromDirectory(directoryUrl).onClose(() -> {
try {
nativeImageResourceFileSystem.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
});
}
}
return loadFromDirectory(directoryUrl);
}

private static List<String> loadFromDirectory(final URL directoryUrl) throws URISyntaxException, IOException {
private static Stream<String> loadFromDirectory(final URL directoryUrl) throws URISyntaxException, IOException {
Path directoryPath = Paths.get(directoryUrl.toURI());
// noinspection resource
Stream<Path> walkStream = Files.find(directoryPath, Integer.MAX_VALUE, (path, basicFileAttributes) -> !basicFileAttributes.isDirectory(), FileVisitOption.FOLLOW_LINKS);
return walkStream.map(path -> path.subpath(directoryPath.getNameCount() - 1, path.getNameCount()).toString())
.collect(Collectors.toList());
return walkStream.map(path -> path.subpath(directoryPath.getNameCount() - 1, path.getNameCount()).toString());
}
}

0 comments on commit 3a62733

Please sign in to comment.