Search in sources :

Example 61 with FileVisitResult

use of java.nio.file.FileVisitResult in project neo4j by neo4j.

the class FileVisitorsDecoratorsTest method shouldPropagateReturnValueFromPostVisitDirectory.

@Test
public void shouldPropagateReturnValueFromPostVisitDirectory() throws IOException {
    for (FileVisitResult result : FileVisitResult.values()) {
        when(wrapped.postVisitDirectory(any(), any())).thenReturn(result);
        assertThat(decorator.postVisitDirectory(null, null), is(result));
    }
}
Also used : FileVisitResult(java.nio.file.FileVisitResult) Test(org.junit.Test)

Example 62 with FileVisitResult

use of java.nio.file.FileVisitResult in project neo4j by neo4j.

the class FileVisitorsDecoratorsTest method shouldPropagateReturnValueFromVisitFileFailed.

@Test
public void shouldPropagateReturnValueFromVisitFileFailed() throws IOException {
    for (FileVisitResult result : FileVisitResult.values()) {
        when(wrapped.visitFileFailed(any(), any())).thenReturn(result);
        assertThat(decorator.visitFileFailed(null, null), is(result));
    }
}
Also used : FileVisitResult(java.nio.file.FileVisitResult) Test(org.junit.Test)

Example 63 with FileVisitResult

use of java.nio.file.FileVisitResult in project wire by square.

the class SchemaLoader method loadFromDirectories.

private Schema loadFromDirectories(Map<Path, Path> directories) throws IOException {
    final Deque<String> protos = new ArrayDeque<>(this.protos);
    if (protos.isEmpty()) {
        for (final Map.Entry<Path, Path> entry : directories.entrySet()) {
            Files.walkFileTree(entry.getValue(), new SimpleFileVisitor<Path>() {

                @Override
                public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                    if (file.getFileName().toString().endsWith(".proto")) {
                        protos.add(entry.getValue().relativize(file).toString());
                    }
                    return FileVisitResult.CONTINUE;
                }
            });
        }
    }
    Map<String, ProtoFile> loaded = new LinkedHashMap<>();
    loaded.put(DESCRIPTOR_PROTO, loadDescriptorProto());
    while (!protos.isEmpty()) {
        String proto = protos.removeFirst();
        if (loaded.containsKey(proto)) {
            continue;
        }
        ProtoFileElement element = null;
        for (Map.Entry<Path, Path> entry : directories.entrySet()) {
            Source source = source(entry.getValue(), proto);
            if (source == null) {
                continue;
            }
            Path base = entry.getKey();
            try {
                Location location = Location.get(base.toString(), proto);
                String data = Okio.buffer(source).readUtf8();
                element = ProtoParser.parse(location, data);
                break;
            } catch (IOException e) {
                throw new IOException("Failed to load " + proto + " from " + base, e);
            } finally {
                source.close();
            }
        }
        if (element == null) {
            throw new FileNotFoundException("Failed to locate " + proto + " in " + sources);
        }
        ProtoFile protoFile = ProtoFile.get(element);
        loaded.put(proto, protoFile);
        // Queue dependencies to be loaded.
        for (String importPath : element.imports()) {
            protos.addLast(importPath);
        }
    }
    return new Linker(loaded.values()).link();
}
Also used : Path(java.nio.file.Path) FileNotFoundException(java.io.FileNotFoundException) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) ProtoFileElement(com.squareup.wire.schema.internal.parser.ProtoFileElement) ArrayDeque(java.util.ArrayDeque) Source(okio.Source) BufferedSource(okio.BufferedSource) LinkedHashMap(java.util.LinkedHashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Example 64 with FileVisitResult

use of java.nio.file.FileVisitResult in project zaproxy by zaproxy.

the class VulnerabilitiesLoader method getListOfVulnerabilitiesFiles.

/**
	 * Returns a {@code List} of resources files with {@code fileName} and {@code fileExtension} contained in the
	 * {@code directory}.
	 *
	 * @return the list of resources files contained in the {@code directory}
	 * @see LocaleUtils#createResourceFilesPattern(String, String)
	 */
private List<String> getListOfVulnerabilitiesFiles() {
    final Pattern filePattern = LocaleUtils.createResourceFilesPattern(fileName, fileExtension);
    final List<String> fileNames = new ArrayList<>();
    try {
        Files.walkFileTree(directory, Collections.<FileVisitOption>emptySet(), 1, new SimpleFileVisitor<Path>() {

            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                String fileName = file.getFileName().toString();
                if (filePattern.matcher(fileName).matches()) {
                    fileNames.add(fileName);
                }
                return FileVisitResult.CONTINUE;
            }
        });
    } catch (IOException e) {
        logger.error("An error occurred while walking directory: " + directory, e);
    }
    return fileNames;
}
Also used : Path(java.nio.file.Path) Pattern(java.util.regex.Pattern) ArrayList(java.util.ArrayList) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Example 65 with FileVisitResult

use of java.nio.file.FileVisitResult in project alluxio by Alluxio.

the class FileUtils method deletePathRecursively.

/**
   * Deletes a path recursively.
   *
   * @param path pathname to be deleted
   * @throws IOException when fails to delete
   */
public static void deletePathRecursively(String path) throws IOException {
    Path root = Paths.get(path);
    Files.walkFileTree(root, new SimpleFileVisitor<Path>() {

        @Override
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
            Files.delete(file);
            return FileVisitResult.CONTINUE;
        }

        @Override
        public FileVisitResult postVisitDirectory(Path dir, IOException e) throws IOException {
            if (e == null) {
                Files.delete(dir);
                return FileVisitResult.CONTINUE;
            } else {
                throw e;
            }
        }
    });
}
Also used : Path(java.nio.file.Path) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Aggregations

FileVisitResult (java.nio.file.FileVisitResult)74 Path (java.nio.file.Path)67 BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)66 IOException (java.io.IOException)65 File (java.io.File)10 ArrayList (java.util.ArrayList)9 Test (org.junit.Test)8 InputStream (java.io.InputStream)4 HashSet (java.util.HashSet)4 JarEntry (java.util.jar.JarEntry)4 ImmutableList (com.google.common.collect.ImmutableList)3 ImmutableSet (com.google.common.collect.ImmutableSet)3 FileOutputStream (java.io.FileOutputStream)3 JarOutputStream (java.util.jar.JarOutputStream)3 PathSourcePath (com.facebook.buck.rules.PathSourcePath)2 SourcePath (com.facebook.buck.rules.SourcePath)2 ImmutableSortedSet (com.google.common.collect.ImmutableSortedSet)2 FileSystemException (io.vertx.core.file.FileSystemException)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 FileInputStream (java.io.FileInputStream)2