Java Code Examples for java.nio.file.DirectoryIteratorException#getCause()

The following examples show how to use java.nio.file.DirectoryIteratorException#getCause() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PackageOptions.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testEmptyPackageDirectory(Path base) throws Exception {
    Path src = base.resolve("src");
    createSources(src);

    // need an empty package directory, to check whether
    // the behavior of subpackage and package
    Path pkgdir = src.resolve("m1/m1pro/");
    try (DirectoryStream<Path> stream = Files.newDirectoryStream(pkgdir, "*.java")) {
        for (Path entry : stream) {
            Files.deleteIfExists(entry);
        }
    } catch (DirectoryIteratorException ex) {
        // I/O error encounted during the iteration
        throw ex.getCause();
    }
    execTask("--module-source-path", src.toString(),
            "-subpackages", "m1/m1pro");

    checkPackagesSpecified("m1pro", "m1pro.pro1", "m1pro.pro2");

    // empty package directory should cause an error
    execNegativeTask("--module-source-path", src.toString(),
                     "m1/m1pro");

}
 
Example 2
Source File: FileSystemDirectory.java    From container with Apache License 2.0 6 votes vote down vote up
@Override
protected Set<AbstractFile> getFilesNotConsiderPatterns() {
    Set<Path> result = new HashSet<>();
    try (DirectoryStream<Path> stream = Files.newDirectoryStream(representedPath)) {
        for (Path entry : stream) {
            if (Files.isRegularFile(entry)) {
                result.add(entry);
            }
        }
    } catch (DirectoryIteratorException ex) {
        // I/O error encounted during the iteration, the cause is an IOException
        throw new UncheckedIOException((IOException) ex.getCause());
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
    return result.stream().map(FileSystemFile::new).collect(Collectors.toSet());
}
 
Example 3
Source File: FileSystemDirectory.java    From container with Apache License 2.0 6 votes vote down vote up
@Override
public Set<AbstractDirectory> getDirectories() {
    Set<Path> result = new HashSet<>();
    try (DirectoryStream<Path> stream = Files.newDirectoryStream(representedPath)) {
        for (Path entry : stream) {
            if (Files.isDirectory(entry)) {
                result.add(entry);
            }
        }
    } catch (DirectoryIteratorException ex) {
        // I/O error encounted during the iteration, the cause is an IOException
        throw new UncheckedIOException((IOException) ex.getCause());
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
    return result.stream().map(FileSystemDirectory::new).collect(Collectors.toSet());
}
 
Example 4
Source File: DdlMojoTest.java    From hibernate5-ddl-maven-plugin with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Resets the Mojo by setting {@link #mojo} to {@code null} and deletes the
 * test directory.
 *
 * @throws IOException Thrown if the test directory could not be deleted
 */
@After
public void tearDown() throws IOException {
    //Unset Mojo instance
    mojo = null;

    //Delete test directory
    final Path testDir = Paths.get(TEST_DIR);
    if (Files.exists(testDir)) {
        //First get all files in the test directory (if the test directory
        //exists and delete them. This is necessary because there is no
        //method for recursivly deleting a directory in the Java API.
        try (final DirectoryStream<Path> files = Files.newDirectoryStream(
            testDir)) {
            for (final Path file : files) {
                Files.deleteIfExists(file);
            }
        } catch (DirectoryIteratorException ex) {
            throw ex.getCause();
        }
        //Delete the (now empty) test directory.
        Files.deleteIfExists(testDir);
    }
}
 
Example 5
Source File: DdlMojoTest.java    From hibernate5-ddl-maven-plugin with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Resets the Mojo by setting {@link #mojo} to {@code null} and deletes the
 * test directory.
 *
 * @throws IOException Thrown if the test directory could not be deleted
 */
@After
public void tearDown() throws IOException {
    //Unset Mojo instance
    mojo = null;

    //Delete test directory
    final Path testDir = Paths.get(TEST_DIR);
    if (Files.exists(testDir)) {
        //First get all files in the test directory (if the test directory
        //exists and delete them. This is necessary because there is no
        //method for recursivly deleting a directory in the Java API.
        try (final DirectoryStream<Path> files = Files.newDirectoryStream(
            testDir)) {
            for (final Path file : files) {
                Files.deleteIfExists(file);
            }
        } catch (DirectoryIteratorException ex) {
            throw ex.getCause();
        }
        //Delete the (now empty) test directory.
        Files.deleteIfExists(testDir);
    }
}
 
Example 6
Source File: DdlMojoTest.java    From hibernate5-ddl-maven-plugin with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Resets the Mojo by setting {@link #mojo} to {@code null} and deletes the
 * test directory.
 *
 * @throws IOException Thrown if the test directory could not be deleted
 */
@After
public void tearDown() throws IOException {
    //Unset Mojo instance
    mojo = null;

    //Delete test directory
    final Path testDir = Paths.get(TEST_DIR);
    if (Files.exists(testDir)) {
        //First get all files in the test directory (if the test directory
        //exists and delete them. This is necessary because there is no
        //method for recursivly deleting a directory in the Java API.
        try (final DirectoryStream<Path> files = Files.newDirectoryStream(
            testDir)) {
            for (final Path file : files) {
                Files.deleteIfExists(file);
            }
        } catch (DirectoryIteratorException ex) {
            throw ex.getCause();
        }
        //Delete the (now empty) test directory.
        Files.deleteIfExists(testDir);
    }
}
 
Example 7
Source File: DdlMojoTest.java    From hibernate5-ddl-maven-plugin with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Resets the Mojo by setting {@link #mojo} to {@code null} and deletes the
 * test directory.
 *
 * @throws IOException Thrown if the test directory could not be deleted
 */
@After
public void tearDown() throws IOException {
    //Unset Mojo instance
    mojo = null;

    //Delete test directory
    final Path testDir = Paths.get(TEST_DIR);
    if (Files.exists(testDir)) {
        //First get all files in the test directory (if the test directory
        //exists and delete them. This is necessary because there is no
        //method for recursivly deleting a directory in the Java API.
        try (final DirectoryStream<Path> files = Files.newDirectoryStream(
            testDir)) {
            for (final Path file : files) {
                Files.deleteIfExists(file);
            }
        } catch (DirectoryIteratorException ex) {
            throw ex.getCause();
        }
        //Delete the (now empty) test directory.
        Files.deleteIfExists(testDir);
    }
}
 
Example 8
Source File: DdlMojoTest.java    From hibernate5-ddl-maven-plugin with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Resets the Mojo by setting {@link #mojo} to {@code null} and deletes the
 * test directory.
 *
 * @throws IOException Thrown if the test directory could not be deleted
 */
@After
public void tearDown() throws IOException {
    //Unset Mojo instance
    mojo = null;

    //Delete test directory
    final Path testDir = Paths.get(TEST_DIR);
    if (Files.exists(testDir)) {
        //First get all files in the test directory (if the test directory
        //exists and delete them. This is necessary because there is no
        //method for recursivly deleting a directory in the Java API.
        try (final DirectoryStream<Path> files = Files.newDirectoryStream(
            testDir)) {
            for (final Path file : files) {
                Files.deleteIfExists(file);
            }
        } catch (DirectoryIteratorException ex) {
            throw ex.getCause();
        }
        //Delete the (now empty) test directory.
        Files.deleteIfExists(testDir);
    }
}
 
Example 9
Source File: IOUtils.java    From big-c with Apache License 2.0 6 votes vote down vote up
/**
 * Return the complete list of files in a directory as strings.<p/>
 *
 * This is better than File#listDir because it does not ignore IOExceptions.
 *
 * @param dir              The directory to list.
 * @param filter           If non-null, the filter to use when listing
 *                         this directory.
 * @return                 The list of files in the directory.
 *
 * @throws IOException     On I/O error
 */
public static List<String> listDirectory(File dir, FilenameFilter filter)
    throws IOException {
  ArrayList<String> list = new ArrayList<String> ();
  try (DirectoryStream<Path> stream =
           Files.newDirectoryStream(dir.toPath())) {
    for (Path entry: stream) {
      String fileName = entry.getFileName().toString();
      if ((filter == null) || filter.accept(dir, fileName)) {
        list.add(fileName);
      }
    }
  } catch (DirectoryIteratorException e) {
    throw e.getCause();
  }
  return list;
}
 
Example 10
Source File: FileSelection.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
public static FileSelection create(final FileSystem fs, Path combined) throws IOException {
  Stopwatch timer = Stopwatch.createStarted();

  // NFS filesystems has delay before files written by executor shows up in the coordinator.
  // For NFS, fs.exists() will force a refresh if the directory is not found
  // No action is taken if it returns false as the code path already handles the Exception case
  fs.exists(combined);

  final ImmutableList<FileAttributes> fileAttributes;
  try(DirectoryStream<FileAttributes> stream = FileSystemUtils.globRecursive(fs, combined, NO_HIDDEN_FILES)) {
    fileAttributes = ImmutableList.copyOf(stream);
  } catch (DirectoryIteratorException e) {
    throw e.getCause();
  }

  logger.trace("Returned files are: {}", fileAttributes);
  if (fileAttributes == null || fileAttributes.isEmpty()) {
    return null;
  }

  final FileSelection fileSel = createFromExpanded(fileAttributes, combined.toURI().getPath());
  logger.debug("FileSelection.create() took {} ms ", timer.elapsed(TimeUnit.MILLISECONDS));
  return fileSel;
}
 
Example 11
Source File: IOUtils.java    From hadoop with Apache License 2.0 6 votes vote down vote up
/**
 * Return the complete list of files in a directory as strings.<p/>
 *
 * This is better than File#listDir because it does not ignore IOExceptions.
 *
 * @param dir              The directory to list.
 * @param filter           If non-null, the filter to use when listing
 *                         this directory.
 * @return                 The list of files in the directory.
 *
 * @throws IOException     On I/O error
 */
public static List<String> listDirectory(File dir, FilenameFilter filter)
    throws IOException {
  ArrayList<String> list = new ArrayList<String> ();
  try (DirectoryStream<Path> stream =
           Files.newDirectoryStream(dir.toPath())) {
    for (Path entry: stream) {
      String fileName = entry.getFileName().toString();
      if ((filter == null) || filter.accept(dir, fileName)) {
        list.add(fileName);
      }
    }
  } catch (DirectoryIteratorException e) {
    throw e.getCause();
  }
  return list;
}
 
Example 12
Source File: PollingWatchService.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
PollingWatchKey(Path dir, PollingWatchService watcher, Object fileKey)
    throws IOException
{
    super(dir, watcher);
    this.fileKey = fileKey;
    this.valid = true;
    this.tickCount = 0;
    this.entries = new HashMap<Path,CacheEntry>();

    // get the initial entries in the directory
    try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir)) {
        for (Path entry: stream) {
            // don't follow links
            long lastModified =
                Files.getLastModifiedTime(entry, LinkOption.NOFOLLOW_LINKS).toMillis();
            entries.put(entry.getFileName(), new CacheEntry(lastModified, tickCount));
        }
    } catch (DirectoryIteratorException e) {
        throw e.getCause();
    }
}
 
Example 13
Source File: PollingWatchService.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
PollingWatchKey(Path dir, PollingWatchService watcher, Object fileKey)
    throws IOException
{
    super(dir, watcher);
    this.fileKey = fileKey;
    this.valid = true;
    this.tickCount = 0;
    this.entries = new HashMap<Path,CacheEntry>();

    // get the initial entries in the directory
    try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir)) {
        for (Path entry: stream) {
            // don't follow links
            long lastModified =
                Files.getLastModifiedTime(entry, LinkOption.NOFOLLOW_LINKS).toMillis();
            entries.put(entry.getFileName(), new CacheEntry(lastModified, tickCount));
        }
    } catch (DirectoryIteratorException e) {
        throw e.getCause();
    }
}
 
Example 14
Source File: TCChartReporter.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
private static List<Path> listResFiles(Path dir, String pattern) throws IOException {
    List<Path> result = new ArrayList<>();
    try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir, pattern)) {
        for (Path entry : stream) {
            result.add(entry);
        }
    } catch (DirectoryIteratorException ex) {
        throw ex.getCause();
    }
    return result;
}
 
Example 15
Source File: Directories.java    From util with Apache License 2.0 5 votes vote down vote up
/**
 * Count the number of entries in a directory.
 *
 * @param dir directory to evaluate
 * @return number of inodes under it.
 * @throws IOException
 */
@Nonnegative
public static int count(@Nonnull final Path dir) throws IOException {
    try (final DirectoryStream<Path> stream = Files.newDirectoryStream(dir)) {
        return Iterables.size(stream);
    } catch (DirectoryIteratorException ex) {
        // I/O error encounted during the iteration, the cause is an IOException
        throw ex.getCause();
    }
}
 
Example 16
Source File: Directories.java    From util with Apache License 2.0 5 votes vote down vote up
/**
 * Convenience method to return all paths in a SMALL directory.
 * <p>
 * DO NOT USE THIS TO TRAVERSE LARGE (multi-thousand inode) DIRECTORIES!
 * <p>
 * For starters you shouldn't be making directories that big at all, but if you did please use
 * {@link Files#newDirectoryStream(Path)} directly in your code.
 *
 * @param dir directory to evaluate
 * @return all files in that directory
 * @throws IOException
 */
@Nonnull
public static List<Path> list(@Nonnull final Path dir) throws IOException {
    final List<Path> contents = new ArrayList<>();
    try (final DirectoryStream<Path> stream = Files.newDirectoryStream(dir)) {
        for (final Path entry : stream) {
            contents.add(entry);
        }
    } catch (DirectoryIteratorException ex) {
        // I/O error encounted during the iteration, the cause is an IOException
        throw ex.getCause();
    }
    return contents;
}
 
Example 17
Source File: Kanzi.java    From kanzi with Apache License 2.0 4 votes vote down vote up
public static void createFileList(String target, List<Path> files) throws IOException
{
   if (target == null)
      return;
   
   Path root = Paths.get(target);
   
   if (Files.exists(root) == false) 
      throw new IOException("Cannot access input file '"+root+"'");
   
   if ((Files.isRegularFile(root) == true) && (Files.isHidden(root) == true))
      throw new IOException("Cannot access input file '"+root+"'");
   
   if (Files.isRegularFile(root) == true)
   {
      if (target.charAt(0) != '.')
         files.add(root);
      
      return;
   }       
    
   // If not a regular file and not a directory (a link ?), fail 
   if (Files.isDirectory(root) == false)
      throw new IOException("Invalid file type '"+root+"'");
   
   String suffix = File.separator + ".";
   String strRoot = root.toString();
   boolean isRecursive = !strRoot.endsWith(suffix); 
   
   if (isRecursive == true)
   {
      if (strRoot.endsWith(File.separator) == false)
         root = Paths.get(strRoot+File.separator);
   }
   else
   {
      // Remove suffix
      root = Paths.get(strRoot.substring(0, strRoot.length()-1));
   }
   
   try (DirectoryStream<Path> stream = Files.newDirectoryStream(root)) 
   {
      for (Path entry: stream) 
      {
         if ((Files.exists(entry) == false) || (Files.isHidden(entry) == true))
            continue;

         if ((Files.isRegularFile(entry) == true) && (String.valueOf(entry.getFileName()).startsWith(".") == false))
            files.add(entry);
         else if ((isRecursive == true) && (Files.isDirectory(entry) == true))
            createFileList(entry.toString(), files);
      }
   } 
   catch (DirectoryIteratorException e) 
   {
     throw e.getCause();
   }
}