org.aspectj.util
Class FileUtil

java.lang.Object
  extended by org.aspectj.util.FileUtil

public class FileUtil
extends java.lang.Object


Nested Class Summary
static class FileUtil.Pipe
          A pipe when run reads from an input stream to an output stream, optionally sleeping between reads.
 
Field Summary
static java.io.FileFilter ALL
          accept all files
static java.io.FileFilter aspectjSourceFileFilter
           
static java.io.File DEFAULT_PARENT
          default parent directory File when a file has a null parent
static java.io.FileFilter DIRS_AND_WRITABLE_CLASSES
           
static java.util.List<java.lang.String> SOURCE_SUFFIXES
          unmodifiable List of String source file suffixes (including leading ".")
static java.io.FileFilter ZIP_FILTER
           
 
Method Summary
static boolean canReadDir(java.io.File dir)
           
static boolean canReadFile(java.io.File file)
           
static boolean canWriteDir(java.io.File dir)
           
static boolean canWriteFile(java.io.File file)
           
static int copyDir(java.io.File fromDir, java.io.File toDir)
          Copy contents of fromDir into toDir
static int copyDir(java.io.File fromDir, java.io.File toDir, java.lang.String fromSuffix, java.lang.String toSuffix)
          Recursively copy files in fromDir (with any fromSuffix) to toDir, replacing fromSuffix with toSuffix if any.
static int copyDir(java.io.File fromDir, java.io.File toDir, java.lang.String fromSuffix, java.lang.String toSuffix, java.io.FileFilter delegate)
          Recursively copy files in fromDir (with any fromSuffix) to toDir, replacing fromSuffix with toSuffix if any.
static void copyFile(java.io.File fromFile, java.io.File toFile)
          Copy fromFile to toFile, handling file-file, dir-dir, and file-dir copies.
static java.io.File[] copyFiles(java.io.File srcDir, java.lang.String[] relativePaths, java.io.File destDir)
          Copy files from source dir into destination directory, creating any needed directories.
static void copyStream(java.io.DataInputStream in, java.io.PrintStream out)
          do line-based copying
static void copyStream(java.io.InputStream in, java.io.OutputStream out)
           
static void copyStream(java.io.Reader in, java.io.Writer out)
           
static void copyValidFiles(java.io.File fromFile, java.io.File toFile)
          Copy file to file.
static int deleteContents(java.io.File dir)
          Recursively delete the contents of dir, but not the dir itself
static int deleteContents(java.io.File dir, java.io.FileFilter filter)
          Recursively delete some contents of dir, but not the dir itself.
static int deleteContents(java.io.File dir, java.io.FileFilter filter, boolean deleteEmptyDirs)
          Recursively delete some contents of dir, but not the dir itself.
static java.io.File ensureParentWritable(java.io.File path)
          Ensure that the parent directory to path can be written.
static java.lang.String fileToClassName(java.io.File basedir, java.io.File classFile)
          Extract the name of a class from the path to its file.
static java.lang.String flatten(java.io.File[] files, java.lang.String infix)
          Render a set of files to String as a path by getting absolute paths of each and delimiting with infix.
static java.lang.String flatten(java.lang.String[] paths, java.lang.String infix)
          Flatten File[] to String.
static java.lang.String[] getAbsolutePaths(java.io.File[] files)
           
static java.io.File[] getBaseDirFiles(java.io.File basedir, java.lang.String[] paths)
          Convert String[] paths to File[] as offset of base directory
static java.io.File[] getBaseDirFiles(java.io.File basedir, java.lang.String[] paths, java.lang.String[] suffixes)
          Convert String[] paths to File[] as offset of base directory
static java.io.File getBestFile(java.io.File file)
          Render as best file, canonical or absolute.
static java.io.File getBestFile(java.lang.String[] paths)
          Get best File for the first-readable path in input paths, treating entries prefixed "sp:" as system property keys.
static java.lang.String getBestPath(java.io.File file)
          Render as best path, canonical or absolute.
static java.net.URL getFileURL(java.io.File file)
          Get URL for a File.
static java.net.URL[] getFileURLs(java.io.File[] files)
           
static java.lang.String[] getPaths(java.io.File[] files)
           
static java.lang.String[] getPaths(java.util.List<java.io.File> files)
           
static java.io.InputStream getStreamFromZip(java.lang.String zipFile, java.lang.String name)
           
static java.io.File getTempDir(java.lang.String name)
          Make a new temporary directory in the same directory that the system uses for temporary files, or if that files, in the current directory.
static boolean hasSourceSuffix(java.io.File file)
           
static boolean hasSourceSuffix(java.lang.String path)
           
static boolean isZipFile(java.io.File file)
           
static java.util.List<java.lang.String> lineSeek(java.lang.String sought, java.util.List<java.lang.String> sources, boolean listAll, java.io.PrintStream errorSink)
          Do line-based search for literal text in source files, returning file:line where found.
static java.lang.String lineSeek(java.lang.String sought, java.lang.String sourcePath, boolean listAll, java.util.ArrayList<java.lang.String> sink)
          Do line-based search for literal text in source file, returning line where found as a String in the form {sourcePath}:line:column submitted to the collecting parameter sink.
static java.util.List<java.io.File> listClassFiles(java.io.File dir)
          Recursively list .class files in specified directory
static java.lang.String[] listFiles(java.io.File srcDir)
          Recursively list files in srcDir.
static java.io.File[] listFiles(java.io.File srcDir, java.io.FileFilter fileFilter)
          Recursively list files in srcDir.
static java.util.List<java.lang.String> makeClasspath(java.net.URL[] urls)
           
static java.io.File makeNewChildDir(java.io.File parent, java.lang.String child)
          Make a new child directory of parent
static java.io.BufferedOutputStream makeOutputStream(java.io.File file)
           
static java.lang.String normalizedPath(java.io.File file)
          Normalize path for comparisons by rendering absolute trimming and changing '\\' to '/'
static java.lang.String normalizedPath(java.io.File file, java.io.File basedir)
          Normalize path for comparisons by rendering absolute, clipping basedir prefix, trimming and changing '\\' to '/'
static byte[] readAsByteArray(java.io.File file)
          Returns the contents of this file as a byte[]
static byte[] readAsByteArray(java.io.InputStream inStream)
          Reads this input stream and returns contents as a byte[]
static java.lang.String readAsString(java.io.File file)
          Returns the contents of this file as a String
static boolean[] readBooleanArray(java.io.DataInputStream s)
          Reads a boolean array with our encoding
static int[] readIntArray(java.io.DataInputStream s)
          Reads an int array with our encoding
static java.lang.String[] readStringArray(java.io.DataInputStream s)
          Reads an int array with our encoding
static boolean sleepPastFinalModifiedTime(java.io.File[] files)
          Sleep until after the last last-modified stamp from the files.
static int sourceSuffixLength(java.io.File file)
           
static int sourceSuffixLength(java.lang.String path)
           
static void throwIaxUnlessCanReadDir(java.io.File dir, java.lang.String label)
           
static void throwIaxUnlessCanWriteDir(java.io.File dir, java.lang.String label)
           
static void throwIaxUnlessCanWriteFile(java.io.File file, java.lang.String label)
           
static java.lang.String weakNormalize(java.lang.String path)
          Weakly normalize path for comparisons by trimming and changing '\\' to '/'
static java.lang.String writeAsString(java.io.File file, java.lang.String contents)
          Write contents to file, returning null on success or error message otherwise.
static void writeBooleanArray(boolean[] a, java.io.DataOutputStream s)
          Writes a boolean array with our encoding
static void writeIntArray(int[] a, java.io.DataOutputStream s)
          Writes an int array with our encoding
static void writeStringArray(java.lang.String[] a, java.io.DataOutputStream s)
          Writes an int array with our encoding
static int zipSuffixLength(java.io.File file)
           
static int zipSuffixLength(java.lang.String path)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_PARENT

public static final java.io.File DEFAULT_PARENT
default parent directory File when a file has a null parent


SOURCE_SUFFIXES

public static final java.util.List<java.lang.String> SOURCE_SUFFIXES
unmodifiable List of String source file suffixes (including leading ".")


ZIP_FILTER

public static final java.io.FileFilter ZIP_FILTER

ALL

public static final java.io.FileFilter ALL
accept all files


DIRS_AND_WRITABLE_CLASSES

public static final java.io.FileFilter DIRS_AND_WRITABLE_CLASSES

aspectjSourceFileFilter

public static final java.io.FileFilter aspectjSourceFileFilter
Method Detail

isZipFile

public static boolean isZipFile(java.io.File file)
Returns:
true if file exists and is a zip file

zipSuffixLength

public static int zipSuffixLength(java.io.File file)
Returns:
0 if file has no zip/jar suffix or 4 otherwise

zipSuffixLength

public static int zipSuffixLength(java.lang.String path)
Returns:
0 if no zip/jar suffix or 4 otherwise

hasSourceSuffix

public static boolean hasSourceSuffix(java.io.File file)
Returns:
true if file path has a source suffix

hasSourceSuffix

public static boolean hasSourceSuffix(java.lang.String path)
Returns:
true if path ends with .java or .aj

sourceSuffixLength

public static int sourceSuffixLength(java.io.File file)
Returns:
0 if file has no source suffix or the length of the suffix otherwise

sourceSuffixLength

public static int sourceSuffixLength(java.lang.String path)
Returns:
0 if no source suffix or the length of the suffix otherwise

canReadDir

public static boolean canReadDir(java.io.File dir)
Returns:
true if this is a readable directory

canReadFile

public static boolean canReadFile(java.io.File file)
Returns:
true if this is a readable file

canWriteDir

public static boolean canWriteDir(java.io.File dir)
Returns:
true if dir is a writable directory

canWriteFile

public static boolean canWriteFile(java.io.File file)
Returns:
true if this is a writable file

throwIaxUnlessCanReadDir

public static void throwIaxUnlessCanReadDir(java.io.File dir,
                                            java.lang.String label)
Throws:
java.lang.IllegalArgumentException - unless dir is a readable directory

throwIaxUnlessCanWriteFile

public static void throwIaxUnlessCanWriteFile(java.io.File file,
                                              java.lang.String label)
Throws:
java.lang.IllegalArgumentException - unless file is readable and not a directory

throwIaxUnlessCanWriteDir

public static void throwIaxUnlessCanWriteDir(java.io.File dir,
                                             java.lang.String label)
Throws:
java.lang.IllegalArgumentException - unless dir is a readable directory

getPaths

public static java.lang.String[] getPaths(java.io.File[] files)
Returns:
array same length as input, with String paths

getPaths

public static java.lang.String[] getPaths(java.util.List<java.io.File> files)
Returns:
array same length as input, with String paths

fileToClassName

public static java.lang.String fileToClassName(java.io.File basedir,
                                               java.io.File classFile)
Extract the name of a class from the path to its file. If the basedir is null, then the class is assumed to be in the default package unless the classFile has one of the top-level suffixes { com, org, java, javax } as a parent directory.

Parameters:
basedir - the File of the base directory (prefix of classFile)
classFile - the File of the class to extract the name for
Throws:
java.lang.IllegalArgumentException - if classFile is null or does not end with ".class" or a non-null basedir is not a prefix of classFile

normalizedPath

public static java.lang.String normalizedPath(java.io.File file,
                                              java.io.File basedir)
Normalize path for comparisons by rendering absolute, clipping basedir prefix, trimming and changing '\\' to '/'

Parameters:
file - the File with the path to normalize
basedir - the File for the prefix of the file to normalize - ignored if null
Returns:
"" if null or normalized path otherwise
Throws:
java.lang.IllegalArgumentException - if basedir is not a prefix of file

flatten

public static java.lang.String flatten(java.io.File[] files,
                                       java.lang.String infix)
Render a set of files to String as a path by getting absolute paths of each and delimiting with infix.

Parameters:
files - the File[] to flatten - may be null or empty
infix - the String delimiter internally between entries (if null, then use File.pathSeparator). (alias to flatten(getAbsolutePaths(files), infix)
Returns:
String with absolute paths to entries in order, delimited with infix

flatten

public static java.lang.String flatten(java.lang.String[] paths,
                                       java.lang.String infix)
Flatten File[] to String.

Parameters:
files - the File[] of paths to flatten - null ignored
infix - the String infix to use - null treated as File.pathSeparator

normalizedPath

public static java.lang.String normalizedPath(java.io.File file)
Normalize path for comparisons by rendering absolute trimming and changing '\\' to '/'

Returns:
"" if null or normalized path otherwise

weakNormalize

public static java.lang.String weakNormalize(java.lang.String path)
Weakly normalize path for comparisons by trimming and changing '\\' to '/'


getBestFile

public static java.io.File getBestFile(java.lang.String[] paths)
Get best File for the first-readable path in input paths, treating entries prefixed "sp:" as system property keys. Safe to call in static initializers.

Parameters:
paths - the String[] of paths to check.
Returns:
null if not found, or valid File otherwise

getBestFile

public static java.io.File getBestFile(java.io.File file)
Render as best file, canonical or absolute.

Parameters:
file - the File to get the best File for (not null)
Returns:
File of the best-available path
Throws:
java.lang.IllegalArgumentException - if file is null

getBestPath

public static java.lang.String getBestPath(java.io.File file)
Render as best path, canonical or absolute.

Parameters:
file - the File to get the path for (not null)
Returns:
String of the best-available path
Throws:
java.lang.IllegalArgumentException - if file is null

getAbsolutePaths

public static java.lang.String[] getAbsolutePaths(java.io.File[] files)
Returns:
array same length as input, with String absolute paths

deleteContents

public static int deleteContents(java.io.File dir)
Recursively delete the contents of dir, but not the dir itself

Returns:
the total number of files deleted

deleteContents

public static int deleteContents(java.io.File dir,
                                 java.io.FileFilter filter)
Recursively delete some contents of dir, but not the dir itself. This deletes any subdirectory which is empty after its files are deleted.

Returns:
the total number of files deleted

deleteContents

public static int deleteContents(java.io.File dir,
                                 java.io.FileFilter filter,
                                 boolean deleteEmptyDirs)
Recursively delete some contents of dir, but not the dir itself. If deleteEmptyDirs is true, this deletes any subdirectory which is empty after its files are deleted.

Parameters:
dir - the File directory (if a file, the the file is deleted)
Returns:
the total number of files deleted

copyDir

public static int copyDir(java.io.File fromDir,
                          java.io.File toDir)
                   throws java.io.IOException
Copy contents of fromDir into toDir

Parameters:
fromDir - must exist and be readable
toDir - must exist or be creatable and be writable
Returns:
the total number of files copied
Throws:
java.io.IOException

copyDir

public static int copyDir(java.io.File fromDir,
                          java.io.File toDir,
                          java.lang.String fromSuffix,
                          java.lang.String toSuffix)
                   throws java.io.IOException
Recursively copy files in fromDir (with any fromSuffix) to toDir, replacing fromSuffix with toSuffix if any. This silently ignores dirs and files that are not readable but throw IOException for directories that are not writable. This does not clean out the original contents of toDir. (subdirectories are not renamed per directory rules)

Parameters:
fromSuffix - select files with this suffix - select all if null or empty
toSuffix - replace fromSuffix with toSuffix in the destination file name - ignored if null or empty, appended to name if fromSuffix is null or empty
Returns:
the total number of files copied
Throws:
java.io.IOException

copyDir

public static int copyDir(java.io.File fromDir,
                          java.io.File toDir,
                          java.lang.String fromSuffix,
                          java.lang.String toSuffix,
                          java.io.FileFilter delegate)
                   throws java.io.IOException
Recursively copy files in fromDir (with any fromSuffix) to toDir, replacing fromSuffix with toSuffix if any. This silently ignores dirs and files that are not readable but throw IOException for directories that are not writable. This does not clean out the original contents of toDir. (subdirectories are not renamed per directory rules) This calls any delegate FilenameFilter to collect any selected file.

Parameters:
fromSuffix - select files with this suffix - select all if null or empty
toSuffix - replace fromSuffix with toSuffix in the destination file name - ignored if null or empty, appended to name if fromSuffix is null or empty
Returns:
the total number of files copied
Throws:
java.io.IOException

listFiles

public static java.lang.String[] listFiles(java.io.File srcDir)
Recursively list files in srcDir.

Returns:
ArrayList with String paths of File under srcDir (relative to srcDir)

listFiles

public static java.io.File[] listFiles(java.io.File srcDir,
                                       java.io.FileFilter fileFilter)
Recursively list files in srcDir.

Returns:
ArrayList with String paths of File under srcDir (relative to srcDir)

listClassFiles

public static java.util.List<java.io.File> listClassFiles(java.io.File dir)
Recursively list .class files in specified directory

Returns:
List of File objects

getBaseDirFiles

public static java.io.File[] getBaseDirFiles(java.io.File basedir,
                                             java.lang.String[] paths)
Convert String[] paths to File[] as offset of base directory

Parameters:
basedir - the non-null File base directory for File to create with paths
paths - the String[] of paths to create
Returns:
File[] with same length as paths

getBaseDirFiles

public static java.io.File[] getBaseDirFiles(java.io.File basedir,
                                             java.lang.String[] paths,
                                             java.lang.String[] suffixes)
Convert String[] paths to File[] as offset of base directory

Parameters:
basedir - the non-null File base directory for File to create with paths
paths - the String[] of paths to create
suffixes - the String[] of suffixes to limit sources to - ignored if null
Returns:
File[] with same length as paths

copyFiles

public static java.io.File[] copyFiles(java.io.File srcDir,
                                       java.lang.String[] relativePaths,
                                       java.io.File destDir)
                                throws java.lang.IllegalArgumentException,
                                       java.io.IOException
Copy files from source dir into destination directory, creating any needed directories. This differs from copyDir in not being recursive; each input with the source dir creates a full path. However, if the source is a directory, it is copied as such.

Parameters:
srcDir - an existing, readable directory containing relativePaths files
relativePaths - a set of paths relative to srcDir to readable File to copy
destDir - an existing, writable directory to copy files to
Throws:
java.lang.IllegalArgumentException - if input invalid, IOException if operations fail
java.io.IOException

copyFile

public static void copyFile(java.io.File fromFile,
                            java.io.File toFile)
                     throws java.io.IOException
Copy fromFile to toFile, handling file-file, dir-dir, and file-dir copies.

Parameters:
fromFile - the File path of the file or directory to copy - must be readable
toFile - the File path of the target file or directory - must be writable (will be created if it does not exist)
Throws:
java.io.IOException

ensureParentWritable

public static java.io.File ensureParentWritable(java.io.File path)
Ensure that the parent directory to path can be written. If the path has a null parent, DEFAULT_PARENT is tested. If the path parent does not exist, this tries to create it.

Parameters:
path - the File path whose parent should be writable
Returns:
the File path of the writable parent directory
Throws:
java.lang.IllegalArgumentException - if parent cannot be written or path is null.

copyValidFiles

public static void copyValidFiles(java.io.File fromFile,
                                  java.io.File toFile)
                           throws java.io.IOException
Copy file to file.

Parameters:
fromFile - the File to copy (readable, non-null file)
toFile - the File to copy to (non-null, parent dir exists)
Throws:
java.io.IOException

copyStream

public static void copyStream(java.io.DataInputStream in,
                              java.io.PrintStream out)
                       throws java.io.IOException
do line-based copying

Throws:
java.io.IOException

copyStream

public static void copyStream(java.io.InputStream in,
                              java.io.OutputStream out)
                       throws java.io.IOException
Throws:
java.io.IOException

copyStream

public static void copyStream(java.io.Reader in,
                              java.io.Writer out)
                       throws java.io.IOException
Throws:
java.io.IOException

makeNewChildDir

public static java.io.File makeNewChildDir(java.io.File parent,
                                           java.lang.String child)
Make a new child directory of parent

Parameters:
parent - a File for the parent (writable)
child - a prefix for the child directory
Returns:
a File dir that exists with parentDir as the parent file or null

getTempDir

public static java.io.File getTempDir(java.lang.String name)
Make a new temporary directory in the same directory that the system uses for temporary files, or if that files, in the current directory.

Parameters:
name - the preferred (simple) name of the directory - may be null.
Returns:
File of an existing new temp dir, or null if unable to create

getFileURLs

public static java.net.URL[] getFileURLs(java.io.File[] files)

getFileURL

public static java.net.URL getFileURL(java.io.File file)
Get URL for a File. This appends "/" for directories. prints errors to System.err

Parameters:
file - the File to convert to URL (not null)

writeAsString

public static java.lang.String writeAsString(java.io.File file,
                                             java.lang.String contents)
Write contents to file, returning null on success or error message otherwise. This tries to make any necessary parent directories first.

Parameters:
file - the File to write (not null)
contents - the String to write (use "" if null)
Returns:
String null on no error, error otherwise

readBooleanArray

public static boolean[] readBooleanArray(java.io.DataInputStream s)
                                  throws java.io.IOException
Reads a boolean array with our encoding

Throws:
java.io.IOException

writeBooleanArray

public static void writeBooleanArray(boolean[] a,
                                     java.io.DataOutputStream s)
                              throws java.io.IOException
Writes a boolean array with our encoding

Throws:
java.io.IOException

readIntArray

public static int[] readIntArray(java.io.DataInputStream s)
                          throws java.io.IOException
Reads an int array with our encoding

Throws:
java.io.IOException

writeIntArray

public static void writeIntArray(int[] a,
                                 java.io.DataOutputStream s)
                          throws java.io.IOException
Writes an int array with our encoding

Throws:
java.io.IOException

readStringArray

public static java.lang.String[] readStringArray(java.io.DataInputStream s)
                                          throws java.io.IOException
Reads an int array with our encoding

Throws:
java.io.IOException

writeStringArray

public static void writeStringArray(java.lang.String[] a,
                                    java.io.DataOutputStream s)
                             throws java.io.IOException
Writes an int array with our encoding

Throws:
java.io.IOException

readAsString

public static java.lang.String readAsString(java.io.File file)
                                     throws java.io.IOException
Returns the contents of this file as a String

Throws:
java.io.IOException

readAsByteArray

public static byte[] readAsByteArray(java.io.File file)
                              throws java.io.IOException
Returns the contents of this file as a byte[]

Throws:
java.io.IOException

readAsByteArray

public static byte[] readAsByteArray(java.io.InputStream inStream)
                              throws java.io.IOException
Reads this input stream and returns contents as a byte[]

Throws:
java.io.IOException

getStreamFromZip

public static java.io.InputStream getStreamFromZip(java.lang.String zipFile,
                                                   java.lang.String name)

lineSeek

public static java.util.List<java.lang.String> lineSeek(java.lang.String sought,
                                                        java.util.List<java.lang.String> sources,
                                                        boolean listAll,
                                                        java.io.PrintStream errorSink)
Do line-based search for literal text in source files, returning file:line where found.

Parameters:
sought - the String text to seek in the file
sources - the List of String paths to the source files
listAll - if false, only list first match in file
errorSink - the PrintStream to print any errors to (one per line) (use null to silently ignore errors)
Returns:
List of String of the form file:line for each found entry (never null, might be empty)

lineSeek

public static java.lang.String lineSeek(java.lang.String sought,
                                        java.lang.String sourcePath,
                                        boolean listAll,
                                        java.util.ArrayList<java.lang.String> sink)
Do line-based search for literal text in source file, returning line where found as a String in the form {sourcePath}:line:column submitted to the collecting parameter sink. Any error is rendered to String and returned as the result.

Parameters:
sought - the String text to seek in the file
sources - the List of String paths to the source files
listAll - if false, only list first match in file
List - sink the List for String entries of the form {sourcePath}:line:column
Returns:
String error if any, or add String entries to sink

makeOutputStream

public static java.io.BufferedOutputStream makeOutputStream(java.io.File file)
                                                     throws java.io.FileNotFoundException
Throws:
java.io.FileNotFoundException

sleepPastFinalModifiedTime

public static boolean sleepPastFinalModifiedTime(java.io.File[] files)
Sleep until after the last last-modified stamp from the files.

Parameters:
files - the File[] of files to inspect for last modified times (this ignores null or empty files array and null or non-existing components of files array)
Returns:
true if succeeded without 100 interrupts

makeClasspath

public static java.util.List<java.lang.String> makeClasspath(java.net.URL[] urls)