Class FileUtil
java.lang.Object
com.biglybt.core.util.FileUtil
File utility class.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classstatic class(package private) static interfacestatic interface -
Field Summary
FieldsModifier and TypeFieldDescription(package private) static final Objectprivate static char[]private static final AEMonitorstatic final Stringprivate static AEDiagnosticsLoggerprivate static final FileHandlerprivate static booleanprivate static AsyncDispatcherprivate static String[]private static AsyncDispatcherprivate static longprivate static Objectprivate static booleanprivate static booleanprivate static File[]private static longprivate static final LogIDsprivate static AsyncDispatcherprivate static final intprivate static final Listprivate static AESemaphoreprivate static AsyncDispatcherprivate static booleanGets the encoding that should be used when writing script files (currently only tested for windows as this is where an issue can arise...) We also only test based on the user-data directory name to see if an explicit encoding switch is required...private static Stringprivate static final ThreadLocal<IdentityHashSet<CoreOperationTask>> -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic booleanareFilePathsIdentical(File f1, File f2) static voidbackupFile(File _file, boolean _make_copy) Backup the given file to filename.bak, removing the old .bak file if necessary.static voidbackupFile(String _filename, boolean _make_copy) Backup the given file to filename.bak, removing the old .bak file if necessary.static booleancanReadWithTimeout(File file) static booleancanReadWithTimeout(File file, long strict_timeout) static booleanstatic booleancanWriteToDirectory(File dir) static booleancontainsPathSegment(File f, String path, boolean caseSensitive) static StringconvertOSSpecificChars(String file_name_in, boolean is_folder) static booleanstatic booleancopyFile(File _source, OutputStream _dest, boolean closeInputStream) static voidcopyFile(InputStream _source, File _dest) copys the input stream to the file.static voidcopyFile(InputStream _source, File _dest, boolean _close_input_stream) static voidcopyFile(InputStream is, OutputStream os) static voidcopyFile(InputStream is, OutputStream os, boolean closeInputStream) static voidcopyFile(InputStream is, OutputStream os, boolean closeInputStream, FileUtil.ProgressListener pl) static voidcopyFile(InputStream is, OutputStream os, FileUtil.ProgressListener pl) static voidcopyFileOrDirectory(File from_file_or_dir, File to_parent_dir) static booleancopyFileWithDates(File from_file, File to_file) static voidcopyFileWithException(File _source, File _dest, FileUtil.ProgressListener pl) static voidstatic voiddeleteResilientFile(File file) static booleandeleteWithRecycle(File file, boolean force_no_recycle) static final booleanstatic booleanexistsWithCache(File file) caches positive existence checks for a short time, therefore may answer incorrectly...static final booleanexistsWithCache(File parent, String name) caches directory contents for a short time, therefore may answer incorrectly...static final voidexistsWithCacheClear(File parent) static booleanexistsWithTimeout(File file) static booleanexistsWithTimeout(File file, long strict_timeout) private static booleanexistsWithTimeoutAndException(File file, long strict_timeout) static FilegetApplicationFile(String filename) Get a file relative to this program's install directory.static FilegetCanonicalFile(File file) static StringgetCanonicalFileName(String filename) static FilegetCanonicalFileSafe(File file) Preserves the case of the file.name when the file exists but differs in casestatic StringgetCanonicalPath(File file, boolean lax) If lax then the parent folder's canonical form is cached and checked, the file name itself isn't canonicalised via the file systemstatic StringgetCanonicalPathSafe(File file) Preserves the case of the file.name when the file exists but differs in casestatic Stringstatic StringgetCanonicalPathWithTimeout(File file, long strict_timeout) static StringgetExtension(String fName) Gets the extension of a file name, ensuring we don't go into the pathstatic longgetFileCreationTime(File file) Copy the given source file to the given destination file.static FilegetFileOrBackup(String _filename) Returns the file handle for the given filename or it's equivalent .bak backup file if the original doesn't exist or is 0-sized.static longgetFileOrDirectorySize(File file) static StringgetFileStoreName(File file) static String[]getFileStoreNames(long timeout) static String[]getFileStoreNames(File... files) static voidgetFileStoreNames(Consumer<String[]> callback) static FilegetJarFileFromClass(Class cla) static FilegetJarFileFromURL(String url_str) static StringgetRelativePath(File parentDir, File file) private static voidstatic Stringstatic longstatic FilegetUserFile(String filename) static booleanstatic booleanisAncestorOf(File _parent, File _child) static booleanisDirectoryWithTimeout(File file) static booleanisDirectoryWithTimeout(File file, long strict_timeout) static booleanstatic booleanisResponding(File file, long strict_timeout) static longlengthWithTimeout(File file) static longlengthWithTimeout(File file, long strict_timeout) static File[]static File[]listRootsWithTimeout(long timeout) static voidstatic voidstatic voidstatic booleanMakes Directories as long as the directory isn't directly in Volumes (OSX)static Filestatic Filestatic Filestatic FMFileAccess.FileAccessornewFileAccessor(File file, String access_mode, boolean explicit_sparse) static FileInputStreamnewFileInputStream(File from_file) static FileOutputStreamnewFileOutputStream(File file) static FileOutputStreamnewFileOutputStream(File file, boolean append) static byte[]readFileAsByteArray(File file) static StringreadFileAsString(File file, int size_limit) static StringreadFileAsString(File file, int size_limit, String charset) static StringreadFileEndAsString(File file, int size_limit, String charset) static StringreadGZippedFileAsString(File file, int size_limit) static byte[]static byte[]readInputStreamAsByteArray(InputStream is, int size_limit) static StringreadInputStreamAsString(InputStream is, int size_limit) static StringreadInputStreamAsString(InputStream is, int size_limit, int timeoutMillis, String charSet) static StringreadInputStreamAsString(InputStream is, int size_limit, String charSet) static StringreadInputStreamAsStringWithTruncation(InputStream is, int size_limit) static MapreadResilientConfigFile(String file_name) static MapreadResilientConfigFile(String file_name, boolean use_backups) static MapreadResilientFile(File file) static MapreadResilientFile(File parent_dir, String file_name, boolean use_backup) static MapreadResilientFile(File parent_dir, String file_name, boolean use_backup, boolean intern_keys) private static MapreadResilientFile(String original_file_name, File parent_dir, String file_name, int fail_count, boolean recovery_mode, boolean skip_key_intern) private static MapreadResilientFileSupport(File parent_dir, String file_name, boolean attempt_recovery, boolean intern_keys) private static StringreallyCopyFile(File from_file, File to_file, FileUtil.ProgressListener pl) static booleanreallyExists(File file) Verifies the case of the file.namestatic booleanDeletes the given dir and all files/dirs underneathstatic booleanprotected static voidrecursiveEmptyDirDelete(File f, Set<String> ignore_set, boolean log_warnings) private static voidrecursiveEmptyDirDelete(File f, Set<String> ignore_set, int level, boolean log_warnings) private static voidstatic Stringstatic booleanrenameFile(File from_file, File to_file) private static StringrenameFile(File from_file, File to_file, boolean fail_on_existing_directory) static StringrenameFile(File from_file, File to_file, boolean fail_on_existing_directory, FileFilter file_filter, FileUtil.ProgressListener pl) static booleanrenameFile(File from_file, File to_file, FileUtil.ProgressListener pl) private static StringrenameFileSupport(File from_file, File to_file, boolean fail_on_existing_directory, FileFilter file_filter, FileUtil.ProgressListener pl) static booleanstatic voidrunAsTask(int op_type, CoreOperationTask task) static voidrunAsTask(CoreOperationTask task) private static <T> TrunFileOpWithTimeout(File file, FileUtil.FileOpWithTimeout<T> fo, T def, long strict_timeout) private static <T> TrunFileOpWithTimeoutEx(File file, FileUtil.FileOpWithTimeout<T> fo, IOException def_error, long strict_timeout) private static <T> TrunFileOpWithTimeoutEx(File file, FileUtil.FileOpWithTimeout<T> fo, T def_result, IOException def_error, long strict_timeout) static StringtranslateMoveFilePath(String old_root, String new_root, String file_to_move) static voidwriteBytesAsFile(String filename, byte[] file_data) static booleanwriteBytesAsFile2(String filename, byte[] file_data) static voidwriteResilientConfigFile(String file_name, Map data) private static booleanwriteResilientFile(File parent_dir, String file_name, Map data) static voidwriteResilientFile(File parent_dir, String file_name, Map data, boolean use_backup) static voidwriteResilientFile(File parent_dir, String file_name, Map data, boolean use_backup, boolean copy_to_backup) static voidwriteResilientFile(File file, Map data) private static booleanwriteResilientFileIncrementally(File parent_dir, String file_name, Map data) static voidwriteResilientFileIncrementally(File file, Map data) static booleanwriteResilientFileWithResult(File parent_dir, String file_name, Map data) static booleanwriteStringAsFile(File file, String text) static booleanwriteStringAsFile(File file, String text, String charset)
-
Field Details
-
LOGID
-
DIR_SEP
-
RESERVED_FILE_HANDLE_COUNT
private static final int RESERVED_FILE_HANDLE_COUNT- See Also:
-
first_reservation
private static boolean first_reservation -
is_my_lock_file
private static boolean is_my_lock_file -
reserved_file_handles
-
class_mon
-
char_conversion_mapping
private static char[] char_conversion_mapping -
fileHandling
-
file_logger
-
tls
-
clax_cache
-
exists_dir_cache
-
CACHE_TIME_KEY
-
exists_cache
-
fs_names
-
fs_names_last
private static long fs_names_last -
fs_names_lock
-
fs_names_dispatcher
-
fs_updating
private static boolean fs_updating -
fs_pending_callbacks
-
recycler
-
sce_checked
private static boolean sce_checkedGets the encoding that should be used when writing script files (currently only tested for windows as this is where an issue can arise...) We also only test based on the user-data directory name to see if an explicit encoding switch is required... -
script_encoding
-
bad_roots
-
fot_dispatcher
-
last_roots
-
last_roots_time
private static long last_roots_time -
root_updater
-
root_update_sem
-
-
Constructor Details
-
FileUtil
public FileUtil()
-
-
Method Details
-
areFilePathsIdentical
-
reallyExists
Verifies the case of the file.name- Parameters:
file-- Returns:
-
getCanonicalFileSafe
-
getCanonicalPathSafe
-
isAncestorOf
-
getCanonicalFile
-
getCanonicalPath
If lax then the parent folder's canonical form is cached and checked, the file name itself isn't canonicalised via the file system- Parameters:
file-lax-- Returns:
- Throws:
IOException
-
exists
-
existsWithCache
caches directory contents for a short time, therefore may answer incorrectly... NOTE this does not cater for file system case insensitivity issues so don't use it unless you are sure these will NOT ARISE!!!!- Parameters:
parent-name-- Returns:
-
existsWithCacheClear
-
existsWithCache
caches positive existence checks for a short time, therefore may answer incorrectly...- Parameters:
file-- Returns:
-
getCanonicalFileName
-
getUserFile
-
getApplicationFile
-
recursiveDelete
Deletes the given dir and all files/dirs underneath -
recursiveDeleteNoCheck
-
getFileOrDirectorySize
-
recursiveEmptyDirDelete
-
recursiveEmptyDirDelete
-
convertOSSpecificChars
-
writeResilientConfigFile
-
writeResilientFile
-
writeResilientFileIncrementally
-
writeResilientFileWithResult
-
writeResilientFile
-
writeResilientFile
-
writeResilientFile
-
writeResilientFileIncrementally
-
resilientConfigFileExists
-
readResilientConfigFile
-
readResilientConfigFile
-
readResilientFile
-
readResilientFile
-
readResilientFile
-
readResilientFileSupport
-
readResilientFile
-
deleteResilientFile
-
deleteResilientConfigFile
-
getReservedFileHandles
private static void getReservedFileHandles() -
releaseReservedFileHandles
private static void releaseReservedFileHandles() -
isMyFileLock
public static boolean isMyFileLock() -
backupFile
Backup the given file to filename.bak, removing the old .bak file if necessary. If _make_copy is true, the original file will copied to backup, rather than moved.- Parameters:
_filename- name of file to backup_make_copy- copy instead of move
-
backupFile
Backup the given file to filename.bak, removing the old .bak file if necessary. If _make_copy is true, the original file will copied to backup, rather than moved.- Parameters:
_file- file to backup_make_copy- copy instead of move
-
getFileCreationTime
Copy the given source file to the given destination file. Returns file copy success or not.- Parameters:
_source- source file_dest- destination file- Returns:
- true if file copy successful, false if copy failed
-
copyFileWithDates
-
copyFile
-
copyFileWithException
public static void copyFileWithException(File _source, File _dest, FileUtil.ProgressListener pl) throws IOException - Throws:
IOException
-
copyFile
-
copyFile
copys the input stream to the file. always closes the input stream- Parameters:
_source-_dest-- Throws:
IOException
-
copyFile
public static void copyFile(InputStream _source, File _dest, boolean _close_input_stream) throws IOException - Throws:
IOException
-
copyFile
- Throws:
IOException
-
copyFile
public static void copyFile(InputStream is, OutputStream os, FileUtil.ProgressListener pl) throws IOException - Throws:
IOException
-
copyFile
public static void copyFile(InputStream is, OutputStream os, boolean closeInputStream) throws IOException - Throws:
IOException
-
copyFile
public static void copyFile(InputStream is, OutputStream os, boolean closeInputStream, FileUtil.ProgressListener pl) throws IOException - Throws:
IOException
-
copyFileOrDirectory
public static void copyFileOrDirectory(File from_file_or_dir, File to_parent_dir) throws IOException - Throws:
IOException
-
getFileOrBackup
Returns the file handle for the given filename or it's equivalent .bak backup file if the original doesn't exist or is 0-sized. If neither the original nor the backup are available, a null handle is returned.- Parameters:
_filename- root name of file- Returns:
- file if successful, null if failed
-
getJarFileFromClass
-
getJarFileFromURL
-
renameFile
-
renameFile
-
renameFile
-
renameFile
public static String renameFile(File from_file, File to_file, boolean fail_on_existing_directory, FileFilter file_filter, FileUtil.ProgressListener pl) -
renameFileSupport
private static String renameFileSupport(File from_file, File to_file, boolean fail_on_existing_directory, FileFilter file_filter, FileUtil.ProgressListener pl) -
getFileStoreNames
-
getFileStoreNames
-
getFileStoreName
-
getFileStoreNames
-
reallyCopyFile
-
newFileInputStream
- Throws:
FileNotFoundException
-
newFileOutputStream
- Throws:
FileNotFoundException
-
newFileOutputStream
public static FileOutputStream newFileOutputStream(File file, boolean append) throws FileNotFoundException - Throws:
FileNotFoundException
-
writeStringAsFile
-
writeStringAsFile
-
writeBytesAsFile
-
writeBytesAsFile2
-
deleteWithRecycle
-
translateMoveFilePath
-
hasTask
-
runAsTask
-
runAsTask
-
mkdirs
Makes Directories as long as the directory isn't directly in Volumes (OSX)- Parameters:
f-- Returns:
-
getExtension
-
readFileAsString
- Throws:
IOException
-
readFileAsString
- Throws:
IOException
-
readGZippedFileAsString
- Throws:
IOException
-
readInputStreamAsString
- Throws:
IOException
-
readInputStreamAsString
public static String readInputStreamAsString(InputStream is, int size_limit, String charSet) throws IOException - Throws:
IOException
-
readInputStreamAsString
public static String readInputStreamAsString(InputStream is, int size_limit, int timeoutMillis, String charSet) throws IOException - Throws:
IOException
-
readInputStreamAsStringWithTruncation
public static String readInputStreamAsStringWithTruncation(InputStream is, int size_limit) throws IOException - Throws:
IOException
-
readFileEndAsString
public static String readFileEndAsString(File file, int size_limit, String charset) throws IOException - Throws:
IOException
-
readInputStreamAsByteArray
- Throws:
IOException
-
readInputStreamAsByteArray
- Throws:
IOException
-
readFileAsByteArray
- Throws:
IOException
-
getUsableSpace
-
canReallyWriteToAppDirectory
public static boolean canReallyWriteToAppDirectory() -
canWriteToDirectory
-
log
-
log
-
log
-
removeTrailingSeparators
-
getScriptCharsetEncoding
-
runFileOpWithTimeout
private static <T> T runFileOpWithTimeout(File file, FileUtil.FileOpWithTimeout<T> fo, T def, long strict_timeout) -
runFileOpWithTimeoutEx
private static <T> T runFileOpWithTimeoutEx(File file, FileUtil.FileOpWithTimeout<T> fo, IOException def_error, long strict_timeout) throws IOException - Throws:
IOException
-
runFileOpWithTimeoutEx
private static <T> T runFileOpWithTimeoutEx(File file, FileUtil.FileOpWithTimeout<T> fo, T def_result, IOException def_error, long strict_timeout) throws IOException - Throws:
IOException
-
lengthWithTimeout
-
lengthWithTimeout
-
canReadWithTimeout
-
canReadWithTimeout
-
isDirectoryWithTimeout
-
isDirectoryWithTimeout
-
existsWithTimeout
-
existsWithTimeout
-
existsWithTimeoutAndException
private static boolean existsWithTimeoutAndException(File file, long strict_timeout) throws IOException - Throws:
IOException
-
isResponding
-
getCanonicalPathWithTimeout
- Throws:
IOException
-
getCanonicalPathWithTimeout
- Throws:
IOException
-
listRootsWithTimeout
-
listRootsWithTimeout
-
newFile
-
newFile
-
newFile
-
newFileAccessor
public static FMFileAccess.FileAccessor newFileAccessor(File file, String access_mode, boolean explicit_sparse) throws FileNotFoundException - Throws:
FileNotFoundException
-
containsPathSegment
- Returns:
File.getAbsolutePath().contains(File.separator+ path +File.separator)
-
getRelativePath
-