Class FileUtil

java.lang.Object
com.biglybt.core.util.FileUtil

public class FileUtil extends Object
File utility class.
  • Field Details

    • LOGID

      private static final LogIDs LOGID
    • DIR_SEP

      public static final String 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

      private static final List reserved_file_handles
    • class_mon

      private static final AEMonitor class_mon
    • char_conversion_mapping

      private static char[] char_conversion_mapping
    • fileHandling

      private static final FileHandler fileHandling
    • file_logger

      private static AEDiagnosticsLogger file_logger
    • tls

      private static final ThreadLocal<IdentityHashSet<CoreOperationTask>> tls
    • clax_cache

      static final Map<String,String> clax_cache
    • exists_dir_cache

      static final Map<String,Map<Object,Object>> exists_dir_cache
    • CACHE_TIME_KEY

      static final Object CACHE_TIME_KEY
    • exists_cache

      static final Map<String,Long> exists_cache
    • recycler

      private static AsyncDispatcher recycler
    • sce_checked

      private static boolean sce_checked
      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...
    • script_encoding

      private static String script_encoding
    • bad_roots

      private static Map<Path,int[]> bad_roots
    • fot_dispatcher

      private static AsyncDispatcher fot_dispatcher
    • last_roots

      private static volatile File[] last_roots
    • last_roots_time

      private static long last_roots_time
    • root_updater

      private static AsyncDispatcher root_updater
    • root_update_sem

      private static AESemaphore root_update_sem
  • Constructor Details

    • FileUtil

      public FileUtil()
  • Method Details

    • areFilePathsIdentical

      public static boolean areFilePathsIdentical(File f1, File f2)
    • reallyExists

      public static boolean reallyExists(File file)
      Verifies the case of the file.name
      Parameters:
      file -
      Returns:
    • getCanonicalFileSafe

      public static File getCanonicalFileSafe(File file)
      Preserves the case of the file.name when the file exists but differs in case
      Parameters:
      file -
      Returns:
    • getCanonicalPathSafe

      public static String getCanonicalPathSafe(File file)
      Preserves the case of the file.name when the file exists but differs in case
      Parameters:
      file -
      Returns:
    • isAncestorOf

      public static boolean isAncestorOf(File _parent, File _child)
    • getCanonicalFile

      public static File getCanonicalFile(File file)
    • getCanonicalPath

      public static String getCanonicalPath(File file, boolean lax) throws IOException
      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

      public static final boolean exists(File file)
    • existsWithCache

      public static final boolean existsWithCache(File parent, String name)
      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

      public static final void existsWithCacheClear(File parent)
    • existsWithCache

      public static boolean existsWithCache(File file)
      caches positive existence checks for a short time, therefore may answer incorrectly...
      Parameters:
      file -
      Returns:
    • getCanonicalFileName

      public static String getCanonicalFileName(String filename)
    • getUserFile

      public static File getUserFile(String filename)
    • getApplicationFile

      public static File getApplicationFile(String filename)
      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

      public static boolean recursiveDelete(File f)
      Deletes the given dir and all files/dirs underneath
    • recursiveDeleteNoCheck

      public static boolean recursiveDeleteNoCheck(File f)
    • getFileOrDirectorySize

      public static long getFileOrDirectorySize(File file)
    • recursiveEmptyDirDelete

      protected static void recursiveEmptyDirDelete(File f, Set<String> ignore_set, boolean log_warnings)
    • recursiveEmptyDirDelete

      private static void recursiveEmptyDirDelete(File f, Set<String> ignore_set, int level, boolean log_warnings)
    • convertOSSpecificChars

      public static String convertOSSpecificChars(String file_name_in, boolean is_folder)
    • writeResilientConfigFile

      public static void writeResilientConfigFile(String file_name, Map data)
    • writeResilientFile

      public static void writeResilientFile(File file, Map data)
    • writeResilientFileIncrementally

      public static void writeResilientFileIncrementally(File file, Map data)
    • writeResilientFileWithResult

      public static boolean writeResilientFileWithResult(File parent_dir, String file_name, Map data)
    • writeResilientFile

      public static void writeResilientFile(File parent_dir, String file_name, Map data, boolean use_backup)
    • writeResilientFile

      public static void writeResilientFile(File parent_dir, String file_name, Map data, boolean use_backup, boolean copy_to_backup)
    • writeResilientFile

      private static boolean writeResilientFile(File parent_dir, String file_name, Map data)
    • writeResilientFileIncrementally

      private static boolean writeResilientFileIncrementally(File parent_dir, String file_name, Map data)
    • resilientConfigFileExists

      public static boolean resilientConfigFileExists(String name)
    • readResilientConfigFile

      public static Map readResilientConfigFile(String file_name)
      Returns:
      Map read from config file, or empty HashMap if error
    • readResilientConfigFile

      public static Map readResilientConfigFile(String file_name, boolean use_backups)
      Returns:
      Map read from config file, or empty HashMap if error
    • readResilientFile

      public static Map readResilientFile(File file)
      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)
      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

      private static Map readResilientFileSupport(File parent_dir, String file_name, boolean attempt_recovery, boolean intern_keys)
    • readResilientFile

      private static Map readResilientFile(String original_file_name, File parent_dir, String file_name, int fail_count, boolean recovery_mode, boolean skip_key_intern)
    • deleteResilientFile

      public static void deleteResilientFile(File file)
    • deleteResilientConfigFile

      public static void deleteResilientConfigFile(String name)
    • getReservedFileHandles

      private static void getReservedFileHandles()
    • releaseReservedFileHandles

      private static void releaseReservedFileHandles()
    • isMyFileLock

      public static boolean isMyFileLock()
    • backupFile

      public static void backupFile(String _filename, boolean _make_copy)
      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

      public static void backupFile(File _file, boolean _make_copy)
      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

      public static boolean copyFileWithDates(File from_file, File to_file)
      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

      public static boolean copyFile(File _source, File _dest)
    • copyFileWithException

      public static void copyFileWithException(File _source, File _dest, FileUtil.ProgressListener pl) throws IOException
      Throws:
      IOException
    • copyFile

      public static boolean copyFile(File _source, OutputStream _dest, boolean closeInputStream)
    • copyFile

      public static void copyFile(InputStream _source, File _dest) throws IOException
      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

      public static void copyFile(InputStream is, OutputStream os) throws IOException
      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

      public static 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. 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

      public static File getJarFileFromClass(Class cla)
    • getJarFileFromURL

      public static File getJarFileFromURL(String url_str)
    • renameFile

      public static boolean renameFile(File from_file, File to_file)
    • renameFile

      public static boolean renameFile(File from_file, File to_file, FileUtil.ProgressListener pl)
    • renameFile

      private static String renameFile(File from_file, File to_file, boolean fail_on_existing_directory)
    • 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

      public static Object getFileStore(File file)
    • getFileStoreNames

      public static String[] getFileStoreNames(File... files)
    • reallyCopyFile

      private static String reallyCopyFile(File from_file, File to_file, FileUtil.ProgressListener pl)
    • newFileInputStream

      public static FileInputStream newFileInputStream(File from_file) throws FileNotFoundException
      Throws:
      FileNotFoundException
    • newFileOutputStream

      public static FileOutputStream newFileOutputStream(File file) throws FileNotFoundException
      Throws:
      FileNotFoundException
    • newFileOutputStream

      public static FileOutputStream newFileOutputStream(File file, boolean append) throws FileNotFoundException
      Throws:
      FileNotFoundException
    • writeStringAsFile

      public static boolean writeStringAsFile(File file, String text)
    • writeStringAsFile

      public static boolean writeStringAsFile(File file, String text, String charset)
    • writeBytesAsFile

      public static void writeBytesAsFile(String filename, byte[] file_data)
    • writeBytesAsFile2

      public static boolean writeBytesAsFile2(String filename, byte[] file_data)
    • deleteWithRecycle

      public static boolean deleteWithRecycle(File file, boolean force_no_recycle)
    • translateMoveFilePath

      public static String translateMoveFilePath(String old_root, String new_root, String file_to_move)
    • hasTask

      public static boolean hasTask(DownloadManager dm)
    • runAsTask

      public static void runAsTask(CoreOperationTask task)
    • runAsTask

      public static void runAsTask(int op_type, CoreOperationTask task)
    • mkdirs

      public static boolean mkdirs(File f)
      Makes Directories as long as the directory isn't directly in Volumes (OSX)
      Parameters:
      f -
      Returns:
    • getExtension

      public static String getExtension(String fName)
      Gets the extension of a file name, ensuring we don't go into the path
      Parameters:
      fName - File name
      Returns:
      extension, with the '.'
    • readFileAsString

      public static String readFileAsString(File file, int size_limit, String charset) throws IOException
      Throws:
      IOException
    • readFileAsString

      public static String readFileAsString(File file, int size_limit) throws IOException
      Throws:
      IOException
    • readGZippedFileAsString

      public static String readGZippedFileAsString(File file, int size_limit) throws IOException
      Throws:
      IOException
    • readInputStreamAsString

      public static String readInputStreamAsString(InputStream is, int size_limit) throws IOException
      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

      public static byte[] readInputStreamAsByteArray(InputStream is) throws IOException
      Throws:
      IOException
    • readInputStreamAsByteArray

      public static byte[] readInputStreamAsByteArray(InputStream is, int size_limit) throws IOException
      Throws:
      IOException
    • readFileAsByteArray

      public static byte[] readFileAsByteArray(File file) throws IOException
      Throws:
      IOException
    • getUsableSpace

      public static long getUsableSpace(File f)
    • canReallyWriteToAppDirectory

      public static boolean canReallyWriteToAppDirectory()
    • canWriteToDirectory

      public static boolean canWriteToDirectory(File dir)
    • log

      public static void log(String str)
    • log

      public static void log(String str, boolean addLocation)
    • log

      public static void log(String str, Throwable error)
    • removeTrailingSeparators

      public static String removeTrailingSeparators(String str)
    • getScriptCharsetEncoding

      public static String 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

      public static long lengthWithTimeout(File file)
    • lengthWithTimeout

      public static long lengthWithTimeout(File file, long strict_timeout)
    • canReadWithTimeout

      public static boolean canReadWithTimeout(File file)
    • canReadWithTimeout

      public static boolean canReadWithTimeout(File file, long strict_timeout)
    • isDirectoryWithTimeout

      public static boolean isDirectoryWithTimeout(File file)
    • isDirectoryWithTimeout

      public static boolean isDirectoryWithTimeout(File file, long strict_timeout)
    • existsWithTimeout

      public static boolean existsWithTimeout(File file)
    • existsWithTimeout

      public static boolean existsWithTimeout(File file, long strict_timeout)
    • existsWithTimeoutAndException

      private static boolean existsWithTimeoutAndException(File file, long strict_timeout) throws IOException
      Throws:
      IOException
    • isResponding

      public static boolean isResponding(File file, long strict_timeout)
    • getCanonicalPathWithTimeout

      public static String getCanonicalPathWithTimeout(File file) throws IOException
      Throws:
      IOException
    • getCanonicalPathWithTimeout

      public static String getCanonicalPathWithTimeout(File file, long strict_timeout) throws IOException
      Throws:
      IOException
    • listRootsWithTimeout

      public static File[] listRootsWithTimeout()
    • listRootsWithTimeout

      public static File[] listRootsWithTimeout(long timeout)
    • newFile

      public static File newFile(String parent, String... subDirs)
    • newFile

      public static File newFile(File parent_file, String... subDirs)
    • newFile

      public static File newFile(URI uri)
    • newFileAccessor

      public static FMFileAccess.FileAccessor newFileAccessor(File file, String access_mode) throws FileNotFoundException
      Throws:
      FileNotFoundException
    • containsPathSegment

      public static boolean containsPathSegment(File f, String path, boolean caseSensitive)
      Returns:
      File.getAbsolutePath().contains(File.separator + path + File.separator)
    • getRelativePath

      public static String getRelativePath(File parentDir, File file)
      Returns:
      path string relative to parentDir. null if file is not in parentDir. Empty String if file is parentDir.