Search in sources :

Example 11 with DirectoryStream

use of java.nio.file.DirectoryStream in project gerrit by GerritCodeReview.

the class StaleLibraryRemover method remove.

public void remove(String pattern) {
    if (!Strings.isNullOrEmpty(pattern)) {
        DirectoryStream.Filter<Path> filter = new DirectoryStream.Filter<Path>() {

            @Override
            public boolean accept(Path entry) {
                return entry.getFileName().toString().matches("^" + pattern + "$");
            }
        };
        try (DirectoryStream<Path> paths = Files.newDirectoryStream(lib_dir, filter)) {
            for (Path p : paths) {
                String old = p.getFileName().toString();
                String bak = "." + old + ".backup";
                Path dest = p.resolveSibling(bak);
                if (Files.exists(dest)) {
                    ui.message("WARNING: not renaming %s to %s: already exists\n", old, bak);
                    continue;
                }
                ui.message("Renaming %s to %s\n", old, bak);
                try {
                    Files.move(p, dest);
                } catch (IOException e) {
                    throw new Die("cannot rename " + old, e);
                }
            }
        } catch (IOException e) {
            throw new Die("cannot remove stale library versions", e);
        }
    }
}
Also used : Path(java.nio.file.Path) Die(com.google.gerrit.common.Die) DirectoryStream(java.nio.file.DirectoryStream) IOException(java.io.IOException)

Example 12 with DirectoryStream

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

the class SeedGenerator method getSystemEntropy.

/**
     * Retrieve some system information, hashed.
     */
static byte[] getSystemEntropy() {
    byte[] ba;
    final MessageDigest md;
    try {
        md = MessageDigest.getInstance("SHA");
    } catch (NoSuchAlgorithmException nsae) {
        throw new InternalError("internal error: SHA-1 not available.", nsae);
    }
    // The current time in millis
    byte b = (byte) System.currentTimeMillis();
    md.update(b);
    java.security.AccessController.doPrivileged(new java.security.PrivilegedAction<Void>() {

        @Override
        public Void run() {
            try {
                // System properties can change from machine to machine
                String s;
                Properties p = System.getProperties();
                Enumeration<?> e = p.propertyNames();
                while (e.hasMoreElements()) {
                    s = (String) e.nextElement();
                    md.update(s.getBytes());
                    md.update(p.getProperty(s).getBytes());
                }
                // Include network adapter names (and a Mac address)
                addNetworkAdapterInfo(md);
                // The temporary dir
                File f = new File(p.getProperty("java.io.tmpdir"));
                int count = 0;
                try (DirectoryStream<Path> stream = Files.newDirectoryStream(f.toPath())) {
                    // We use a Random object to choose what file names
                    // should be used. Otherwise on a machine with too
                    // many files, the same first 1024 files always get
                    // used. Any, We make sure the first 512 files are
                    // always used.
                    Random r = new Random();
                    for (Path entry : stream) {
                        if (count < 512 || r.nextBoolean()) {
                            md.update(entry.getFileName().toString().getBytes());
                        }
                        if (count++ > 1024) {
                            break;
                        }
                    }
                }
            } catch (Exception ex) {
                md.update((byte) ex.hashCode());
            }
            // get Runtime memory stats
            Runtime rt = Runtime.getRuntime();
            byte[] memBytes = longToByteArray(rt.totalMemory());
            md.update(memBytes, 0, memBytes.length);
            memBytes = longToByteArray(rt.freeMemory());
            md.update(memBytes, 0, memBytes.length);
            return null;
        }
    });
    return md.digest();
}
Also used : Path(java.nio.file.Path) Enumeration(java.util.Enumeration) DirectoryStream(java.nio.file.DirectoryStream) Properties(java.util.Properties) java.security(java.security) Random(java.util.Random)

Example 13 with DirectoryStream

use of java.nio.file.DirectoryStream in project jena by apache.

the class FusekiConfig method readConfigurationDirectory.

// ---- Directory of assemblers
/** Read service descriptions in the given directory */
public static List<DataAccessPoint> readConfigurationDirectory(String dir) {
    Path pDir = Paths.get(dir).normalize();
    File dirFile = pDir.toFile();
    if (!dirFile.exists()) {
        log.warn("Not found: directory for assembler files for services: '" + dir + "'");
        return Collections.emptyList();
    }
    if (!dirFile.isDirectory()) {
        log.warn("Not a directory: '" + dir + "'");
        return Collections.emptyList();
    }
    // Files that are not hidden.
    DirectoryStream.Filter<Path> filter = (entry) -> {
        File f = entry.toFile();
        final Lang lang = filenameToLang(f.getName());
        return !f.isHidden() && f.isFile() && lang != null && isRegistered(lang);
    };
    List<DataAccessPoint> dataServiceRef = new ArrayList<>();
    try (DirectoryStream<Path> stream = Files.newDirectoryStream(pDir, filter)) {
        for (Path p : stream) {
            DatasetDescriptionRegistry dsDescMap = FusekiServer.registryForBuild();
            String fn = IRILib.filenameToIRI(p.toString());
            log.info("Load configuration: " + fn);
            Model m = readAssemblerFile(fn);
            readConfiguration(m, dsDescMap, dataServiceRef);
        }
    } catch (IOException ex) {
        log.warn("IOException:" + ex.getMessage(), ex);
    }
    return dataServiceRef;
}
Also used : Path(java.nio.file.Path) RDF(org.apache.jena.vocabulary.RDF) StrUtils(org.apache.jena.atlas.lib.StrUtils) RDFParserRegistry.isRegistered(org.apache.jena.riot.RDFParserRegistry.isRegistered) ArrayList(java.util.ArrayList) JA(org.apache.jena.assembler.JA) DirectoryStream(java.nio.file.DirectoryStream) QuerySolution(org.apache.jena.query.QuerySolution) Fuseki(org.apache.jena.fuseki.Fuseki) Iter(org.apache.jena.atlas.iterator.Iter) FusekiLib(org.apache.jena.fuseki.FusekiLib) Method(java.lang.reflect.Method) Path(java.nio.file.Path) Dataset(org.apache.jena.query.Dataset) Lang(org.apache.jena.riot.Lang) Logger(org.slf4j.Logger) Files(java.nio.file.Files) IOException(java.io.IOException) org.apache.jena.fuseki.server(org.apache.jena.fuseki.server) RDFLanguages.filenameToLang(org.apache.jena.riot.RDFLanguages.filenameToLang) File(java.io.File) org.apache.jena.rdf.model(org.apache.jena.rdf.model) IRILib(org.apache.jena.atlas.lib.IRILib) FusekiConfigException(org.apache.jena.fuseki.FusekiConfigException) List(java.util.List) Paths(java.nio.file.Paths) ReadWrite(org.apache.jena.query.ReadWrite) AssemblerUtils(org.apache.jena.sparql.core.assembler.AssemblerUtils) Collections(java.util.Collections) ResultSet(org.apache.jena.query.ResultSet) DirectoryStream(java.nio.file.DirectoryStream) ArrayList(java.util.ArrayList) Lang(org.apache.jena.riot.Lang) RDFLanguages.filenameToLang(org.apache.jena.riot.RDFLanguages.filenameToLang) IOException(java.io.IOException) File(java.io.File)

Example 14 with DirectoryStream

use of java.nio.file.DirectoryStream in project karaf by apache.

the class ArchiveMojo method archive.

public //ArchiverException,
File archive(//ArchiverException,
File source, //ArchiverException,
File dest, //ArchiverException,
Artifact artifact) throws IOException {
    String serverName = null;
    if (targetFile != null) {
        serverName = targetFile.getName();
    } else {
        serverName = artifact.getArtifactId() + "-" + artifact.getVersion();
    }
    dest = new File(dest, serverName + "." + artifact.getType());
    String prefix = "";
    if (usePathPrefix) {
        prefix = pathPrefix.trim();
        if (prefix.length() > 0 && !prefix.endsWith("/")) {
            prefix += "/";
        }
    }
    if ("tar.gz".equals(artifact.getType())) {
        try (OutputStream fOut = Files.newOutputStream(dest.toPath());
            OutputStream bOut = new BufferedOutputStream(fOut);
            OutputStream gzOut = new GzipCompressorOutputStream(bOut);
            TarArchiveOutputStream tOut = new TarArchiveOutputStream(gzOut);
            DirectoryStream<Path> children = Files.newDirectoryStream(source.toPath())) {
            tOut.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX);
            tOut.setBigNumberMode(TarArchiveOutputStream.BIGNUMBER_POSIX);
            for (Path child : children) {
                addFileToTarGz(tOut, child, prefix);
            }
        }
    } else if ("zip".equals(artifact.getType())) {
        try (OutputStream fOut = Files.newOutputStream(dest.toPath());
            OutputStream bOut = new BufferedOutputStream(fOut);
            ZipArchiveOutputStream tOut = new ZipArchiveOutputStream(bOut);
            DirectoryStream<Path> children = Files.newDirectoryStream(source.toPath())) {
            for (Path child : children) {
                addFileToZip(tOut, child, prefix);
            }
        }
    } else {
        throw new IllegalArgumentException("Unknown target type: " + artifact.getType());
    }
    return dest;
}
Also used : Path(java.nio.file.Path) GzipCompressorOutputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream) OutputStream(java.io.OutputStream) BufferedOutputStream(java.io.BufferedOutputStream) TarArchiveOutputStream(org.apache.commons.compress.archivers.tar.TarArchiveOutputStream) ZipArchiveOutputStream(org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream) GzipCompressorOutputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream) DirectoryStream(java.nio.file.DirectoryStream) ZipArchiveOutputStream(org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream) TarArchiveOutputStream(org.apache.commons.compress.archivers.tar.TarArchiveOutputStream) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream)

Example 15 with DirectoryStream

use of java.nio.file.DirectoryStream in project chilo-producer by cccties.

the class Epub3Maker method main.

public static void main(String[] args) {
    /*
         * 引数を処理
         */
    String homeDir = null;
    String configFile = null;
    String seriesDir = null;
    String template = "basic";
    String inputPath = "./";
    String outputPath = "./";
    String outputName = "";
    boolean doWeko = false;
    for (int ai = 0; ai < args.length && args[ai].startsWith("-"); ai++) {
        if (args[ai].equals("-home")) {
            if (ai < args.length - 1) {
                homeDir = args[++ai];
                continue;
            } else {
                usage();
            }
        } else if (args[ai].equals("-config")) {
            if (ai < args.length - 1) {
                configFile = args[++ai];
                continue;
            } else {
                usage();
            }
        } else if (args[ai].equals("-series")) {
            if (ai < args.length - 1) {
                seriesDir = args[++ai];
                continue;
            } else {
                usage();
            }
        } else if (args[ai].equals("-template")) {
            if (ai < args.length - 1) {
                template = args[++ai];
                continue;
            } else {
                usage();
            }
        } else if (args[ai].equals("-input-path")) {
            if (ai < args.length - 1) {
                inputPath = args[++ai];
            } else {
                usage();
            }
        } else if (args[ai].equals("-output-path")) {
            if (ai < args.length - 1) {
                outputPath = args[++ai];
            } else {
                usage();
            }
        } else if (args[ai].equals("-output-name")) {
            if (ai < args.length - 1) {
                outputName = args[++ai];
            } else {
                usage();
            }
        } else if (args[ai].equals("-weko")) {
            doWeko = true;
        }
    }
    Velocity.addProperty(Log4JLogChute.RUNTIME_LOG_LOG4J_LOGGER, "velocity");
    try {
        /*
             * 設定値を読込
             */
        new Config(configFile, homeDir);
        Config.setTemplate(template);
        Config.setInputPath(inputPath);
        Config.setOutputPath(outputPath);
        Config.setOutputName(outputName);
        ArrayList<Path> seriesList = new ArrayList<Path>();
        Path seriesBasePath = Paths.get(Config.getSeriesBaseDir());
        if (seriesDir == null) {
            /*
                 * series dir 一覧を取得
                 */
            DirectoryStream<Path> stream = Files.newDirectoryStream(seriesBasePath, new DirectoryStream.Filter<Path>() {

                @Override
                public boolean accept(Path entry) throws IOException {
                    return Files.isDirectory(entry);
                }
            });
            for (Path p : stream) {
                seriesList.add(p);
            }
        } else if (seriesDir.endsWith(".xlsx")) {
            seriesList.add(Paths.get(seriesDir));
        } else {
            seriesList.add(seriesBasePath.resolve(seriesDir));
        }
        Process proc = new Process();
        for (Path c : seriesList) {
            proc.process(c, doWeko);
        }
    } catch (Exception e) {
        log.fatal(e.getMessage());
        e.printStackTrace();
    }
}
Also used : Path(java.nio.file.Path) ArrayList(java.util.ArrayList) DirectoryStream(java.nio.file.DirectoryStream) IOException(java.io.IOException) IOException(java.io.IOException)

Aggregations

DirectoryStream (java.nio.file.DirectoryStream)15 Path (java.nio.file.Path)15 IOException (java.io.IOException)7 List (java.util.List)5 File (java.io.File)4 ArrayList (java.util.ArrayList)4 Files (java.nio.file.Files)3 Optional (java.util.Optional)3 Collectors (java.util.stream.Collectors)3 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 FileInputStream (java.io.FileInputStream)2 Paths (java.nio.file.Paths)2 Collections (java.util.Collections)2 Objects (java.util.Objects)2 Properties (java.util.Properties)2 Logger (org.slf4j.Logger)2 Pair (com.facebook.buck.model.Pair)1 DirectoryCleaner (com.facebook.buck.util.DirectoryCleaner)1 DirectoryCleanerArgs (com.facebook.buck.util.DirectoryCleanerArgs)1 Escaper (com.facebook.buck.util.Escaper)1