Class FileUtils

java.lang.Object
net.sourceforge.jnlp.util.FileUtils

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

    • INVALID_PATH

      public static final List<Character> INVALID_PATH
      list of characters not allowed in filenames
    • INVALID_NAME

      public static final List<Character> INVALID_NAME
  • 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)
    • createRestrictedDirectory

      public static void createRestrictedDirectory(File directory) throws IOException
      Creates a new directory with minimum permissions. The directory is not readable or writable by anyone other than the owner. The parent directories are not created; they must exist before this is called.
      Parameters:
      directory - directory to be created
      Throws:
      IOException - if IO fails
    • createRestrictedFile

      public static void createRestrictedFile(File file, boolean writableByOwner) throws IOException
      Creates a new file with minimum permissions. The file is not readable or writable by anyone other than the owner. If writeableByOnwer is false, even the owner can not write to it.
      Parameters:
      file - path to file
      writableByOwner - true if can be writable by owner
      Throws:
      IOException - if IO fails
    • 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).
    • deleteWithErrMesg

      public static void deleteWithErrMesg(File f)
      Tries to delete file f. If the file exists but couldn't be deleted, print an error message to stderr with the file name.
      Parameters:
      f - the file to be deleted
    • testDirectoryPermissions

      public static DirectoryValidator.DirectoryCheckResults testDirectoryPermissions(File file)
      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
      Returns:
      a DirectoryValidator.DirectoryCheckResults object representing the results of the test
    • testFilePermissions

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

      public static void showReadOnlyDialog(Component frame)
      Show a dialog informing the user that the file is currently read-only
      Parameters:
      frame - a JFrame to act as parent to this dialog
    • showCouldNotOpenFilepathDialog

      public static void showCouldNotOpenFilepathDialog(Component frame, String filePath)
      Show a generic error dialog indicating the file could not be opened
      Parameters:
      frame - a JFrame to act as parent to this dialog
      filePath - a String representing the path to the file we failed to open
    • showCouldNotOpenFileDialog

      public static void showCouldNotOpenFileDialog(Component frame, String filePath, FileUtils.OpenFileResult reason)
      Show an error dialog indicating the file could not be opened, with a particular reason
      Parameters:
      frame - a JFrame to act as parent to this dialog
      filePath - a String representing the path to the file we failed to open
      reason - a FileUtils.OpenFileResult specifying more precisely why we failed to open the file
    • showCouldNotOpenDialog

      public static void showCouldNotOpenDialog(Component frame, String message)
      Show a dialog informing the user that the file could not be opened
      Parameters:
      frame - a JFrame to act as parent to this dialog
      message - a String giving the specific reason the file could not be opened
    • 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.
    • saveFile

      public static void saveFile(String content, File f) throws IOException
      Method to save String as file in UTF-8 encoding.
      Parameters:
      content - which will be saved as it is saved in this String
      f - file to be saved. No warnings provided
      Throws:
      IOException - if save fails
    • saveFile

      public static void saveFile(String content, File f, String encoding) throws IOException
      Method to save String as file in specified encoding/.
      Parameters:
      content - which will be saved as it is saved in this String
      f - file to be saved. No warnings provided
      encoding - of output byte representation
      Throws:
      IOException - if save fails
    • getContentOfStream

      public static String getContentOfStream(InputStream is, String encoding) throws IOException
      utility method which can read from any stream as one long String
      Parameters:
      is - stream
      encoding - the encoding to use to convert the bytes from the stream
      Returns:
      stream as string
      Throws:
      IOException - if connection can't be established or resource does not exist
    • getContentOfReader

      public static String getContentOfReader(Reader r) throws IOException
      Throws:
      IOException
    • getContentOfStream

      public static String getContentOfStream(InputStream is) throws IOException
      utility method which can read from any stream as one long String
      Parameters:
      is - stream
      Returns:
      stream as string
      Throws:
      IOException - if connection can't be established or resource does not exist
    • loadFileAsString

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

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

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