Package com.biglybt.core.util
Class FileUtil
java.lang.Object
com.biglybt.core.util.FileUtil
File utility class.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic class
static class
(package private) static interface
static interface
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static char[]
private static final AEMonitor
static final String
private static AEDiagnosticsLogger
private static final FileHandler
private static boolean
private static AsyncDispatcher
private static boolean
private static File[]
private static long
private static final LogIDs
private static AsyncDispatcher
private static final int
private static final List
private static AESemaphore
private static AsyncDispatcher
private static boolean
Gets 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 String
private static final ThreadLocal<IdentityHashSet<CoreOperationTask>>
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic boolean
areFilePathsIdentical
(File f1, File f2) static void
backupFile
(File _file, boolean _make_copy) Backup the given file to filename.bak, removing the old .bak file if necessary.static void
backupFile
(String _filename, boolean _make_copy) Backup the given file to filename.bak, removing the old .bak file if necessary.static File
static boolean
canReadWithTimeout
(File file) static boolean
canReadWithTimeout
(File file, long strict_timeout) static boolean
static boolean
canWriteToDirectory
(File dir) static boolean
containsPathSegment
(File f, String path, boolean caseSensitive) static String
convertOSSpecificChars
(String file_name_in, boolean is_folder) static boolean
static boolean
copyFile
(File _source, OutputStream _dest, boolean closeInputStream) static void
copyFile
(InputStream _source, File _dest) copys the input stream to the file.static void
copyFile
(InputStream _source, File _dest, boolean _close_input_stream) static void
copyFile
(InputStream is, OutputStream os) static void
copyFile
(InputStream is, OutputStream os, boolean closeInputStream) static void
copyFile
(InputStream is, OutputStream os, boolean closeInputStream, FileUtil.ProgressListener pl) static void
copyFile
(InputStream is, OutputStream os, FileUtil.ProgressListener pl) static void
copyFileOrDirectory
(File from_file_or_dir, File to_parent_dir) static boolean
copyFileWithDates
(File from_file, File to_file) Copy the given source file to the given destination file.static void
copyFileWithException
(File _source, File _dest, FileUtil.ProgressListener pl) static void
static void
deleteResilientFile
(File file) static boolean
deleteWithRecycle
(File file, boolean force_no_recycle) static boolean
existsWithTimeout
(File file) static boolean
existsWithTimeout
(File file, long strict_timeout) static File
getApplicationFile
(String filename) Get a file relative to this program's install directory.static String
getCanonicalFileName
(String filename) static File
getCanonicalFileSafe
(File file) Preserves the case of the file.name when the file exists but differs in casestatic String
getCanonicalPathSafe
(File file) Preserves the case of the file.name when the file exists but differs in casestatic String
static String
getCanonicalPathWithTimeout
(File file, long strict_timeout) static String
getExtension
(String fName) Gets the extension of a file name, ensuring we don't go into the pathstatic File
getFileOrBackup
(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 long
getFileOrDirectorySize
(File file) static Object
getFileStore
(File file) static String[]
getFileStoreNames
(File... files) static File
getJarFileFromClass
(Class cla) static File
getJarFileFromURL
(String url_str) static String
getRelativePath
(File parentDir, File file) private static void
static String
static long
static File
getUserFile
(String filename) static boolean
static boolean
isAncestorOf
(File _parent, File _child) static boolean
isDirectoryWithTimeout
(File file) static boolean
isDirectoryWithTimeout
(File file, long strict_timeout) static boolean
static long
lengthWithTimeout
(File file) static long
lengthWithTimeout
(File file, long strict_timeout) static File[]
static File[]
listRootsWithTimeout
(long timeout) static void
static void
static void
static boolean
Makes Directories as long as the directory isn't directly in Volumes (OSX)static File
static File
static File
static FMFileAccess.FileAccessor
newFileAccessor
(File file, String access_mode) static FileInputStream
newFileInputStream
(File from_file) static FileOutputStream
newFileOutputStream
(File file) static FileOutputStream
newFileOutputStream
(File file, boolean append) static byte[]
readFileAsByteArray
(File file) static String
readFileAsString
(File file, int size_limit) static String
readFileAsString
(File file, int size_limit, String charset) static String
readFileEndAsString
(File file, int size_limit, String charset) static String
readGZippedFileAsString
(File file, int size_limit) static byte[]
static byte[]
readInputStreamAsByteArray
(InputStream is, int size_limit) static String
readInputStreamAsString
(InputStream is, int size_limit) static String
readInputStreamAsString
(InputStream is, int size_limit, int timeoutMillis, String charSet) static String
readInputStreamAsString
(InputStream is, int size_limit, String charSet) static String
readInputStreamAsStringWithTruncation
(InputStream is, int size_limit) static Map
readResilientConfigFile
(String file_name) static Map
readResilientConfigFile
(String file_name, boolean use_backups) static Map
readResilientFile
(File file) static Map
readResilientFile
(File parent_dir, String file_name, boolean use_backup) static Map
readResilientFile
(File parent_dir, String file_name, boolean use_backup, boolean intern_keys) private static Map
readResilientFile
(String original_file_name, File parent_dir, String file_name, int fail_count, boolean recovery_mode, boolean skip_key_intern) private static Map
readResilientFileSupport
(File parent_dir, String file_name, boolean attempt_recovery, boolean intern_keys) private static String
reallyCopyFile
(File from_file, File to_file, FileUtil.ProgressListener pl) static boolean
reallyExists
(File file) Verifies the case of the file.namestatic boolean
Deletes the given dir and all files/dirs underneathstatic boolean
protected static void
recursiveEmptyDirDelete
(File f, Set ignore_set, boolean log_warnings) private static void
static String
static boolean
renameFile
(File from_file, File to_file) private static String
renameFile
(File from_file, File to_file, boolean fail_on_existing_directory) static String
renameFile
(File from_file, File to_file, boolean fail_on_existing_directory, FileFilter file_filter, FileUtil.ProgressListener pl) static boolean
renameFile
(File from_file, File to_file, FileUtil.ProgressListener pl) private static String
renameFileSupport
(File from_file, File to_file, boolean fail_on_existing_directory, FileFilter file_filter, FileUtil.ProgressListener pl) static boolean
static void
runAsTask
(int op_type, CoreOperationTask task) static void
runAsTask
(CoreOperationTask task) private static <T> T
runFileOpWithTimeout
(File file, FileUtil.FileOpWithTimeout<T> fo, T def, long strict_timeout) private static <T> T
runFileOpWithTimeoutEx
(File file, FileUtil.FileOpWithTimeout<T> fo, IOException def_error, long strict_timeout) private static <T> T
runFileOpWithTimeoutEx
(File file, FileUtil.FileOpWithTimeout<T> fo, T def_result, IOException def_error, long strict_timeout) static String
translateMoveFilePath
(String old_root, String new_root, String file_to_move) static void
writeBytesAsFile
(String filename, byte[] file_data) static boolean
writeBytesAsFile2
(String filename, byte[] file_data) static void
writeResilientConfigFile
(String file_name, Map data) private static boolean
writeResilientFile
(File parent_dir, String file_name, Map data) static void
writeResilientFile
(File parent_dir, String file_name, Map data, boolean use_backup) static void
writeResilientFile
(File parent_dir, String file_name, Map data, boolean use_backup, boolean copy_to_backup) static void
writeResilientFile
(File file, Map data) private static boolean
writeResilientFileIncrementally
(File parent_dir, String file_name, Map data) static void
writeResilientFileIncrementally
(File file, Map data) static boolean
writeResilientFileWithResult
(File parent_dir, String file_name, Map data) static boolean
writeStringAsFile
(File file, String text) static boolean
writeStringAsFile
(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
-
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
Preserves the case of the file.name when the file exists but differs in case- Parameters:
file
-- Returns:
-
getCanonicalPathSafe
Preserves the case of the file.name when the file exists but differs in case- Parameters:
file
-- Returns:
-
isAncestorOf
-
canonise
-
getCanonicalFileName
-
getUserFile
-
getApplicationFile
Get a file relative to this program's install directory.On Windows, this is usually %Program Files%\[AppName]\[filename]
On *nix, this is usually the [Launch Dir]/[filename]
On Mac, this is usually "/Applications/.[AppName]/[filename]" -
recursiveDelete
Deletes the given dir and all files/dirs underneath -
recursiveDeleteNoCheck
-
getFileOrDirectorySize
-
recursiveEmptyDirDelete
-
convertOSSpecificChars
-
writeResilientConfigFile
-
writeResilientFile
-
writeResilientFileIncrementally
-
writeResilientFileWithResult
-
writeResilientFile
-
writeResilientFile
-
writeResilientFile
-
writeResilientFileIncrementally
-
resilientConfigFileExists
-
readResilientConfigFile
- Returns:
- Map read from config file, or empty HashMap if error
-
readResilientConfigFile
- Returns:
- Map read from config file, or empty HashMap if error
-
readResilientFile
- Returns:
- Map read from config file, or empty HashMap if error
-
readResilientFile
- Returns:
- Map read from config file, or empty HashMap if error
-
readResilientFile
public static Map readResilientFile(File parent_dir, String file_name, boolean use_backup, boolean intern_keys) - Parameters:
parent_dir
-file_name
-use_backup
-intern_keys
-- Returns:
- Map read from config file, or empty HashMap if error
-
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
-
copyFileWithDates
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
-
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) -
getFileStore
-
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
Gets the extension of a file name, ensuring we don't go into the path- Parameters:
fName
- File name- Returns:
- extension, with the '.'
-
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
-
getCanonicalPathWithTimeout
- Throws:
IOException
-
getCanonicalPathWithTimeout
- Throws:
IOException
-
listRootsWithTimeout
-
listRootsWithTimeout
-
newFile
-
newFile
-
newFile
-
newFileAccessor
public static FMFileAccess.FileAccessor newFileAccessor(File file, String access_mode) throws FileNotFoundException - Throws:
FileNotFoundException
-
containsPathSegment
- Returns:
File.getAbsolutePath()
.contains(File.separator
+ path +File.separator
)
-
getRelativePath
- Returns:
- path string relative to
parentDir
.null
if file is not in parentDir. Empty String if file is parentDir.
-