java.nio.file.CopyOption Java Examples

The following examples show how to use java.nio.file.CopyOption. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SFTPFileSystemTest.java    From sftp-fs with Apache License 2.0 6 votes vote down vote up
@Test
public void testMoveSFTPFailure() throws IOException {
    addDirectory("/foo");
    addFile("/foo/bar");

    // failure: non-existing target parent

    CopyOption[] options = {};
    FileSystemException exception = assertThrows(FileSystemException.class,
            () -> fileSystem.move(createPath("/foo/bar"), createPath("/baz/bar"), options));
    assertEquals("/foo/bar", exception.getFile());
    assertEquals("/baz/bar", exception.getOtherFile());

    verify(getExceptionFactory()).createMoveException(eq("/foo/bar"), eq("/baz/bar"), any(SftpException.class));
    assertTrue(Files.isDirectory(getPath("/foo")));
    assertTrue(Files.isRegularFile(getPath("/foo/bar")));
    assertFalse(Files.exists(getPath("/baz")));
}
 
Example #2
Source File: MCRFileSystemProvider.java    From mycore with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void move(Path source, Path target, CopyOption... options) throws IOException {
    HashSet<CopyOption> copyOptions = Sets.newHashSet(options);
    if (copyOptions.contains(StandardCopyOption.ATOMIC_MOVE)) {
        throw new AtomicMoveNotSupportedException(source.toString(), target.toString(),
            "ATOMIC_MOVE not supported yet");
    }
    if (Files.isDirectory(source)) {
        MCRPath src = MCRFileSystemUtils.checkPathAbsolute(source);
        MCRDirectory srcRootDirectory = getRootDirectory(src);
        if (srcRootDirectory.hasChildren()) {
            throw new IOException("Directory is not empty");
        }
    }
    copy(source, target, options);
    delete(source);
}
 
Example #3
Source File: MCRFileSystemProvider.java    From mycore with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void move(Path source, Path target, CopyOption... options) throws IOException {
    HashSet<CopyOption> copyOptions = Sets.newHashSet(options);
    if (copyOptions.contains(StandardCopyOption.ATOMIC_MOVE)) {
        throw new AtomicMoveNotSupportedException(source.toString(), target.toString(),
            "ATOMIC_MOVE not supported yet");
    }
    if (Files.isDirectory(source)) {
        MCRPath src = MCRFileSystemUtils.checkPathAbsolute(source);
        MCRDirectory srcRootDirectory = MCRFileSystemUtils.getFileCollection(src.getOwner());
        if (srcRootDirectory.hasChildren()) {
            throw new IOException("Directory is not empty");
        }
    }
    copy(source, target, options);
    delete(source);
}
 
Example #4
Source File: SFTPFileSystemTest.java    From sftp-fs with Apache License 2.0 6 votes vote down vote up
@Test
public void testCopySFTPFailure() throws IOException {
    addDirectory("/foo");
    addFile("/foo/bar");

    // failure: target parent does not exist

    CopyOption[] options = {};
    NoSuchFileException exception = assertThrows(NoSuchFileException.class,
            () -> fileSystem.copy(createPath("/foo/bar"), createPath("/baz/bar"), options));
    assertEquals("/baz/bar", exception.getFile());

    verify(getExceptionFactory()).createNewOutputStreamException(eq("/baz/bar"), any(SftpException.class), anyCollection());
    assertTrue(Files.isDirectory(getPath("/foo")));
    assertTrue(Files.isRegularFile(getPath("/foo/bar")));
    assertFalse(Files.exists(getPath("/baz")));
    assertFalse(Files.exists(getPath("/baz/bar")));
}
 
Example #5
Source File: SFTPFileSystemTest.java    From sftp-fs with Apache License 2.0 6 votes vote down vote up
@Test
public void testCopyReplaceNonEmptyDirAllowed() throws IOException {
    addDirectory("/foo");
    addFile("/foo/bar");
    addFile("/baz");

    CopyOption[] options = { StandardCopyOption.REPLACE_EXISTING };
    FileSystemException exception = assertThrows(FileSystemException.class,
            () -> fileSystem.copy(createPath("/baz"), createPath("/foo"), options));
    assertEquals("/foo", exception.getFile());

    verify(getExceptionFactory()).createDeleteException(eq("/foo"), any(SftpException.class), eq(true));
    assertTrue(Files.isDirectory(getPath("/foo")));
    assertTrue(Files.isRegularFile(getPath("/foo/bar")));
    assertTrue(Files.isRegularFile(getPath("/baz")));
}
 
Example #6
Source File: SFTPFileSystemTest.java    From sftp-fs with Apache License 2.0 6 votes vote down vote up
@Test
public void testMoveNonEmptyDirDifferentFileSystem() throws IOException {
    addDirectory("/foo");
    addDirectory("/baz");
    addFile("/baz/qux");

    CopyOption[] options = {};
    FileSystemException exception = assertThrows(FileSystemException.class,
            () -> fileSystem.move(createPath("/baz"), createPath(fileSystem2, "/foo/bar"), options));
    assertEquals("/baz", exception.getFile());

    verify(getExceptionFactory()).createDeleteException(eq("/baz"), any(SftpException.class), eq(true));
    assertTrue(Files.isDirectory(getPath("/baz")));
    assertTrue(Files.isRegularFile(getPath("/baz/qux")));
    assertEquals(1, getChildCount("/baz"));

    assertTrue(Files.isDirectory(getPath("/foo")));
    assertTrue(Files.isDirectory(getPath("/foo/bar")));
    assertFalse(Files.isRegularFile(getPath("/foo/bar/qux")));
    assertEquals(1, getChildCount("/foo"));
    assertEquals(0, getChildCount("/foo/bar"));
}
 
Example #7
Source File: SFTPFileSystemTest.java    From sftp-fs with Apache License 2.0 6 votes vote down vote up
@Test
public void testMoveReplaceFileDifferentFileSystem() throws IOException {
    addDirectory("/foo");
    addDirectory("/foo/bar");
    addFile("/baz");

    CopyOption[] options = {};
    FileAlreadyExistsException exception = assertThrows(FileAlreadyExistsException.class,
            () -> fileSystem.move(createPath("/baz"), createPath(fileSystem2, "/foo/bar"), options));
    assertEquals("/foo/bar", exception.getFile());

    verify(getExceptionFactory(), never()).createMoveException(anyString(), anyString(), any(SftpException.class));
    assertTrue(Files.isDirectory(getPath("/foo")));
    assertTrue(Files.isDirectory(getPath("/foo/bar")));
    assertTrue(Files.isRegularFile(getPath("/baz")));
}
 
Example #8
Source File: SFTPFileSystemTest.java    From sftp-fs with Apache License 2.0 6 votes vote down vote up
@Test
public void testCopyReplaceNonEmptyDirAllowedDifferentFileSystems() throws IOException {
    addDirectory("/foo");
    addFile("/foo/bar");
    addFile("/baz");

    CopyOption[] options = { StandardCopyOption.REPLACE_EXISTING };
    FileSystemException exception = assertThrows(FileSystemException.class,
            () -> fileSystem.copy(createPath("/baz"), createPath(fileSystem2, "/foo"), options));
    assertEquals("/foo", exception.getFile());

    verify(getExceptionFactory()).createDeleteException(eq("/foo"), any(SftpException.class), eq(true));
    assertTrue(Files.isDirectory(getPath("/foo")));
    assertTrue(Files.isRegularFile(getPath("/foo/bar")));
    assertTrue(Files.isRegularFile(getPath("/baz")));
}
 
Example #9
Source File: SFTPFileSystemTest.java    From sftp-fs with Apache License 2.0 5 votes vote down vote up
@Test
public void testCopyReplaceEmptyDirAllowedDifferentFileSystems() throws IOException {
    addDirectory("/foo");
    addDirectory("/baz");

    CopyOption[] options = { StandardCopyOption.REPLACE_EXISTING };
    fileSystem.copy(createPath("/baz"), createPath(fileSystem2, "/foo"), options);

    assertTrue(Files.isDirectory(getPath("/foo")));
    assertTrue(Files.isDirectory(getPath("/foo")));
}
 
Example #10
Source File: SFTPFileSystemTest.java    From sftp-fs with Apache License 2.0 5 votes vote down vote up
@Test
public void testCopyFileDifferentFileSystems() throws IOException {
    addDirectory("/foo");
    addFile("/baz");

    CopyOption[] options = {};
    fileSystem.copy(createPath("/baz"), createPath(fileSystem2, "/foo/bar"), options);

    assertTrue(Files.isDirectory(getPath("/foo")));
    assertTrue(Files.isRegularFile(getPath("/foo/bar")));
    assertTrue(Files.isRegularFile(getPath("/baz")));
}
 
Example #11
Source File: SFTPFileSystemTest.java    From sftp-fs with Apache License 2.0 5 votes vote down vote up
@Test
public void testMoveEmptyDir() throws IOException {
    addDirectory("/foo");
    addDirectory("/baz");

    CopyOption[] options = {};
    fileSystem.move(createPath("/baz"), createPath("/foo/bar"), options);

    assertTrue(Files.isDirectory(getPath("/foo")));
    assertTrue(Files.isDirectory(getPath("/foo/bar")));
    assertFalse(Files.exists(getPath("/baz")));
}
 
Example #12
Source File: FileUtils.java    From hkxpack with MIT License 5 votes vote down vote up
/**
 * Convert a given resource to a temporary {@link File}.
 * @param resourceName the resource to convert
 * @return the {@link File} object pointing to the temporary File.
 * @throws Exception
 */
public static File resourceToTemporaryFile(final String resourceName) throws IOException {
	InputStream inputStream = FileUtils.class.getResourceAsStream(resourceName);
	File tempFile = File.createTempFile(resourceName, ".tmp");
    Files.copy(inputStream, tempFile.toPath(), new CopyOption[]{StandardCopyOption.REPLACE_EXISTING});
    inputStream.close();
    return tempFile;
}
 
Example #13
Source File: SFTPFileSystemTest.java    From sftp-fs with Apache License 2.0 5 votes vote down vote up
@Test
public void testCopyEmptyDir() throws IOException {
    addDirectory("/foo");
    addDirectory("/baz");

    CopyOption[] options = {};
    fileSystem.copy(createPath("/baz"), createPath("/foo/bar"), options);

    assertTrue(Files.isDirectory(getPath("/foo")));
    assertTrue(Files.isDirectory(getPath("/foo/bar")));
    assertTrue(Files.isDirectory(getPath("/baz")));

    assertEquals(0, getChildCount("/foo/bar"));
}
 
Example #14
Source File: SFTPFileSystemTest.java    From sftp-fs with Apache License 2.0 5 votes vote down vote up
@Test
public void testMoveFile() throws IOException {
    addDirectory("/foo");
    addFile("/baz");

    CopyOption[] options = {};
    fileSystem.move(createPath("/baz"), createPath("/foo/bar"), options);

    assertTrue(Files.isDirectory(getPath("/foo")));
    assertTrue(Files.isRegularFile(getPath("/foo/bar")));
    assertFalse(Files.exists(getPath("/baz")));
}
 
Example #15
Source File: Copy.java    From marathonv5 with Apache License 2.0 5 votes vote down vote up
/**
 * Copy source file to target location.
 *
 * @return
 */
static boolean copyFile(Path source, Path target) {
    CopyOption[] options = new CopyOption[] { COPY_ATTRIBUTES, REPLACE_EXISTING };
    target = getUnique(target);
    try {
        Files.copy(source, target, options);
        return true;
    } catch (Exception x) {
        System.err.format("Unable to copy: %s: %s%n", source, x);
        return false;
    }
}
 
Example #16
Source File: VerboseFS.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public void move(Path source, Path target, CopyOption... options) throws IOException {
  Throwable exception = null;
  try {
    super.move(source, target, options);
  } catch (Throwable t) {
    exception = t;
  } finally {
    sop("move" + Arrays.toString(options) + ": " + path(source) + " -> " + path(target), exception);
  }
}
 
Example #17
Source File: SFTPFileSystemTest.java    From sftp-fs with Apache License 2.0 5 votes vote down vote up
@Test
public void testCopyRoot() throws IOException {
    // copying a directory (including the root) will not copy its contents, so copying the root is allowed
    addDirectory("/foo");

    CopyOption[] options = {};
    fileSystem.copy(createPath("/"), createPath("/foo/bar"), options);

    assertTrue(Files.isDirectory(getPath("/foo")));
    assertTrue(Files.isDirectory(getPath("/foo/bar")));
    assertEquals(0, getChildCount("/foo/bar"));
}
 
Example #18
Source File: SFTPFileSystemTest.java    From sftp-fs with Apache License 2.0 5 votes vote down vote up
@Test
public void testCopyReplaceFile() throws IOException {
    addDirectory("/foo");
    addFile("/foo/bar");
    addFile("/baz");

    CopyOption[] options = {};
    FileAlreadyExistsException exception = assertThrows(FileAlreadyExistsException.class,
            () -> fileSystem.copy(createPath("/baz"), createPath("/foo/bar"), options));
    assertEquals("/foo/bar", exception.getFile());

    assertTrue(Files.isDirectory(getPath("/foo")));
    assertTrue(Files.isRegularFile(getPath("/foo/bar")));
    assertTrue(Files.isRegularFile(getPath("/baz")));
}
 
Example #19
Source File: AndroidRipperStarter.java    From AndroidRipper with GNU Affero General Public License v3.0 5 votes vote down vote up
protected void createAPKs(String testSuitePath, String appPackage, String appMainActivity, String extractorClass,
		String toolsPath, String debugKeyStorePath, String autAPK, String tempPath) {
	// replace strings
	println("Editing 'Configuration.java'");
	replaceStringsInFile(
			testSuitePath + "/smali/it/unina/android/ripper/configuration/Configuration.smali.template",
			testSuitePath + "/smali/it/unina/android/ripper/configuration/Configuration.smali", appPackage,
			appMainActivity);
	println("Editing 'AndroidManifest.xml'");
	replaceStringsInFile(testSuitePath + "/AndroidManifest.xml.template", testSuitePath + "/AndroidManifest.xml",
			appPackage, appMainActivity);

	try {
		println("Cleaning apks...");
		println("Building AndroidRipper...");

		execCommand("java -jar " + toolsPath + "apktool.jar b " + testSuitePath + " -o " + tempPath + "/ar.apk");

		println("Signing AndroidRipper...");

		execCommand("jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1 -keystore " + debugKeyStorePath
				+ "/debug.keystore -storepass android -keypass android " + tempPath + "/ar.apk androiddebugkey");
		execCommand("zipalign 4 " + tempPath + "/ar.apk " + tempPath + "/ripper.apk");

		Files.copy(FileSystems.getDefault().getPath(autAPK),
				FileSystems.getDefault().getPath(tempPath + "/temp.apk"),
				(CopyOption) StandardCopyOption.REPLACE_EXISTING);

		println("Signing AUT...");
		ZipUtils.deleteFromZip(tempPath + "/temp.apk");
		execCommand("jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1 -keystore " + debugKeyStorePath
				+ "/debug.keystore -storepass android -keypass android " + tempPath
				+ "/temp.apk androiddebugkey");
		execCommand("jarsigner -verify " + tempPath + "/temp.apk");
		execCommand("zipalign -v 4 " + tempPath + "/temp.apk " + tempPath + "/aut.apk");
		
	} catch (Throwable t) {
		throw new RipperRuntimeException(AndroidRipperStarter.class, "createAPKs", "apk build failed", t);
	}
}
 
Example #20
Source File: SFTPFileSystemTest.java    From sftp-fs with Apache License 2.0 5 votes vote down vote up
@Test
public void testMoveReplaceEmptyDirDifferentFileSystem() throws IOException {
    addDirectory("/foo");
    addFile("/baz");

    CopyOption[] options = {};
    FileAlreadyExistsException exception = assertThrows(FileAlreadyExistsException.class,
            () -> fileSystem.move(createPath("/baz"), createPath(fileSystem2, "/foo"), options));
    assertEquals("/foo", exception.getFile());

    verify(getExceptionFactory(), never()).createMoveException(anyString(), anyString(), any(SftpException.class));
    assertTrue(Files.isDirectory(getPath("/foo")));
    assertTrue(Files.isRegularFile(getPath("/baz")));
}
 
Example #21
Source File: SFTPFileSystemTest.java    From sftp-fs with Apache License 2.0 5 votes vote down vote up
@Test
public void testMoveEmptyDirDifferentFileSystem() throws IOException {
    addDirectory("/foo");
    addDirectory("/baz");

    CopyOption[] options = {};
    fileSystem.move(createPath("/baz"), createPath(fileSystem2, "/foo/bar"), options);

    assertTrue(Files.isDirectory(getPath("/foo")));
    assertTrue(Files.isDirectory(getPath("/foo/bar")));
    assertFalse(Files.exists(getPath("/baz")));
}
 
Example #22
Source File: SFTPFileSystemTest.java    From sftp-fs with Apache License 2.0 5 votes vote down vote up
@Test
public void testCopyFileMultipleConnections() throws IOException {
    addDirectory("/foo");
    addFile("/baz");

    CopyOption[] options = {};
    fileSystem2.copy(createPath(fileSystem2, "/baz"), createPath(fileSystem2, "/foo/bar"), options);

    assertTrue(Files.isDirectory(getPath("/foo")));
    assertTrue(Files.isRegularFile(getPath("/foo/bar")));
    assertTrue(Files.isRegularFile(getPath("/baz")));
}
 
Example #23
Source File: PathUtil.java    From manifold with Apache License 2.0 5 votes vote down vote up
public static boolean renameTo( Path from, Path to, CopyOption... options )
{
  try
  {
    return Files.move( from, to, options ) != null;
  }
  catch( IOException e )
  {
    return false;
  }
}
 
Example #24
Source File: SFTPFileSystemTest.java    From sftp-fs with Apache License 2.0 5 votes vote down vote up
@Test
public void testMoveNonEmptyRoot() throws IOException {
    addDirectory("/foo");

    CopyOption[] options = {};
    DirectoryNotEmptyException exception = assertThrows(DirectoryNotEmptyException.class,
            () -> fileSystem.move(createPath("/"), createPath("/baz"), options));
    assertEquals("/", exception.getFile());

    assertTrue(Files.isDirectory(getPath("/foo")));
    assertFalse(Files.exists(getPath("/baz")));
}
 
Example #25
Source File: VerboseFS.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public void copy(Path source, Path target, CopyOption... options) throws IOException {
  Throwable exception = null;
  try {
    super.copy(source, target, options);
  } catch (Throwable t) {
    exception = t;
  } finally {
    sop("copy" + Arrays.toString(options) + ": " + path(source) + " -> " + path(target), exception);
  }
}
 
Example #26
Source File: SFTPFileSystemTest.java    From sftp-fs with Apache License 2.0 5 votes vote down vote up
@Test
public void testCopyReplaceFileAllowedDifferentFileSystems() throws IOException {
    addDirectory("/foo");
    addFile("/foo/bar");
    addFile("/baz");

    CopyOption[] options = { StandardCopyOption.REPLACE_EXISTING };
    fileSystem.copy(createPath("/baz"), createPath(fileSystem2, "/foo/bar"), options);

    assertTrue(Files.isDirectory(getPath("/foo")));
    assertTrue(Files.isRegularFile(getPath("/foo/bar")));
    assertTrue(Files.isRegularFile(getPath("/foo/bar")));
}
 
Example #27
Source File: WindowsFS.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public void move(Path source, Path target, CopyOption... options) throws IOException {
  synchronized (openFiles) {
    checkDeleteAccess(source);
    Object key = getKeyOrNull(target);
    super.move(source, target, options);
    if (key != null) {
      Object newKey = getKey(target);
      if (newKey.equals(key) == false) {
        // we need to transfer ownership here if we have open files on this file since the getKey() method will
        // return a different i-node next time we call it with the target path and our onClose method will
        // trip an assert
        Map<Path, Integer> map = openFiles.get(key);
        if (map != null) {
          Integer v = map.remove(target);
          if (v != null) {
            Map<Path, Integer> pathIntegerMap = openFiles.computeIfAbsent(newKey, k -> new HashMap<>());
            Integer existingValue = pathIntegerMap.getOrDefault(target, 0);
            pathIntegerMap.put(target, existingValue + v);
          }
          if (map.isEmpty()) {
            openFiles.remove(key);
          }
        }
      }
    }
  }
}
 
Example #28
Source File: MCRFileSystemProvider.java    From mycore with GNU General Public License v3.0 5 votes vote down vote up
private void checkCopyOptions(CopyOption[] options) {
    for (CopyOption option : options) {
        if (!SUPPORTED_COPY_OPTIONS.contains(option)) {
            throw new UnsupportedOperationException("Unsupported copy option: " + option);
        }
    }
}
 
Example #29
Source File: SFTPFileSystemTest.java    From sftp-fs with Apache License 2.0 5 votes vote down vote up
@Test
public void testMoveReplaceEmptyDir() throws IOException {
    addDirectory("/foo");
    addFile("/baz");

    CopyOption[] options = {};
    FileSystemException exception = assertThrows(FileSystemException.class,
            () -> fileSystem.move(createPath("/baz"), createPath("/foo"), options));
    assertEquals("/baz", exception.getFile());
    assertEquals("/foo", exception.getOtherFile());

    verify(getExceptionFactory()).createMoveException(eq("/baz"), eq("/foo"), any(SftpException.class));
    assertTrue(Files.isDirectory(getPath("/foo")));
    assertTrue(Files.isRegularFile(getPath("/baz")));
}
 
Example #30
Source File: JFileSystemProvider.java    From baratine with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void copy(Path source, Path target, CopyOption... options)
  throws IOException
{
  JPath jSource = (JPath) source;
  JPath jTarget = (JPath) target;

  try (InputStream is = jSource.getBfsFile().openRead()) {
    if (is == null) {
      throw new IOException(L.l("unable to read from file {0}", source.toUri()));
    }

    ArrayList<WriteOption> bfsOptionList = new ArrayList<>();
    for (CopyOption option : options) {
      if (option == StandardCopyOption.REPLACE_EXISTING) {
        bfsOptionList.add(WriteOption.Standard.OVERWRITE);
      }
    }

    WriteOption []bfsOptions = new WriteOption[bfsOptionList.size()];
    bfsOptionList.toArray(bfsOptions);

    try (OutputStream os = jTarget.getBfsFile().openWrite(bfsOptions)) {
      if (os == null) {
        throw new IOException(L.l("unable to write to file {0}", target.toUri()));
      }

      IoUtil.copy(is, os);
    }
  }
}