com.pixelmed.utils
Class FileUtilities

java.lang.Object
  extended by com.pixelmed.utils.FileUtilities

public class FileUtilities
extends Object

Various static methods helpful for handling files.


Method Summary
static String digest(InputStream in, String algorithm)
          Return a message digest of an InputStream.
static String digest(String fileName, String algorithm)
          Return a message digest of a file.
static File getFileFromNameInsensitiveToCaseIfNecessary(String fileName)
          Determine if a file corresponding to the specified name exists, checking for case insensitive variants if necessary.
static List<String> getFilePathComponents(File path)
          Get the individual components of the canonical form of the path as a list.
static ArrayList listFilesRecursively(File initialPath)
          Recursively traverse the specified directory and its sub-directory and produce a list of all the files contained therein, in no particular order.
static String makePathToFileInUsersHomeDirectory(String fileName)
          Given a file name, such as the properties file name, make a path to it in the user's home directory.
static File makeSameRelativePathNameInDifferentFolder(File srcFolder, File dstFolder, File srcFile)
          Create a new path that re-creates the relative path of the source file in the destination folder.
static File makeSameRelativePathNameInDifferentFolder(String srcFolderName, String dstFolderName, String srcFileName)
          Create a new path that re-creates the relative path of the source file in the destination folder.
static String makeTemporaryFileName()
          Create a temporary filename.
static String md5(InputStream in)
          Return an MD5 message digest of an InputStream.
static String md5(String fileName)
          Return an MD5 message digest of a file.
static String readFile(File file)
          Read an entire file into a string.
static String readFile(InputStream stream)
          Read an entire file into a string.
static String readFile(Reader reader)
          Read an entire file into a string.
static String readFile(String filename)
          Read an entire file into a string.
static void renameElseCopyTo(File srcFile, File dstFile)
          Rename a file, if possible, else make a copy of it.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

renameElseCopyTo

public static final void renameElseCopyTo(File srcFile,
                                          File dstFile)
                                   throws IOException

Rename a file, if possible, else make a copy of it.

Parameters:
srcFile - the source
dstFile - the destination
Throws:
IOException - thrown if the copying fails for any reason

listFilesRecursively

public static final ArrayList listFilesRecursively(File initialPath)

Recursively traverse the specified directory and its sub-directory and produce a list of all the files contained therein, in no particular order.

If the path is a file, just return that.

Any security (permission) exceptions are caught and logged to stderr and not propagated.

Parameters:
initialPath - The abstract pathname of the directory to begin searching
Returns:
An ArrayList of abstract pathnames denoting the files found. The ArrayList will be empty if the path is empty or does not exist or if an error occurs.

readFile

public static final String readFile(Reader reader)
                             throws IOException

Read an entire file into a string.

Parameters:
reader - The file reader
Returns:
The contents of the file as a String.
Throws:
If - an IO error occurs.
IOException

readFile

public static final String readFile(InputStream stream)
                             throws IOException

Read an entire file into a string.

Parameters:
stream - The input stream (e.g., from class.getResourceAsStream())
Returns:
The contents of the file as a String.
Throws:
If - an IO error occurs.
IOException

readFile

public static final String readFile(File file)
                             throws IOException

Read an entire file into a string.

Parameters:
file - The file
Returns:
The contents of the file as a String.
Throws:
If - an IO error occurs.
IOException

readFile

public static final String readFile(String filename)
                             throws IOException

Read an entire file into a string.

Parameters:
filename - The file
Returns:
The contents of the file as a String.
Throws:
If - an IO error occurs.
IOException

getFileFromNameInsensitiveToCaseIfNecessary

public static final File getFileFromNameInsensitiveToCaseIfNecessary(String fileName)
                                                              throws FileNotFoundException

Determine if a file corresponding to the specified name exists, checking for case insensitive variants if necessary.

Parameters:
fileName - The name of the file to find
Returns:
A file if found.
Throws:
If - the file cannot be found.
FileNotFoundException

makeTemporaryFileName

public static final String makeTemporaryFileName()

Create a temporary filename.

Returns:
a string that does not include delimiter characters and is unique within this JVM.

makePathToFileInUsersHomeDirectory

public static final String makePathToFileInUsersHomeDirectory(String fileName)

Given a file name, such as the properties file name, make a path to it in the user's home directory.

Parameters:
fileName - the file name to make a path to

digest

public static final String digest(String fileName,
                                  String algorithm)
                           throws IOException,
                                  NoSuchAlgorithmException

Return a message digest of a file.

Parameters:
fileName - the file name
algorithm - the digest algorithm, such as "MD5" or "SHA"
Returns:
string representation of the digest
Throws:
IOException
NoSuchAlgorithmException

digest

public static final String digest(InputStream in,
                                  String algorithm)
                           throws IOException,
                                  NoSuchAlgorithmException

Return a message digest of an InputStream.

Parameters:
in - the InputStream
algorithm - the digest algorithm, such as "MD5" or "SHA"
Returns:
string representation of the digest
Throws:
IOException
NoSuchAlgorithmException

md5

public static final String md5(String fileName)
                        throws IOException,
                               NoSuchAlgorithmException

Return an MD5 message digest of a file.

Parameters:
fileName - the file name
Returns:
string representation of the digest
Throws:
IOException
NoSuchAlgorithmException

md5

public static final String md5(InputStream in)
                        throws IOException,
                               NoSuchAlgorithmException

Return an MD5 message digest of an InputStream.

Parameters:
in - the InputStream
Returns:
string representation of the digest
Throws:
IOException
NoSuchAlgorithmException

getFilePathComponents

public static List<String> getFilePathComponents(File path)
                                          throws IOException

Get the individual components of the canonical form of the path as a list.

Parameters:
path -
Returns:
each component of the path, starting with the root
Throws:
IOException

makeSameRelativePathNameInDifferentFolder

public static File makeSameRelativePathNameInDifferentFolder(String srcFolderName,
                                                             String dstFolderName,
                                                             String srcFileName)
                                                      throws IOException

Create a new path that re-creates the relative path of the source file in the destination folder.

Parameters:
srcFolderName -
dstFolderName -
srcFileName -
Returns:
a File in the destination folder
Throws:
IOException

makeSameRelativePathNameInDifferentFolder

public static File makeSameRelativePathNameInDifferentFolder(File srcFolder,
                                                             File dstFolder,
                                                             File srcFile)
                                                      throws IOException

Create a new path that re-creates the relative path of the source file in the destination folder.

Parameters:
srcFolder -
dstFolder -
srcFile -
Returns:
a File in the destination folder
Throws:
IOException