Class FileUtils

java.lang.Object
net.adoptopenjdk.icedteaweb.io.FileUtils

public final class FileUtils extends Object
This class contains a few file-related utility functions.
  • Field Details

  • Constructor Details

    • FileUtils

      public FileUtils()
  • Method Details

    • sanitizePath

      public static String sanitizePath(String path)
      Clean up a string by removing characters that can't appear in a local file name.
      Parameters:
      path - the path to sanitize
      Returns:
      a sanitized version of the input which is suitable for using as a file path
    • sanitizePath

      public static String sanitizePath(String path, char substitute)
    • sanitizeFileName

      public static String sanitizeFileName(String filename)
      Given an input, return a sanitized form of the input suitable for use as a file/directory name
      Parameters:
      filename - the filename to sanitize.
      Returns:
      a sanitized version of the input
    • sanitizeFileName

      public static String sanitizeFileName(String filename, char substitute)
    • createParentDir

      public static void createParentDir(File f, String eMsg) throws IOException
      Tries to create the ancestor directories of file f. Throws an IOException if it can't be created (but not if it was already there).
      Parameters:
      f - file to provide parent directory
      eMsg - - the message to use for the exception. null if the file name is to be used.
      Throws:
      IOException - if the directory can't be created and doesn't exist.
    • createParentDir

      public static void createParentDir(File f) throws IOException
      Tries to create the ancestor directories of file f. Throws an IOException if it can't be created (but not if it was already there).
      Parameters:
      f - file which parent will be created
      Throws:
      IOException - if the directory can't be created and doesn't exist.
    • deleteWithErrMesg

      public static void deleteWithErrMesg(File f, String eMsg)
      Tries to delete file f. If the file exists but couldn't be deleted, print an error message to stderr with the file name, or eMsg if eMsg is not null.
      Parameters:
      f - the file to be deleted
      eMsg - the message to print on failure (or null to print the the file name).
    • testDirectoryPermissions

      private static DirectoryCheckResults testDirectoryPermissions(File file, boolean isDebug)
      Ensure that the parent directory of the file exists and that we are able to create and access files within this directory
      Parameters:
      file - the File representing a Java Policy file to test
      isDebug - output debug information
      Returns:
      a DirectoryCheckResults object representing the results of the test
    • testFilePermissions

      public static FileUtils.OpenFileResult testFilePermissions(File file, boolean isDebug)
      Verify that a given file object points to a real, accessible plain file.
      Parameters:
      file - the File to verify
      isDebug - output debug information
      Returns:
      an FileUtils.OpenFileResult representing the accessibility level of the file
    • displayablePath

      public static String displayablePath(String path)
      Returns a String that is suitable for using in GUI elements for displaying (long) paths to users.
      Parameters:
      path - a path that should be shortened
      Returns:
      a shortened path suitable for displaying to the user
    • displayablePath

      public static String displayablePath(String path, int visibleChars)
      Return a String that is suitable for using in GUI elements for displaying paths to users. If the path is longer than visibleChars, it is truncated in a display-friendly way
      Parameters:
      path - a path that should be shorted
      visibleChars - the maximum number of characters that path should fit into. Also the length of the returned string
      Returns:
      a shortened path that contains limited number of chars
    • recursiveDelete

      public static void recursiveDelete(File file, File base) throws IOException
      Recursively delete everything under a directory. Works on either files or directories
      Parameters:
      file - the file object representing what to delete. Can be either a file or a directory.
      base - the directory under which the file and its subdirectories must be located
      Throws:
      IOException - on an io exception or if trying to delete something outside the base
    • getFileLock

      public static FileLock getFileLock(String path, boolean shared, boolean allowBlock) throws FileNotFoundException
      This will return a lock to the file specified.
      Parameters:
      path - File path to file we want to lock.
      shared - Specify if the lock will be a shared lock.
      allowBlock - Specify if we should block when we can not get the lock. Getting a shared lock will always block.
      Returns:
      FileLock if we were successful in getting a lock, otherwise null.
      Throws:
      FileNotFoundException - If the file does not exist.
    • loadFileAsUtf8String

      public static String loadFileAsUtf8String(File f) throws IOException
      Throws:
      IOException
    • loadFileAsString

      public static String loadFileAsString(File f, Charset encoding) throws IOException
      Throws:
      IOException
    • saveFileUtf8

      public static void saveFileUtf8(String content, File f) throws IOException
      Save String into a file in UTF-8 encoding.
      Parameters:
      content - which will be saved to the file
      f - file to be saved. No warnings provided
      Throws:
      IOException - if save fails
    • getFileMD5Sum

      public static byte[] getFileMD5Sum(File file) throws NoSuchAlgorithmException, IOException
      Throws:
      NoSuchAlgorithmException
      IOException