Java Code Examples for java.nio.file.StandardCopyOption#REPLACE_EXISTING

The following examples show how to use java.nio.file.StandardCopyOption#REPLACE_EXISTING . 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 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 2
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 3
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 4
Source File: FetchFile.java    From nifi with Apache License 2.0 5 votes vote down vote up
protected void move(final File source, final File target, final boolean overwrite) throws IOException {
    final File targetDirectory = target.getParentFile();

    // convert to path and use Files.move instead of file.renameTo so that if we fail, we know why
    final Path targetPath = target.toPath();
    if (!targetDirectory.exists()) {
        Files.createDirectories(targetDirectory.toPath());
    }

    final CopyOption[] copyOptions = overwrite ? new CopyOption[] {StandardCopyOption.REPLACE_EXISTING} : new CopyOption[] {};
    Files.move(source.toPath(), targetPath, copyOptions);
}
 
Example 5
Source File: CopyMoveVisitor.java    From copybara with Apache License 2.0 5 votes vote down vote up
CopyMoveVisitor(Path before, Path after, @Nullable PathMatcher pathMatcher, boolean overwrite, boolean isCopy) {
  this.before = before;
  this.after = after;
  this.pathMatcher = pathMatcher;
  this.isCopy = isCopy;
  if (overwrite) {
    moveMode = new CopyOption[]{LinkOption.NOFOLLOW_LINKS, StandardCopyOption.REPLACE_EXISTING};
  } else {
    moveMode = new CopyOption[]{LinkOption.NOFOLLOW_LINKS};
  }
}
 
Example 6
Source File: SFTPFileSystemTest.java    From sftp-fs with Apache License 2.0 5 votes vote down vote up
public void testMoveReplaceEmptyDirAllowedDifferentFileSystem() throws IOException {
    addDirectory("/foo");
    addFile("/baz");

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

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

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

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

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

    assertTrue(Files.isRegularFile(getPath("/foo")));
    assertFalse(Files.exists(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 testMoveReplaceFileAllowed() throws IOException {
    addDirectory("/foo");
    addFile("/foo/bar");
    addFile("/baz");

    CopyOption[] options = { StandardCopyOption.REPLACE_EXISTING };
    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 10
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 11
Source File: FetchFile.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
protected void move(final File source, final File target, final boolean overwrite) throws IOException {
    final File targetDirectory = target.getParentFile();

    // convert to path and use Files.move instead of file.renameTo so that if we fail, we know why
    final Path targetPath = target.toPath();
    if (!targetDirectory.exists()) {
        Files.createDirectories(targetDirectory.toPath());
    }

    final CopyOption[] copyOptions = overwrite ? new CopyOption[] {StandardCopyOption.REPLACE_EXISTING} : new CopyOption[] {};
    Files.move(source.toPath(), targetPath, copyOptions);
}
 
Example 12
Source File: SFTPFileSystemTest.java    From sftp-fs with Apache License 2.0 5 votes vote down vote up
@Test
public void testCopyReplaceEmptyDirAllowed() throws IOException {
    addDirectory("/foo");
    addDirectory("/baz");

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

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

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

    assertTrue(Files.isDirectory(getPath("/foo")));
    assertTrue(Files.isRegularFile(getPath("/foo/bar")));
    assertTrue(Files.isRegularFile(getPath("/foo/bar")));
}
 
Example 14
Source File: CopyOptions.java    From sftp-fs with Apache License 2.0 5 votes vote down vote up
static CopyOptions forMove(boolean sameFileSystem, CopyOption... options) {
    boolean replaceExisting = false;

    for (CopyOption option : options) {
        if (option == StandardCopyOption.REPLACE_EXISTING) {
            replaceExisting = true;
        } else if (!(option == StandardCopyOption.ATOMIC_MOVE && sameFileSystem) && !isIgnoredCopyOption(option)) {
            throw Messages.fileSystemProvider().unsupportedCopyOption(option);
        }
    }

    return new CopyOptions(replaceExisting, Arrays.asList(options));
}
 
Example 15
Source File: CopyOptions.java    From sftp-fs with Apache License 2.0 5 votes vote down vote up
static CopyOptions forCopy(CopyOption... options) {

        boolean replaceExisting = false;

        for (CopyOption option : options) {
            if (option == StandardCopyOption.REPLACE_EXISTING) {
                replaceExisting = true;
            } else if (!isIgnoredCopyOption(option)) {
                throw Messages.fileSystemProvider().unsupportedCopyOption(option);
            }
        }

        return new CopyOptions(replaceExisting, Arrays.asList(options));
    }
 
Example 16
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);
    }
  }
}
 
Example 17
Source File: UnixCopyFile.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
static Flags fromMoveOptions(CopyOption... options) {
    Flags flags = new Flags();
    for (CopyOption option: options) {
        if (option == StandardCopyOption.ATOMIC_MOVE) {
            flags.atomicMove = true;
            continue;
        }
        if (option == StandardCopyOption.REPLACE_EXISTING) {
            flags.replaceExisting = true;
            continue;
        }
        if (option == LinkOption.NOFOLLOW_LINKS) {
            // ignore
            continue;
        }
        if (option == null)
            throw new NullPointerException();
        throw new UnsupportedOperationException("Unsupported copy option");
    }

    // a move requires that all attributes be copied but only fail if
    // the basic attributes cannot be copied
    flags.copyBasicAttributes = true;
    flags.copyPosixAttributes = true;
    flags.copyNonPosixAttributes = true;
    flags.failIfUnableToCopyBasic = true;
    return flags;
}
 
Example 18
Source File: UnixCopyFile.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
static Flags fromCopyOptions(CopyOption... options) {
    Flags flags = new Flags();
    flags.followLinks = true;
    for (CopyOption option: options) {
        if (option == StandardCopyOption.REPLACE_EXISTING) {
            flags.replaceExisting = true;
            continue;
        }
        if (option == LinkOption.NOFOLLOW_LINKS) {
            flags.followLinks = false;
            continue;
        }
        if (option == StandardCopyOption.COPY_ATTRIBUTES) {
            // copy all attributes but only fail if basic attributes
            // cannot be copied
            flags.copyBasicAttributes = true;
            flags.copyPosixAttributes = true;
            flags.copyNonPosixAttributes = true;
            flags.failIfUnableToCopyBasic = true;
            continue;
        }
        if (ExtendedOptions.INTERRUPTIBLE.matches(option)) {
            flags.interruptible = true;
            continue;
        }
        if (option == null)
            throw new NullPointerException();
        throw new UnsupportedOperationException("Unsupported copy option");
    }
    return flags;
}
 
Example 19
Source File: IOHandler.java    From PolyGlot with MIT License 4 votes vote down vote up
public static void copyFile(Path fromLocation, Path toLocation, boolean replaceExisting) throws IOException {
    StandardCopyOption option = replaceExisting ? StandardCopyOption.REPLACE_EXISTING : StandardCopyOption.ATOMIC_MOVE;
    Files.copy(fromLocation, toLocation, option);
}