Package org.aspectj.util
Class FileUtil
java.lang.Object
org.aspectj.util.FileUtil
- Author:
- Andy Clement, Kris De Volder
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic class
A pipe when run reads from an input stream to an output stream, optionally sleeping between reads. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic FileFilter
accept all filesstatic FileFilter
static File
default parent directory File when a file has a null parentstatic FileFilter
unmodifiable List of String source file suffixes (including leading ".")static FileFilter
-
Method Summary
Modifier and TypeMethodDescriptionstatic boolean
canReadDir(File dir)
static boolean
canReadFile(File file)
static boolean
canWriteDir(File dir)
static boolean
canWriteFile(File file)
static int
Copy contents of fromDir into toDirstatic int
Recursively copy files in fromDir (with any fromSuffix) to toDir, replacing fromSuffix with toSuffix if any.static int
Recursively copy files in fromDir (with any fromSuffix) to toDir, replacing fromSuffix with toSuffix if any.static void
Copy fromFile to toFile, handling file-file, dir-dir, and file-dir copies.static File[]
Copy files from source dir into destination directory, creating any needed directories.static void
copyStream(DataInputStream in, PrintStream out)
do line-based copyingstatic void
copyStream(InputStream in, OutputStream out)
static void
copyStream(Reader in, Writer out)
static void
copyValidFiles(File fromFile, File toFile)
Copy file to file.static int
deleteContents(File dir)
Recursively delete the contents of dir, but not the dir itselfstatic int
deleteContents(File dir, FileFilter filter)
Recursively delete some contents of dir, but not the dir itself.static int
deleteContents(File dir, FileFilter filter, boolean deleteEmptyDirs)
Recursively delete some contents of dir, but not the dir itself.static File
ensureParentWritable(File path)
Ensure that the parent directory to path can be written.static String
fileToClassName(File basedir, File classFile)
Extract the name of a class from the path to its file.static String
Render a set of files to String as a path by getting absolute paths of each and delimiting with infix.static String
Flatten File[] to String.static String[]
getAbsolutePaths(File[] files)
static File[]
getBaseDirFiles(File basedir, String[] paths)
Convert String[] paths to File[] as offset of base directorystatic File[]
getBaseDirFiles(File basedir, String[] paths, String[] suffixes)
Convert String[] paths to File[] as offset of base directorystatic File
getBestFile(File file)
Render as best file, canonical or absolute.static File
getBestFile(String[] paths)
Get best File for the first-readable path in input paths, treating entries prefixed "sp:" as system property keys.static File
getBestFile(String[] paths, boolean mustBeJar)
static String
getBestPath(File file)
Render as best path, canonical or absolute.static URL
getFileURL(File file)
Get URL for a File.static URL[]
getFileURLs(File[] files)
static String[]
static String[]
static InputStream
getStreamFromZip(String zipFile, String name)
static File
getTempDir(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(File file)
static boolean
hasSourceSuffix(String path)
static boolean
static String
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.lineSeek(String sought, List<String> sources, boolean listAll, PrintStream errorSink)
Do line-based search for literal text in source files, returning file:line where found.listClassFiles(File dir)
Recursively list .class files in specified directorystatic String[]
Recursively list files in srcDir.static File[]
listFiles(File srcDir, FileFilter fileFilter)
Recursively list files in srcDir.makeClasspath(URL[] urls)
static File
makeNewChildDir(File parent, String child)
Make a new child directory of parentstatic BufferedOutputStream
makeOutputStream(File file)
static String
normalizedPath(File file)
Normalize path for comparisons by rendering absolute trimming and changing '\\' to '/'static String
normalizedPath(File file, File basedir)
Normalize path for comparisons by rendering absolute, clipping basedir prefix, trimming and changing '\\' to '/'static byte[]
readAsByteArray(File file)
Returns the contents of this file as a byte[]static byte[]
readAsByteArray(InputStream inStream)
Reads this input stream and returns contents as a byte[]readAsLines(File file)
static String
readAsString(File file)
Returns the contents of this file as a Stringstatic boolean[]
Reads a boolean array with our encodingstatic int[]
Reads an int array with our encodingstatic String[]
Reads an int array with our encodingstatic boolean
sleepPastFinalModifiedTime(File[] files)
Sleep until after the last last-modified stamp from the files.static int
sourceSuffixLength(File file)
static int
sourceSuffixLength(String path)
static void
throwIaxUnlessCanReadDir(File dir, String label)
static void
throwIaxUnlessCanWriteDir(File dir, String label)
static void
throwIaxUnlessCanWriteFile(File file, String label)
static String
weakNormalize(String path)
Weakly normalize path for comparisons by trimming and changing '\\' to '/'static String
writeAsString(File file, String contents)
Write contents to file, returning null on success or error message otherwise.static void
writeBooleanArray(boolean[] a, DataOutputStream s)
Writes a boolean array with our encodingstatic void
writeIntArray(int[] a, DataOutputStream s)
Writes an int array with our encodingstatic void
writeStringArray(String[] a, DataOutputStream s)
Writes an int array with our encodingstatic int
zipSuffixLength(File file)
static int
zipSuffixLength(String path)
-
Field Details
-
DEFAULT_PARENT
default parent directory File when a file has a null parent -
SOURCE_SUFFIXES
unmodifiable List of String source file suffixes (including leading ".") -
ZIP_FILTER
-
ALL
accept all files -
DIRS_AND_WRITABLE_CLASSES
-
aspectjSourceFileFilter
-
-
Method Details
-
isZipFile
- Returns:
- true if file exists and is a zip file
-
zipSuffixLength
- Returns:
- 0 if file has no zip/jar suffix or 4 otherwise
-
zipSuffixLength
- Returns:
- 0 if no zip/jar suffix or 4 otherwise
-
hasSourceSuffix
- Returns:
- true if file path has a source suffix
-
hasSourceSuffix
- Returns:
- true if path ends with .java or .aj
-
sourceSuffixLength
- Returns:
- 0 if file has no source suffix or the length of the suffix otherwise
-
sourceSuffixLength
- Returns:
- 0 if no source suffix or the length of the suffix otherwise
-
canReadDir
- Returns:
- true if this is a readable directory
-
canReadFile
- Returns:
- true if this is a readable file
-
canWriteDir
- Returns:
- true if dir is a writable directory
-
canWriteFile
- Returns:
- true if this is a writable file
-
throwIaxUnlessCanReadDir
- Throws:
IllegalArgumentException
- unless dir is a readable directory
-
throwIaxUnlessCanWriteFile
- Throws:
IllegalArgumentException
- unless file is readable and not a directory
-
throwIaxUnlessCanWriteDir
- Throws:
IllegalArgumentException
- unless dir is a readable directory
-
getPaths
- Returns:
- array same length as input, with String paths
-
getPaths
- Returns:
- array same length as input, with String paths
-
fileToClassName
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:
IllegalArgumentException
- if classFile is null or does not end with ".class" or a non-null basedir is not a prefix of classFile
-
normalizedPath
Normalize path for comparisons by rendering absolute, clipping basedir prefix, trimming and changing '\\' to '/'- Parameters:
file
- the File with the path to normalizebasedir
- the File for the prefix of the file to normalize - ignored if null- Returns:
- "" if null or normalized path otherwise
- Throws:
IllegalArgumentException
- if basedir is not a prefix of file
-
flatten
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 emptyinfix
- the String delimiter internally between entries (if null, then use File.pathSeparator). (alias toflatten(getAbsolutePaths(files), infix)
- Returns:
- String with absolute paths to entries in order, delimited with infix
-
flatten
Flatten File[] to String.- Parameters:
paths
- the String[] of paths to flatten - null ignoredinfix
- the String infix to use - null treated as File.pathSeparator
-
normalizedPath
Normalize path for comparisons by rendering absolute trimming and changing '\\' to '/'- Returns:
- "" if null or normalized path otherwise
-
weakNormalize
Weakly normalize path for comparisons by trimming and changing '\\' to '/' -
getBestFile
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
-
getBestFile
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:
IllegalArgumentException
- if file is null
-
getBestPath
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:
IllegalArgumentException
- if file is null
-
getAbsolutePaths
- Returns:
- array same length as input, with String absolute paths
-
deleteContents
Recursively delete the contents of dir, but not the dir itself- Returns:
- the total number of files deleted
-
deleteContents
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
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
Copy contents of fromDir into toDir- Parameters:
fromDir
- must exist and be readabletoDir
- must exist or be creatable and be writable- Returns:
- the total number of files copied
- Throws:
IOException
-
copyDir
public static int copyDir(File fromDir, File toDir, String fromSuffix, String toSuffix) throws IOExceptionRecursively 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 emptytoSuffix
- 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:
IOException
-
copyDir
public static int copyDir(File fromDir, File toDir, String fromSuffix, String toSuffix, FileFilter delegate) throws IOExceptionRecursively 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 emptytoSuffix
- 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:
IOException
-
listFiles
Recursively list files in srcDir.- Returns:
- ArrayList with String paths of File under srcDir (relative to srcDir)
-
listFiles
Recursively list files in srcDir.- Returns:
- ArrayList with String paths of File under srcDir (relative to srcDir)
-
listClassFiles
Recursively list .class files in specified directory- Returns:
- List of File objects
-
getBaseDirFiles
Convert String[] paths to File[] as offset of base directory- Parameters:
basedir
- the non-null File base directory for File to create with pathspaths
- the String[] of paths to create- Returns:
- File[] with same length as paths
-
getBaseDirFiles
Convert String[] paths to File[] as offset of base directory- Parameters:
basedir
- the non-null File base directory for File to create with pathspaths
- the String[] of paths to createsuffixes
- the String[] of suffixes to limit sources to - ignored if null- Returns:
- File[] with same length as paths
-
copyFiles
public static File[] copyFiles(File srcDir, String[] relativePaths, File destDir) throws IllegalArgumentException, IOExceptionCopy 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 filesrelativePaths
- a set of paths relative to srcDir to readable File to copydestDir
- an existing, writable directory to copy files to- Throws:
IllegalArgumentException
- if input invalid, IOException if operations failIOException
-
copyFile
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 readabletoFile
- the File path of the target file or directory - must be writable (will be created if it does not exist)- Throws:
IOException
-
ensureParentWritable
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:
IllegalArgumentException
- if parent cannot be written or path is null.
-
copyValidFiles
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:
IOException
-
copyStream
do line-based copying- Throws:
IOException
-
copyStream
- Throws:
IOException
-
copyStream
- Throws:
IOException
-
makeNewChildDir
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
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
-
getFileURL
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
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
Reads a boolean array with our encoding- Throws:
IOException
-
writeBooleanArray
Writes a boolean array with our encoding- Throws:
IOException
-
readIntArray
Reads an int array with our encoding- Throws:
IOException
-
writeIntArray
Writes an int array with our encoding- Throws:
IOException
-
readStringArray
Reads an int array with our encoding- Throws:
IOException
-
writeStringArray
Writes an int array with our encoding- Throws:
IOException
-
readAsString
Returns the contents of this file as a String- Throws:
IOException
-
readAsLines
-
readAsByteArray
Returns the contents of this file as a byte[]- Throws:
IOException
-
readAsByteArray
Reads this input stream and returns contents as a byte[]- Throws:
IOException
-
getStreamFromZip
-
lineSeek
public static List<String> lineSeek(String sought, List<String> sources, boolean listAll, 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 filesources
- the List of String paths to the source fileslistAll
- if false, only list first match in fileerrorSink
- 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 String lineSeek(String sought, String sourcePath, boolean listAll, List<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 filesourcePath
- the String of paths to the source fileslistAll
- if false, only list first match in filesink
- the List of String entries of the form {sourcePath}:line:column- Returns:
- String error if any, or add String entries to sink
-
makeOutputStream
- Throws:
FileNotFoundException
-
sleepPastFinalModifiedTime
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
-