org.deduced.utilities
Class FileUtilities

java.lang.Object
  extended by org.deduced.utilities.FileUtilities

public class FileUtilities
extends java.lang.Object

Utility methods used on files.

Author:
Steve McDuff

Field Summary
static int CREATE_CODE
          code returned when a file was created
private static int DIRECTORY_DELETE_RETRY_COUNT
          directory delete retry count
private static int DIRECTORY_DELETE_RETRY_DELAY
          delay between directory delete attempts in milliseconds.
static java.lang.String DOT
          Dot file name
static java.lang.String FOLDER_SEPARATOR
          folder separator constant
static int IGNORED_CODE
          code returned when a file was ignored
private static java.lang.String JAR_FILE_NAME_REGULAR_EXPRESSION
          regular expression used to find JAR file names.
private static java.util.logging.Logger LOGGER
          logger
static int MODIFIED_CODE
          code returned when a file was modified
static int READ_BUFFER_SIZE
          buffer size used to read files
 
Constructor Summary
FileUtilities()
           
 
Method Summary
static void checkForErrorOnWriter(java.io.PrintWriter writer)
          check For Error On Writer.
static void closeQuietly(java.util.zip.ZipFile input)
          close a zip file quietly.
protected static void copyPathSkipDot(java.lang.String[] path, java.util.List<java.lang.String> target)
          copy a list of path fragments into a target list and skip all the useless parts (such as the "." path)
static java.lang.String extractRelativePath(java.lang.String sourcePath, java.lang.String targetPath)
          extract a relative path between a source directory and a target directory or file.
static java.lang.String extractRelativePathToWorkingDirectory(java.lang.String filePath)
          extract a relative file path from the current working directory
static boolean fileExists(java.lang.String filePath)
          test if a file exists based on a path
static void forceCreateDirectory(java.lang.String folderName)
          force a directory creation
static void forceDeleteDirectory(java.io.File directoryToDelete)
          force Delete Directory through a retry mechanism
static java.util.List<java.lang.String> getCanonicalFilePathList(java.io.File[] fileList)
          get Canonical File Path List, ignoring any files from which we fail to get the path.
static java.lang.String getCanonicalPathSilent(java.io.File currentFile)
          get Canonical Path and log any exception that might occur.
static java.lang.ClassLoader getCurrentThreadClassLoader()
          get the Current Thread Class Loader
static java.lang.String getCurrentWorkingDirectory()
           
static java.io.InputStream getFileContentStream(java.lang.String fileName)
          get File Content Stream using either a system resource or a file system reference.
static java.io.File[] getJarListFromFolder(java.lang.String folderName)
          get the Jar List From a folder name.
static java.io.InputStream getMandatoryFileContentStream(java.lang.String fileName)
          get File Content Stream using either a system resource or a file system reference.
static boolean isFileReadOnly(java.lang.String filePath)
          test if a file is read only.
static boolean isFileResource(java.lang.String filePath)
          test if a file can be reached as a resource.
static boolean isStreamContentEqual(java.io.InputStream readStream, java.io.InputStream secondStream)
          check if 2 streams have the same content
static java.lang.String mergeFolderName(java.lang.String parentFolderName, java.lang.String subFolderName)
          merge two folder names together.
static void overwriteFileContent(java.lang.String fileName, byte[] content)
          overwrite the content of a file, whatever the previous content was
static byte[] stringToByteArray(java.lang.String content)
          convert a string To a ByteArray
static int writeFileContent(java.lang.String fileName, byte[] content)
          write the file content
static int writeFileContent(java.lang.String fileName, java.lang.String content)
          write the file content
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

LOGGER

private static final java.util.logging.Logger LOGGER
logger


JAR_FILE_NAME_REGULAR_EXPRESSION

private static final java.lang.String JAR_FILE_NAME_REGULAR_EXPRESSION
regular expression used to find JAR file names.

See Also:
Constant Field Values

DIRECTORY_DELETE_RETRY_COUNT

private static final int DIRECTORY_DELETE_RETRY_COUNT
directory delete retry count

See Also:
Constant Field Values

DIRECTORY_DELETE_RETRY_DELAY

private static final int DIRECTORY_DELETE_RETRY_DELAY
delay between directory delete attempts in milliseconds.

See Also:
Constant Field Values

FOLDER_SEPARATOR

public static final java.lang.String FOLDER_SEPARATOR
folder separator constant

See Also:
Constant Field Values

DOT

public static final java.lang.String DOT
Dot file name

See Also:
Constant Field Values

READ_BUFFER_SIZE

public static final int READ_BUFFER_SIZE
buffer size used to read files

See Also:
Constant Field Values

CREATE_CODE

public static final int CREATE_CODE
code returned when a file was created

See Also:
Constant Field Values

MODIFIED_CODE

public static final int MODIFIED_CODE
code returned when a file was modified

See Also:
Constant Field Values

IGNORED_CODE

public static final int IGNORED_CODE
code returned when a file was ignored

See Also:
Constant Field Values
Constructor Detail

FileUtilities

public FileUtilities()
Method Detail

writeFileContent

public static int writeFileContent(java.lang.String fileName,
                                   java.lang.String content)
                            throws java.io.IOException
write the file content

Parameters:
fileName - the name of the file
content - content of the file
Returns:
the result of the operation, can be either CREATE_CODE, MODIFIED_CODE or IGNORED_CODE
Throws:
java.io.IOException - any error generated when accessing the file

stringToByteArray

public static byte[] stringToByteArray(java.lang.String content)
convert a string To a ByteArray

Parameters:
content - the string content
Returns:
the matching byte array

writeFileContent

public static int writeFileContent(java.lang.String fileName,
                                   byte[] content)
                            throws java.io.IOException
write the file content

Parameters:
fileName - the name of the file
content - content of the file
Returns:
the result of the operation, can be either CREATE_CODE, MODIFIED_CODE or IGNORED_CODE
Throws:
java.io.IOException - any error generated when accessing the file

overwriteFileContent

public static void overwriteFileContent(java.lang.String fileName,
                                        byte[] content)
                                 throws java.io.IOException
overwrite the content of a file, whatever the previous content was

Parameters:
fileName - the file name
content - the file content
Throws:
java.io.IOException - any error that occurred while accessing the file

isStreamContentEqual

public static boolean isStreamContentEqual(java.io.InputStream readStream,
                                           java.io.InputStream secondStream)
check if 2 streams have the same content

Parameters:
readStream - the first stream
secondStream - the second stream
Returns:
true if both streams have the same content

extractRelativePath

public static java.lang.String extractRelativePath(java.lang.String sourcePath,
                                                   java.lang.String targetPath)
extract a relative path between a source directory and a target directory or file.

Parameters:
sourcePath - the source folder to extract a relative path, must be an absolute path
targetPath - the target file or folder to extract a relative path, must be an absolute path
Returns:
the relative path in the form "../../someFolder/MyRelativeFile.txt". If no relationship can be found between the files (for instance, if the two files are on a separate drive), the target path is returned.

copyPathSkipDot

protected static void copyPathSkipDot(java.lang.String[] path,
                                      java.util.List<java.lang.String> target)
copy a list of path fragments into a target list and skip all the useless parts (such as the "." path)

Parameters:
path - the path fragments
target - the target list that receives the path fragments

extractRelativePathToWorkingDirectory

public static java.lang.String extractRelativePathToWorkingDirectory(java.lang.String filePath)
extract a relative file path from the current working directory

Parameters:
filePath - the absolute file path to differentiate from the current working directory
Returns:
the relative file path

getCurrentWorkingDirectory

public static java.lang.String getCurrentWorkingDirectory()
Returns:
the current working directory absolute path

isFileReadOnly

public static boolean isFileReadOnly(java.lang.String filePath)
test if a file is read only.

Parameters:
filePath - the file path
Returns:
true if the file doesn't exist or if the file can't be written.

isFileResource

public static boolean isFileResource(java.lang.String filePath)
test if a file can be reached as a resource. This method will first look for the file on the file system. If it exists, then the output will be false.

Parameters:
filePath - the file path
Returns:
true if there is no file system resource matching this name and the file can be reached as a resource.

fileExists

public static boolean fileExists(java.lang.String filePath)
test if a file exists based on a path

Parameters:
filePath - the file path
Returns:
true if the file exists

mergeFolderName

public static java.lang.String mergeFolderName(java.lang.String parentFolderName,
                                               java.lang.String subFolderName)
merge two folder names together. If the first one is null or empty, then only the subFolderName is returned.

Execution example:

mergeFolderName("", "abc/def") = "abc/def"

mergeFolderName(null, "abc/def") = "abc/def"

mergeFolderName("test/me", null) = "test/me"

mergeFolderName("test/me", "abc/def") = "test/me/abc/def"

mergeFolderName("test/me/", "abc/def") = "test/me/abc/def"

Parameters:
parentFolderName - the parent folder name
subFolderName - the child folder name
Returns:
the merged folder name

forceCreateDirectory

public static void forceCreateDirectory(java.lang.String folderName)
                                 throws java.io.IOException
force a directory creation

Parameters:
folderName - the folder name
Throws:
java.io.IOException - any error encountered during the creation

getFileContentStream

public static java.io.InputStream getFileContentStream(java.lang.String fileName)
                                                throws java.io.IOException
get File Content Stream using either a system resource or a file system reference. The file system reference will take precedence over the system resource.

Parameters:
fileName - file name
Returns:
the file content stream. Null if no file could be found.
Throws:
java.io.IOException - for read errors.

getCurrentThreadClassLoader

public static java.lang.ClassLoader getCurrentThreadClassLoader()
get the Current Thread Class Loader

Returns:
the current thread class loader

getMandatoryFileContentStream

public static java.io.InputStream getMandatoryFileContentStream(java.lang.String fileName)
                                                         throws java.io.IOException
get File Content Stream using either a system resource or a file system reference. The file system reference will take precedence over the system resource.

Parameters:
fileName - file name
Returns:
the file content stream
Throws:
java.io.IOException - for read errors.

closeQuietly

public static void closeQuietly(java.util.zip.ZipFile input)
close a zip file quietly.

Parameters:
input - the zip file to close

forceDeleteDirectory

public static void forceDeleteDirectory(java.io.File directoryToDelete)
                                 throws java.io.IOException
force Delete Directory through a retry mechanism

Parameters:
directoryToDelete - the directory to delete
Throws:
java.io.IOException - the last error

checkForErrorOnWriter

public static void checkForErrorOnWriter(java.io.PrintWriter writer)
check For Error On Writer. Raise an assertion failure if there are problems.

Parameters:
writer - the writer to verify.

getJarListFromFolder

public static java.io.File[] getJarListFromFolder(java.lang.String folderName)
get the Jar List From a folder name. This method will only return the list of JARS immediately under the specified folder. It won't search in sub folders.

Parameters:
folderName - the folder name to scan
Returns:
the array of jar file names

getCanonicalFilePathList

public static java.util.List<java.lang.String> getCanonicalFilePathList(java.io.File[] fileList)
get Canonical File Path List, ignoring any files from which we fail to get the path.

Parameters:
fileList - the file list from which to get the canonical path.
Returns:
the list of canonical file paths

getCanonicalPathSilent

public static java.lang.String getCanonicalPathSilent(java.io.File currentFile)
get Canonical Path and log any exception that might occur.

Parameters:
currentFile - the file from which to get the canonical path
Returns:
the canonical path. Null if any error was encountered.