Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8039910

Files.walk and find methods should provide a means to ignore exceptions

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Future Project
    • Icon: P4 P4
    • tbd
    • 8
    • core-libs
    • x86_64
    • windows_7

      FULL PRODUCT VERSION :
      Java(TM) SE Runtime Environment (build 1.8.0-b132)
      Java HotSpot(TM) 64-Bit Server VM (build 25.0-b70, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows [Version 6.1.7601]

      A DESCRIPTION OF THE PROBLEM :
      This line fails with the given exception:

      Files.walk(Paths.get("C:\\")).forEach(System.out::println);

      Exception in thread "main" java.io.UncheckedIOException: java.nio.file.AccessDeniedException: C:\$Recycle.Bin\S-1-5-21-1123174451-621858187-3073451497-1000
      at java.nio.file.FileTreeIterator.fetchNextIfNeeded(FileTreeIterator.java:88)
      at java.nio.file.FileTreeIterator.hasNext(FileTreeIterator.java:104)
      at java.util.Iterator.forEachRemaining(Iterator.java:115)
      at java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1801)
      at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:512)
      at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:502)
      at java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:150)
      at java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:173)
      at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
      at java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:418)
      at DirectoryScanner.main(DirectoryScanner.java:36)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:483)
      at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
      Caused by: java.nio.file.AccessDeniedException: C:\$Recycle.Bin\S-1-5-21-1123174451-621858187-3073451497-1000
      at sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:83)
      at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:97)
      at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:102)
      at sun.nio.fs.WindowsDirectoryStream.<init>(WindowsDirectoryStream.java:86)
      at sun.nio.fs.WindowsFileSystemProvider.newDirectoryStream(WindowsFileSystemProvider.java:510)
      at java.nio.file.Files.newDirectoryStream(Files.java:457)
      at java.nio.file.FileTreeWalker.visit(FileTreeWalker.java:300)
      at java.nio.file.FileTreeWalker.next(FileTreeWalker.java:372)
      at java.nio.file.FileTreeIterator.fetchNextIfNeeded(FileTreeIterator.java:84)

      It looks like there is no way with the current API to recover from an exception when consuming the Files.walk stream. This makes the Files.walk API useless for traversing a tree of any non-trivial size where error recovery is required.

      REGRESSION. Last worked in version 7u51

      ADDITIONAL REGRESSION INFORMATION:
      This is a new API, however the Java 7 Files.walkFileTree API does offer a recovery route through the FileVisitor.visitFileFailed method.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Execute this line under windows:

      Files.walk(Paths.get("C:\\")).forEach(System.out::println);

      Consuming the stream will fail when one of the $Recycle.Bin directories is encountered.


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      I would expect the API to offer a way for the stream returned by Files.walk to be configured to continue when encountering an exception.

      Possibly an overloaded version of Files.walk that takes an error consumer, or even a generic way of skipping errors on stream itself, for example:
      Stream.onError(Function<Throwable,ContinueOrNot>) : Stream<T>


      ACTUAL -
      There is no way to recover from errors (caused by IOExceptions) encountered while consuming the stream created by Files.walk.

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      Exception in thread "main" java.io.UncheckedIOException: java.nio.file.AccessDeniedException: C:\$Recycle.Bin\S-1-5-21-1123174451-621858187-3073451497-1000
      at java.nio.file.FileTreeIterator.fetchNextIfNeeded(FileTreeIterator.java:88)
      at java.nio.file.FileTreeIterator.hasNext(FileTreeIterator.java:104)
      at java.util.Iterator.forEachRemaining(Iterator.java:115)
      at java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1801)
      at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:512)
      at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:502)
      at java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:150)
      at java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:173)
      at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
      at java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:418)
      at DirectoryScanner.main(DirectoryScanner.java:36)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:483)
      at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
      Caused by: java.nio.file.AccessDeniedException: C:\$Recycle.Bin\S-1-5-21-1123174451-621858187-3073451497-1000
      at sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:83)
      at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:97)
      at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:102)
      at sun.nio.fs.WindowsDirectoryStream.<init>(WindowsDirectoryStream.java:86)
      at sun.nio.fs.WindowsFileSystemProvider.newDirectoryStream(WindowsFileSystemProvider.java:510)
      at java.nio.file.Files.newDirectoryStream(Files.java:457)
      at java.nio.file.FileTreeWalker.visit(FileTreeWalker.java:300)
      at java.nio.file.FileTreeWalker.next(FileTreeWalker.java:372)
      at java.nio.file.FileTreeIterator.fetchNextIfNeeded(FileTreeIterator.java:84)

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import org.junit.Test;

      import java.nio.file.Files;
      import java.nio.file.Path;
      import java.nio.file.Paths;
      import java.util.stream.Stream;

      /**
       * Fails under windows.
       */
      public class FilesWalkTest {
          @Test
          public void test() throws Exception {
              try (Stream<Path> stream = Files.walk(Paths.get("C:\\"), 3)) {
                  stream.forEach(System.out::println);
              }
          }
      }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      There is no workaround for the Files.walk method returning a java.util.stream.Stream.

      Files.walkFileTree can be used instead, but does not offer the convenience of the Stream API.

            Unassigned Unassigned
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            6 Start watching this issue

              Created:
              Updated:
              Resolved: