Example usage for java.nio.file Files newDirectoryStream

List of usage examples for java.nio.file Files newDirectoryStream

Introduction

In this page you can find the example usage for java.nio.file Files newDirectoryStream.

Prototype

public static DirectoryStream<Path> newDirectoryStream(Path dir, DirectoryStream.Filter<? super Path> filter)
        throws IOException 

Source Link

Document

Opens a directory, returning a DirectoryStream to iterate over the entries in the directory.

Usage

From source file:org.sonar.java.AbstractJavaClasspath.java

private static List<File> getLibs(Path dir) throws IOException {
    Filter<Path> filter = path -> {
        String name = path.getFileName().toString();
        return name.endsWith(".jar") || name.endsWith(".zip") || name.endsWith(".aar");
    };/*from   w w w . j a v a 2s  . c  o  m*/

    List<File> files = new ArrayList<>();
    try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir, filter)) {
        stream.forEach(p -> files.add(p.toFile()));
    }
    return files;
}

From source file:net.kemuri9.sling.filesystemprovider.impl.PersistenceHelper.java

private static Path getPropertyFile(FileSystemProviderResource resource) {
    try {/* ww  w  .j a va 2  s .  c o  m*/
        Iterator<Path> files = Files.newDirectoryStream(resource.getFile(), DIR_STREAM_FILTER_PROPERTIES)
                .iterator();
        return (files.hasNext()) ? files.next() : null;
    } catch (IOException e) {
        log.error("unable to find properties file");
    }
    return null;
}

From source file:org.alfresco.repo.bulkimport.impl.DirectoryAnalyserImpl.java

private List<Path> listFiles(Path sourceDirectory, DirectoryStream.Filter<Path> filter) {
    List<Path> files = new ArrayList<Path>();
    try (DirectoryStream<Path> paths = (filter != null) ? Files.newDirectoryStream(sourceDirectory, filter)
            : Files.newDirectoryStream(sourceDirectory)) {
        for (Iterator<Path> it = paths.iterator(); it.hasNext();) {
            files.add(it.next());/*from w  ww. j av a 2 s.  com*/
        }
    } catch (IOException e) {
        log.error(e.getMessage());
    }
    return files;
}

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.
 * /* www .  j  a  v a2 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:neembuu.uploader.zip.generator.NUZipFileGenerator.java

private void handleClassEntry(Path pathInZip, final Class c, FileSystem fs, Path uploadersDirectory)
        throws IOException {
    Path classLocationOnDisk = uploadersDirectory.resolve(pathInZip.toString());
    DirectoryStream<Path> ds = Files.newDirectoryStream(classLocationOnDisk,
            new DirectoryStream.Filter<Path>() {
                @Override/*from   ww  w .  j  av a 2  s .com*/
                public boolean accept(Path entry) throws IOException {
                    String fn = entry.getFileName().toString();
                    String cn = c.getSimpleName();
                    return fn.equals(cn + ".class") || fn.startsWith(cn + "$");
                }
            });
    for (Path p : ds) {
        byte[] b = Files.readAllBytes(p);
        Files.write(pathInZip.resolve(p.getFileName().toString()), b);
    }

    // say we want to zie SomeClass.class
    // then we also need to zip SomeClass$1.class
    // That is, we also need to zip inner classes and inner annoymous classes 
    // into the zip as well
}

From source file:com.cpjit.swagger4j.util.ReflectUtils.java

/**
 * ????//ww  w . java 2 s.  c om
 * 
 * @param packageName
 * @return ????null
 *       0
 *       <li>?packNames0</li>
 *       <li>??</li>
 * @throws FileNotFoundException ?
 * @throws IllegalArgumentException ???
 * @since 1.0.0
 */
public static List<String> scanClazzName(String packageName)
        throws FileNotFoundException, IllegalArgumentException {
    String pack2path = packageName.replaceAll("\\.", "/");
    if (StringUtils.isBlank(packageName)) {
        pack2path = "";
    }

    URL fullPath = ResourceUtil.getResource(pack2path);
    if (fullPath == null) {
        throw new FileNotFoundException("[" + packageName + "]?");
    }
    Path pack;
    try {
        pack = Paths.get(fullPath.toURI());
    } catch (URISyntaxException e) {
        throw new IllegalArgumentException(e);
    }
    if (!Files.isDirectory(pack)) {
        throw new IllegalArgumentException("[" + packageName + "]????");
    }

    Set<String> clazzNames = new HashSet<String>();
    String prefix = StringUtils.isBlank(packageName) ? "" : packageName + ".";
    // ?Java?
    try (DirectoryStream<Path> clazzs = Files.newDirectoryStream(pack, "*.class");) {
        clazzs.forEach(clazz -> { // ???
            String clazzName = prefix + clazz.getFileName().toString().replace(".class", "");
            clazzNames.add(clazzName);
        });
    } catch (IOException e) {
        throw new IllegalArgumentException(e);
    }

    if (clazzNames.size() < 1) { // ?
        return Collections.emptyList();
    }
    return new ArrayList<String>(clazzNames);
}

From source file:org.opencastproject.staticfiles.impl.StaticFileServiceImpl.java

private Path getFile(final String org, final String uuid) throws NotFoundException, IOException {
    // First check if the file is part of the durable storage section
    try (DirectoryStream<Path> dirs = Files.newDirectoryStream(getDurableStorageDir(org),
            getDirsEqualsUuidFilter(uuid))) {
        for (Path dir : dirs) {
            try (DirectoryStream<Path> files = Files.newDirectoryStream(dir)) {
                for (Path file : files) {
                    return file;
                }// w  ww.j av  a2s.c  o  m
            }
        }
    }

    // Second check if the file is part of the temporary storage section
    try (DirectoryStream<Path> dirs = Files.newDirectoryStream(getTemporaryStorageDir(org),
            getDirsEqualsUuidFilter(uuid))) {
        for (Path dir : dirs) {
            try (DirectoryStream<Path> files = Files.newDirectoryStream(dir)) {
                for (Path file : files) {
                    return file;
                }
            }
        }
    }

    throw new NotFoundException(format("No file with UUID '%s' found.", uuid));
}

From source file:org.codice.ddf.configuration.admin.ConfigurationAdminMigration.java

private DirectoryStream<Path> getFailedDirectoryStream() throws IOException {
    return Files.newDirectoryStream(failedDirectory, FILE_FILTER);
}

From source file:dk.dma.msiproxy.common.repo.RepositoryService.java

/**
 * Returns a list of files in the folder specified by the path
 * @param path the path//from  ww w  .j a v  a 2  s.c om
 * @return the list of files in the folder specified by the path
 */
@GET
@javax.ws.rs.Path("/list/{folder:.+}")
@Produces("application/json;charset=UTF-8")
@NoCache
public List<Attachment> listFiles(@PathParam("folder") String path) throws IOException {

    List<Attachment> result = new ArrayList<>();
    Path folder = repoRoot.resolve(path);

    if (Files.exists(folder) && Files.isDirectory(folder)) {

        // Filter out directories, hidden files, thumbnails and map images
        DirectoryStream.Filter<Path> filter = file -> Files.isRegularFile(file)
                && !file.getFileName().toString().startsWith(".")
                && !file.getFileName().toString().matches(".+_thumb_\\d{1,3}\\.\\w+") && // Thumbnails
                !file.getFileName().toString().matches("map_\\d{1,3}\\.png"); // Map image

        try (DirectoryStream<Path> stream = Files.newDirectoryStream(folder, filter)) {
            stream.forEach(f -> {
                Attachment vo = new Attachment();
                vo.setName(f.getFileName().toString());
                vo.setPath(WebUtils.encodeURI(path + "/" + f.getFileName().toString()));
                vo.setDirectory(Files.isDirectory(f));
                try {
                    vo.setUpdated(new Date(Files.getLastModifiedTime(f).toMillis()));
                    vo.setSize(Files.size(f));
                } catch (Exception e) {
                    log.trace("Error reading file attribute for " + f);
                }
                result.add(vo);
            });
        }
    }
    return result;
}

From source file:codes.thischwa.c5c.impl.LocalConnector.java

/**
 * Construct from dir request./*from w w  w.  j  a  v  a  2 s . c  om*/
 * @param dir the dir
 * @param needSize the need size
 * @return the folder info
 * @throws C5CException the connector exception
 */
private Set<FileProperties> constructFromDirRequest(Path dir, boolean needSize) throws C5CException {
    Set<FileProperties> props = new HashSet<>();

    // add dirs
    try {
        for (Path d : Files.newDirectoryStream(dir, new DirectoryStream.Filter<Path>() {
            @Override
            public boolean accept(Path entry) throws IOException {
                return Files.isDirectory(entry) && checkFolderName(entry.getFileName().toString());
            }
        })) {
            boolean isProtected = isProtected(d);
            FileProperties fp = buildForDirectory(d.getFileName().toString(), isProtected,
                    new Date(Files.getLastModifiedTime(d).toMillis()));
            props.add(fp);
        }
    } catch (IOException | SecurityException e) {
        throw new C5CException(String.format("Error while fetching sub-directories from [%s]: %s",
                dir.toAbsolutePath().toString(), e.getMessage()));
    }

    // add files
    try {
        for (Path f : Files.newDirectoryStream(dir, new DirectoryStream.Filter<Path>() {
            @Override
            public boolean accept(Path entry) throws IOException {
                return Files.isRegularFile(entry) && checkFilename(entry.getFileName().toString());
            }
        })) {
            props.add(constructFileInfo(f, needSize));
        }
    } catch (IOException | SecurityException e) {
        throw new C5CException(String.format("Error while fetching files from [%s]: %s",
                dir.toAbsolutePath().toString(), e.getMessage()));
    }

    return props;
}