Example usage for java.nio.file DirectoryStream close

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

Introduction

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

Prototype

public void close() throws IOException;

Source Link

Document

Closes this stream and releases any system resources associated with it.

Usage

From source file:org.glite.authz.pep.pip.provider.InInfoFileIssuerDNMatcher.java

/**
 * Adds all .info files from the grid-security/certificates folder to the
 * variable infoFilesAll. As last the method returns a list containing
 * Strings. The Strings contain all *.info files.
 * /*from w w w .  j  ava 2  s  .  c  o  m*/
 * @return A list of strings. The strings represent *.info file.
 */
private List<String> findAllInfoFiles() throws IOException {
    List<String> infoFilesAll = new ArrayList<String>();
    DirectoryStream<Path> stream = null;
    try {
        stream = Files.newDirectoryStream(Paths.get(acceptedtrustInfoDir), "*.info");
    } catch (Exception e) {
        log.error(e.getMessage());
        return infoFilesAll;
    }

    try {
        for (Path entry : stream) {
            if (Files.isRegularFile(entry, LinkOption.NOFOLLOW_LINKS)) {
                infoFilesAll.add(entry.getFileName().toString());
            }
        }

    } catch (Exception e) {
        log.error(e.getMessage());
    } finally {
        stream.close();
    }

    return infoFilesAll;
}

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  www. ja  v a2s .  co  m*/

    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;
}

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

/**
 * List content of the certain folder/* 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  w w  w  .ja v  a 2s  .c o m*/
 * 
 * @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 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 {/*w ww  .j  a  va 2  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;
}

From source file:org.wso2.carbon.inbound.localfile.LocalFileOneTimePolling.java

/**
 * Before start the watch directory if watchedDir has any files it will start to process.
 *///w w  w  . ja va 2s .  c  o m
private void setProcessAndWatch() {
    Path dir = FileSystems.getDefault().getPath(watchedDir);
    DirectoryStream<Path> stream = null;
    try {
        stream = Files.newDirectoryStream(dir);
        for (Path path : stream) {
            processFile(path, contentType);
        }
    } catch (IOException e) {
        log.error("Error while processing the directory." + e.getMessage(), e);
    } finally {
        try {
            if (stream != null) {
                stream.close();
            }
        } catch (IOException e) {
            log.error("Error while close the DirectoryStream." + e.getMessage(), e);
        }
    }
    startWatch();
}

From source file:training.w2d34e2.java

public static void main(String[] args) {

    final String srcDirPath = "/Users/venkataraghav/Work/training/Sample/Week2/source";
    final String desDirPath = "/Users/venkataraghav/Work/training/Sample/Week2/destination";

    DirectoryStream<Path> directoryStream = null;

    try {/*from  w  w w.  j  a  va  2  s  . c  om*/

        directoryStream = Files.newDirectoryStream(Paths.get(srcDirPath));

        File f = null;

        for (Path filePath : directoryStream) {

            String fName = filePath.getFileName().toString();

            f = new File(desDirPath + "/" + fName);

            int i = 0;

            while (f.exists()) {

                i++;

                String name = fName.substring(0, fName.indexOf('.'));

                String extension = fName.substring(fName.indexOf('.'));

                f = new File(desDirPath + "/" + name + "-" + i + extension);

            }

            FileUtils.copyFile(filePath.toFile(), f);
        }

        directoryStream.close();

    }

    catch (IOException ioe) {

        ioe.printStackTrace();
    }

}