Class FileUtils

java.lang.Object
com.sun.enterprise.util.io.FileUtils

public final class FileUtils extends Object
  • Field Details

    • USER_HOME

      public static final File USER_HOME
      Current user's home directory resolved from the system property user.home
  • Method Details

    • ensureWritableDir

      public static void ensureWritableDir(File dir)
      Result: existing writable directory given in parameter OR IllegalStateException.
      Parameters:
      dir -
      Throws:
      IllegalStateException - The exception should not be catched, because it means that the client can have unpredictable issues.
    • mkdirsMaybe

      public static boolean mkdirsMaybe(File f)
      Wrapper for File.mkdirs This version will return true if the directory exists when the method returns. Unlike File.mkdirs which returns false if the directory already exists.
      Parameters:
      f - The file pointing to the directory to be created
      Returns:
      true if the directory exists or was created by this method.
    • deleteFileMaybe

      public static boolean deleteFileMaybe(File f)
      Wrapper for File.delete This version will return true if the file does not exist when the method returns. Unlike File.delete which returns false if the file does not exist.
      Parameters:
      f - The file to be deleted
      Returns:
      true if the directory does not exist or was deleted by this method.
    • listFiles

      public static File[] listFiles(File f)
      Wrapper for File.listFiles Guaranteed to return an array in all cases. File.listFiles() returns either null or an empty array. This is annoying and results in harder than neccessry to read code -- i.e. there are 3 results possible:
      • an array with files in it
      • an empty array
      • a null
    • listFiles

      public static File[] listFiles(File f, FileFilter ff)
    • listFiles

      public static File[] listFiles(File f, FilenameFilter fnf)
    • safeIsDirectory

      public static boolean safeIsDirectory(File f)
    • safeIsRealDirectory

      public static boolean safeIsRealDirectory(File f)
    • safeGetCanonicalPath

      public static String safeGetCanonicalPath(File f)
    • safeGetCanonicalFile

      public static File safeGetCanonicalFile(File f)
    • hasExtension

      public static boolean hasExtension(File f, String ext)
      Parameters:
      f -
      ext -
      Returns:
      true if the file exists and it's name ends with ext
    • hasExtensionIgnoreCase

      public static boolean hasExtensionIgnoreCase(File f, String ext)
      Parameters:
      f -
      ext -
      Returns:
      true if the file exists and it's name ends with ext (ignoring case)
    • getExtension

      public static String getExtension(File file)
      Gets the extension of the file.

      This method returns the extension of the file with the leading dot.

      Parameters:
      file - the file
      Returns:
      the file extension
    • removeExtension

      public static String removeExtension(File file)
      Removes the extension from a file name for the file.

      This method returns the textual part of the file name before last dot.

      Parameters:
      file - the file
      Returns:
      the file name without extension or null if file is null
    • isLegalFilename

      public static boolean isLegalFilename(String filename)
    • isFriendlyFilename

      public static boolean isFriendlyFilename(String filename)
    • makeLegalFilename

      public static String makeLegalFilename(String filename)
    • makeLegalNoBlankFileName

      public static String makeLegalNoBlankFileName(String filename)
    • makeFriendlyFilename

      public static String makeFriendlyFilename(String filename)
    • makeFriendlyFilenameExtension

      public static String makeFriendlyFilenameExtension(String filename)
    • revertFriendlyFilenameExtension

      public static String revertFriendlyFilenameExtension(String filename)
    • revertFriendlyFilename

      public static String revertFriendlyFilename(String filename)
    • liquidate

      public static void liquidate(File parent)
    • isJar

      public static boolean isJar(File f)
    • isZip

      public static boolean isZip(File f)
    • whack

      public static boolean whack(File parent)
      Deletes a directory and its contents.

      If this method encounters a symbolic link in the subtree below "parent" then it deletes the link but not any of the files pointed to by the link. Note that whack will delete files if a symbolic link appears in the path above the specified parent directory in the path.

      Parameters:
      parent - the File at the top of the subtree to delete
      Returns:
      success or failure of deleting the directory
    • whack

      public static boolean whack(File parent, Collection<File> undeletedFiles)
      Deletes a directory and its contents.

      If this method encounters a symbolic link in the subtree below "parent" then it deletes the link but not any of the files pointed to by the link. Note that whack will delete files if a symbolic link appears in the path above the specified parent directory in the path.

      Parameters:
      parent - the File at the top of the subtree to delete
      Returns:
      success or failure of deleting the directory
    • deleteFileNowOrLater

      @Deprecated public static void deleteFileNowOrLater(File f)
      Deprecated.
      Usually points to an IO leak
      Delete a file. If impossible to delete then try to delete it when the JVM exits. E.g. when Windows is using a jar in the current JVM -- you can not delete the jar until the JVM dies.
      Parameters:
      f - file to delete
    • deleteFileWithWaitLoop

      public static boolean deleteFileWithWaitLoop(File f)
      Delete a file. Will retry every ten milliseconds for five seconds, doing a gc after each second.
      Parameters:
      f - file to delete
      Returns:
      boolean indicating success or failure of the deletion atttempt; returns true if file is absent
    • deleteFile

      public static boolean deleteFile(File f)
      Delete a file. If on Windows and the delete fails, run the gc and retry the deletion.
      Parameters:
      f - file to delete
      Returns:
      boolean indicating success or failure of the deletion atttempt; returns true if file is absent
    • openFileOutputStream

      public static FileOutputStream openFileOutputStream(File out) throws IOException
      Opens a stream to the specified output file, retrying if necessary.
      Parameters:
      out - the output File for which a stream is needed
      Returns:
      the FileOutputStream
      Throws:
      IOException - for any errors opening the stream
    • getAllFilesAndDirectoriesUnder

      public static Set<File> getAllFilesAndDirectoriesUnder(File directory) throws IOException
      Throws:
      IOException
    • copyTree

      public static void copyTree(File din, File dout) throws IOException
      Copies the entire tree to a new location.
      Parameters:
      din - File pointing at root of tree to copy
      dout - File pointing at root of new tree
      Throws:
      IOException - if an error while copying the content
    • copyResourceToDirectory

      public static File copyResourceToDirectory(String resourcePath, File outputDirectory) throws IOException
      Throws:
      IOException
    • copyResource

      public static File copyResource(String resourcePath, File outputFile) throws IOException
      If the path dir/file does not exist, look for it in the classpath. If found in classpath, create dir/file.

      Existing file will not be overwritten.

      Parameters:
      resourcePath - - resource loadable by the thread context classloader.
      outputFile - - if the file exists, it will be overwritten
      Returns:
      the File representing dir/file. If the resource does not exist, return null.
      Throws:
      IOException
    • copy

      public static void copy(File fin, File fout) throws IOException
      Copies a file.
      Parameters:
      fin - File to copy
      fout - New file
      Throws:
      IOException - if an error while copying the content
    • makeForwardSlashes

      public static String makeForwardSlashes(String inputStr)
      Returns a String with uniform slashes such that all the occurances of '\\' are replaced with '/'. In other words, the returned string will have all forward slashes. Accepts non-null strings only.
      Parameters:
      inputStr - non null String
      Returns:
      a String which does not contain `\\` character
    • quoteString

      public static String quoteString(String s)
      Given a string (typically a path), quote the string such that spaces are protected from interpretation by a Unix or Windows command shell. Note that this method does not handle quoting for all styles of special characters. Just for the basic case of strings with spaces.
      Parameters:
      s - input string
      Returns:
      a String which is quoted to protect spaces
    • copy

      public static void copy(File in, OutputStream out) throws IOException
      This method should be used instead of copy(InputStream, File, long) if you don't know the size of the input stream.
      Parameters:
      in - It will NOT be closed after processing. That is caller's responsibility.
      out - Target output file. If the file already exists, it will be overwritten!
      Throws:
      IOException
    • copy

      public static void copy(InputStream in, File out, long byteCount) throws IOException, IllegalArgumentException
      Fast method using NIO to copy data from the input to the output file, when you already do know the size of the input.

      WARNING: Don't use it when you don't know the byteCount value.

      Parameters:
      in - It will be closed after processing.
      out - Target output file.
      byteCount - count of bytes to be transferred.
      Throws:
      IOException - if the operation failed.
      IllegalArgumentException - if the byte count is less then 0 or equal to Long.MAX_VALUE (obvious hacks)
    • copy

      public static void copy(InputStream in, File out) throws IOException
      This method should be used instead of copy(InputStream, File, long) if you don't know the size of the input stream.
      Parameters:
      in - It will NOT be closed after processing. That is caller's responsibility.
      out - Target output file. If the file already exists, it will be overwritten!
      Throws:
      IOException
    • copy

      public static void copy(InputStream in, OutputStream os) throws IOException
      Copies stream with internal 8K buffer.
      Parameters:
      in - It is NOT closed after processing, caller is responsible for that.
      os - It is NOT closed after processing, caller is responsible for that.
      Throws:
      IOException
    • renameFile

      public static boolean renameFile(File fromFile, File toFile)
      Rename, running gc on Windows if needed to try to force open streams to close.
      Parameters:
      fromFile - to be renamed
      toFile - name for the renamed file
      Returns:
      boolean result of the rename attempt
    • readSmallFile

      public static String readSmallFile(File file) throws IOException
      A utility routine to read a text file efficiently and return the contents as a String. Sometimes while reading log files of spawned processes this kind of facility is handy. Instead of opening files, coding FileReaders etc. this method could be employed. It is expected that the file to be read is small .
      Parameters:
      file - Absolute path of the file
      Returns:
      String representing the contents of the file. Lines are separated by System.lineSeparator().
      Throws:
      IOException - if there is an i/o error.
      FileNotFoundException - if the file could not be found
    • writeStringToFile

      public static void writeStringToFile(String s, File f) throws IOException
      Write the String to a file. Then make the file readable and writable. If the file already exists it will be truncated and the contents replaced with the String argument.
      Parameters:
      s - The String to write to the file
      f - The file to write the String to
      Throws:
      IOException - if any errors
    • findFilesInDir

      public static File[] findFilesInDir(File dir, String regexp)
      Find files matching the regular expression in the given directory
      Parameters:
      dir - the directory to search
      regexp - the regular expression pattern
      Returns:
      either an array of matching File objects or an empty array. Guaranteed to never return null