Class FileUtils

java.lang.Object
org.apache.nifi.util.file.FileUtils

public class FileUtils extends Object
A utility class containing a few useful static methods to do typical IO operations.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final long
     
    static final long
     
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static void
    Closes the given closeable quietly - no logging, no exceptions...
    static byte[]
    Returns the hash of the given file using default digest algorithm
    static long
     
    static long
    copyBytes(byte[] bytes, File destination, boolean lockOutputFile)
     
    static long
    copyFile(File source, File destination, boolean lockInputFile, boolean lockOutputFile, boolean move, org.slf4j.Logger logger)
    Copies the given source file to the given destination file.
    static long
    copyFile(File source, File destination, boolean lockInputFile, boolean lockOutputFile, org.slf4j.Logger logger)
    Copies the given source file to the given destination file.
    static long
    copyFile(File source, OutputStream stream, boolean closeOutputStream, boolean lockInputFile)
     
    static long
    copyFile(InputStream stream, File destination, boolean closeInputStream, boolean lockOutputFile)
     
    static void
    deleteFile(File file, boolean recurse)
     
    static boolean
    deleteFile(File file, org.slf4j.Logger logger)
    Deletes the given file.
    static boolean
    deleteFile(File file, org.slf4j.Logger logger, int attempts)
    Deletes the given file.
    static void
    deleteFile(List<File> files, org.slf4j.Logger logger)
    Deletes all of the given files.
    static void
    deleteFile(List<File> files, org.slf4j.Logger logger, int attempts)
    Deletes all of the given files.
    static void
    deleteFiles(Collection<File> files, boolean recurse)
    Deletes given files.
    static void
    deleteFilesInDir(File directory, FilenameFilter filter, org.slf4j.Logger logger)
    Deletes all files (not directories..) in the given directory (non recursive) that match the given filename filter.
    static void
    deleteFilesInDir(File directory, FilenameFilter filter, org.slf4j.Logger logger, boolean recurse)
    Deletes all files (not directories) in the given directory (recursive) that match the given filename filter.
    static void
    deleteFilesInDir(File directory, FilenameFilter filter, org.slf4j.Logger logger, boolean recurse, boolean deleteEmptyDirectories)
    Deletes all files (not directories) in the given directory (recursive) that match the given filename filter.
    static void
     
    static long
    Returns the capacity for a given path
    static long
    Returns the free capacity for a given path
    static boolean
    isSame(File file1, File file2)
    Returns true if the given files are the same according to their hash.
    static void
    Releases the given lock quietly no logging, no exception
    static void
    renameFile(File source, File destination, int maxAttempts)
    Renames the given file from the source path to the destination path.
    static void
    renameFile(File source, File destination, int maxAttempts, boolean replace)
    Renames the given file from the source path to the destination path.
    static void
    shredFile(File file, int passes)
    Randomly generates a sequence of bytes and overwrites the contents of the file a number of times.
    static void
    sleepQuietly(long millis)
     
    static void
    syncWithRestore(File primaryFile, File restoreFile, org.slf4j.Logger logger)
    Syncs a primary copy of a file with the copy in the restore directory.

    Methods inherited from class java.lang.Object

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

    • TRANSFER_CHUNK_SIZE_BYTES

      public static final long TRANSFER_CHUNK_SIZE_BYTES
      See Also:
    • MILLIS_BETWEEN_ATTEMPTS

      public static final long MILLIS_BETWEEN_ATTEMPTS
      See Also:
  • Constructor Details

    • FileUtils

      public FileUtils()
  • Method Details

    • closeQuietly

      public static void closeQuietly(Closeable closeable)
      Closes the given closeable quietly - no logging, no exceptions...
      Parameters:
      closeable - the thing to close
    • releaseQuietly

      public static void releaseQuietly(FileLock lock)
      Releases the given lock quietly no logging, no exception
      Parameters:
      lock - the lock to release
    • ensureDirectoryExistAndCanAccess

      public static void ensureDirectoryExistAndCanAccess(File dir) throws IOException
      Throws:
      IOException
    • deleteFile

      public static boolean deleteFile(File file, org.slf4j.Logger logger)
      Deletes the given file. If the given file exists but could not be deleted this will be printed as a warning to the given logger
      Parameters:
      file - the file to delete
      logger - the logger to provide logging information to about the operation
      Returns:
      true if given file no longer exists
    • deleteFile

      public static boolean deleteFile(File file, org.slf4j.Logger logger, int attempts)
      Deletes the given file. If the given file exists but could not be deleted this will be printed as a warning to the given logger
      Parameters:
      file - the file to delete
      logger - the logger to write to
      attempts - indicates how many times an attempt to delete should be made
      Returns:
      true if given file no longer exists
    • deleteFile

      public static void deleteFile(List<File> files, org.slf4j.Logger logger)
      Deletes all of the given files. If any exist and cannot be deleted that will be printed at warn to the given logger.
      Parameters:
      files - can be null
      logger - can be null
    • deleteFile

      public static void deleteFile(List<File> files, org.slf4j.Logger logger, int attempts)
      Deletes all of the given files. If any exist and cannot be deleted that will be printed at warn to the given logger.
      Parameters:
      files - can be null
      logger - can be null
      attempts - indicates how many times an attempt should be made to delete each file
    • deleteFilesInDir

      public static void deleteFilesInDir(File directory, FilenameFilter filter, org.slf4j.Logger logger)
      Deletes all files (not directories..) in the given directory (non recursive) that match the given filename filter. If any file cannot be deleted then this is printed at warn to the given logger.
      Parameters:
      directory - the directory to scan for files to delete
      filter - if null then no filter is used
      logger - the logger to use
    • deleteFilesInDir

      public static void deleteFilesInDir(File directory, FilenameFilter filter, org.slf4j.Logger logger, boolean recurse)
      Deletes all files (not directories) in the given directory (recursive) that match the given filename filter. If any file cannot be deleted then this is printed at warn to the given logger.
      Parameters:
      directory - the directory to scan
      filter - if null then no filter is used
      logger - the logger to use
      recurse - indicates whether to recurse subdirectories
    • deleteFilesInDir

      public static void deleteFilesInDir(File directory, FilenameFilter filter, org.slf4j.Logger logger, boolean recurse, boolean deleteEmptyDirectories)
      Deletes all files (not directories) in the given directory (recursive) that match the given filename filter. If any file cannot be deleted then this is printed at warn to the given logger.
      Parameters:
      directory - the directory to scan
      filter - if null then no filter is used
      logger - the logger
      recurse - whether to recurse subdirectories or not
      deleteEmptyDirectories - default is false; if true will delete directories found that are empty
    • deleteFiles

      public static void deleteFiles(Collection<File> files, boolean recurse) throws IOException
      Deletes given files.
      Parameters:
      files - the files to delete
      recurse - will recurse if true; false otherwise
      Throws:
      IOException - if any issues deleting specified files
    • deleteFile

      public static void deleteFile(File file, boolean recurse) throws IOException
      Throws:
      IOException
    • shredFile

      public static void shredFile(File file, int passes) throws IOException
      Randomly generates a sequence of bytes and overwrites the contents of the file a number of times. The file is then deleted.
      Parameters:
      file - File to be overwritten a number of times and, ultimately, deleted
      passes - Number of times file should be overwritten
      Throws:
      IOException - if something makes shredding or deleting a problem
    • copy

      public static long copy(InputStream in, OutputStream out) throws IOException
      Throws:
      IOException
    • copyBytes

      public static long copyBytes(byte[] bytes, File destination, boolean lockOutputFile) throws FileNotFoundException, IOException
      Throws:
      FileNotFoundException
      IOException
    • copyFile

      public static long copyFile(File source, File destination, boolean lockInputFile, boolean lockOutputFile, boolean move, org.slf4j.Logger logger) throws FileNotFoundException, IOException
      Copies the given source file to the given destination file. The given destination will be overwritten if it already exists.
      Parameters:
      source - the file to copy
      destination - the file to copy to
      lockInputFile - if true will lock input file during copy; if false will not
      lockOutputFile - if true will lock output file during copy; if false will not
      move - if true will perform what is effectively a move operation rather than a pure copy. This allows for potentially highly efficient movement of the file but if not possible this will revert to a copy then delete behavior. If false, then the file is copied and the source file is retained. If a true rename/move occurs then no lock is held during that time.
      logger - if failures occur, they will be logged to this logger if possible. If this logger is null, an IOException will instead be thrown, indicating the problem.
      Returns:
      long number of bytes copied
      Throws:
      FileNotFoundException - if the source file could not be found
      IOException - if unable to read or write the underlying streams
      SecurityException - if a security manager denies the needed file operations
    • copyFile

      public static long copyFile(File source, File destination, boolean lockInputFile, boolean lockOutputFile, org.slf4j.Logger logger) throws FileNotFoundException, IOException
      Copies the given source file to the given destination file. The given destination will be overwritten if it already exists.
      Parameters:
      source - the file to copy from
      destination - the file to copy to
      lockInputFile - if true will lock input file during copy; if false will not
      lockOutputFile - if true will lock output file during copy; if false will not
      logger - the logger to use
      Returns:
      long number of bytes copied
      Throws:
      FileNotFoundException - if the source file could not be found
      IOException - if unable to read or write to file
      SecurityException - if a security manager denies the needed file operations
    • copyFile

      public static long copyFile(File source, OutputStream stream, boolean closeOutputStream, boolean lockInputFile) throws FileNotFoundException, IOException
      Throws:
      FileNotFoundException
      IOException
    • copyFile

      public static long copyFile(InputStream stream, File destination, boolean closeInputStream, boolean lockOutputFile) throws FileNotFoundException, IOException
      Throws:
      FileNotFoundException
      IOException
    • renameFile

      public static void renameFile(File source, File destination, int maxAttempts) throws IOException
      Renames the given file from the source path to the destination path. This handles multiple attempts. This should only be used to rename within a given directory. Renaming across directories might not work well. See the File.renameTo for more information.
      Parameters:
      source - the file to rename
      destination - the file path to rename to
      maxAttempts - the max number of attempts to attempt the rename
      Throws:
      IOException - if rename isn't successful
    • renameFile

      public static void renameFile(File source, File destination, int maxAttempts, boolean replace) throws IOException
      Renames the given file from the source path to the destination path. This handles multiple attempts. This should only be used to rename within a given directory. Renaming across directories might not work well. See the File.renameTo for more information.
      Parameters:
      source - the file to rename
      destination - the file path to rename to
      maxAttempts - the max number of attempts to attempt the rename
      replace - if true and a rename attempt fails will check if a file is already at the destination path. If so it will delete that file and attempt the rename according the remaining maxAttempts. If false, any conflicting files will be left as they were and the rename attempts will fail if conflicting.
      Throws:
      IOException - if rename isn't successful
    • sleepQuietly

      public static void sleepQuietly(long millis)
    • syncWithRestore

      public static void syncWithRestore(File primaryFile, File restoreFile, org.slf4j.Logger logger) throws IOException
      Syncs a primary copy of a file with the copy in the restore directory. If the restore directory does not have a file and the primary has a file, the the primary's file is copied to the restore directory. Else if the restore directory has a file, but the primary does not, then the restore's file is copied to the primary directory. Else if the primary file is different than the restore file, then an IllegalStateException is thrown. Otherwise, if neither file exists, then no syncing is performed.
      Parameters:
      primaryFile - the primary file
      restoreFile - the restore file
      logger - a logger
      Throws:
      IOException - if an I/O problem was encountered during syncing
      IllegalStateException - if the primary and restore copies exist but are different
    • isSame

      public static boolean isSame(File file1, File file2) throws IOException
      Returns true if the given files are the same according to their hash.
      Parameters:
      file1 - a file
      file2 - a file
      Returns:
      true if the files are the same; false otherwise
      Throws:
      IOException - if the hash could not be computed
    • computeDigest

      public static byte[] computeDigest(File file) throws IOException
      Returns the hash of the given file using default digest algorithm
      Parameters:
      file - a file
      Returns:
      Digest Hash Bytes
      Throws:
      IOException - if the hash could not be computed
    • getContainerCapacity

      public static long getContainerCapacity(Path path)
      Returns the capacity for a given path
      Parameters:
      path - path
      Returns:
      total space
    • getContainerUsableSpace

      public static long getContainerUsableSpace(Path path)
      Returns the free capacity for a given path
      Parameters:
      path - path
      Returns:
      usable space