Example usage for java.nio.file Path subpath

List of usage examples for java.nio.file Path subpath

Introduction

In this page you can find the example usage for java.nio.file Path subpath.

Prototype

Path subpath(int beginIndex, int endIndex);

Source Link

Document

Returns a relative Path that is a subsequence of the name elements of this path.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    Path path = Paths.get("/home", "docs", "users.txt");
    System.out.println("subpath: " + path.subpath(0, 3));

}

From source file:Main.java

public static void main(String[] args) {

    Path path = Paths.get("C:", "tutorial/Java/JavaFX", "Topic.txt");

    System.out.println("Subpath (0,3): " + path.subpath(0, 3));
}

From source file:at.spardat.xma.xdelta.JarPatcher.java

/**
 * Main method to make {@link #applyDelta(ZipFile, ZipFile, ZipArchiveOutputStream, BufferedReader)} available at
 * the command line.<br>//from  w  w  w. j  ava 2  s  .  com
 * usage JarPatcher source patch output
 *
 * @param args the arguments
 * @throws IOException Signals that an I/O exception has occurred.
 */
public static void main(String[] args) throws IOException {
    String patchName = null;
    String outputName = null;
    String sourceName = null;
    if (args.length == 0) {
        System.err.println("usage JarPatcher patch [output [source]]");
        System.exit(1);
    } else {
        patchName = args[0];
        if (args.length > 1) {
            outputName = args[1];
            if (args.length > 2) {
                sourceName = args[2];
            }
        }
    }
    ZipFile patch = new ZipFile(patchName);
    ZipArchiveEntry listEntry = patch.getEntry("META-INF/file.list");
    if (listEntry == null) {
        System.err.println("Invalid patch - list entry 'META-INF/file.list' not found");
        System.exit(2);
    }
    BufferedReader list = new BufferedReader(new InputStreamReader(patch.getInputStream(listEntry)));
    String next = list.readLine();
    if (sourceName == null) {
        sourceName = next;
    }
    next = list.readLine();
    if (outputName == null) {
        outputName = next;
    }
    int ignoreSourcePaths = Integer.parseInt(System.getProperty("patcher.ignoreSourcePathElements", "0"));
    int ignoreOutputPaths = Integer.parseInt(System.getProperty("patcher.ignoreOutputPathElements", "0"));
    Path sourcePath = Paths.get(sourceName);
    Path outputPath = Paths.get(outputName);
    if (ignoreOutputPaths >= outputPath.getNameCount()) {
        patch.close();
        StringBuilder b = new StringBuilder().append("Not enough path elements to ignore in output (")
                .append(ignoreOutputPaths).append(" in ").append(outputName).append(")");
        throw new IOException(b.toString());
    }
    if (ignoreSourcePaths >= sourcePath.getNameCount()) {
        patch.close();
        StringBuilder b = new StringBuilder().append("Not enough path elements to ignore in source (")
                .append(sourcePath).append(" in ").append(sourceName).append(")");
        throw new IOException(b.toString());
    }
    sourcePath = sourcePath.subpath(ignoreSourcePaths, sourcePath.getNameCount());
    outputPath = outputPath.subpath(ignoreOutputPaths, outputPath.getNameCount());
    File sourceFile = sourcePath.toFile();
    File outputFile = outputPath.toFile();
    if (!(outputFile.getAbsoluteFile().getParentFile().mkdirs()
            || outputFile.getAbsoluteFile().getParentFile().exists())) {
        patch.close();
        throw new IOException("Failed to create " + outputFile.getAbsolutePath());
    }
    new JarPatcher(patchName, sourceFile.getName()).applyDelta(patch, new ZipFile(sourceFile),
            new ZipArchiveOutputStream(new FileOutputStream(outputFile)), list);
    list.close();
}

From source file:com.github.blindpirate.gogradle.util.StringUtils.java

public static Stream<String> eachSubPath(String packagePath) {
    Path path = Paths.get(packagePath);
    return IntStream.range(0, path.getNameCount()).mapToObj(i -> path.subpath(0, i + 1))
            .map(StringUtils::toUnixString);
}

From source file:com.github.blindpirate.gogradle.util.StringUtils.java

public static Stream<String> eachSubPathReverse(String packagePath) {
    Path path = Paths.get(packagePath);
    return IntStream.range(0, path.getNameCount()).mapToObj(i -> path.subpath(0, path.getNameCount() - i))
            .map(StringUtils::toUnixString);
}

From source file:jvmoptions.OptionAnalyzer.java

static Map<String, Map<String, String>> parse(Path hpp) {
    System.out.printf("process %s %n", hpp);
    String file = hpp.subpath(4, hpp.getNameCount()).toString().replace(File.separatorChar, '/');
    try {//  w  w  w.j  a  v  a  2  s . c om
        String all = preprocess(hpp);

        String categories = "(?<kind>develop|develop_pd|product|product_pd|diagnostic|experimental|notproduct|manageable|product_rw|lp64_product)";

        // detect Regex bugs.
        List<String> names = parseNames(all, categories);
        Set<String> nameSet = new HashSet<>();

        Pattern descPtn = Pattern.compile("\"((\\\\\"|[^\"])+)\"");
        Pattern pattern = Pattern.compile(categories
                + "\\((?<type>\\w+?),[ ]*(?<name>\\w+)[ ]*(,[ ]*(?<default>[\\w ()\\-+/*.\"]+))?,[ ]*(?<desc>("
                + descPtn.pattern() + "[ ]*)+)\\)");

        Map<String, Map<String, String>> result = new HashMap<>();

        int times = 0;
        for (Matcher matcher = pattern.matcher(all); matcher.find(); times++) {
            String name = matcher.group("name");
            verify(names, nameSet, times, name);

            String def = Objects.toString(matcher.group("default"), "");

            String d = matcher.group("desc");
            StringBuilder desc = new StringBuilder();
            for (Matcher m = descPtn.matcher(d); m.find();) {
                desc.append(m.group(1).replaceAll("\\\\", ""));
            }

            Map<String, String> m = new HashMap<>();
            m.put("kind", matcher.group("kind"));
            m.put("type", matcher.group("type"));
            m.put("default", def);
            m.put("description", desc.toString());
            m.put("file", file);
            result.put(name, m);
        }
        System.out.printf("        %s contains %d options%n", hpp, times);

        return result;
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
}

From source file:io.crate.testing.Utils.java

static void uncompressTarGZ(File tarFile, File dest) throws IOException {
    TarArchiveInputStream tarIn = new TarArchiveInputStream(
            new GzipCompressorInputStream(new BufferedInputStream(new FileInputStream(tarFile))));

    TarArchiveEntry tarEntry = tarIn.getNextTarEntry();
    // tarIn is a TarArchiveInputStream
    while (tarEntry != null) {
        Path entryPath = Paths.get(tarEntry.getName());

        if (entryPath.getNameCount() == 1) {
            tarEntry = tarIn.getNextTarEntry();
            continue;
        }/*from   ww  w.jav a 2 s  . c  o m*/

        Path strippedPath = entryPath.subpath(1, entryPath.getNameCount());
        File destPath = new File(dest, strippedPath.toString());

        if (tarEntry.isDirectory()) {
            destPath.mkdirs();
        } else {
            destPath.createNewFile();
            byte[] btoRead = new byte[1024];
            BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream(destPath));
            int len;
            while ((len = tarIn.read(btoRead)) != -1) {
                bout.write(btoRead, 0, len);
            }

            bout.close();
            if (destPath.getParent().equals(dest.getPath() + "/bin")) {
                destPath.setExecutable(true);
            }
        }
        tarEntry = tarIn.getNextTarEntry();
    }
    tarIn.close();
}

From source file:de.elomagic.carafile.client.CaraCloud.java

String getSubPath(final Path path) {
    Path p = path.subpath(basePath.getNameCount(), path.getNameCount());

    return p.toString().replace("\\", "/");
}

From source file:at.tfr.securefs.process.ProcessFilesBean.java

public void copy(Path fromPath, int fromStartIndex, Path toRootPath, CrypterProvider cp, BigInteger newSecret,
        ProcessFilesData cfd) {/*from  w  ww. j a  v  a 2  s.  c om*/
    Path toPath = toRootPath.resolve(fromPath.subpath(fromStartIndex, fromPath.getNameCount()));
    try {
        if (Files.isRegularFile(fromPath)) {
            if (Files.isRegularFile(toPath)) {
                if (!cfd.isAllowOverwriteExisting()) {
                    throw new SecureFSError("overwrite of existing file not allowed: " + toPath);
                }
                if (cfd.isUpdate() && Files.getLastModifiedTime(fromPath).toInstant()
                        .isBefore(Files.getLastModifiedTime(toPath).toInstant())) {
                    log.info("not overwriting from: " + fromPath.toAbsolutePath() + " to: "
                            + toPath.toAbsolutePath());
                    return;
                }
            }

            // write source to target
            cfd.setCurrentFromPath(fromPath.toAbsolutePath().toString());
            cfd.setCurrentToPath(toPath.toAbsolutePath().toString());
            updateCache(cfd);
            try (OutputStream os = cp.getEncrypter(toPath, newSecret);
                    InputStream is = cp.getDecrypter(fromPath)) {
                IOUtils.copy(is, os);
            }
            log.info("copied from: " + fromPath.toAbsolutePath() + " to: " + toPath.toAbsolutePath());
        }
        if (Files.isDirectory(fromPath)) {
            Path subDir = Files.createDirectories(toPath);
            log.info("created subDir: " + subDir.toAbsolutePath());
        }
    } catch (Exception e) {
        throw new SecureFSError("cannot copy from: " + fromPath + " to: " + toPath, e);
    }
}

From source file:de.tudarmstadt.ukp.dkpro.core.api.datasets.internal.actions.Explode.java

private String stripLeadingFolders(String aName, int aLevels) {
    if (aLevels > 0) {
        Path p = Paths.get(aName);
        if (p.getNameCount() <= aLevels) {
            return null;
        } else {//  www  .  j a  v  a 2  s  .  c o m
            p = p.subpath(aLevels, p.getNameCount());
            aName = p.toString();
            return aName;
        }
    } else {
        return aName;
    }
}