You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "garydgregory (via GitHub)" <gi...@apache.org> on 2023/10/01 13:06:12 UTC

[GitHub] [commons-io] garydgregory commented on a diff in pull request #489: IO-811: Files.walk() direct and indirect callers fail to close the re…

garydgregory commented on code in PR #489:
URL: https://github.com/apache/commons-io/pull/489#discussion_r1342131817


##########
src/main/java/org/apache/commons/io/FileUtils.java:
##########
@@ -1996,7 +1996,29 @@ public static Iterator<File> iterateFiles(final File directory, final IOFileFilt
      * @since 1.2
      */
     public static Iterator<File> iterateFiles(final File directory, final String[] extensions, final boolean recursive) {
-        return Uncheck.apply(d -> streamFiles(d, recursive, extensions).iterator(), directory);
+        return Uncheck.apply(d -> {
+            Stream<File> stream = streamFiles(d, recursive, extensions);
+            Iterator<File> iter = stream.iterator();
+
+            // Wrap the iterator to allow closing the stream after consumption
+            return new Iterator<File>() {
+                @Override
+                public boolean hasNext() {
+                    boolean ret = iter.hasNext();
+
+                    if (!ret) {
+                        stream.close();
+                    }
+
+                    return ret;
+                }
+
+                @Override
+                public File next() {
+                    return iter.next();
+                }
+            };
+        }, directory);

Review Comment:
   No need for an anonymous class:
   ```
   return StreamIterator.iterator(Uncheck.get(() -> streamFiles(directory, recursive, extensions)));
   ```
   



##########
src/main/java/org/apache/commons/io/FileUtils.java:
##########
@@ -2238,7 +2260,11 @@ public static Collection<File> listFiles(final File directory, final IOFileFilte
      * @return a collection of java.io.File with the matching files
      */
     public static Collection<File> listFiles(final File directory, final String[] extensions, final boolean recursive) {
-        return Uncheck.apply(d -> toList(streamFiles(d, recursive, extensions)), directory);
+        return Uncheck.apply(d -> {

Review Comment:
   I've adopted a variant of this fix in git master. You are credited in `changes.xml` :-)



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@commons.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org