List of usage examples for java.nio.file Path resolveSibling
default Path resolveSibling(String other)
From source file:org.elasticsearch.plugins.PluginManagerIT.java
private void writeMd5(Path file, boolean corrupt) throws IOException { String md5Hex = MessageDigests.toHexString(MessageDigests.md5().digest(Files.readAllBytes(file))); try (BufferedWriter out = Files.newBufferedWriter(file.resolveSibling(file.getFileName() + ".md5"), StandardCharsets.UTF_8)) { out.write(md5Hex);/*from ww w . jav a 2s. c o m*/ if (corrupt) { out.write("bad"); } } }
From source file:org.elasticsearch.plugins.PluginManagerIT.java
private void writeSha1(Path file, boolean corrupt) throws IOException { String sha1Hex = MessageDigests.toHexString(MessageDigests.sha1().digest(Files.readAllBytes(file))); try (BufferedWriter out = Files.newBufferedWriter(file.resolveSibling(file.getFileName() + ".sha1"), StandardCharsets.UTF_8)) { out.write(sha1Hex);/*w ww . j ava 2 s .com*/ if (corrupt) { out.write("bad"); } } }
From source file:edu.msViz.mzTree.MzTree.java
private Path getConvertDestinationPath(Path sourceFilePath) throws Exception { // date time file name String suggestedFileName = new SimpleDateFormat("MM-dd-yyyy_HH-mm-ss").format(new java.util.Date()) + ".mzTree"; Path suggestedFilePath = sourceFilePath.resolveSibling(suggestedFileName); // if a ConvertDestinationProvider has been given, use it to determine the output path if (convertDestinationProvider == null) { return suggestedFilePath; } else {// w w w .j a va2 s. co m return convertDestinationProvider.getDestinationPath(suggestedFilePath); } }
From source file:at.tfr.securefs.client.SecurefsClient.java
public void run() { DateTime start = new DateTime(); try (FileSystem fs = FileSystems.newFileSystem(new URI(baseDir), null)) { for (Path path : files) { Path sec = fs.getPath(path.toString() + (asyncTest ? "." + Thread.currentThread().getId() : "")); if (write) { if (!path.toFile().exists()) { System.err.println(Thread.currentThread() + ": NoSuchFile: " + path + " currentWorkdir=" + Paths.get("./").toAbsolutePath()); continue; }/*from w w w . j a va 2s .c o m*/ if (path.getParent() != null) { fs.provider().createDirectory(fs.getPath(path.getParent().toString())); } final OutputStream secOs = Files.newOutputStream(sec); System.out.println(Thread.currentThread() + ": Sending file: " + start + " : " + sec); IOUtils.copyLarge(Files.newInputStream(path), secOs, new byte[128 * 1024]); secOs.close(); } Path out = path.resolveSibling( path.getFileName() + (asyncTest ? "." + Thread.currentThread().getId() : "") + ".out"); if (read) { System.out.println(Thread.currentThread() + ": Reading file: " + new DateTime() + " : " + out); if (out.getParent() != null) { Files.createDirectories(out.getParent()); } final InputStream secIs = Files.newInputStream(sec); IOUtils.copyLarge(secIs, Files.newOutputStream(out), new byte[128 * 1024]); secIs.close(); } if (write && read) { long inputChk = FileUtils.checksumCRC32(path.toFile()); long outputChk = FileUtils.checksumCRC32(out.toFile()); if (inputChk != outputChk) { throw new IOException(Thread.currentThread() + ": Checksum Failed: failure to write/read: in=" + path + ", out=" + out); } System.out.println(Thread.currentThread() + ": Checked Checksums: " + new DateTime() + " : " + inputChk + " / " + outputChk); } if (delete) { boolean deleted = fs.provider().deleteIfExists(sec); if (!deleted) { throw new IOException( Thread.currentThread() + ": Delete Failed: failure to delete: in=" + path); } else { System.out.println(Thread.currentThread() + ": Deleted File: in=" + path); } } } } catch (Throwable t) { t.printStackTrace(); throw new RuntimeException(t); } }
From source file:com.github.podd.example.ExamplePoddClient.java
public Map<Path, String> uploadToStorage(final List<Path> bagsToUpload, final String sshServerFingerprint, final String sshHost, final int portNo, final String username, final Path pathToPublicKey, final Path localRootPath, final Path remoteRootPath, final PasswordFinder keyExtractor) throws PoddClientException, NoSuchAlgorithmException, IOException { final Map<Path, String> results = new ConcurrentHashMap<>(); final ConcurrentMap<Path, ConcurrentMap<PoddDigestUtils.Algorithm, String>> digests = PoddDigestUtils .getDigests(bagsToUpload);//from w w w.j a v a2 s . c o m try (SSHClient sshClient = new SSHClient(ExamplePoddClient.DEFAULT_CONFIG);) { sshClient.useCompression(); sshClient.addHostKeyVerifier(sshServerFingerprint); sshClient.connect(sshHost, portNo); if (!Files.exists(pathToPublicKey)) { throw new PoddClientException("Could not find public key: " + pathToPublicKey); } if (!SecurityUtils.isBouncyCastleRegistered()) { throw new PoddClientException("Bouncy castle needed"); } final FileKeyProvider rsa = new PKCS8KeyFile(); rsa.init(pathToPublicKey.toFile(), keyExtractor); sshClient.authPublickey(username, rsa); // Session session = sshClient.startSession(); try (SFTPClient sftp = sshClient.newSFTPClient();) { for (final Path nextBag : bagsToUpload) { // Check to make sure that the bag was under the local root path final Path localPath = nextBag.toAbsolutePath(); if (!localPath.startsWith(localRootPath)) { this.log.error( "Local bag path was not a direct descendant of the local root path: {} {} {}", localRootPath, nextBag, localPath); throw new PoddClientException( "Local bag path was not a direct descendant of the local root path: " + localPath + " " + localRootPath); } // Take the local root path out to get the subpath to use on the remote final Path remoteSubPath = localPath.subpath(localRootPath.getNameCount(), nextBag.getNameCount() - 1); this.log.info("Remote sub path: {}", remoteSubPath); final Path remoteDirPath = remoteRootPath.resolve(remoteSubPath); this.log.info("Remote dir path: {}", remoteDirPath); final Path remoteBagPath = remoteDirPath.resolve(nextBag.getFileName()); this.log.info("Remote bag path: {}", remoteBagPath); boolean fileFound = false; boolean sizeCorrect = false; try { // check details of a remote bag final FileAttributes attribs = sftp.lstat(remoteBagPath.toAbsolutePath().toString()); final long localSize = Files.size(nextBag); final long remoteSize = attribs.getSize(); if (localSize <= 0) { this.log.error("Local bag was empty: {}", nextBag); sizeCorrect = false; fileFound = false; } else if (remoteSize <= 0) { this.log.warn("Remote bag was empty: {} {}", nextBag, attribs); sizeCorrect = false; fileFound = false; } else if (localSize == remoteSize) { this.log.info("Found file on remote already with same size as local: {} {}", nextBag, remoteBagPath); sizeCorrect = true; fileFound = true; } else { sizeCorrect = false; fileFound = true; // We always assume that a non-zero local file is correct // The bags contain time-stamps that will be modified when they are // regenerated, likely changing the file-size, and hopefully changing // the digest checksums // throw new PoddClientException( // "Could not automatically compare file sizes (need manual intervention to delete one) : " // + nextBag + " " + remoteBagPath + " localSize=" + localSize // + " remoteSize=" + remoteSize); } } catch (final IOException e) { // lstat() throws an IOException if the file does not exist // Ignore sizeCorrect = false; fileFound = false; } final ConcurrentMap<Algorithm, String> bagDigests = digests.get(nextBag); if (bagDigests.isEmpty()) { this.log.error("No bag digests were generated for bag: {}", nextBag); } for (final Entry<Algorithm, String> entry : bagDigests.entrySet()) { final Path localDigestPath = localPath .resolveSibling(localPath.getFileName() + entry.getKey().getExtension()); // Create the local digest file Files.copy( new ReaderInputStream(new StringReader(entry.getValue()), StandardCharsets.UTF_8), localDigestPath); final Path remoteDigestPath = remoteBagPath .resolveSibling(remoteBagPath.getFileName() + entry.getKey().getExtension()); boolean nextDigestFileFound = false; boolean nextDigestCorrect = false; try { final Path tempFile = Files.createTempFile("podd-digest-", entry.getKey().getExtension()); final SFTPFileTransfer sftpFileTransfer = new SFTPFileTransfer(sftp.getSFTPEngine()); sftpFileTransfer.download(remoteBagPath.toAbsolutePath().toString(), tempFile.toAbsolutePath().toString()); nextDigestFileFound = true; final List<String> allLines = Files.readAllLines(tempFile, StandardCharsets.UTF_8); if (allLines.isEmpty()) { nextDigestCorrect = false; } else if (allLines.size() > 1) { nextDigestCorrect = false; } // Check if the digests match exactly else if (allLines.get(0).equals(entry.getValue())) { nextDigestCorrect = true; } else { nextDigestCorrect = false; } } catch (final IOException e) { nextDigestFileFound = false; nextDigestCorrect = false; } if (nextDigestFileFound && nextDigestCorrect) { this.log.info( "Not copying digest to remote as it exists and contains the same content as the local digest"); } else if (nextDigestFileFound && !nextDigestCorrect) { this.log.error("Found remote digest but content was not correct: {} {}", localDigestPath, remoteDigestPath); sftp.rm(remoteDigestPath.toString()); this.log.info("Copying digest to remote: {}", remoteDigestPath); sftp.put(new FileSystemFile(localDigestPath.toString()), remoteDigestPath.toString()); } else if (!nextDigestFileFound) { this.log.info("About to make directories on remote: {}", remoteDirPath); sftp.mkdirs(remoteDirPath.toString()); this.log.info("Copying digest to remote: {}", remoteDigestPath); sftp.put(new FileSystemFile(localDigestPath.toString()), remoteDigestPath.toString()); } } if (fileFound && sizeCorrect) { this.log.info("Not copying bag to remote as it exists and is the same size as local bag"); } else if (fileFound && !sizeCorrect) { this.log.error("Found remote bag but size was not correct: {} {}", nextBag, remoteBagPath); sftp.rm(remoteBagPath.toString()); this.log.info("Copying bag to remote: {}", remoteBagPath); sftp.put(new FileSystemFile(localPath.toString()), remoteBagPath.toString()); } else if (!fileFound) { this.log.info("About to make directories on remote: {}", remoteDirPath); sftp.mkdirs(remoteDirPath.toString()); this.log.info("Copying bag to remote: {}", remoteBagPath); sftp.put(new FileSystemFile(localPath.toString()), remoteBagPath.toString()); } } } } catch (final IOException e) { throw new PoddClientException("Could not copy a bag to the remote location", e); } return results; }
From source file:org.apache.beam.sdk.io.FileSystemsTest.java
@Test public void testDeleteThrowsNoSuchFileException() throws Exception { Path existingPath = temporaryFolder.newFile().toPath(); Path nonExistentPath = existingPath.resolveSibling("non-existent"); createFileWithContent(existingPath, "content1"); thrown.expect(NoSuchFileException.class); FileSystems.delete(toResourceIds(ImmutableList.of(existingPath, nonExistentPath), false /* isDirectory */)); }
From source file:org.apache.beam.sdk.io.FileSystemsTest.java
@Test public void testDeleteIgnoreMissingFiles() throws Exception { Path existingPath = temporaryFolder.newFile().toPath(); Path nonExistentPath = existingPath.resolveSibling("non-existent"); createFileWithContent(existingPath, "content1"); FileSystems.delete(toResourceIds(ImmutableList.of(existingPath, nonExistentPath), false /* isDirectory */), MoveOptions.StandardMoveOptions.IGNORE_MISSING_FILES); }
From source file:org.apache.beam.sdk.io.FileSystemsTest.java
@Test public void testCopyThrowsNoSuchFileException() throws Exception { Path existingPath = temporaryFolder.newFile().toPath(); Path nonExistentPath = existingPath.resolveSibling("non-existent"); Path destPath1 = existingPath.resolveSibling("dest1"); Path destPath2 = nonExistentPath.resolveSibling("dest2"); createFileWithContent(existingPath, "content1"); thrown.expect(NoSuchFileException.class); FileSystems.copy(toResourceIds(ImmutableList.of(existingPath, nonExistentPath), false /* isDirectory */), toResourceIds(ImmutableList.of(destPath1, destPath2), false /* isDirectory */)); }
From source file:org.apache.beam.sdk.io.FileSystemsTest.java
@Test public void testCopyIgnoreMissingFiles() 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.copy(/*from w ww . ja va 2 s.co m*/ toResourceIds(ImmutableList.of(srcPath1, nonExistentPath, srcPath3), false /* isDirectory */), toResourceIds(ImmutableList.of(destPath1, destPath2, destPath3), false /* isDirectory */), MoveOptions.StandardMoveOptions.IGNORE_MISSING_FILES); assertTrue(srcPath1.toFile().exists()); assertTrue(srcPath3.toFile().exists()); assertThat(Files.readLines(srcPath1.toFile(), StandardCharsets.UTF_8), containsInAnyOrder("content1")); assertFalse(destPath2.toFile().exists()); assertThat(Files.readLines(srcPath3.toFile(), StandardCharsets.UTF_8), containsInAnyOrder("content3")); }
From source file:org.apache.beam.sdk.io.FileSystemsTest.java
@Test public void testRenameThrowsNoSuchFileException() throws Exception { Path existingPath = temporaryFolder.newFile().toPath(); Path nonExistentPath = existingPath.resolveSibling("non-existent"); Path destPath1 = existingPath.resolveSibling("dest1"); Path destPath2 = nonExistentPath.resolveSibling("dest2"); createFileWithContent(existingPath, "content1"); thrown.expect(NoSuchFileException.class); FileSystems.rename(toResourceIds(ImmutableList.of(existingPath, nonExistentPath), false /* isDirectory */), toResourceIds(ImmutableList.of(destPath1, destPath2), false /* isDirectory */)); }