public class FileUtils extends Object
Modifier and Type | Field and Description |
---|---|
static long |
MILLIS_BETWEEN_ATTEMPTS |
static long |
TRANSFER_CHUNK_SIZE_BYTES |
Constructor and Description |
---|
FileUtils() |
Modifier and Type | Method and Description |
---|---|
static void |
closeQuietly(Closeable closeable)
Closes the given closeable quietly - no logging, no exceptions...
|
static byte[] |
computeMd5Digest(File file)
Returns the MD5 hash of the given file.
|
static byte[] |
computeMd5Digest(InputStream stream)
Returns the MD5 hash of the given stream.
|
static long |
copy(InputStream in,
OutputStream out) |
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 |
ensureDirectoryExistAndCanAccess(File dir) |
static boolean |
isSame(File file1,
File file2)
Returns true if the given files are the same according to their MD5 hash.
|
static void |
releaseQuietly(FileLock lock)
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.
|
public static final long TRANSFER_CHUNK_SIZE_BYTES
public static final long MILLIS_BETWEEN_ATTEMPTS
public static void closeQuietly(Closeable closeable)
closeable
- the thing to closepublic static void releaseQuietly(FileLock lock)
lock
- the lock to releasepublic static void ensureDirectoryExistAndCanAccess(File dir) throws IOException
IOException
public static boolean deleteFile(File file, org.slf4j.Logger logger)
file
- the file to deletelogger
- the logger to provide logging information to about the operationpublic static boolean deleteFile(File file, org.slf4j.Logger logger, int attempts)
file
- the file to deletelogger
- the logger to write toattempts
- indicates how many times an attempt to delete should be madepublic static void deleteFile(List<File> files, org.slf4j.Logger logger)
files
- can be nulllogger
- can be nullpublic static void deleteFile(List<File> files, org.slf4j.Logger logger, int attempts)
files
- can be nulllogger
- can be nullattempts
- indicates how many times an attempt should be made to delete each filepublic static void deleteFilesInDir(File directory, FilenameFilter filter, org.slf4j.Logger logger)
directory
- the directory to scan for files to deletefilter
- if null then no filter is usedlogger
- the logger to usepublic static void deleteFilesInDir(File directory, FilenameFilter filter, org.slf4j.Logger logger, boolean recurse)
directory
- the directory to scanfilter
- if null then no filter is usedlogger
- the logger to userecurse
- indicates whether to recurse subdirectoriespublic static void deleteFilesInDir(File directory, FilenameFilter filter, org.slf4j.Logger logger, boolean recurse, boolean deleteEmptyDirectories)
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 emptypublic static void deleteFiles(Collection<File> files, boolean recurse) throws IOException
files
- the files to deleterecurse
- will recurse if true; false otherwiseIOException
- if any issues deleting specified filespublic static void deleteFile(File file, boolean recurse) throws IOException
IOException
public static void shredFile(File file, int passes) throws IOException
file
- File to be overwritten a number of times and, ultimately, deletedpasses
- Number of times file should be overwrittenIOException
- if something makes shredding or deleting a problempublic static long copy(InputStream in, OutputStream out) throws IOException
IOException
public static long copyBytes(byte[] bytes, File destination, boolean lockOutputFile) throws FileNotFoundException, IOException
FileNotFoundException
IOException
public static long copyFile(File source, File destination, boolean lockInputFile, boolean lockOutputFile, boolean move, org.slf4j.Logger logger) throws FileNotFoundException, IOException
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.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 operationspublic static long copyFile(File source, File destination, boolean lockInputFile, boolean lockOutputFile, org.slf4j.Logger logger) throws FileNotFoundException, IOException
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 useFileNotFoundException
- if the source file could not be foundIOException
- if unable to read or write to fileSecurityException
- if a security manager denies the needed file operationspublic static long copyFile(File source, OutputStream stream, boolean closeOutputStream, boolean lockInputFile) throws FileNotFoundException, IOException
FileNotFoundException
IOException
public static long copyFile(InputStream stream, File destination, boolean closeInputStream, boolean lockOutputFile) throws FileNotFoundException, IOException
FileNotFoundException
IOException
public static void renameFile(File source, File destination, int maxAttempts) throws IOException
File.renameTo
for more information.source
- the file to renamedestination
- the file path to rename tomaxAttempts
- the max number of attempts to attempt the renameIOException
- if rename isn't successfulpublic static void renameFile(File source, File destination, int maxAttempts, boolean replace) throws IOException
File.renameTo
for more information.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.IOException
- if rename isn't successfulpublic static void sleepQuietly(long millis)
public static void syncWithRestore(File primaryFile, File restoreFile, org.slf4j.Logger logger) throws IOException
primaryFile
- the primary filerestoreFile
- the restore filelogger
- a loggerIOException
- if an I/O problem was encountered during syncingIllegalStateException
- if the primary and restore copies exist but are differentpublic static boolean isSame(File file1, File file2) throws IOException
file1
- a filefile2
- a fileIOException
- if the MD5 hash could not be computedpublic static byte[] computeMd5Digest(File file) throws IOException
file
- a fileIOException
- if the MD5 hash could not be computedpublic static byte[] computeMd5Digest(InputStream stream) throws IOException
stream
- an input streamIOException
- if the MD5 hash could not be computedCopyright © 2017 Apache NiFi Project. All rights reserved.