Package org.apache.nifi.util.file
Class FileUtils
java.lang.Object
org.apache.nifi.util.file.FileUtils
A utility class containing a few useful static methods to do typical IO operations.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final long
static final long
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic void
closeQuietly
(Closeable closeable) Closes the given closeable quietly - no logging, no exceptions...static byte[]
computeDigest
(File file) Returns the hash of the given file using default digest algorithmstatic long
copy
(InputStream in, OutputStream out) static long
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
getContainerCapacity
(Path path) Returns the capacity for a given pathstatic long
getContainerUsableSpace
(Path path) Returns the free capacity for a given pathstatic boolean
Returns true if the given files are the same according to their hash.static void
releaseQuietly
(FileLock lock) Releases the given lock quietly no logging, no exceptionstatic 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
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.
-
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
Closes the given closeable quietly - no logging, no exceptions...- Parameters:
closeable
- the thing to close
-
releaseQuietly
Releases the given lock quietly no logging, no exception- Parameters:
lock
- the lock to release
-
ensureDirectoryExistAndCanAccess
- Throws:
IOException
-
deleteFile
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 deletelogger
- the logger to provide logging information to about the operation- Returns:
- true if given file no longer exists
-
deleteFile
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 deletelogger
- the logger to write toattempts
- indicates how many times an attempt to delete should be made- Returns:
- true if given file no longer exists
-
deleteFile
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 nulllogger
- can be null
-
deleteFile
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 nulllogger
- can be nullattempts
- indicates how many times an attempt should be made to delete each file
-
deleteFilesInDir
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 deletefilter
- if null then no filter is usedlogger
- 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 scanfilter
- if null then no filter is usedlogger
- the logger to userecurse
- 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 scanfilter
- if null then no filter is usedlogger
- the loggerrecurse
- whether to recurse subdirectories or notdeleteEmptyDirectories
- default is false; if true will delete directories found that are empty
-
deleteFiles
Deletes given files.- Parameters:
files
- the files to deleterecurse
- will recurse if true; false otherwise- Throws:
IOException
- if any issues deleting specified files
-
deleteFile
- Throws:
IOException
-
shredFile
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, deletedpasses
- Number of times file should be overwritten- Throws:
IOException
- if something makes shredding or deleting a problem
-
copy
- 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 copydestination
- the file to copy tolockInputFile
- if true will lock input file during copy; if false will notlockOutputFile
- if true will lock output file during copy; if false will notmove
- 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 foundIOException
- if unable to read or write the underlying streamsSecurityException
- 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 fromdestination
- the file to copy tolockInputFile
- if true will lock input file during copy; if false will notlockOutputFile
- if true will lock output file during copy; if false will notlogger
- the logger to use- Returns:
- long number of bytes copied
- Throws:
FileNotFoundException
- if the source file could not be foundIOException
- if unable to read or write to fileSecurityException
- 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
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 theFile.renameTo
for more information.- Parameters:
source
- the file to renamedestination
- the file path to rename tomaxAttempts
- 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 theFile.renameTo
for more information.- Parameters:
source
- the file to renamedestination
- the file path to rename tomaxAttempts
- the max number of attempts to attempt the renamereplace
- 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 filerestoreFile
- the restore filelogger
- a logger- Throws:
IOException
- if an I/O problem was encountered during syncingIllegalStateException
- if the primary and restore copies exist but are different
-
isSame
Returns true if the given files are the same according to their hash.- Parameters:
file1
- a filefile2
- a file- Returns:
- true if the files are the same; false otherwise
- Throws:
IOException
- if the hash could not be computed
-
computeDigest
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
Returns the capacity for a given path- Parameters:
path
- path- Returns:
- total space
-
getContainerUsableSpace
Returns the free capacity for a given path- Parameters:
path
- path- Returns:
- usable space
-