Package com.sun.enterprise.util.io
Class FileUtils
java.lang.Object
com.sun.enterprise.util.io.FileUtils
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionstatic voidCopies a file.static voidcopy(File in, OutputStream out) This method should be used instead ofcopy(InputStream, File, long)if you don't know the size of the input stream.static voidcopy(InputStream in, File out) This method should be used instead ofcopy(InputStream, File, long)if you don't know the size of the input stream.static voidcopy(InputStream in, File out, long byteCount) Fast method using NIO to copy data from the input to the output file, when you already do know the size of the input.static voidcopy(InputStream in, OutputStream os) Copies stream with internal 8K buffer.static FilecopyResource(String resourcePath, File outputFile) If the path dir/file does not exist, look for it in the classpath.static FilecopyResourceToDirectory(String resourcePath, File outputDirectory) static voidCopies the entire tree to a new location.static booleandeleteFile(File f) Delete a file.static booleanWrapper for File.delete This version will return true if the file does not exist when the method returns.static voidDeprecated.Usually points to an IO leakstatic booleanDelete a file.static voidensureWritableDir(File dir) Result: existing writable directory given in parameter ORIllegalStateException.static File[]findFilesInDir(File dir, String regexp) Find files matching the regular expression in the given directorygetAllFilesAndDirectoriesUnder(File directory) static StringgetExtension(File file) Gets the extension of thefile.static booleanhasExtension(File f, String ext) static booleanhasExtensionIgnoreCase(File f, String ext) static booleanisFriendlyFilename(String filename) static booleanstatic booleanisLegalFilename(String filename) static booleanstatic voidstatic File[]Wrapper for File.listFiles Guaranteed to return an array in all cases.static File[]listFiles(File f, FileFilter ff) static File[]listFiles(File f, FilenameFilter fnf) static StringmakeForwardSlashes(String inputStr) Returns a String with uniform slashes such that all the occurances of '\\' are replaced with '/'.static StringmakeFriendlyFilename(String filename) static StringmakeFriendlyFilenameExtension(String filename) static StringmakeLegalFilename(String filename) static StringmakeLegalNoBlankFileName(String filename) static booleanmkdirsMaybe(File f) Wrapper for File.mkdirs This version will return true if the directory exists when the method returns.static FileOutputStreamopenFileOutputStream(File out) Opens a stream to the specified output file, retrying if necessary.static StringGiven a string (typically a path), quote the string such that spaces are protected from interpretation by a Unix or Windows command shell.static StringreadSmallFile(File file) A utility routine to read a text file efficiently and return the contents as a String.static StringremoveExtension(File file) Removes the extension from a file name for thefile.static booleanrenameFile(File fromFile, File toFile) Rename, running gc on Windows if needed to try to force open streams to close.static StringrevertFriendlyFilename(String filename) static StringrevertFriendlyFilenameExtension(String filename) static Filestatic Stringstatic booleanstatic booleanstatic booleanDeletes a directory and its contents.static booleanwhack(File parent, Collection<File> undeletedFiles) Deletes a directory and its contents.static voidwriteStringToFile(String s, File f) Write the String to a file.
-
Field Details
-
USER_HOME
Current user's home directory resolved from the system property user.home
-
-
Method Details
-
ensureWritableDir
Result: existing writable directory given in parameter ORIllegalStateException.- Parameters:
dir-- Throws:
IllegalStateException- The exception should not be catched, because it means that the client can have unpredictable issues.
-
mkdirsMaybe
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
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
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
-
listFiles
-
safeIsDirectory
-
safeIsRealDirectory
-
safeGetCanonicalPath
-
safeGetCanonicalFile
-
hasExtension
- Parameters:
f-ext-- Returns:
- true if the file exists and it's name ends with ext
-
hasExtensionIgnoreCase
- Parameters:
f-ext-- Returns:
- true if the file exists and it's name ends with ext (ignoring case)
-
getExtension
Gets the extension of thefile.This method returns the extension of the
filewith the leading dot.- Parameters:
file- the file- Returns:
- the file extension
-
removeExtension
Removes the extension from a file name for thefile.This method returns the textual part of the file name before last dot.
- Parameters:
file- the file- Returns:
- the file name without extension or
nulliffileisnull
-
isLegalFilename
-
isFriendlyFilename
-
makeLegalFilename
-
makeLegalNoBlankFileName
-
makeFriendlyFilename
-
makeFriendlyFilenameExtension
-
revertFriendlyFilenameExtension
-
revertFriendlyFilename
-
liquidate
-
isJar
-
isZip
-
whack
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
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.Usually points to an IO leakDelete 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
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
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
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
- Throws:
IOException
-
copyTree
Copies the entire tree to a new location.- Parameters:
din- File pointing at root of tree to copydout- 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
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
Copies a file.- Parameters:
fin- File to copyfout- New file- Throws:
IOException- if an error while copying the content
-
makeForwardSlashes
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
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
This method should be used instead ofcopy(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 toLong.MAX_VALUE(obvious hacks)
-
copy
This method should be used instead ofcopy(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
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
Rename, running gc on Windows if needed to try to force open streams to close.- Parameters:
fromFile- to be renamedtoFile- name for the renamed file- Returns:
- boolean result of the rename attempt
-
readSmallFile
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 issmall.- 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
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 filef- The file to write the String to- Throws:
IOException- if any errors
-
findFilesInDir
Find files matching the regular expression in the given directory- Parameters:
dir- the directory to searchregexp- the regular expression pattern- Returns:
- either an array of matching File objects or an empty array. Guaranteed to never return null
-