From 583948fb36a601303d6a5d5bd397df466be54b60 Mon Sep 17 00:00:00 2001 From: Puneet Behl Date: Tue, 28 Jun 2022 10:40:29 +0530 Subject: [PATCH] Update IOUtils.java Refactor code around file system to synchronously access file system when using JAR. Fixes grails/grails-core#12589 --- .../java/io/micronaut/core/io/IOUtils.java | 66 ++++++++++++------- 1 file changed, 44 insertions(+), 22 deletions(-) diff --git a/core/src/main/java/io/micronaut/core/io/IOUtils.java b/core/src/main/java/io/micronaut/core/io/IOUtils.java index 2150177b8da..9bb847bb5d5 100644 --- a/core/src/main/java/io/micronaut/core/io/IOUtils.java +++ b/core/src/main/java/io/micronaut/core/io/IOUtils.java @@ -26,7 +26,9 @@ import java.net.URI; import java.net.URISyntaxException; import java.net.URL; +import java.nio.file.ClosedFileSystemException; 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; @@ -82,39 +84,59 @@ public static void eachFile(@NonNull URI uri, String path, @NonNull Consumer walk = Files.walk(myPath, 1)) { - for (Iterator it = walk.iterator(); it.hasNext();) { - final Path currentPath = it.next(); - if (currentPath.equals(myPath) || Files.isHidden(currentPath) || currentPath.getFileName().startsWith(".")) { - continue; - } - consumer.accept(currentPath); - } - } finally { - if (fileSystem != null && fileSystem.isOpen()) { - fileSystem.close(); + + } catch (IOException e) { + // ignore, can't do anything here and can't log because class used in compiler + } + } + + private static void walkFiles(Consumer consumer, Path myPath) throws IOException { + if (myPath != null) { + try (Stream walk = Files.walk(myPath, 1)) { + for (Iterator it = walk.iterator(); it.hasNext();) { + final Path currentPath = it.next(); + if (currentPath.equals(myPath) || Files.isHidden(currentPath) || currentPath.getFileName().startsWith(".")) { + continue; } + consumer.accept(currentPath); } } - } catch (IOException e) { - // ignore, can't do anything here and can't log because class used in compiler } }