Class Files.CopyingFileVisitor

  • All Implemented Interfaces:
    java.nio.file.FileVisitor<java.nio.file.Path>
    Enclosing class:
    Files

    private static class Files.CopyingFileVisitor
    extends java.nio.file.SimpleFileVisitor<java.nio.file.Path>
    A FileVisitor implementation that copies a directory tree.
    • Constructor Summary

      Constructors 
      Constructor Description
      CopyingFileVisitor​(java.nio.file.Path source, java.nio.file.Path target, java.util.Set<java.nio.file.CopyOption> copyOptions, java.nio.file.LinkOption[] linkOptions)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      private void checkFileStore​(java.nio.file.Path path, java.nio.file.LinkOption... linkOptions)
      Throws a Files.FileStoreConstraintException if the path provided, with links resolved according to linkOptions, is not in the same FileStore as the specified copy source.
      private java.util.Optional<java.nio.file.Path> containedBy​(java.nio.file.Path tree, java.nio.file.Path candidate)
      Determines if a path is contained in the tree rooted by another.
      java.nio.file.FileVisitResult preVisitDirectory​(java.nio.file.Path dir, java.nio.file.attribute.BasicFileAttributes attrs)
      Create the target directories using Files.copy(Path, Path, CopyOption...) to in order to copy the directory attributes if requested.
      private java.nio.file.Path relocate​(java.nio.file.Path path)
      Make the source path provided refer to the same place in the target.
      java.nio.file.FileVisitResult visitFile​(java.nio.file.Path file, java.nio.file.attribute.BasicFileAttributes attrs)
      Copy the source file using Files.copy(Path, Path, CopyOption...) to the like location in the target copying attributes as requested.
      • Methods inherited from class java.nio.file.SimpleFileVisitor

        postVisitDirectory, visitFileFailed
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • sourceFileStore

        private final java.nio.file.FileStore sourceFileStore
      • target

        private final java.nio.file.Path target
      • copyOptions

        private final java.util.Set<java.nio.file.CopyOption> copyOptions
      • linkOptions

        private final java.nio.file.LinkOption[] linkOptions
      • source

        private final java.nio.file.Path source
      • noSpan

        private final boolean noSpan
      • deepCopy

        private final boolean deepCopy
      • effectiveCopyOptions

        private final java.nio.file.CopyOption[] effectiveCopyOptions
    • Constructor Detail

      • CopyingFileVisitor

        public CopyingFileVisitor​(java.nio.file.Path source,
                                  java.nio.file.Path target,
                                  java.util.Set<java.nio.file.CopyOption> copyOptions,
                                  java.nio.file.LinkOption[] linkOptions)
                           throws java.io.IOException
        Throws:
        java.io.IOException
    • Method Detail

      • relocate

        private java.nio.file.Path relocate​(java.nio.file.Path path)
        Make the source path provided refer to the same place in the target.
        Parameters:
        path - the source path to relativize/resolve
        Returns:
        the resolved target path
      • checkFileStore

        private void checkFileStore​(java.nio.file.Path path,
                                    java.nio.file.LinkOption... linkOptions)
                             throws java.io.IOException
        Throws a Files.FileStoreConstraintException if the path provided, with links resolved according to linkOptions, is not in the same FileStore as the specified copy source.
        Parameters:
        path - the Path for which the FileStore is to be determined
        linkOptions - LinkOption values used for interpreting path
        Throws:
        java.io.IOException - if an error is raised in determining the FileStore
      • containedBy

        private java.util.Optional<java.nio.file.Path> containedBy​(java.nio.file.Path tree,
                                                                   java.nio.file.Path candidate)
        Determines if a path is contained in the tree rooted by another. This method is used instead of Path.startsWith(Path) to properly handle differences observed under Windows with short- versus long-names under Windows.

        Both the tree and candidate paths must be non-relative

        Parameters:
        tree - the root of the tree in which candidate is tested for residence
        candidate - the candidate Path to test
        Returns:
        an Optional containing the relative path between tree and candidate iff candidate is within tree; otherwise an Optional.empty()
      • preVisitDirectory

        public java.nio.file.FileVisitResult preVisitDirectory​(java.nio.file.Path dir,
                                                               java.nio.file.attribute.BasicFileAttributes attrs)
                                                        throws java.io.IOException
        Create the target directories using Files.copy(Path, Path, CopyOption...) to in order to copy the directory attributes if requested.
        Specified by:
        preVisitDirectory in interface java.nio.file.FileVisitor<java.nio.file.Path>
        Overrides:
        preVisitDirectory in class java.nio.file.SimpleFileVisitor<java.nio.file.Path>
        Parameters:
        dir - the source directory to copy
        attrs - the attributes of the source directory
        Returns:
        FileVisitResult.CONTINUE
        Throws:
        java.io.IOException - if the copy fails
      • visitFile

        public java.nio.file.FileVisitResult visitFile​(java.nio.file.Path file,
                                                       java.nio.file.attribute.BasicFileAttributes attrs)
                                                throws java.io.IOException
        Copy the source file using Files.copy(Path, Path, CopyOption...) to the like location in the target copying attributes as requested. Links presented here are either replicated -- if the link target is within the source tree -- or copied -- if the link target resides outside of the source tree.
        Specified by:
        visitFile in interface java.nio.file.FileVisitor<java.nio.file.Path>
        Overrides:
        visitFile in class java.nio.file.SimpleFileVisitor<java.nio.file.Path>
        Parameters:
        file - the source file/link to copy
        attrs - the attributes of the source file
        Returns:
        FileVisitResult.CONTINUE
        Throws:
        java.io.IOException - if the copy fails