Example usage for java.nio.file Path resolveSibling

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

Introduction

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

Prototype

default Path resolveSibling(String other) 

Source Link

Document

Converts a given path string to a Path and resolves it against this path's #getParent parent path in exactly the manner specified by the #resolveSibling(Path) resolveSibling method.

Usage

From source file:org.apache.beam.sdk.io.FileSystemsTest.java

@Test
public void testRenameIgnoreMissingFiles() throws Exception {
    Path srcPath1 = temporaryFolder.newFile().toPath();
    Path nonExistentPath = srcPath1.resolveSibling("non-existent");
    Path srcPath3 = temporaryFolder.newFile().toPath();

    Path destPath1 = srcPath1.resolveSibling("dest1");
    Path destPath2 = nonExistentPath.resolveSibling("dest2");
    Path destPath3 = srcPath1.resolveSibling("dest3");

    createFileWithContent(srcPath1, "content1");
    createFileWithContent(srcPath3, "content3");

    FileSystems.rename(/*  w w  w. ja  v  a 2 s .c  o  m*/
            toResourceIds(ImmutableList.of(srcPath1, nonExistentPath, srcPath3), false /* isDirectory */),
            toResourceIds(ImmutableList.of(destPath1, destPath2, destPath3), false /* isDirectory */),
            MoveOptions.StandardMoveOptions.IGNORE_MISSING_FILES);

    assertFalse(srcPath1.toFile().exists());
    assertFalse(srcPath3.toFile().exists());
    assertThat(Files.readLines(destPath1.toFile(), StandardCharsets.UTF_8), containsInAnyOrder("content1"));
    assertFalse(destPath2.toFile().exists());
    assertThat(Files.readLines(destPath3.toFile(), StandardCharsets.UTF_8), containsInAnyOrder("content3"));
}

From source file:org.apache.beam.sdk.io.LocalFileSystemTest.java

@Test
public void testCopyWithExistingSrcFile() throws Exception {
    Path srcPath1 = temporaryFolder.newFile().toPath();
    Path srcPath2 = temporaryFolder.newFile().toPath();

    Path destPath1 = temporaryFolder.getRoot().toPath().resolve("nonexistentdir").resolve("dest1");
    Path destPath2 = srcPath2.resolveSibling("dest2");

    createFileWithContent(srcPath1, "content1");
    createFileWithContent(srcPath2, "content2");

    localFileSystem.copy(toLocalResourceIds(ImmutableList.of(srcPath1, srcPath2), false /* isDirectory */),
            toLocalResourceIds(ImmutableList.of(destPath1, destPath2), false /* isDirectory */));

    assertContents(ImmutableList.of(destPath1, destPath2), ImmutableList.of("content1", "content2"));
}

From source file:org.apache.beam.sdk.io.LocalFileSystemTest.java

@Test
public void testMoveWithExistingSrcFile() throws Exception {
    Path srcPath1 = temporaryFolder.newFile().toPath();
    Path srcPath2 = temporaryFolder.newFile().toPath();

    Path destPath1 = temporaryFolder.getRoot().toPath().resolve("nonexistentdir").resolve("dest1");
    Path destPath2 = srcPath2.resolveSibling("dest2");

    createFileWithContent(srcPath1, "content1");
    createFileWithContent(srcPath2, "content2");

    localFileSystem.rename(toLocalResourceIds(ImmutableList.of(srcPath1, srcPath2), false /* isDirectory */),
            toLocalResourceIds(ImmutableList.of(destPath1, destPath2), false /* isDirectory */));

    assertContents(ImmutableList.of(destPath1, destPath2), ImmutableList.of("content1", "content2"));

    assertFalse(srcPath1 + "exists", srcPath1.toFile().exists());
    assertFalse(srcPath2 + "exists", srcPath2.toFile().exists());
}

From source file:org.apache.taverna.prov.Saver.java

private Path writeIfLocal(List<ExternalReferenceSPI> externalReferences, Path file, String mimeType)
        throws IOException {

    ValueCarryingExternalReference<?> valRef = null;
    for (ExternalReferenceSPI ref : externalReferences) {
        if (ref instanceof ValueCarryingExternalReference) {
            valRef = (ValueCarryingExternalReference<?>) ref;
            break;
        }/*from w  w  w .j  a  v  a  2s .  c  om*/
    }

    if (valRef == null) {
        return null;
    }

    String fileExtension;
    try {
        fileExtension = MimeTypes.getDefaultMimeTypes().forName(mimeType).getExtension();
    } catch (MimeTypeException e1) {
        fileExtension = "";
    }
    Path targetFile = file.resolveSibling(file.getFileName() + fileExtension);

    MessageDigest sha = null;
    MessageDigest sha512 = null;
    OutputStream output = Files.newOutputStream(targetFile);
    try {
        try {
            sha = MessageDigest.getInstance("SHA");
            output = new DigestOutputStream(output, sha);

            sha512 = MessageDigest.getInstance("SHA-512");
            output = new DigestOutputStream(output, sha512);
        } catch (NoSuchAlgorithmException e) {
            logger.info("Could not find digest", e);
        }

        IOUtils.copyLarge(valRef.openStream(getContext()), output);
    } finally {
        output.close();
    }

    if (sha != null) {
        getSha1sums().put(targetFile.toRealPath(), hexOfDigest(sha));
    }
    if (sha512 != null) {
        sha512.digest();
        getSha512sums().put(targetFile.toRealPath(), hexOfDigest(sha512));
    }

    return targetFile;
}

From source file:org.apache.taverna.robundle.Bundles.java

protected static Path withExtension(Path path, String extension) {
    if (!extension.isEmpty() && !extension.startsWith("."))
        throw new IllegalArgumentException("Extension must be empty or start with .");
    String p = path.getFileName().toString();
    if (!extension.isEmpty() && p.toLowerCase().endsWith(extension.toLowerCase()))
        return path;
    // Everything after the last . - or just the end
    String newP = p.replaceFirst("(\\.[^.]*)?$", extension);
    return path.resolveSibling(newP);
}

From source file:org.carlspring.strongbox.storage.metadata.nuget.TempNupkgFileTest.java

@Test
public void testHashTempFile() throws Exception {
    // GIVEN/*w w  w . j  av  a2s  .c  o m*/
    String packageId = "NUnit";
    String packageVersion = "2.5.9.10348";
    Path packageFilePath = TestCaseWithNugetPackageGeneration.generatePackageFile(baseDirectoryPath, packageId,
            packageVersion, (String[]) null/*
                                            * dependencyList
                                            */);

    String checksumFileName = packageId + "." + packageVersion + ".nupkg.sha512";
    Path checksumPath = packageFilePath.resolveSibling(checksumFileName);

    String expectedHash = MessageDigestUtils.readChecksumFile(checksumPath.toString());

    // WHEN
    try (InputStream nupkgInputStream = new BufferedInputStream(Files.newInputStream(packageFilePath));
            TempNupkgFile nupkgFile = new TempNupkgFile(nupkgInputStream);) {
        // THEN
        assertNotNull(nupkgFile.getHash().toString(), "Hash file created from stream");
        assertEquals(expectedHash, nupkgFile.getHash().toString(), "Hash file created from stream");
    }
}

From source file:org.cgiar.ccafs.marlo.action.BaseAction.java

public FileDB getFileDB(FileDB preview, File file, String fileFileName, String path) {

    try {// ww  w  .  ja v a  2s .com

        FileInputStream fis = new FileInputStream(file);
        String md5 = DigestUtils.md5Hex(fis);
        FileDB dbFile = null;

        if (preview != null) {

            if (preview.getFileName().equals(fileFileName) && !md5.equals(preview.getTokenId())) {
                dbFile = new FileDB(fileFileName, md5);
                FileDB dbFilePrev = preview;
                Path prevFile = Paths.get(path + dbFilePrev.getFileName());
                String newName = FilenameUtils.removeExtension(fileFileName) + "_"
                        + UUID.randomUUID().toString() + "." + FilenameUtils.getExtension(fileFileName);
                newName = newName.replaceAll(":", "-");
                Files.move(prevFile, prevFile.resolveSibling(newName));
                dbFilePrev.setFileName(newName);
                fileDBManager.saveFileDB(dbFilePrev);
            } else {
                if (preview.getFileName().equals(fileFileName) && md5.equals(preview.getTokenId())) {
                    dbFile = preview;
                } else {
                    dbFile = new FileDB(fileFileName, md5);
                }
            }

        } else {
            dbFile = new FileDB(fileFileName, md5);
        }
        fileDBManager.saveFileDB(dbFile);
        return dbFile;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }

}

From source file:org.cirdles.webServices.calamari.PrawnFileHandlerService.java

private Path zip(Path target) throws IOException {
    Path zipFilePath = target.resolveSibling("reports.zip");

    try (FileSystem zipFileFileSystem = FileSystems.newFileSystem(URI.create("jar:" + zipFilePath.toUri()),
            ZIP_FILE_ENV)) {/* www . ja v a2 s  .  c o m*/

        Files.list(target).forEach(entry -> {
            try {
                Files.copy(entry, zipFileFileSystem.getPath("/" + entry.getFileName()));
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
        });
    }

    return zipFilePath;
}

From source file:org.forgerock.openidm.maintenance.upgrade.FileStateCheckerTest.java

@Test
public void testFileMissing() throws IOException, NoSuchAlgorithmException, URISyntaxException {
    Path path = Paths.get(getClass().getResource("/checksums.csv").toURI());
    ChecksumFile checksumFile = new ChecksumFile(path);
    FileStateChecker checker = new FileStateChecker(checksumFile);
    assertThat(checker.getCurrentFileState(path.resolveSibling("file0"))).isEqualTo(FileState.NONEXISTENT);
}

From source file:org.mule.modules.hdfs.automation.functional.CopyToLocalFileTestCases.java

@After
public void tearDown() throws Exception {
    getConnector().deleteFile(MYFILE_PATH);
    Files.delete(Paths.get(LOCAL_TAGET_PATH));
    Path localTarget = Paths.get(LOCAL_TAGET_PATH);
    Files.delete(localTarget.resolveSibling("." + localTarget.getFileName().toString() + ".crc"));
}