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.codice.ddf.configuration.admin.ConfigurationAdminMigrationTest.java

private void setUpTwoConfigFileIterator(DirectoryStream<Path> stream) {
    when(stream.iterator()).thenReturn(configFilesIterator);
    when(configFilesIterator.hasNext()).thenReturn(true).thenReturn(true).thenReturn(false);
    when(configFilesIterator.next()).thenReturn(CONFIG_PATH1).thenReturn(CONFIG_PATH2);
}

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

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

    CloseableIterable<BinaryVersion> iterable;

    if (!FSUtils.exists(parent)) {
        return new EmptyClosableIterable<>();
    }/*from   w  w  w. j  a va 2  s  . c om*/

    try {
        final DirectoryStream<Path> directoryStream = Files.newDirectoryStream(parent,
                new DirectoryStream.Filter<Path>() {

                    @Override
                    public boolean accept(Path entry) throws IOException {
                        return entry.getFileName().toString().startsWith(baseName);
                    }
                });

        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 = FSUtils.convertPathToBinaryVersion(historyDataPath, historyMetadataPath,
                                    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 find versions of " + storagePath, e);
    } catch (IOException e) {
        throw new GenericException("Error finding version of " + storagePath, e);
    }

    return iterable;
}