Interface PathProcessor

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Interface Description
      static interface  PathProcessor.CollocatedTempFile
      A collocated temporary file, that resides next to a "target" file, and is removed when closed.
      static interface  PathProcessor.ProgressListener
      A listener object that is notified for every progress made while copying files.
      static interface  PathProcessor.TempFile
      A temporary file, that is removed when closed.
    • Method Summary

      All Methods Instance Methods Abstract Methods Default Methods 
      Modifier and Type Method Description
      default void copy​(java.nio.file.Path source, java.nio.file.Path target)
      Copies the specified source file to the given target file.
      long copy​(java.nio.file.Path source, java.nio.file.Path target, PathProcessor.ProgressListener listener)
      Copies the specified source file to the given target file.
      default void copyWithTimestamp​(java.nio.file.Path source, java.nio.file.Path target)
      Same as copy(Path, Path) but sets source file last modified timestamp on target as well.
      default long lastModified​(java.nio.file.Path path, long defValue)
      Returns last modified of path in milliseconds, if exists.
      void move​(java.nio.file.Path source, java.nio.file.Path target)
      Moves the specified source file to the given target file.
      PathProcessor.TempFile newTempFile()
      Creates a PathProcessor.TempFile instance and backing temporary file on file system.
      PathProcessor.CollocatedTempFile newTempFile​(java.nio.file.Path file)
      Creates a PathProcessor.CollocatedTempFile instance for given file without backing file.
      boolean setLastModified​(java.nio.file.Path path, long value)
      Sets last modified of path in milliseconds, if exists.
      default long size​(java.nio.file.Path path, long defValue)
      Returns size of file, if exists.
      void write​(java.nio.file.Path target, java.io.InputStream source)
      Writes the given stream to a file.
      void write​(java.nio.file.Path target, java.lang.String data)
      Writes the given data to a file.
      void writeWithBackup​(java.nio.file.Path target, java.io.InputStream source)
      Writes the given stream to a file.
      void writeWithBackup​(java.nio.file.Path target, java.lang.String data)
      Writes the given data to a file.
    • Method Detail

      • lastModified

        default long lastModified​(java.nio.file.Path path,
                                  long defValue)
        Returns last modified of path in milliseconds, if exists.
        Parameters:
        path - The path, may be null.
        Throws:
        java.io.UncheckedIOException - If an I/O error occurs.
      • setLastModified

        boolean setLastModified​(java.nio.file.Path path,
                                long value)
                         throws java.io.IOException
        Sets last modified of path in milliseconds, if exists.
        Parameters:
        path - The path, may be null.
        Throws:
        java.io.IOException - If an I/O error occurs. Some exceptions/reasons of failure to set mtime may be swallowed, and can be multiple, ranging from "file not found" to cases when FS does not support the setting the mtime.
        Since:
        2.0.0
      • size

        default long size​(java.nio.file.Path path,
                          long defValue)
        Returns size of file, if exists.
        Parameters:
        path - The path, may be null.
        Throws:
        java.io.UncheckedIOException - If an I/O error occurs.
      • write

        void write​(java.nio.file.Path target,
                   java.lang.String data)
            throws java.io.IOException
        Writes the given data to a file. UTF-8 is assumed as encoding for the data. Creates the necessary directories for the target file. In case of an error, the created directories will be left on the file system.
        Parameters:
        target - The file to write to, must not be null. This file will be overwritten.
        data - The data to write, may be null.
        Throws:
        java.io.IOException - If an I/O error occurs.
      • write

        void write​(java.nio.file.Path target,
                   java.io.InputStream source)
            throws java.io.IOException
        Writes the given stream to a file. Creates the necessary directories for the target file. In case of an error, the created directories will be left on the file system.
        Parameters:
        target - The file to write to, must not be null. This file will be overwritten.
        source - The stream to write to the file, must not be null.
        Throws:
        java.io.IOException - If an I/O error occurs.
      • writeWithBackup

        void writeWithBackup​(java.nio.file.Path target,
                             java.lang.String data)
                      throws java.io.IOException
        Writes the given data to a file. UTF-8 is assumed as encoding for the data. Creates the necessary directories for the target file. In case of an error, the created directories will be left on the file system.
        Parameters:
        target - The file to write to, must not be null. This file will be overwritten.
        data - The data to write, may be null.
        Throws:
        java.io.IOException - If an I/O error occurs.
        Since:
        2.0.13
      • writeWithBackup

        void writeWithBackup​(java.nio.file.Path target,
                             java.io.InputStream source)
                      throws java.io.IOException
        Writes the given stream to a file. Creates the necessary directories for the target file. In case of an error, the created directories will be left on the file system.
        Parameters:
        target - The file to write to, must not be null. This file will be overwritten.
        source - The stream to write to the file, must not be null.
        Throws:
        java.io.IOException - If an I/O error occurs.
        Since:
        2.0.13
      • move

        void move​(java.nio.file.Path source,
                  java.nio.file.Path target)
           throws java.io.IOException
        Moves the specified source file to the given target file. If the target file already exists, it is overwritten. Creates the necessary directories for the target file. In case of an error, the created directories will be left on the file system.
        Parameters:
        source - The file to move from, must not be null.
        target - The file to move to, must not be null.
        Throws:
        java.io.IOException - If an I/O error occurs.
      • copy

        default void copy​(java.nio.file.Path source,
                          java.nio.file.Path target)
                   throws java.io.IOException
        Copies the specified source file to the given target file. Creates the necessary directories for the target file. In case of an error, the created directories will be left on the file system.
        Parameters:
        source - The file to copy from, must not be null.
        target - The file to copy to, must not be null.
        Throws:
        java.io.IOException - If an I/O error occurs.
      • copyWithTimestamp

        default void copyWithTimestamp​(java.nio.file.Path source,
                                       java.nio.file.Path target)
                                throws java.io.IOException
        Same as copy(Path, Path) but sets source file last modified timestamp on target as well.
        Parameters:
        source - The file to copy from, must not be null.
        target - The file to copy to, must not be null.
        Throws:
        java.io.IOException - If an I/O error occurs.
        Since:
        2.0.0
        See Also:
        setLastModified(Path, long)
      • copy

        long copy​(java.nio.file.Path source,
                  java.nio.file.Path target,
                  PathProcessor.ProgressListener listener)
           throws java.io.IOException
        Copies the specified source file to the given target file. Creates the necessary directories for the target file. In case of an error, the created directories will be left on the file system.
        Parameters:
        source - The file to copy from, must not be null.
        target - The file to copy to, must not be null.
        listener - The listener to notify about the copy progress, may be null.
        Returns:
        The number of copied bytes.
        Throws:
        java.io.IOException - If an I/O error occurs.
      • newTempFile

        PathProcessor.TempFile newTempFile()
                                    throws java.io.IOException
        Creates a PathProcessor.TempFile instance and backing temporary file on file system. It will be located in the default temporary-file directory. Returned instance should be handled in try-with-resource construct and created temp file is removed (if exists) when returned instance is closed.

        This method uses Files.createTempFile(String, String, java.nio.file.attribute.FileAttribute[]) to create the temporary file on file system.

        Throws:
        java.io.IOException
        Since:
        2.0.13
      • newTempFile

        PathProcessor.CollocatedTempFile newTempFile​(java.nio.file.Path file)
                                              throws java.io.IOException
        Creates a PathProcessor.CollocatedTempFile instance for given file without backing file. The path will be located in same directory where given file is, and will reuse its name for generated (randomized) name. Returned instance should be handled in try-with-resource and created temp path is removed (if exists) when returned instance is closed. The PathProcessor.CollocatedTempFile.move() makes possible to atomically replace passed in file with the processed content written into a file backing the PathProcessor.CollocatedTempFile instance.

        The file nor it's parent directories have to exist. The parent directories are created if needed.

        This method uses Path.resolve(String) to create the temporary file path in passed in file parent directory, but it does NOT create backing file on file system.

        Throws:
        java.io.IOException
        Since:
        2.0.13