Class FileUtils

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

public 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
  • Constructor Details

    • FileUtils

      public FileUtils()
  • Method Details

    • getParentFile

      public static File getParentFile(File f)
      The method, java.io.File.getParentFile() does not necessarily do what you would think it does. What it really does is to simply chop off the final element in the path and return what is left-over. E.g. if the file is /foo/. then the "parent" that is returned is /foo which is probably not what you expected. This method really returns the parent directory - or null if there is none.
      Parameters:
      f -
      Returns:
    • 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:
    • 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:
    • listFiles

      public static File[] listFiles(File f)
    • 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(String s)
    • safeIsRealDirectory

      public static boolean safeIsRealDirectory(File f)
    • safeIsDirectory

      public static boolean safeIsDirectory(String s)
    • safeGetCanonicalPath

      public static String safeGetCanonicalPath(File f)
    • safeGetCanonicalFile

      public static File safeGetCanonicalFile(File f)
    • hasExtension

      public static boolean hasExtension(String filename, String ext)
    • hasExtension

      public static boolean hasExtension(File f, String ext)
    • hasExtensionIgnoreCase

      public static boolean hasExtensionIgnoreCase(String filename, String ext)
    • hasExtensionIgnoreCase

      public static boolean hasExtensionIgnoreCase(File f, String ext)
    • 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)
    • makeFriendlyFilenameNoExtension

      public static String makeFriendlyFilenameNoExtension(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(String filename)
    • isZip

      public static boolean isZip(String filename)
    • 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

      public static void deleteFileNowOrLater(File f)
      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
    • getAllFilesUnder

      public static Set<File> getAllFilesUnder(File directory, FilenameFilter filenameFilter) throws IOException
      Return a set of all the files (File objects) under the directory specified, with relative pathnames filtered with the filename filter (can be null for all files).
      Throws:
      IOException
    • getAllFilesUnder

      public static Set<File> getAllFilesUnder(File directory, FilenameFilter filenameFilter, boolean relativize) throws IOException
      Throws:
      IOException
    • getAllFilesAndDirectoriesUnder

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

      public static File relativize(File parent, File child)
      Given a directory and a fully-qualified file somewhere under that directory, return the portion of the child that is relative to the parent.
    • formatFileCollection

      public static String formatFileCollection(Collection<File> files)
      Creates a String listing the absolute paths of files, separated by the platform's line separator.
      Parameters:
      files - the Collection of File objects to be listed
      Returns:
      String containing the absolute paths of the files with the line separator between them
    • getDirectory

      public static File getDirectory(File f)
    • createTempFile

      public static File createTempFile(File directory)
    • listAllFiles

      public static File[] listAllFiles(File dirName, String ext)
      Returns an array of abstract pathnames that matches with the given file extension. If the given abstract pathname does not denote a directory, then this method returns null. If there is no matching file under the given directory and its sub directories, it returns null;
      Parameters:
      dirName - dir name under which search will begin
      ext - file extension to look for
      Returns:
      an array of abstract pathnames that matches with the extension
    • searchDir

      public static List<File> searchDir(File dirName, String ext)
      Returns a list of abstract pathnames that matches with the given file extension. If the given abstract pathname does not denote a directory, then this method returns null. If there is no matching file under the given directory and its sub directories, it returns an empty list.
      Parameters:
      dirName - dir name under which search will begin
      ext - file extension to look for
      Returns:
      a list of abstract pathnames of type java.io.File that matches with the given extension
    • copy

      public static void copy(String from, String to) throws IOException
      Copies a file.
      Parameters:
      from - Name of file to copy
      to - Name of new file
      Throws:
      IOException - if an error while copying the content
    • 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
    • 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
    • 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
    • getIllegalFilenameCharacters

      public static String getIllegalFilenameCharacters()
    • copyFile

      public static void copyFile(File fin, File fout) throws IOException
      This method is used to copy a given file to another file using the buffer sixe specified
      Parameters:
      fin - the source file
      fout - the destination file
      Throws:
      IOException
    • copy

      public static void copy(InputStream in, FileOutputStream out, long size) throws IOException
      Throws:
      IOException
    • copyWithoutClose

      public static void copyWithoutClose(InputStream in, FileOutputStream out, long size) throws IOException
      Throws:
      IOException
    • copy

      public static void copy(InputStream in, OutputStream os, long size) throws IOException
      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
    • appendText

      public static void appendText(String fileName, String line) throws RuntimeException
      Appends the given line at the end of given text file. If the given file does not exist, an attempt is made to create it. Note that this method can handle only text files.
      Parameters:
      fileName - name of the text file that needs to be appended to
      line - the line to append to
      Throws:
      RuntimeException - in case of any error - that makes it callable from a code not within try-catch. Note that NPE will be thrown if either argument is null. Note that this method is not tested with String containing characters with 2 bytes.
    • appendText

      public static void appendText(String fileName, StringBuffer buffer) throws IOException, FileNotFoundException
      Throws:
      IOException
      FileNotFoundException
    • readSmallFile

      public static String readSmallFile(String fileName) throws IOException, FileNotFoundException
      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:
      fileName - String representing absolute path of the file
      Returns:
      String representing the contents of the file, empty String for an empty file
      Throws:
      IOException - if there is an i/o error.
      FileNotFoundException - if the file could not be found
    • readSmallFile

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

      public static File getManagedFile(String file, File dir) throws IOException
      If the path dir/file does not exist, look for it in the classpath. If found in classpath, create dir/file.
      Parameters:
      file - - path to look for
      dir - - directory where the path file should exist
      Returns:
      the File representing dir/file. If that does not exist, return null.
      Throws:
      IOException
    • 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
    • resourceToString

      public static String resourceToString(String resourceName)
      Read in the given resourceName as a resource, and convert to a String
      Parameters:
      resourceName -
      Returns:
      the contents of the resource as a String or null if absent
    • resourceToBytes

      public static byte[] resourceToBytes(String resourceName)
      Read in the given resourceName as a resource, and convert to a byte array
      Parameters:
      resourceName -
      Returns:
      the contents of the resource as a byte array or null if absent