org.archive.util
Class FileUtils

java.lang.Object
  extended by org.archive.util.FileUtils

public class FileUtils
extends java.lang.Object

Utility methods for manipulating files and directories.

Author:
John Erik Halse

Field Summary
(package private) static java.util.LinkedList<java.io.File> pendingDeletes
           
static java.io.File TMPDIR
           
 
Method Summary
static boolean copyFile(java.io.File src, java.io.File dest)
          Copy the src file to the destination.
static boolean copyFile(java.io.File src, java.io.File dest, boolean overwrite)
          Copy the src file to the destination.
static boolean copyFile(java.io.File src, java.io.File dest, long extent)
          Copy up to extent bytes of the source file to the destination
static boolean copyFile(java.io.File src, java.io.File dest, long extent, boolean overwrite)
          Copy up to extent bytes of the source file to the destination
static void copyFiles(java.io.File src, java.io.File dest)
          Recursively copy all files from one directory to another.
static void copyFiles(java.io.File src, java.io.FilenameFilter filter, java.io.File dest, boolean inSortedOrder, boolean overwrite)
          Recursively copy all files from one directory to another.
static void copyFiles(java.io.File src, java.io.FilenameFilter filter, java.io.File dest, boolean inSortedOrder, boolean overwrite, java.util.List<java.io.IOException> exceptions)
          Recursively copy all files from one directory to another.
static int copyFiles(java.io.File srcDir, java.util.Set srcFile, java.io.File dest)
           
static boolean deleteDir(java.io.File dir)
          Deletes all files and subdirectories under dir.
static void deleteSoonerOrLater(java.io.File fileToDelete)
          Delete the file now -- but in the event of failure, keep trying in the future.
static java.io.File[] getFilesWithPrefix(java.io.File dir, java.lang.String prefix)
          Get a list of all files in directory that have passed prefix.
static java.io.FileFilter getRegexpFileFilter(java.lang.String regexp)
          Get a @link java.io.FileFilter that filters files based on a regular expression.
static java.lang.String[] getSortedDirContent(java.io.File src, java.io.FilenameFilter filter)
           
static java.io.File isReadable(java.io.File f)
          Test file exists and is readable.
static boolean isReadableWithExtensionAndMagic(java.io.File f, java.lang.String uncompressedExtension, java.lang.String magic)
           
static java.io.File maybeRelative(java.io.File context, java.lang.String path)
          Turn path into a File, relative to context (which may be ignored if path is absolute).
static java.lang.String readFileAsString(java.io.File file)
          Utility method to read an entire file as a String.
static void syncDirectories(java.io.File src, java.io.FilenameFilter filter, java.io.File tgt)
          Use for case where files are being added to src.
protected static void workaroundCopyFile(java.io.File src, java.io.File dest)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

TMPDIR

public static final java.io.File TMPDIR

pendingDeletes

static java.util.LinkedList<java.io.File> pendingDeletes
Method Detail

copyFiles

public static int copyFiles(java.io.File srcDir,
                            java.util.Set srcFile,
                            java.io.File dest)
                     throws java.io.IOException
Throws:
java.io.IOException

copyFiles

public static void copyFiles(java.io.File src,
                             java.io.File dest)
                      throws java.io.IOException
Recursively copy all files from one directory to another.

Parameters:
src - file or directory to copy from.
dest - file or directory to copy to.
Throws:
java.io.IOException

getSortedDirContent

public static java.lang.String[] getSortedDirContent(java.io.File src,
                                                     java.io.FilenameFilter filter)
Parameters:
src - Directory of files to fetch.
filter - Filter to apply to filenames.
Returns:
Files in directory sorted.

copyFiles

public static void copyFiles(java.io.File src,
                             java.io.FilenameFilter filter,
                             java.io.File dest,
                             boolean inSortedOrder,
                             boolean overwrite)
                      throws java.io.IOException
Recursively copy all files from one directory to another.

Parameters:
src - File or directory to copy from.
filter - Filename filter to apply to src. May be null if no filtering wanted.
dest - File or directory to copy to.
inSortedOrder - Copy in order of natural sort.
overwrite - If target file already exits, and this parameter is true, overwrite target file (We do this by first deleting the target file before we begin the copy).
Throws:
java.io.IOException

copyFiles

public static void copyFiles(java.io.File src,
                             java.io.FilenameFilter filter,
                             java.io.File dest,
                             boolean inSortedOrder,
                             boolean overwrite,
                             java.util.List<java.io.IOException> exceptions)
                      throws java.io.IOException
Recursively copy all files from one directory to another.

Parameters:
src - File or directory to copy from.
filter - Filename filter to apply to src. May be null if no filtering wanted.
dest - File or directory to copy to.
inSortedOrder - Copy in order of natural sort.
overwrite - If target file already exits, and this parameter is true, overwrite target file (We do this by first deleting the target file before we begin the copy).
exceptions - if non-null, add any individual-file IOExceptions to this List rather than throwing, and proceed with the deep copy
Throws:
java.io.IOException

copyFile

public static boolean copyFile(java.io.File src,
                               java.io.File dest)
                        throws java.io.FileNotFoundException,
                               java.io.IOException
Copy the src file to the destination.

Parameters:
src -
dest -
Returns:
True if the extent was greater than actual bytes copied.
Throws:
java.io.FileNotFoundException
java.io.IOException

copyFile

public static boolean copyFile(java.io.File src,
                               java.io.File dest,
                               boolean overwrite)
                        throws java.io.FileNotFoundException,
                               java.io.IOException
Copy the src file to the destination.

Parameters:
src -
dest -
overwrite - If target file already exits, and this parameter is true, overwrite target file (We do this by first deleting the target file before we begin the copy).
Returns:
True if the extent was greater than actual bytes copied.
Throws:
java.io.FileNotFoundException
java.io.IOException

copyFile

public static boolean copyFile(java.io.File src,
                               java.io.File dest,
                               long extent)
                        throws java.io.FileNotFoundException,
                               java.io.IOException
Copy up to extent bytes of the source file to the destination

Parameters:
src -
dest -
extent - Maximum number of bytes to copy
Returns:
True if the extent was greater than actual bytes copied.
Throws:
java.io.FileNotFoundException
java.io.IOException

copyFile

public static boolean copyFile(java.io.File src,
                               java.io.File dest,
                               long extent,
                               boolean overwrite)
                        throws java.io.FileNotFoundException,
                               java.io.IOException
Copy up to extent bytes of the source file to the destination

Parameters:
src -
dest -
extent - Maximum number of bytes to copy
overwrite - If target file already exits, and this parameter is true, overwrite target file (We do this by first deleting the target file before we begin the copy).
Returns:
True if the extent was greater than actual bytes copied.
Throws:
java.io.FileNotFoundException
java.io.IOException

workaroundCopyFile

protected static void workaroundCopyFile(java.io.File src,
                                         java.io.File dest)
                                  throws java.io.IOException
Throws:
java.io.IOException

deleteDir

public static boolean deleteDir(java.io.File dir)
Deletes all files and subdirectories under dir.

Parameters:
dir -
Returns:
true if all deletions were successful. If a deletion fails, the method stops attempting to delete and returns false.

readFileAsString

public static java.lang.String readFileAsString(java.io.File file)
                                         throws java.io.IOException
Utility method to read an entire file as a String.

Parameters:
file -
Returns:
File as String.
Throws:
java.io.IOException

getFilesWithPrefix

public static java.io.File[] getFilesWithPrefix(java.io.File dir,
                                                java.lang.String prefix)
Get a list of all files in directory that have passed prefix.

Parameters:
dir - Dir to look in.
prefix - Basename of files to look for. Compare is case insensitive.
Returns:
List of files in dir that start w/ passed basename.

getRegexpFileFilter

public static java.io.FileFilter getRegexpFileFilter(java.lang.String regexp)
Get a @link java.io.FileFilter that filters files based on a regular expression.

Parameters:
regexp - the regular expression the files must match.
Returns:
the newly created filter.

syncDirectories

public static void syncDirectories(java.io.File src,
                                   java.io.FilenameFilter filter,
                                   java.io.File tgt)
                            throws java.io.IOException
Use for case where files are being added to src. Will break off copy when tgt is same as src.

Parameters:
src - Source directory to copy from.
tgt - Target to copy to.
filter - Filter to apply to files to copy.
Throws:
java.io.IOException

isReadable

public static java.io.File isReadable(java.io.File f)
                               throws java.io.IOException
Test file exists and is readable.

Parameters:
f - File to test.
Throws:
java.io.IOException - If file does not exist or is not unreadable.

isReadableWithExtensionAndMagic

public static boolean isReadableWithExtensionAndMagic(java.io.File f,
                                                      java.lang.String uncompressedExtension,
                                                      java.lang.String magic)
                                               throws java.io.IOException
Parameters:
f - File to test.
Returns:
True if file is readable, has uncompressed extension, and magic string at file start.
Throws:
java.io.IOException - If file does not exist or is not readable.

maybeRelative

public static java.io.File maybeRelative(java.io.File context,
                                         java.lang.String path)
Turn path into a File, relative to context (which may be ignored if path is absolute).

Parameters:
context - File context if path is relative
path - String path to make into a File
Returns:
File created

deleteSoonerOrLater

public static void deleteSoonerOrLater(java.io.File fileToDelete)
Delete the file now -- but in the event of failure, keep trying in the future. VERY IMPORTANT: Do not use with any file whose name/path may be reused, because the lagged delete could then wind up deleting the newer file. Essentially, only to be used with uniquely-named temp files. Necessary because some platforms (looking at you, JVM-on-Windows) will have deletes fail because of things like file-mapped buffers remaining, and there's no explicit way to unmap a buffer. (See 6-year-old Sun-stumping Java bug http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4724038 ) We just have to wait and retry. (Why not just File.deleteOnExit? There could be an arbitrary, unbounded number of files in such a situation, that are only deletable a few seconds or minutes after our first attempt. Waiting for JVM exist could mean disk exhaustion. It's also unclear if the native FS class implementations of deleteOnExit use RAM per pending file.)

Parameters:
fileToDelete -


Copyright © 2003-2011 Internet Archive. All Rights Reserved.