com.pepper.platform.util
Class FileUtils

java.lang.Object
  extended by com.pepper.platform.util.FileUtils

public class FileUtils
extends Object

Utilities for file manipulation.


Field Summary
static int DEFAULT_BUFFER_SIZE
          Size (in bytes) of the buffer to use when copying files
 
Constructor Summary
FileUtils()
           
 
Method Summary
static void copyFile(File sourceFile, File destFile)
          Copy the specified file.
static void copyFile(File sourceFile, File destFile, boolean recurse)
          Copy the specified file.
static void copyFile(File srcFile, File destFile, int bufferSize)
          Copies a file to another file
static void copyFile(File srcFile, FileOutputStream outStream, int bufferSize)
          Copies source file to destination output stream
static void copyFile(File srcFile, FileOutputStream outStream, int bufferSize, boolean syncToDisk, com.pepper.platform.util.CopyListener copyListener)
          Copies source file to destination output stream
static String copyFile(File progBaseDir, String destDataPath, File srcFile)
          Copies the file to the specified location.
static void copyFile(File srcFile, URL destURL, int bufferSize)
          Copies source file to destination url
static String[] copyFiles(File progBaseDir, String destDataPath, File[] files)
          Copy a set of files to a specified destimation (from pepperfilechooser)
static void copyFileToDir(File sourceFile, File destDir, boolean recurse)
          Copy the specified file to the specified directory.
static void copyStream(InputStream in, OutputStream out, int bufferSize)
          Wraps the streams in buffered streams and copies the input stream to the output stream.
static void copyStream(InputStream in, OutputStream out, int bufferSize, com.pepper.platform.util.CopyListener listener)
          Wraps the streams in buffered streams and copies the input stream to the output stream.
static File generateUniqueFile(File file)
          Given a file, returns a new file with a unique name if the the specified file exists.
static String getFileNameStripExtension(File f)
          Return the name of the file without the file name extension
static long getFileSizes(List files)
          Return the size (in bytes) of all Files in the given list.
static String getFileType(File f)
           
static List getRecursiveDirListing(File dir)
          Return a list of all files in the the given directory and any subdirectories.
static List getRecursiveDirListing(File dir, FileFilter filter)
          Return a list of all files in the the given directory and any subdirectories.
static List getRecursiveDirListing(File dir, List fileTypes)
          Return a list of all files in the the given directory and any subdirectories.
static String getRelativeFilename(File file, File baseDir)
           
static String getRelativeFilename(File file, String baseDir)
          Given a file and a base directory specification, return the path of the file relative to the base directory iff the file is in the base directory or one of its subdirectories; otherwise return the file's path as passed in.
static Collection getRelativeFiles(Collection files, File baseDir)
          Given an collection of Files, return a collection of NameValue objects where the name is the path of the file relative to the baseDir and the value is the File object itself.
static File getTempDir()
          Return a directory specification suitable for use as a temporary directory.
static boolean wipedir(File dir)
          Recursively delete a directory and its contents, including all subdirectories.
static boolean wipedir(File dir, boolean onExit)
          Recursively delete a directory and its contents, including all subdirectories.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_BUFFER_SIZE

public static final int DEFAULT_BUFFER_SIZE
Size (in bytes) of the buffer to use when copying files

See Also:
Constant Field Values
Constructor Detail

FileUtils

public FileUtils()
Method Detail

wipedir

public static boolean wipedir(File dir)
Recursively delete a directory and its contents, including all subdirectories. Returns true if the operation was successful, false otherwise. Note that even if wipedir returns false, some files and subdirectories may have been deleted.

Parameters:
dir - directory to wipe out
Returns:
true if the directory was deleted, otherwise false.

wipedir

public static boolean wipedir(File dir,
                              boolean onExit)
Recursively delete a directory and its contents, including all subdirectories. Returns true if the operation was successful, false otherwise. Note that even if wipedir returns false, some files and subdirectories may have been deleted.

Parameters:
dir - directory to wipe out
onExit - true to delete files on VM exit, false to delete them immediately
Returns:
true if the directory was deleted, otherwise false.

getRelativeFilename

public static String getRelativeFilename(File file,
                                         String baseDir)
Given a file and a base directory specification, return the path of the file relative to the base directory iff the file is in the base directory or one of its subdirectories; otherwise return the file's path as passed in.

TODO: This method needs a rewrite. There are a number of edge cases (basedir = "", etc) that aren't supported well.

Parameters:
file - file to get the path from. Must not be null, but file does not need to exist on disk.
baseDir - directory specification. Must not be null, but directory does not need to exist on disk.
Returns:
If the file is in the baseDir or one of its subdirs, return the file's path relative to the base dir. Otherwise return the value of file.getPath().

getRelativeFilename

public static String getRelativeFilename(File file,
                                         File baseDir)
See Also:
FileUtils.getRelativeFilename(File, String)

getRelativeFiles

public static Collection getRelativeFiles(Collection files,
                                          File baseDir)
Given an collection of Files, return a collection of NameValue objects where the name is the path of the file relative to the baseDir and the value is the File object itself.

Parameters:
files - Collection of Files
baseDir - base directory, used to calculate relative paths
Returns:
Collection of name/value pairs, where the name is the path of the file relative to the baseDir and the value is the file itself.

getFileType

public static String getFileType(File f)

getFileNameStripExtension

public static String getFileNameStripExtension(File f)
Return the name of the file without the file name extension

Since:
3.2

getRecursiveDirListing

public static List getRecursiveDirListing(File dir)
Return a list of all files in the the given directory and any subdirectories.

Parameters:
dir - Directory to return the listing for
Returns:
list of Files for all files in the directory and its subdirectories

getRecursiveDirListing

public static List getRecursiveDirListing(File dir,
                                          List fileTypes)
Return a list of all files in the the given directory and any subdirectories.

Parameters:
dir - Directory to return the listing for
fileTypes - List of file types to filter on; null to return all file types
Returns:
list of Files for all files in the directory and its subdirectories

getRecursiveDirListing

public static List getRecursiveDirListing(File dir,
                                          FileFilter filter)
Return a list of all files in the the given directory and any subdirectories.

Parameters:
dir - Directory to return the listing for
filter - Filter for files to accept, or null to accept all files.
Returns:
list of Files for all files in the directory and its subdirectories

copyFile

public static void copyFile(File srcFile,
                            File destFile,
                            int bufferSize)
                     throws Exception
Copies a file to another file

Parameters:
srcFile - a File value
destFile - a File value
bufferSize - an int value
Throws:
Exception

copyFile

public static void copyFile(File srcFile,
                            URL destURL,
                            int bufferSize)
                     throws Exception
Copies source file to destination url

Parameters:
srcFile - a File value
destURL - an URL value
bufferSize - an int value
Throws:
Exception - if an error occurs

copyFile

public static void copyFile(File srcFile,
                            FileOutputStream outStream,
                            int bufferSize)
                     throws Exception
Copies source file to destination output stream

Parameters:
srcFile - a File value
outStream - a FileOutputStream value
bufferSize - an int value
Throws:
Exception - if an error occurs

copyFile

public static void copyFile(File srcFile,
                            FileOutputStream outStream,
                            int bufferSize,
                            boolean syncToDisk,
                            com.pepper.platform.util.CopyListener copyListener)
                     throws Exception
Copies source file to destination output stream

Parameters:
srcFile - a File value
outStream - a FileOutputStream value
bufferSize - an int value
syncToDisk - if true performs a sync operation on the FileDescriptor for the FileOutputStream which causes the data to get flushed to the removable media
copyListener - optional listener that gets notified on each chunk read and when the copy is complete
Throws:
Exception - if an error occurs

copyFileToDir

public static void copyFileToDir(File sourceFile,
                                 File destDir,
                                 boolean recurse)
Copy the specified file to the specified directory. If the source file is a directory, copies the entire contents to the destination directory.

Parameters:
sourceFile - the source file. Must exist on disk.
destDir - the destination directory.
recurse - true to recursively copy all subdirectories of sourceFile if it is a directory

copyFile

public static void copyFile(File sourceFile,
                            File destFile,
                            boolean recurse)
Copy the specified file. If the file is a directory, copies the entire contents of the directory.

Parameters:
sourceFile - the source file. Must exist on disk.
destFile - the destination file.
recurse - true to recursively copy all subdirectories of sourceFile if it is a directory

copyFile

public static void copyFile(File sourceFile,
                            File destFile)
Copy the specified file. If the file is a directory, copies the entire contents of the directory.

Parameters:
sourceFile - the source file. Must exist on disk.
destFile - the destination file.

getFileSizes

public static long getFileSizes(List files)
Return the size (in bytes) of all Files in the given list. Doesn't include the size of any files in subdirectories.

Parameters:
files - List of Files
Returns:
size in bytes of all files in the list.

copyFiles

public static String[] copyFiles(File progBaseDir,
                                 String destDataPath,
                                 File[] files)
                          throws Exception
Copy a set of files to a specified destimation (from pepperfilechooser)

Parameters:
progBaseDir - a File value
destDataPath - a String value
files - a File[] value
Returns:
a File[] value
Throws:
Exception - if an error occurs

copyFile

public static String copyFile(File progBaseDir,
                              String destDataPath,
                              File srcFile)
                       throws Exception
Copies the file to the specified location.

Parameters:
progBaseDir - a File value
destDataPath - a String value
srcFile - a File value
Returns:
a File value
Throws:
Exception - if an error occurs

copyStream

public static void copyStream(InputStream in,
                              OutputStream out,
                              int bufferSize)
                       throws IOException
Wraps the streams in buffered streams and copies the input stream to the output stream. Closes both streams on completion.

Parameters:
in - the input stream.
out - the output stream.
bufferSize - the buffer size to use.
Throws:
IOException

copyStream

public static void copyStream(InputStream in,
                              OutputStream out,
                              int bufferSize,
                              com.pepper.platform.util.CopyListener listener)
                       throws IOException
Wraps the streams in buffered streams and copies the input stream to the output stream. Closes both streams on completion.

Parameters:
in - the input stream.
out - the output stream.
bufferSize - the buffer size to use.
listener - the class that is notified of copy events.
Throws:
IOException

getTempDir

public static File getTempDir()
Return a directory specification suitable for use as a temporary directory. The directory is guaranteed to not exist.

Returns:
directory specification for a new directory in the operating system's temp directory, such as /tmp, /var/tmp, or c:\temp. Note that when running with security turned on, this is generally $PEPPER_HOME/tmp.

generateUniqueFile

public static File generateUniqueFile(File file)
Given a file, returns a new file with a unique name if the the specified file exists. Unique name is generated by using original name and inserting a number into file name before extension.

Parameters:
file - the file to use to determine if the name is unique.
Returns:
a new file represented by a unique name or the source file if there was no file of that name.


Copyright © 2006-2007 Pepper Computer, Inc. All Rights Reserved.