Search in sources :

Example 11 with FileVisitResult

use of java.nio.file.FileVisitResult in project dex2jar by pxb1988.

the class WebApp method main.

/**
     * @param args
     * @throws IOException
     */
public static void main(String[] args) throws IOException {
    if (args.length < 2) {
        System.out.println("webapp pathToWebApp config [ignoreJarConfig]");
        return;
    }
    File webApp = new File(args[0]);
    File config = new File(args[1]);
    Path jarIgnore = args.length > 2 ? new File(args[2]).toPath() : null;
    Path clz = new File(webApp, "WEB-INF/classes").toPath();
    Path tmpClz = new File(webApp, "WEB-INF/tmp-classes").toPath();
    final InvocationWeaver ro = (InvocationWeaver) new InvocationWeaver().withConfig(config.toPath());
    Files.deleteIfExists(tmpClz);
    copyDirectory(clz, tmpClz);
    System.out.println("InvocationWeaver from [" + tmpClz + "] to [" + clz + "]");
    ro.wave(tmpClz, clz);
    Files.deleteIfExists(tmpClz);
    final File lib = new File(webApp, "WEB-INF/lib");
    Path tmpLib = new File(webApp, "WEB-INF/Nlib").toPath();
    final Set<String> ignores = new HashSet<String>();
    if (jarIgnore != null && Files.exists(jarIgnore)) {
        ignores.addAll(Files.readAllLines(jarIgnore, StandardCharsets.UTF_8));
    } else {
        System.out.println("ignoreJarConfig ignored");
    }
    Files.deleteIfExists(tmpLib);
    copyDirectory(lib.toPath(), tmpLib);
    Files.walkFileTree(tmpLib, new SimpleFileVisitor<Path>() {

        @Override
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
            if (file.getFileName().toString().endsWith(".jar")) {
                final String s = file.getFileName().toString();
                boolean ignore = false;
                for (String i : ignores) {
                    if (s.startsWith(i)) {
                        ignore = true;
                        break;
                    }
                }
                if (!ignore) {
                    Path nJar = new File(lib, s).toPath();
                    System.out.println("InvocationWeaver from [" + file + "] to [" + nJar + "]");
                    ro.wave(file, nJar);
                }
            }
            return super.visitFile(file, attrs);
        }
    });
    Files.deleteIfExists(tmpLib);
}
Also used : Path(java.nio.file.Path) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) File(java.io.File) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) HashSet(java.util.HashSet)

Example 12 with FileVisitResult

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

the class AfcClient method upload.

/**
     * Uploads a local file or directory to the device.
     * 
     * @param localFile the file or directory to upload.
     * @param targetPath the path of the directory on the device where to place 
     *                   the uploaded files.
     * @param callback callback which will receive progress and status updates.
     *                 If <code>null</code> no progress will be reported.
     */
public void upload(File localFile, final String targetPath, final UploadProgressCallback callback) throws IOException {
    makeDirectory(targetPath);
    final Path root = localFile.toPath().getParent();
    // 64k seems to be a good buffer size. If smaller we will not get
    // acceptable write speeds.
    final byte[] buffer = new byte[64 * 1024];
    class FileCounterVisitor extends SimpleFileVisitor<Path> {

        int count;

        @Override
        public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
            count++;
            return FileVisitResult.CONTINUE;
        }

        @Override
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
            count++;
            return FileVisitResult.CONTINUE;
        }
    }
    FileCounterVisitor visitor = new FileCounterVisitor();
    if (callback != null) {
        Files.walkFileTree(localFile.toPath(), visitor);
    }
    try {
        final int fileCount = visitor.count;
        Files.walkFileTree(localFile.toPath(), new SimpleFileVisitor<Path>() {

            int filesUploaded = 0;

            private void reportProgress(Path path) {
                if (callback != null) {
                    callback.progress(path.toFile(), 100 * filesUploaded / fileCount);
                }
                filesUploaded++;
            }

            @Override
            public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
                reportProgress(dir);
                String deviceDir = toAbsoluteDevicePath(targetPath, root.relativize(dir));
                makeDirectory(deviceDir);
                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                reportProgress(file);
                String deviceFile = toAbsoluteDevicePath(targetPath, root.relativize(file));
                if (Files.isSymbolicLink(file)) {
                    Path linkTargetPath = Files.readSymbolicLink(file);
                    makeLink(AfcLinkType.AFC_SYMLINK, toRelativeDevicePath(linkTargetPath), deviceFile);
                } else if (Files.isRegularFile(file, LinkOption.NOFOLLOW_LINKS)) {
                    long fd = fileOpen(deviceFile, AfcFileMode.AFC_FOPEN_WRONLY);
                    try (InputStream is = Files.newInputStream(file)) {
                        int n = 0;
                        while ((n = is.read(buffer)) != -1) {
                            fileWrite(fd, buffer, 0, n);
                        }
                    } finally {
                        fileClose(fd);
                    }
                }
                return FileVisitResult.CONTINUE;
            }
        });
        if (callback != null) {
            callback.success();
        }
    } catch (IOException e) {
        if (callback != null) {
            callback.error(e.getMessage());
        }
        throw e;
    } catch (LibIMobileDeviceException e) {
        if (callback != null) {
            callback.error(e.getMessage());
        }
        throw e;
    }
}
Also used : Path(java.nio.file.Path) SimpleFileVisitor(java.nio.file.SimpleFileVisitor) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Example 13 with FileVisitResult

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

the class ParsingTester method main.

public static void main(String... args) throws IOException {
    final AtomicLong total = new AtomicLong();
    final AtomicLong failed = new AtomicLong();
    Files.walkFileTree(ROOT, new SimpleFileVisitor<Path>() {

        @Override
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
            if (file.getFileName().toString().endsWith(".proto")) {
                total.incrementAndGet();
                String data = new String(Files.readAllBytes(file), UTF_8);
                Location location = Location.get(ROOT.toString(), file.toString());
                try {
                    ProtoParser.parse(location, data);
                } catch (Exception e) {
                    e.printStackTrace();
                    failed.incrementAndGet();
                }
            }
            return FileVisitResult.CONTINUE;
        }
    });
    System.out.println("\nTotal: " + total.get() + "  Failed: " + failed.get());
    if (failed.get() == 0) {
        new SchemaLoader().addSource(ROOT).load();
        System.out.println("All files linked successfully.");
    }
}
Also used : Path(java.nio.file.Path) AtomicLong(java.util.concurrent.atomic.AtomicLong) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) IOException(java.io.IOException)

Example 14 with FileVisitResult

use of java.nio.file.FileVisitResult in project bazel by bazelbuild.

the class SimpleJavaLibraryBuilder method setUpSourceJars.

/**
   * Extracts the all source jars from the build request into the temporary directory specified in
   * the build request. Empties the temporary directory, if it exists.
   */
private void setUpSourceJars(JavaLibraryBuildRequest build) throws IOException {
    String sourcesDir = build.getTempDir();
    Path sourceDirFile = Paths.get(sourcesDir);
    if (Files.exists(sourceDirFile)) {
        cleanupDirectory(sourceDirFile);
    }
    if (build.getSourceJars().isEmpty()) {
        return;
    }
    final ByteArrayOutputStream protobufMetadataBuffer = new ByteArrayOutputStream();
    for (String sourceJar : build.getSourceJars()) {
        for (Path root : getJarFileSystem(Paths.get(sourceJar)).getRootDirectories()) {
            Files.walkFileTree(root, new SimpleFileVisitor<Path>() {

                @Override
                public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) throws IOException {
                    String fileName = path.getFileName().toString();
                    if (fileName.endsWith(".java")) {
                        build.getSourceFiles().add(path);
                    } else if (fileName.equals(PROTOBUF_META_NAME)) {
                        Files.copy(path, protobufMetadataBuffer);
                    }
                    return FileVisitResult.CONTINUE;
                }
            });
        }
    }
    Path output = Paths.get(build.getClassDir(), PROTOBUF_META_NAME);
    if (protobufMetadataBuffer.size() > 0) {
        try (OutputStream outputStream = Files.newOutputStream(output)) {
            protobufMetadataBuffer.writeTo(outputStream);
        }
    } else if (Files.exists(output)) {
        // Delete stalled meta file.
        Files.delete(output);
    }
}
Also used : Path(java.nio.file.Path) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) FileVisitResult(java.nio.file.FileVisitResult) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Example 15 with FileVisitResult

use of java.nio.file.FileVisitResult in project jdk8u_jdk by JetBrains.

the class TestWsImport method main.

public static void main(String[] args) throws IOException {
    String javaHome = System.getProperty("java.home");
    if (javaHome.endsWith("jre")) {
        javaHome = new File(javaHome).getParent();
    }
    String wsimport = javaHome + File.separator + "bin" + File.separator + "wsimport";
    if (System.getProperty("os.name").startsWith("Windows")) {
        wsimport = wsimport.concat(".exe");
    }
    Endpoint endpoint = Endpoint.create(new TestService());
    HttpServer httpServer = null;
    try {
        // Manually create HttpServer here using ephemeral address for port
        // so as to not end up with attempt to bind to an in-use port
        httpServer = HttpServer.create(new InetSocketAddress(0), 0);
        HttpContext httpContext = httpServer.createContext("/hello");
        int port = httpServer.getAddress().getPort();
        System.out.println("port = " + port);
        httpServer.start();
        endpoint.publish(httpContext);
        String address = "http://localhost:" + port + "/hello";
        Service service = Service.create(new URL(address + "?wsdl"), new QName("http://test/jaxws/sample/", "TestService"));
        String[] wsargs = { wsimport, "-p", "wstest", "-J-Djavax.xml.accessExternalSchema=all", "-J-Dcom.sun.tools.internal.ws.Invoker.noSystemProxies=true", address + "?wsdl", "-clientjar", "wsjar.jar" };
        ProcessBuilder pb = new ProcessBuilder(wsargs);
        pb.redirectErrorStream(true);
        Process p = pb.start();
        BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
        String s = r.readLine();
        while (s != null) {
            System.out.println(s.trim());
            s = r.readLine();
        }
        p.waitFor();
        p.destroy();
        try (JarFile jarFile = new JarFile("wsjar.jar")) {
            for (Enumeration em = jarFile.entries(); em.hasMoreElements(); ) {
                String fileName = em.nextElement().toString();
                if (fileName.contains("\\")) {
                    throw new RuntimeException("\"\\\" character detected in jar file: " + fileName);
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        throw new RuntimeException(e.getMessage());
    } finally {
        endpoint.stop();
        if (httpServer != null) {
            httpServer.stop(0);
        }
        Path p = Paths.get("wsjar.jar");
        Files.deleteIfExists(p);
        p = Paths.get("wstest");
        if (Files.exists(p)) {
            try {
                Files.walkFileTree(p, new SimpleFileVisitor<Path>() {

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

                    @Override
                    public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
                        if (exc == null) {
                            Files.delete(dir);
                            return CONTINUE;
                        } else {
                            throw exc;
                        }
                    }
                });
            } catch (IOException ioe) {
                ioe.printStackTrace();
            }
        }
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) URL(java.net.URL) Endpoint(javax.xml.ws.Endpoint) HttpServer(com.sun.net.httpserver.HttpServer) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) Path(java.nio.file.Path) Enumeration(java.util.Enumeration) InputStreamReader(java.io.InputStreamReader) QName(javax.xml.namespace.QName) HttpContext(com.sun.net.httpserver.HttpContext) Service(javax.xml.ws.Service) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) JarFile(java.util.jar.JarFile) Endpoint(javax.xml.ws.Endpoint) IOException(java.io.IOException) BufferedReader(java.io.BufferedReader) JarFile(java.util.jar.JarFile) File(java.io.File)

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