diff --git a/compiler/src/dotty/tools/io/Directory.scala b/compiler/src/dotty/tools/io/Directory.scala index 4e670d50c39b..facccada7277 100644 --- a/compiler/src/dotty/tools/io/Directory.scala +++ b/compiler/src/dotty/tools/io/Directory.scala @@ -49,8 +49,10 @@ class Directory(jpath: JPath) extends Path(jpath) { /** An iterator over the contents of this directory. */ def list: Iterator[Path] = - if (isDirectory) Files.list(jpath).iterator.asScala.map(Path.apply) - else Iterator.empty + jpath.toFile.listFiles match { + case null => Iterator.empty + case xs => xs.iterator.map(x => Path(x.toPath)) + } def dirs: Iterator[Directory] = list collect { case x: Directory => x } def files: Iterator[File] = list collect { case x: File => x } diff --git a/compiler/src/dotty/tools/io/PlainFile.scala b/compiler/src/dotty/tools/io/PlainFile.scala index b7e3e171f557..2a381cb5745a 100644 --- a/compiler/src/dotty/tools/io/PlainFile.scala +++ b/compiler/src/dotty/tools/io/PlainFile.scala @@ -55,13 +55,14 @@ class PlainFile(val givenPath: Path) extends AbstractFile { /** Returns all abstract subfiles of this abstract directory. */ def iterator: Iterator[AbstractFile] = { - try { - import scala.collection.JavaConverters._ - val it = Files.newDirectoryStream(jpath).iterator() - it.asScala.map(p => new PlainFile(Path(p))) - } catch { - case _: NotDirectoryException => Iterator.empty + // Optimization: Assume that the file was not deleted and did not have permissions changed + // between the call to `list` and the iteration. This saves a call to `exists`. + def existsFast(path: Path) = path match { + case (_: Directory | _: io.File) => true + case _ => path.exists } + if (!isDirectory) Iterator.empty + else givenPath.toDirectory.list filter existsFast map (new PlainFile(_)) } /**