Example usage for java.nio.file DirectoryStream iterator

List of usage examples for java.nio.file DirectoryStream iterator

Introduction

In this page you can find the example usage for java.nio.file DirectoryStream iterator.

Prototype

@Override
Iterator<T> iterator();

Source Link

Document

Returns the iterator associated with this DirectoryStream .

Usage

From source file:org.bigbluebutton.api.RecordingService.java

private static List<File> getDirectories(String path) {
    List<File> files = new ArrayList<File>();
    try {//  ww w .  j  av  a  2  s. c  o m
        DirectoryStream<Path> stream = Files.newDirectoryStream(FileSystems.getDefault().getPath(path));
        Iterator<Path> iter = stream.iterator();
        while (iter.hasNext()) {
            Path next = iter.next();
            files.add(next.toFile());
        }
        stream.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return files;
}

From source file:org.azyva.dragom.util.Util.java

public static boolean isDirectoryEmpty(Path path) {
    DirectoryStream<Path> directoryStream;

    try {/*from   ww w.j a  v a2  s  .com*/
        directoryStream = Files.newDirectoryStream(path);
    } catch (IOException ioe) {
        throw new RuntimeException(ioe);
    }

    return !directoryStream.iterator().hasNext();
}

From source file:org.roda.core.storage.fs.FSUtils.java

/**
 * List content of the certain folder/*from   w  w w  .j a  v  a  2 s. c o  m*/
 * 
 * @param basePath
 *          base path
 * @param path
 *          relative path to base path
 * @throws NotFoundException
 * @throws GenericException
 */
public static CloseableIterable<Resource> listPath(final Path basePath, final Path path)
        throws NotFoundException, GenericException {
    CloseableIterable<Resource> resourceIterable;
    try {
        final DirectoryStream<Path> directoryStream = Files.newDirectoryStream(path);
        final Iterator<Path> pathIterator = directoryStream.iterator();
        resourceIterable = new CloseableIterable<Resource>() {

            @Override
            public Iterator<Resource> iterator() {
                return new Iterator<Resource>() {

                    @Override
                    public boolean hasNext() {
                        return pathIterator.hasNext();
                    }

                    @Override
                    public Resource next() {
                        Path next = pathIterator.next();
                        Resource ret;
                        try {
                            ret = convertPathToResource(basePath, next);
                        } catch (GenericException | NotFoundException | RequestNotValidException e) {
                            LOGGER.error(
                                    "Error while list path " + basePath + " while parsing resource " + next, e);
                            ret = null;
                        }

                        return ret;
                    }

                };
            }

            @Override
            public void close() throws IOException {
                directoryStream.close();
            }
        };

    } catch (NoSuchFileException e) {
        throw new NotFoundException("Could not list contents of entity because it doesn't exist: " + path, e);
    } catch (IOException e) {
        throw new GenericException("Could not list contents of entity at: " + path, e);
    }

    return resourceIterable;
}

From source file:org.roda.core.storage.fs.FSUtils.java

/**
 * List containers//from  ww  w  .  j a  v a2s  .c  om
 * 
 * @param basePath
 *          base path
 * @throws GenericException
 */
public static CloseableIterable<Container> listContainers(final Path basePath) throws GenericException {
    CloseableIterable<Container> containerIterable;
    try {
        final DirectoryStream<Path> directoryStream = Files.newDirectoryStream(basePath);
        final Iterator<Path> pathIterator = directoryStream.iterator();
        containerIterable = new CloseableIterable<Container>() {

            @Override
            public Iterator<Container> iterator() {
                return new Iterator<Container>() {

                    @Override
                    public boolean hasNext() {
                        return pathIterator.hasNext();
                    }

                    @Override
                    public Container next() {
                        Path next = pathIterator.next();
                        Container ret;
                        try {
                            ret = convertPathToContainer(basePath, next);
                        } catch (NoSuchElementException | GenericException | RequestNotValidException e) {
                            LOGGER.error("Error while listing containers, while parsing resource " + next, e);
                            ret = null;
                        }

                        return ret;
                    }

                };
            }

            @Override
            public void close() throws IOException {
                directoryStream.close();
            }
        };

    } catch (IOException e) {
        throw new GenericException("Could not list contents of entity at: " + basePath, e);
    }

    return containerIterable;
}

From source file:org.roda.core.storage.fs.FSUtils.java

public static Long countPath(Path directoryPath) throws NotFoundException, GenericException {
    Long count = 0L;//from ww w .  j  a  va2  s  . c  o  m
    DirectoryStream<Path> directoryStream = null;
    try {
        directoryStream = Files.newDirectoryStream(directoryPath);

        final Iterator<Path> pathIterator = directoryStream.iterator();
        while (pathIterator.hasNext()) {
            count++;
            pathIterator.next();
        }

    } catch (NoSuchFileException e) {
        throw new NotFoundException(
                "Could not list contents of entity because it doesn't exist: " + directoryPath);
    } catch (IOException e) {
        throw new GenericException("Could not list contents of entity at: " + directoryPath, e);
    } finally {
        IOUtils.closeQuietly(directoryStream);
    }

    return count;
}

From source file:de.bayern.gdi.model.DownloadStepConverterIT.java

/**
 * Test to execute.//from   w  w  w .ja v a  2  s. c o  m
 *
 * @throws Exception if an exception occured
 */
@Test
public void testConvert() throws Exception {
    LOG.debug("Start test '{}' in test directory: {}", testName, downloadStep.getPath());
    DownloadStepConverter downloadStepConverter = new DownloadStepConverter();
    JobList jobList = downloadStepConverter.convert(downloadStep);

    FileDownloadJob fileDownloadJob = findFileDownloadJob(jobList);
    assertThat(fileDownloadJob, is(notNullValue()));

    fileDownloadJob.download();

    String downloadPath = downloadStep.getPath();
    Path path = Paths.get(downloadPath);

    DirectoryStream<Path> paths = Files.newDirectoryStream(path);
    Iterator<Path> iterator = paths.iterator();
    assertThat(iterator.hasNext(), is(true));
}

From source file:org.cryptomator.ui.InitializeController.java

private boolean isDirectoryEmpty() {
    try {// w  ww  . ja v a2 s  .c o m
        final DirectoryStream<Path> dirContents = Files.newDirectoryStream(directory.getPath());
        return !dirContents.iterator().hasNext();
    } catch (IOException e) {
        LOG.error("Failed to analyze directory.", e);
        throw new IllegalStateException(e);
    }
}

From source file:com.oneops.util.SearchSenderTest.java

private boolean isRetryDirectoryEmpty() {
    DirectoryStream<Path> dirStream = null;
    try {//ww w. j ava2s  . com
        Path retryPath = FileSystems.getDefault().getPath(retryDir);
        dirStream = java.nio.file.Files.newDirectoryStream(retryPath);
        return !dirStream.iterator().hasNext();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            dirStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return true;
}

From source file:org.codice.ddf.catalog.content.impl.FileSystemStorageProvider.java

private boolean isDirectoryEmpty(Path dir) throws IOException {
    DirectoryStream<Path> dirStream = Files.newDirectoryStream(dir);
    return !dirStream.iterator().hasNext();
}

From source file:org.roda.core.storage.fs.FSUtils.java

public static CloseableIterable<BinaryVersion> listBinaryVersions(final Path historyDataPath,
        final Path historyMetadataPath, final StoragePath storagePath)
        throws GenericException, RequestNotValidException, NotFoundException, AuthorizationDeniedException {
    Path fauxPath = getEntityPath(historyDataPath, storagePath);
    final Path parent = fauxPath.getParent();
    final String baseName = fauxPath.getFileName().toString();

    CloseableIterable<BinaryVersion> iterable;

    try {//from ww w.j ava2 s  .c om
        final DirectoryStream<Path> directoryStream = Files.newDirectoryStream(parent,
                new DirectoryStream.Filter<Path>() {

                    @Override
                    public boolean accept(Path entry) throws IOException {
                        String fileName = entry.getFileName().toString();
                        int lastIndexOfDot = fileName.lastIndexOf(VERSION_SEP);

                        return lastIndexOfDot > 0 ? fileName.substring(0, lastIndexOfDot).equals(baseName)
                                : false;
                    }
                });

        final Iterator<Path> pathIterator = directoryStream.iterator();
        iterable = new CloseableIterable<BinaryVersion>() {

            @Override
            public Iterator<BinaryVersion> iterator() {
                return new Iterator<BinaryVersion>() {

                    @Override
                    public boolean hasNext() {
                        return pathIterator.hasNext();
                    }

                    @Override
                    public BinaryVersion next() {
                        Path next = pathIterator.next();
                        BinaryVersion ret;
                        try {
                            ret = convertPathToBinaryVersion(historyDataPath, historyMetadataPath, next);
                        } catch (GenericException | NotFoundException | RequestNotValidException e) {
                            LOGGER.error("Error while list path " + parent + " while parsing resource " + next,
                                    e);
                            ret = null;
                        }

                        return ret;
                    }

                };
            }

            @Override
            public void close() throws IOException {
                directoryStream.close();
            }
        };

    } catch (NoSuchFileException e) {
        throw new NotFoundException("Could not find versions of " + storagePath, e);
    } catch (IOException e) {
        throw new GenericException("Error finding version of " + storagePath, e);
    }

    return iterable;
}