org.stanwood.media.util
Class FileHelper

java.lang.Object
  extended by org.stanwood.media.util.FileHelper

public class FileHelper
extends java.lang.Object

This is a help class that is used to perform operations on files.


Field Summary
static java.io.File HOME_DIR
          Stores the current users home directory
static java.lang.String LS
          A Line separator property value
static int MAX_RETRIES
          Number of retries to retry fetching of URL's
static long RETRY_SLEEP_TIME
          Time to sleep between retries
 
Constructor Summary
FileHelper()
           
 
Method Summary
static void appendContentsToFile(java.io.File file, java.lang.StringBuilder contents)
          Used to add contents to a file
static void copy(java.io.File src, java.io.File dst)
          Used to copy a source file or a directory to a destination file or directory.
static void copy(java.io.InputStream in, java.io.File dst)
          Used to copy the contents of a input stream to a destination file.
static void copy(java.io.InputStream in, java.io.File dst, java.util.Map<java.lang.String,java.lang.String> params)
          Used to copy the contents of a input stream to a destination file.
static java.lang.String copy(java.net.URL url, java.io.File dest)
          This will copy a file from the web to a destination file on the local system
static java.io.File createTempFile(java.lang.String name, java.lang.String ext)
          Used a temporary file that will be deleted when the JVM exits
static java.io.File createTmpDir(java.lang.String name)
          This will create a temporary directory using the given name.
static java.io.File createTmpFileWithContents(java.lang.StringBuilder testConfig)
          Used to create a temporary file with the give contents
static void delete(java.io.File file)
          Used to delete a file or a directory tree.
static void displayFile(java.io.File file, int startLine, int endLine, java.io.OutputStream os)
          Used to display the contents of a file
static void displayFile(java.io.File file, java.io.PrintStream os)
          Used to display the contents of a file
static java.lang.String getExtension(java.io.File file)
          Used to get the extension of the file
static Stream getInputStream(java.net.URL url)
          Used to get a stream to a URL.
static java.lang.String getMD5Checksum(java.io.File file)
          Used to generate a MD5 checksum string for a file
static java.lang.String getName(java.io.File file)
          Used to get the name of the file
static java.io.File getWorkingDirectory()
          Used to get the current working directory
static java.util.List<java.io.File> listDirectories(java.io.File dir)
          Used to list all the directories in a directory and it's sub directories.
static java.util.List<java.io.File> listFiles(java.io.File dir)
          Used to list all the files in a directory and it's sub directories.
static java.util.List<java.lang.String> listFilesAsStrings(java.io.File dir)
          Used to list all the files in a directory and it's sub directiories.
static void move(java.io.File from, java.io.File to)
          Used to move a directory or file from once location to another
static java.lang.String readFileContents(java.io.File file)
          Used to read the contents of a file into a string
static java.lang.String readFileContents(java.io.InputStream inputStream)
          Used to read the contents of a stream into a string
static void rename(java.io.File oldFile, java.io.File newFile)
          Used to rename a file.
static java.io.File resolveRelativePaths(java.io.File path)
          Used to convert relative paths to absolute paths.
static void unzip(java.io.InputStream is, java.io.File destDir)
          Used to unzip a file to a directory
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

LS

public static final java.lang.String LS
A Line separator property value


HOME_DIR

public static final java.io.File HOME_DIR
Stores the current users home directory


RETRY_SLEEP_TIME

public static final long RETRY_SLEEP_TIME
Time to sleep between retries

See Also:
Constant Field Values

MAX_RETRIES

public static final int MAX_RETRIES
Number of retries to retry fetching of URL's

See Also:
Constant Field Values
Constructor Detail

FileHelper

public FileHelper()
Method Detail

createTmpDir

public static java.io.File createTmpDir(java.lang.String name)
                                 throws java.io.IOException
This will create a temporary directory using the given name.

Parameters:
name - The name of the directory to create
Returns:
A file object pointing to the directory that was created
Throws:
java.io.IOException - Thrown if their is a problme creating the directory

copy

public static void copy(java.io.File src,
                        java.io.File dst)
                 throws java.io.IOException
Used to copy a source file or a directory to a destination file or directory.

Parameters:
src - The source file or directory
dst - The destination file or directory
Throws:
java.io.IOException - Thrown if their is a problem copying the file or directory

copy

public static void copy(java.io.InputStream in,
                        java.io.File dst)
                 throws java.io.IOException
Used to copy the contents of a input stream to a destination file.

Parameters:
in - The input stream
dst - The destination file
Throws:
java.io.IOException - Thrown if their is a problem copying the file

copy

public static void copy(java.io.InputStream in,
                        java.io.File dst,
                        java.util.Map<java.lang.String,java.lang.String> params)
                 throws java.io.IOException
Used to copy the contents of a input stream to a destination file.

Parameters:
in - The input stream
dst - The destination file
params - Parameters which are replaced with values when the file is copied
Throws:
java.io.IOException - Thrown if their is a problem copying the file

copy

public static java.lang.String copy(java.net.URL url,
                                    java.io.File dest)
                             throws java.io.IOException
This will copy a file from the web to a destination file on the local system

Parameters:
url - The url to read from the file from
dest - The file to be created on the location system
Returns:
A MD5 sum of the file
Throws:
java.io.IOException - Thrown if their is a problem reading or wring the file

getMD5Checksum

public static java.lang.String getMD5Checksum(java.io.File file)
                                       throws java.io.IOException
Used to generate a MD5 checksum string for a file

Parameters:
file - The file
Returns:
The checksum
Throws:
java.io.IOException - Thrown if their are any problems

displayFile

public static void displayFile(java.io.File file,
                               java.io.PrintStream os)
                        throws java.io.IOException
Used to display the contents of a file

Parameters:
file - The file to display
os - The output stream to display it to
Throws:
java.io.IOException - Thrown if their is a problem reading or displaying the file

displayFile

public static void displayFile(java.io.File file,
                               int startLine,
                               int endLine,
                               java.io.OutputStream os)
                        throws java.io.IOException
Used to display the contents of a file

Parameters:
file - The file to display
startLine - The line to start displaying from
endLine - The line to finish displaying from
os - The output stream used to print the file to
Throws:
java.io.IOException - Thrown if their is a problem reading the file

readFileContents

public static java.lang.String readFileContents(java.io.File file)
                                         throws java.io.IOException
Used to read the contents of a file into a string

Parameters:
file - The file to read
Returns:
The contents of the file
Throws:
java.io.IOException - Thrown if their is a problem reading the file

readFileContents

public static java.lang.String readFileContents(java.io.InputStream inputStream)
                                         throws java.io.IOException
Used to read the contents of a stream into a string

Parameters:
inputStream - The input stream
Returns:
The contents of the file
Throws:
java.io.IOException - Thrown if their is a problem reading the file

listDirectories

public static java.util.List<java.io.File> listDirectories(java.io.File dir)
Used to list all the directories in a directory and it's sub directories.

Parameters:
dir - The directory to list the files of
Returns:
The files in the directory

listFiles

public static java.util.List<java.io.File> listFiles(java.io.File dir)
Used to list all the files in a directory and it's sub directories.

Parameters:
dir - The directory to list the files of
Returns:
The files in the directory

listFilesAsStrings

public static java.util.List<java.lang.String> listFilesAsStrings(java.io.File dir)
Used to list all the files in a directory and it's sub directiories. File files are returned as a list of absolute paths.

Parameters:
dir - The directory to list the files of
Returns:
The files in the directory

appendContentsToFile

public static void appendContentsToFile(java.io.File file,
                                        java.lang.StringBuilder contents)
                                 throws java.io.IOException
Used to add contents to a file

Parameters:
file - The file to add contetns to
contents - The contents
Throws:
java.io.IOException - Thrown if their is a IO problem

unzip

public static void unzip(java.io.InputStream is,
                         java.io.File destDir)
                  throws java.io.IOException
Used to unzip a file to a directory

Parameters:
is - The input stream containing the file
destDir - The directory to unzip to
Throws:
java.io.IOException - Thrown if their are any problems

move

public static void move(java.io.File from,
                        java.io.File to)
                 throws java.io.IOException
Used to move a directory or file from once location to another

Parameters:
from - The old name of the file or directory
to - The new name of the file or directory
Throws:
java.io.IOException - Thrown if their are any problems

delete

public static void delete(java.io.File file)
                   throws java.io.IOException
Used to delete a file or a directory tree. If a directory is given, then all it's contents are also deleted recusrsivly.

Parameters:
file - The file or directory to delete
Throws:
java.io.IOException - Thrown if their are any problems

createTmpFileWithContents

public static java.io.File createTmpFileWithContents(java.lang.StringBuilder testConfig)
                                              throws java.io.IOException
Used to create a temporary file with the give contents

Parameters:
testConfig - The contents to put in the file
Returns:
A reference to the file
Throws:
java.io.IOException - Thrown if their are any problems

getInputStream

public static Stream getInputStream(java.net.URL url)
                             throws java.io.IOException
Used to get a stream to a URL. If their is a socket timeout exception, then this method will wait 5 seconds and try again. It does this 3 times before throwing the exception out if the method

Parameters:
url - The URL of the stream
Returns:
The stream
Throws:
java.io.IOException - Thrown if their are any problems

createTempFile

public static java.io.File createTempFile(java.lang.String name,
                                          java.lang.String ext)
                                   throws java.io.IOException
Used a temporary file that will be deleted when the JVM exits

Parameters:
name - name of file
ext - extension of the file
Returns:
The file
Throws:
java.io.IOException - Thrown if their is a problem creating the file

getExtension

public static java.lang.String getExtension(java.io.File file)
Used to get the extension of the file

Parameters:
file - The file
Returns:
The extension

getName

public static java.lang.String getName(java.io.File file)
Used to get the name of the file

Parameters:
file - The file
Returns:
The name

getWorkingDirectory

public static java.io.File getWorkingDirectory()
Used to get the current working directory

Returns:
the current working directory

resolveRelativePaths

public static java.io.File resolveRelativePaths(java.io.File path)
Used to convert relative paths to absolute paths. This will remove .. from the path

Parameters:
path - The relative path
Returns:
The absolute path

rename

public static void rename(java.io.File oldFile,
                          java.io.File newFile)
                   throws java.io.IOException
Used to rename a file. This handles the case where java's File.renameTo(File) fails. It that occurs it copies the file and deletes the original

Parameters:
oldFile - The old file name
newFile - The new file name
Throws:
java.io.IOException - Thrown if their are any problems