|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectedu.rice.cs.plt.io.IOUtil
public final class IOUtil
Provides additional operations on Files, InputStreams, OutputStreams,
Readers, and Writers not defined in the java.io package.
| Field Summary | |
|---|---|
static FilePredicate |
ALWAYS_ACCEPT
A FilePredicate that always accepts. |
static FilePredicate |
ALWAYS_REJECT
A FilePredicate that always rejects. |
static Lambda<String,File> |
FILE_FACTORY
A factory for Files based on a String filename. |
static FilePredicate |
IS_DIRECTORY
A predicate that tests whether attemptIsDirectory(java.io.File) holds for a file. |
static FilePredicate |
IS_FILE
A predicate that tests whether attemptIsFile(java.io.File) holds for a file. |
static File |
WORKING_DIRECTORY
The current working directory, used as the base for relative paths. |
| Method Summary | |
|---|---|
static int |
adler32Hash(File file)
Produce an Adler-32 hash for the given file. |
static int |
adler32Hash(InputStream stream)
Produce an Adler-32 hash for the contents of the given stream. |
static FilePredicate |
and(FileFilter... filters)
Produce a conjunction of the given FileFilters. |
static FilePredicate |
and(Iterable<? extends FileFilter> filters)
Produce a conjunction of the given FileFilters. |
static BufferedInputStream |
asBuffered(InputStream in)
If in is a BufferedInputStream, cast it as such; otherwise, wrap it in a
BufferedInputStream. |
static BufferedOutputStream |
asBuffered(OutputStream out)
If out is a BufferedOutputStream, cast it as such; otherwise, wrap it in a
BufferedOutputStream. |
static BufferedReader |
asBuffered(Reader r)
If r is a BufferedReader, cast it as such; otherwise, wrap it in a BufferedReader. |
static BufferedWriter |
asBuffered(Writer w)
If w is a BufferedWriter, cast it as such; otherwise, wrap it in a BufferedWriter. |
static FilePredicate |
asFilePredicate(FileFilter filter)
Define a FilePredicate in terms of a FileFilter (this provides access
to predicate operations like and and or). |
static FilePredicate |
asFilePredicate(Predicate<? super File> p)
Define a FileFilter in terms of a Predicate. |
static File |
attemptAbsoluteFile(File f)
Make a best attempt at evaluating File.getAbsoluteFile(). |
static SizedIterable<File> |
attemptAbsoluteFiles(Iterable<? extends File> files)
Apply attemptAbsoluteFile(java.io.File) to all files in a list |
static File |
attemptCanonicalFile(File f)
Make a best attempt at evaluating File.getCanonicalFile(). |
static SizedIterable<File> |
attemptCanonicalFiles(Iterable<? extends File> files)
Apply attemptCanonicalFile(java.io.File) to all files in a list |
static boolean |
attemptCanRead(File f)
Make a best attempt at evaluating File.canRead(). |
static boolean |
attemptCanWrite(File f)
Make a best attempt at evaluating File.canWrite(). |
static void |
attemptClose(Closeable c)
Attempt to close the given resource, failing silently if an exception occurs. |
static boolean |
attemptCreateNewFile(File f)
Make a best attempt at invoking File.createNewFile(). |
static boolean |
attemptDelete(File f)
Make a best attempt at invoking File.delete(). |
static void |
attemptDeleteOnExit(File f)
Make a best attempt at invoking File.deleteOnExit(). |
static boolean |
attemptExists(File f)
Make a best attempt at evaluating File.exists(). |
static boolean |
attemptIsDirectory(File f)
Make a best attempt at evaluating File.isDirectory(). |
static boolean |
attemptIsFile(File f)
Make a best attempt at evaluating File.isFile(). |
static boolean |
attemptIsHidden(File f)
Make a best attempt at evaluating File.isHidden(). |
static long |
attemptLastModified(File f)
Make a best attempt at evaluating File.lastModified(). |
static long |
attemptLength(File f)
Make a best attempt at evaluating File.length(). |
static File[] |
attemptListFiles(File f)
Make a best attempt at invoking File.listFiles(). |
static File[] |
attemptListFiles(File f,
FileFilter filter)
Make a best attempt at invoking File.listFiles(FileFilter). |
static File[] |
attemptListFiles(File f,
FilePredicate filter)
Make a best attempt at invoking File.listFiles(FileFilter). |
static File[] |
attemptListFiles(File f,
Predicate<? super File> filter)
Make a best attempt at invoking File.listFiles(FileFilter). |
static SizedIterable<File> |
attemptListFilesAsIterable(File f)
Similar to attemptListFiles(File), but returns a non-null Iterable
rather than an array. |
static SizedIterable<File> |
attemptListFilesAsIterable(File f,
FileFilter filter)
Similar to attemptListFiles(File, FileFilter), but returns a non-null Iterable
rather than an array. |
static SizedIterable<File> |
attemptListFilesAsIterable(File f,
FilePredicate filter)
Similar to attemptListFiles(File, FileFilter), but returns a non-null Iterable
rather than an array. |
static SizedIterable<File> |
attemptListFilesAsIterable(File f,
Predicate<? super File> filter)
Similar to attemptListFiles(File, FileFilter), but returns a non-null Iterable
rather than an array. |
static boolean |
attemptMkdir(File f)
Make a best attempt at invoking File.mkdir(). |
static boolean |
attemptMkdirs(File f)
Make a best attempt at invoking File.mkdirs(). |
static boolean |
attemptMove(File f,
File dest)
Like attemptRenameTo(java.io.File, java.io.File), makes a best attempt at renaming f. |
static boolean |
attemptRenameTo(File f,
File dest)
Make a best attempt at invoking File.renameTo(java.io.File). |
static boolean |
attemptSetLastModified(File f,
long time)
Make a best attempt at invoking File.setLastModified(long). |
static boolean |
attemptSetReadOnly(File f)
Make a best attempt at invoking File.setReadOnly(). |
static boolean |
attemptWriteStringToFile(File file,
String text)
Writes text to the file, overwriting whatever was there. |
static boolean |
attemptWriteStringToFile(File file,
String text,
boolean append)
Writes text to the file, ignoring any exceptions that occur. |
static File |
canonicalCase(File f)
Converts a File to all lowercase in case-insensitive systems; otherwise, returns the file
untouched. |
static SizedIterable<File> |
canonicalCases(Iterable<? extends File> files)
Apply canonicalCase(java.io.File) to all files in a list |
static void |
closeOnExit(Closeable c)
Register the given resource to be closed on exit. |
static void |
copyFile(File source,
File dest)
Copies the contents of one file into another. |
static void |
copyFile(File source,
File dest,
byte[] buffer)
Copies the contents of one file into another, using the given array as an intermediate buffer. |
static int |
copyInputStream(InputStream source,
OutputStream dest)
Write the contents of in to out. |
static int |
copyInputStream(InputStream source,
OutputStream dest,
byte[] buffer)
Write the contents of in to out, using the given buffer. |
static int |
copyReader(Reader source,
Writer dest)
Write the contents of in to out. |
static int |
copyReader(Reader source,
Writer dest,
char[] buffer)
Write the contents of in to out, using the given buffer. |
static int |
crc32Hash(File file)
Produce a CRC-32 hash for the given file. |
static int |
crc32Hash(InputStream stream)
Produce a CRC-32 hash for the contents of the given stream. |
static File |
createAndMarkTempDirectory(String prefix,
String suffix)
Create a temporary directory (named by File.createTempFile(java.lang.String, java.lang.String, java.io.File)) and immediately mark it for
deletion. |
static File |
createAndMarkTempDirectory(String prefix,
String suffix,
File location)
Create a temporary directory (named by File.createTempFile(java.lang.String, java.lang.String, java.io.File)) and immediately mark it for
deletion. |
static File |
createAndMarkTempFile(String prefix,
String suffix)
Create a temporary file in the system temp directory (via File.createTempFile(String, String)) and
immediately mark it for deletion |
static File |
createAndMarkTempFile(String prefix,
String suffix,
File location)
Create a temporary file in the specified directory (via File.createTempFile(String, String, File)) and
immediately mark it for deletion |
static void |
deleteOnExitRecursively(File f)
Attempt to mark the given File for deletion. |
static boolean |
deleteRecursively(File f)
Attempt to delete the given File. |
protected static int |
doCopyInputStream(InputStream in,
OutputStream out,
byte[] buffer)
Implementation of stream copying for use by DirectInputStream and DirectOutputStream
(placed here to avoid code duplication). |
protected static int |
doCopyReader(Reader r,
Writer w,
char[] buffer)
Implementation of reader-to-writer copying for use by DirectReader and DirectWriter
(placed here to avoid code duplication). |
protected static int |
doWriteFromInputStream(InputStream in,
OutputStream out,
int bytes,
byte[] buffer)
Implementation of stream-to-stream writing for use by DirectInputStream and
DirectOutputStream (placed here to avoid code duplication). |
protected static int |
doWriteFromReader(Reader r,
Writer w,
int chars,
char[] buffer)
Implementation of reader-to-writer writing for use by DirectReader and DirectWriter
(placed here to avoid code duplication). |
static Exception |
ensureSerializable(Exception e)
Convert the given Exception to a form that will successfully serialize. |
static Iterable<?> |
ensureSerializable(Iterable<?> iter)
Convert the given (non-null) Iterable to a list of objects that will successfully serialize. |
static Object |
ensureSerializable(Object obj)
Converts the given object to a form that will successfully serialize. |
static Object[] |
ensureSerializable(Object[] arr)
Convert the given (non-null) array to an array of objects that will successfully serialize. |
static Octet<?,?,?,?,?,?,?,?> |
ensureSerializable(Octet<?,?,?,?,?,?,?,?> o)
Apply ensureSerializable() to each of the elements of the given tuple. |
static Option<?> |
ensureSerializable(Option<?> opt)
Apply ensureSerializable() to the given option value. |
static Pair<?,?> |
ensureSerializable(Pair<?,?> p)
Apply ensureSerializable() to each of the elements of the given tuple. |
static Quad<?,?,?,?> |
ensureSerializable(Quad<?,?,?,?> q)
Apply ensureSerializable() to each of the elements of the given tuple. |
static Quint<?,?,?,?,?> |
ensureSerializable(Quint<?,?,?,?,?> q)
Apply ensureSerializable() to each of the elements of the given tuple. |
static RuntimeException |
ensureSerializable(RuntimeException e)
Convert the given RuntimeException to a form that will successfully serialize. |
static Septet<?,?,?,?,?,?,?> |
ensureSerializable(Septet<?,?,?,?,?,?,?> s)
Apply ensureSerializable() to each of the elements of the given tuple. |
static Sextet<?,?,?,?,?,?> |
ensureSerializable(Sextet<?,?,?,?,?,?> s)
Apply ensureSerializable() to each of the elements of the given tuple. |
static Throwable |
ensureSerializable(Throwable t)
Convert the given Throwable to a form that will successfully serialize. |
static Triple<?,?,?> |
ensureSerializable(Triple<?,?,?> t)
Apply ensureSerializable() to each of the elements of the given tuple. |
static Tuple |
ensureSerializable(Tuple t)
Apply ensureSerializable() to each of the elements of the given tuple. |
static Wrapper<?> |
ensureSerializable(Wrapper<?> w)
Apply ensureSerializable() to the given wrapped value. |
static FilePredicate |
extensionFilePredicate(String extension)
Define a FilePredicate that accepts file objects with the given extension (that is,
for extension txt, file objects whose canonical-case names (see canonicalCase(java.io.File))
end in .txt). |
static FilePredicate |
fileKey(File f)
Produce a FilePredicate that acts as a "key" representing the current state of
the given file. |
static SizedIterable<File> |
fullPath(File f)
Create a list of files representing the full path of f. |
static SizedIterable<File> |
getAbsoluteFiles(Iterable<? extends File> files)
Apply File.getAbsoluteFile() to all files in a list |
static SizedIterable<File> |
getCanonicalFiles(Iterable<? extends File> files)
Apply File.getCanonicalFile() to all files in a list |
static void |
ignoreSystemErr()
Ignore subsequent writes to System.err until revertSystemErr() is called. |
static void |
ignoreSystemOut()
Ignore subsequent writes to System.out until revertSystemOut() is called. |
static boolean |
isMember(File f,
File ancestor)
Determine if the given file is a member (as determined by File.getParentFile())
of another file. |
static SizedIterable<File> |
listFilesRecursively(File f)
Produce a list of the recursive contents of a file. |
static SizedIterable<File> |
listFilesRecursively(File f,
FileFilter filter)
Produce a list of the recursive contents of a file. |
static SizedIterable<File> |
listFilesRecursively(File f,
FileFilter filter,
FileFilter recursionFilter)
Produce a list of the recursive contents of a file. |
static SizedIterable<File> |
listFilesRecursively(File f,
FilePredicate filter)
Produce a list of the recursive contents of a file. |
static SizedIterable<File> |
listFilesRecursively(File f,
FilePredicate filter,
FilePredicate recursionFilter)
Produce a list of the recursive contents of a file. |
static SizedIterable<File> |
listFilesRecursively(File f,
Predicate<? super File> filter)
Produce a list of the recursive contents of a file. |
static SizedIterable<File> |
listFilesRecursively(File f,
Predicate<? super File> filter,
Predicate<? super File> recursionFilter)
Produce a list of the recursive contents of a file. |
static byte[] |
md5Hash(File file)
Produce an MD5 hash for the given file. |
static byte[] |
md5Hash(InputStream stream)
Produce an MD5 hash for the contents of the given stream. |
static FilePredicate |
negate(FileFilter filter)
Produce the complement of the given FileFilter. |
static FilePredicate |
or(FileFilter... filters)
Produce a disjunction of the given FileFilters. |
static FilePredicate |
or(Iterable<? extends FileFilter> filters)
Produce a disjunction of the given FileFilters. |
static SizedIterable<File> |
parsePath(String path)
Parse a path string -- a list of file names separated by the system-dependent path separator character (':' in Unix, ';' in Windows). |
static String |
pathToString(Iterable<? extends File> path)
Produce a path string from a list of files. |
static Iterator<String> |
readLines(File file)
Get the contents of a file as a sequence of lines, accessed via an iterator. |
static Iterator<String> |
readLines(Reader r)
Get the contents of a reader as a sequence of lines, accessed via an iterator. |
static FilePredicate |
regexCanonicalCaseFilePredicate(String regex)
Define a FilePredicate that accepts files whose (simple) names in the
canonical case (see canonicalCase(java.io.File)) match a regular expression. |
static FilePredicate |
regexFilePredicate(Pattern regex)
Define a FilePredicate that accepts files whose (simple) names match
a regular expression. |
static FilePredicate |
regexFilePredicate(String regex)
Define a FilePredicate that accepts files whose (simple) names match
a regular expression. |
static void |
replaceSystemErr(OutputStream substitute)
Replace System.err with the given stream and remember the current stream. |
static void |
replaceSystemIn(InputStream substitute)
Replace System.in with the given stream and remember the current stream. |
static void |
replaceSystemOut(OutputStream substitute)
Replace System.out with the given stream and remember the current stream. |
static void |
revertSystemErr()
Set System.err to its value before the last call to replaceSystemErr(java.io.OutputStream). |
static void |
revertSystemIn()
Set System.in to its value before the last call to replaceSystemIn(java.io.InputStream). |
static void |
revertSystemOut()
Set System.out to its value before the last call to replaceSystemOut(java.io.OutputStream). |
static FilePredicate |
sameAttributesFilePredicate(File f)
Define a FilePredicate that only accepts a file with the same attributes as
f (at creation time). |
static FilePredicate |
sameContentsFilePredicate(File f)
Define a FilePredicate that only accepts files with contents matching a CRC-32
hash of f (at creation time). |
static FilePredicate |
sameNameFilePredicate(String name)
Define a FilePredicate that only accepts files with the given name (where both names
are converted to the canonical case; see canonicalCase(java.io.File)). |
static FilePredicate |
samePathFilePredicate(File path)
Define a FilePredicate that only accepts files with the given path and name (where
both paths are converted to the canonical case; see canonicalCase(java.io.File)). |
static byte[] |
sha1Hash(File file)
Produce an SHA-1 hash for the given file. |
static byte[] |
sha1Hash(InputStream stream)
Produce an SHA-1 hash for the contents of the given stream. |
static byte[] |
sha256Hash(File file)
Produce an SHA-256 hash for the given file. |
static byte[] |
sha256Hash(InputStream stream)
Produce an SHA-256 hash for the contents of the given stream. |
static byte[] |
toByteArray(File file)
Reads the entire contents of a file and return it as a byte array. |
static byte[] |
toByteArray(InputStream stream)
Create a byte array with the contents of the given stream. |
static String |
toString(File file)
Reads the entire contents of a file and return it as a String. |
static String |
toString(Reader r)
Create a String with the contents of the given Reader. |
static StringBuffer |
toStringBuffer(File file)
Reads the entire contents of a file and return it as a StringBuffer. |
static StringBuffer |
toStringBuffer(Reader r)
Create a StringBuffer with the contents of the given Reader. |
static int |
writeFromInputStream(InputStream source,
OutputStream dest,
int bytes)
Copy the given number of bytes from in to out. |
static int |
writeFromInputStream(InputStream source,
OutputStream dest,
int bytes,
byte[] buffer)
Copy the given number of bytes from in to out, using the given buffer. |
static int |
writeFromReader(Reader source,
Writer dest,
int chars)
Copy the given number of chars from in to out. |
static int |
writeFromReader(Reader source,
Writer dest,
int chars,
char[] buffer)
Copy the given number of chars from in to out, using the given buffer. |
static void |
writeStringToFile(File file,
String text)
Writes text to the file, overwriting whatever was there. |
static void |
writeStringToFile(File file,
String text,
boolean append)
Writes text to the file. |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
|---|
public static final File WORKING_DIRECTORY
user.dir,
if defined, converted to an absolute path.
public static final Lambda<String,File> FILE_FACTORY
public static final FilePredicate IS_FILE
attemptIsFile(java.io.File) holds for a file.
public static final FilePredicate IS_DIRECTORY
attemptIsDirectory(java.io.File) holds for a file.
public static final FilePredicate ALWAYS_ACCEPT
FilePredicate that always accepts.
public static final FilePredicate ALWAYS_REJECT
FilePredicate that always rejects.
| Method Detail |
|---|
public static File attemptAbsoluteFile(File f)
File.getAbsoluteFile(). In the event of a
SecurityException, the result is just f. (Clients cannot
assume, then, that the result is absolute.)
public static SizedIterable<File> getAbsoluteFiles(Iterable<? extends File> files)
File.getAbsoluteFile() to all files in a list
SecurityException - If any of the getAbsoluteFile() invocations triggers
a SecurityExceptionpublic static SizedIterable<File> attemptAbsoluteFiles(Iterable<? extends File> files)
attemptAbsoluteFile(java.io.File) to all files in a list
public static File attemptCanonicalFile(File f)
File.getCanonicalFile(). In the event of an
IOException or a SecurityException, the result is instead the result of
attemptAbsoluteFile(java.io.File). (Clients cannot assume, then, that the result
is canonical.)
public static SizedIterable<File> getCanonicalFiles(Iterable<? extends File> files)
throws IOException
File.getCanonicalFile() to all files in a list
IOException - If any of the getCanonicalFile() invocations triggers
an IOException
SecurityException - If any of the getCanonicalFile() invocations triggers
a SecurityExceptionpublic static SizedIterable<File> attemptCanonicalFiles(Iterable<? extends File> files)
attemptCanonicalFile(java.io.File) to all files in a list
public static boolean attemptCanRead(File f)
File.canRead(). In the event of a
SecurityException, the result is false.
public static boolean attemptCanWrite(File f)
File.canWrite(). In the event of a
SecurityException, the result is false.
public static boolean attemptExists(File f)
File.exists(). In the event of a
SecurityException, the result is false.
public static boolean attemptIsDirectory(File f)
File.isDirectory(). In the event of a
SecurityException, the result is false.
public static boolean attemptIsFile(File f)
File.isFile(). In the event of a
SecurityException, the result is false.
public static boolean attemptIsHidden(File f)
File.isHidden(). In the event of a
SecurityException, the result is false.
public static long attemptLastModified(File f)
File.lastModified(). In the event of a
SecurityException, the result is 0l.
public static long attemptLength(File f)
File.length(). In the event of a
SecurityException, the result is 0l.
public static boolean attemptCreateNewFile(File f)
File.createNewFile(). In the event of an
IOException or a SecurityException, the result is false.
public static boolean attemptDelete(File f)
File.delete(). In the event of a
SecurityException, the result is false.
public static void attemptDeleteOnExit(File f)
File.deleteOnExit(). Any resulting
SecurityException will be ignored.
public static File[] attemptListFiles(File f)
File.listFiles(). In the event of a
SecurityException, the result is null.
public static File[] attemptListFiles(File f,
FileFilter filter)
File.listFiles(FileFilter). In the event of a
SecurityException, the result is null.
public static File[] attemptListFiles(File f,
Predicate<? super File> filter)
File.listFiles(FileFilter). In the event of a
SecurityException, the result is null. The given predicate is converted
to a FileFilter.
public static File[] attemptListFiles(File f,
FilePredicate filter)
File.listFiles(FileFilter). In the event of a
SecurityException, the result is null. (Defined to resolve method
ambiguity when a FilePredicate is used.)
public static SizedIterable<File> attemptListFilesAsIterable(File f)
attemptListFiles(File), but returns a non-null Iterable
rather than an array. Where attemptListFiles(f) returns null,
the result here is an empty iterable.
public static SizedIterable<File> attemptListFilesAsIterable(File f,
FileFilter filter)
attemptListFiles(File, FileFilter), but returns a non-null Iterable
rather than an array. Where attemptListFiles(f) returns null,
the result here is an empty iterable.
public static SizedIterable<File> attemptListFilesAsIterable(File f,
Predicate<? super File> filter)
attemptListFiles(File, FileFilter), but returns a non-null Iterable
rather than an array. Where attemptListFiles(f) returns null,
the result here is an empty iterable. The given predicate is converted to a FileFilter.
public static SizedIterable<File> attemptListFilesAsIterable(File f,
FilePredicate filter)
attemptListFiles(File, FileFilter), but returns a non-null Iterable
rather than an array. Where attemptListFiles(f) returns null,
the result here is an empty iterable. (Defined to resolve method ambiguity when called with
a FilePredicate.)
public static boolean attemptMkdir(File f)
File.mkdir(). In the event of a
SecurityException, the result is false.
public static boolean attemptMkdirs(File f)
File.mkdirs(). In the event of a
SecurityException, the result is false.
public static boolean attemptRenameTo(File f,
File dest)
File.renameTo(java.io.File). In the event of a
SecurityException, the result is false.
public static boolean attemptMove(File f,
File dest)
attemptRenameTo(java.io.File, java.io.File), makes a best attempt at renaming f.
Before doing so, however, dest, if it exists, is deleted. (On some systems,
such as Windows, the rename will otherwise fail.)
public static boolean attemptSetLastModified(File f,
long time)
File.setLastModified(long). In the event of a
SecurityException, the result is false.
public static boolean attemptSetReadOnly(File f)
File.setReadOnly(). In the event of a
SecurityException, the result is false.
public static File canonicalCase(File f)
File to all lowercase in case-insensitive systems; otherwise, returns the file
untouched. This provides a basis for comparing files in case-insensitive file systems.
Note that a canonical file (see File.getCanonicalFile() and attemptCanonicalFile(java.io.File))
is not necessarily in canonical case, and applying this function to a canonical file may
create a file that is no longer canonical. Where the file doesn't exist, it's even possible
that two different canonical files have the same canonical case representation.
public static SizedIterable<File> canonicalCases(Iterable<? extends File> files)
canonicalCase(java.io.File) to all files in a list
public static boolean isMember(File f,
File ancestor)
File.getParentFile())
of another file. If f.equals(ancestor), the result is true.
public static SizedIterable<File> fullPath(File f)
f. This list
has at least one element -- f -- and contains all files that are
produced by repeated invocations of File.getParentFile(). The
outermost file appears first in the list, with f appearing last.
public static boolean deleteRecursively(File f)
File. If f is a directory, this method will first
recur on each of f's members. In all cases, attemptDelete(java.io.File) will be invoked on
f, and the result of this method will be identical to that result.
public static void deleteOnExitRecursively(File f)
File for deletion. If f is a directory, this method
will also recur on each of f's members. In all cases, attemptDeleteOnExit(java.io.File) will
be invoked on f. (Note that f may not actually be deleted on exit if some of its
contents change after invoking this method, or if an error occurs in listing and marking its members
for deletion.)
public static SizedIterable<File> listFilesRecursively(File f)
f, followed (if f is a directory with a canonical path) by a recursive
listing of each of the files belonging to f. The recursion will halt cleanly in
the presence of loops in the system. If an error occurs in listing a directory, that
directory will be skipped.
f - A file (generally a directory) to be listed recursively
public static SizedIterable<File> listFilesRecursively(File f,
FileFilter filter)
f, followed (if f is a directory with a canonical path}) by a recursive
listing of each of the files belonging to f. The recursion will halt cleanly in
the presence of loops in the system. If an error occurs in listing a directory, that
directory will be skipped.
f - A file (generally a directory) to be listed recursivelyfilter - A filter for the list -- files that do not match will not be included
(but directories that do not match will still be traversed)
public static SizedIterable<File> listFilesRecursively(File f,
Predicate<? super File> filter)
f, followed (if f is a directory with a canonical path}) by a recursive
listing of each of the files belonging to f. The recursion will halt cleanly in
the presence of loops in the system. If an error occurs in listing a directory, that
directory will be skipped.
f - A file (generally a directory) to be listed recursivelyfilter - A filter for the list -- files that do not match will not be included
(but directories that do not match will still be traversed)
public static SizedIterable<File> listFilesRecursively(File f,
FilePredicate filter)
f, followed (if f is a directory with a canonical path}) by a recursive
listing of each of the files belonging to f. The recursion will halt cleanly in
the presence of loops in the system. If an error occurs in listing a directory, that
directory will be skipped. (Defined to resolve method ambiguity when a FilePredicate is
used.)
f - A file (generally a directory) to be listed recursivelyfilter - A filter for the list -- files that do not match will not be included
(but directories that do not match will still be traversed)
public static SizedIterable<File> listFilesRecursively(File f,
FileFilter filter,
FileFilter recursionFilter)
f, followed (if f is a directory with a canonical path}) by a recursive
listing of each of the files belonging to f. The recursion will halt cleanly in
the presence of loops in the system.
f - A file (generally a directory) to be listed recursivelyfilter - A filter for the list -- files that do not match will not be included
(but directories that do not match will still be traversed)recursionFilter - A filter controlling recursion -- directories that are rejected will
not be traversed.
public static SizedIterable<File> listFilesRecursively(File f,
Predicate<? super File> filter,
Predicate<? super File> recursionFilter)
f, followed (if f is a directory with a canonical path}) by a recursive
listing of each of the files belonging to f. The recursion will halt cleanly in
the presence of loops in the system.
f - A file (generally a directory) to be listed recursivelyfilter - A filter for the list -- files that do not match will not be included
(but directories that do not match will still be traversed)recursionFilter - A filter controlling recursion -- directories that are rejected will
not be traversed.
public static SizedIterable<File> listFilesRecursively(File f,
FilePredicate filter,
FilePredicate recursionFilter)
f, followed (if f is a directory with a canonical path}) by a recursive
listing of each of the files belonging to f. The recursion will halt cleanly in
the presence of loops in the system. (Defined to resolve method ambiguity where two
FilePredicates are used.)
f - A file (generally a directory) to be listed recursivelyfilter - A filter for the list -- files that do not match will not be included
(but directories that do not match will still be traversed)recursionFilter - A filter controlling recursion -- directories that are rejected will
not be traversed.
public static byte[] toByteArray(File file)
throws IOException
IOException - If the file does not exist or cannot be opened, or if an error occurs during reading
SecurityException - If read access to the file is denied
public static StringBuffer toStringBuffer(File file)
throws IOException
StringWriter supports.)
IOException - If the file does not exist or cannot be opened, or if an error occurs during reading
SecurityException - If read access to the file is denied
public static String toString(File file)
throws IOException
IOException - If the file does not exist or cannot be opened, or if an error occurs during reading
SecurityException - If read access to the file is denied
public static Iterator<String> readLines(File file)
throws IOException
BufferedReader.readLine(boolean). The result iterator's next()
method throws a WrappedException wrapping an IOException if an error occurs while
reading.
IOException - If the file can't be found or an error occurs when reading the first line (for lookahead)
public static int adler32Hash(File file)
throws IOException
IOException - If the file does not exist or cannot be opened, or if an error occurs during reading
SecurityException - If read access to the file is denied
public static int crc32Hash(File file)
throws IOException
IOException - If the file does not exist or cannot be opened, or if an error occurs during reading
SecurityException - If read access to the file is denied
public static byte[] md5Hash(File file)
throws IOException
IOException - If the file does not exist or cannot be opened, or if an error occurs during reading
SecurityException - If read access to the file is denied
public static byte[] sha1Hash(File file)
throws IOException
IOException - If the file does not exist or cannot be opened, or if an error occurs during reading
SecurityException - If read access to the file is denied
public static byte[] sha256Hash(File file)
throws IOException
IOException - If the file does not exist or cannot be opened, or if an error occurs during reading
SecurityException - If read access to the file is denied
public static void copyFile(File source,
File dest)
throws IOException
source - the file to be copieddest - the file to be copied to
IOException - If one of the files does not exist or cannot be opened, or if an error
occurs during reading or writing
SecurityException - If read or write access, respectively, to the file is denied
public static void copyFile(File source,
File dest,
byte[] buffer)
throws IOException
source - The file to be copieddest - The file to be copied tobuffer - A buffer to use in copying
IOException - If one of the files does not exist or cannot be opened, or if an error
occurs during reading or writing
SecurityException - If read or write access, respectively, to the file is denied
public static void writeStringToFile(File file,
String text)
throws IOException
file - File to write totext - Text to write
IOException - If the file does not exist or cannot be opened, or if an error occurs during writing
SecurityException - If write access to the file is denied
public static boolean attemptWriteStringToFile(File file,
String text)
file - File to write totext - Text to write
true iff the operation succeeded without an exception.
public static void writeStringToFile(File file,
String text,
boolean append)
throws IOException
file - File to write totext - Text to writeappend - true iff the file should be opened in "append" mode (rather than
"overwrite" mode)
IOException - If the file does not exist or cannot be opened, or if an error occurs during writing
SecurityException - If write access to the file is denied
public static boolean attemptWriteStringToFile(File file,
String text,
boolean append)
file - File to write totext - Text to writeappend - true iff the file should be opened in "append" mode (rather than
"overwrite" mode)
true iff the operation succeeded without an exception.
public static File createAndMarkTempFile(String prefix,
String suffix)
throws IOException
File.createTempFile(String, String)) and
immediately mark it for deletion
IOException - If an exception occurs in File.createTempFile(String, String)
SecurityException - If write or delete access to the system temp directory is denied
public static File createAndMarkTempFile(String prefix,
String suffix,
File location)
throws IOException
File.createTempFile(String, String, File)) and
immediately mark it for deletion
IOException - If an exception occurs in File.createTempFile(String, String, File)
SecurityException - If write or delete access to location is denied
public static File createAndMarkTempDirectory(String prefix,
String suffix)
throws IOException
File.createTempFile(java.lang.String, java.lang.String, java.io.File)) and immediately mark it for
deletion. (Deletion will only actually occur if the directory is empty on exit, or if all files
created in the directory are also marked for deletion.)
IOException - If an exception occurs in File.createTempFile(java.lang.String, java.lang.String, java.io.File), or if the attempt to
create the directory is unsuccessful
SecurityException - If write access to the system temp directory is denied
public static File createAndMarkTempDirectory(String prefix,
String suffix,
File location)
throws IOException
File.createTempFile(java.lang.String, java.lang.String, java.io.File)) and immediately mark it for
deletion. (Deletion will only actually occur if the directory is empty on exit, or if all files
created in the directory are also marked for deletion.)
IOException - If an exception occurs in File.createTempFile(java.lang.String, java.lang.String, java.io.File), or if the attempt to
create the directory is unsuccessful
SecurityException - If write access to location is deniedpublic static SizedIterable<File> parsePath(String path)
File constructor.
public static String pathToString(Iterable<? extends File> path)
public static int copyReader(Reader source,
Writer dest)
throws IOException
in to out. Processing will continue (or block) until
the end of stream is reached.
-1 if the end of stream has already been reached,
or Integer.MAX_VALUE if the number cannot be represented as an int.
IOException - If an IOException occurs during reading or writing
public static int copyReader(Reader source,
Writer dest,
char[] buffer)
throws IOException
in to out, using the given buffer. Processing will
continue (or block) until the end of stream is reached.
-1 if the end of stream has already been reached,
or Integer.MAX_VALUE if the number cannot be represented as an int.
IOException - If an IOException occurs during reading or writing
public static int writeFromReader(Reader source,
Writer dest,
int chars)
throws IOException
in to out.
-1 if the end of stream has already been reached.
May be less than chars if source does not provide all the requested data.
IOException - If an IOException occurs during reading or writing
public static int writeFromReader(Reader source,
Writer dest,
int chars,
char[] buffer)
throws IOException
in to out, using the given buffer.
-1 if the end of stream has already been reached.
May be less than chars if source does not provide all the requested data.
IOException - If an IOException occurs during reading or writing
public static int copyInputStream(InputStream source,
OutputStream dest)
throws IOException
in to out. Processing will continue (or block) until
the end of stream is reached.
-1 if the end of stream has already been reached,
or Integer.MAX_VALUE if the number cannot be represented as an int.
IOException - If an IOException occurs during reading or writing
public static int copyInputStream(InputStream source,
OutputStream dest,
byte[] buffer)
throws IOException
in to out, using the given buffer. Processing will
continue (or block) until the end of stream is reached.
-1 if the end of stream has already been reached,
or Integer.MAX_VALUE if the number cannot be represented as an int.
IOException - If an IOException occurs during reading or writing
public static int writeFromInputStream(InputStream source,
OutputStream dest,
int bytes)
throws IOException
in to out.
-1 if the end of stream has already been reached.
May be less than bytes if source does not provide all the requested data.
IOException - If an IOException occurs during reading or writing
public static int writeFromInputStream(InputStream source,
OutputStream dest,
int bytes,
byte[] buffer)
throws IOException
in to out, using the given buffer.
-1 if the end of stream has already been reached.
May be less than bytes if source does not provide all the requested data.
IOException - If an IOException occurs during reading or writing
protected static int doCopyReader(Reader r,
Writer w,
char[] buffer)
throws IOException
DirectReader and DirectWriter
(placed here to avoid code duplication). The method will block until an end-of-file is reached.
-1 if the reader is at the end of file; otherwise, the number of characters read
(or, if the number is too large, Integer.MAX_VALUE)
IOException - If an error occurs during reading or writing
IllegalArgumentException - If buffer has size 0
protected static int doCopyInputStream(InputStream in,
OutputStream out,
byte[] buffer)
throws IOException
DirectInputStream and DirectOutputStream
(placed here to avoid code duplication). The method will block until an end-of-file is reached.
-1 if the reader is at the end of file; otherwise, the number of characters read
(or, if the number is too large, Integer.MAX_VALUE)
IOException - If an error occurs during reading or writing
IllegalArgumentException - If buffer has size 0
protected static int doWriteFromReader(Reader r,
Writer w,
int chars,
char[] buffer)
throws IOException
DirectReader and DirectWriter
(placed here to avoid code duplication).
-1 if this reader is at the end of file; otherwise, the number of characters read
IOException - If an error occurs during reading or writing
IllegalArgumentException - If buffer has size 0
protected static int doWriteFromInputStream(InputStream in,
OutputStream out,
int bytes,
byte[] buffer)
throws IOException
DirectInputStream and
DirectOutputStream (placed here to avoid code duplication).
-1 if this reader is at the end of file; otherwise, the number of characters read
IOException - If an error occurs during reading or writing
IllegalArgumentException - If buffer has size 0
public static byte[] toByteArray(InputStream stream)
throws IOException
IOException
public static StringBuffer toStringBuffer(Reader r)
throws IOException
Reader. The method will not return
until an end of stream has been reached. (We use a StringBuffer rather than a
StringBuilder because that is what StringWriter supports.)
IOException
public static String toString(Reader r)
throws IOException
Reader. The method will not return
until an end of stream has been reached.
IOException
public static Iterator<String> readLines(Reader r)
throws IOException
BufferedReader.readLine(boolean). The Reader is closed once iteration
has been completed. The result iterator's next() method throws a WrappedException
wrapping an IOException if an error occurs while reading.
IOException - If an error occurs when reading the first line (for lookahead).
public static int adler32Hash(InputStream stream)
throws IOException
IOException
public static int crc32Hash(InputStream stream)
throws IOException
IOException
public static byte[] md5Hash(InputStream stream)
throws IOException
IOException
public static byte[] sha1Hash(InputStream stream)
throws IOException
IOException
public static byte[] sha256Hash(InputStream stream)
throws IOException
IOExceptionpublic static BufferedReader asBuffered(Reader r)
r is a BufferedReader, cast it as such; otherwise, wrap it in a BufferedReader.
public static BufferedWriter asBuffered(Writer w)
w is a BufferedWriter, cast it as such; otherwise, wrap it in a BufferedWriter.
public static BufferedInputStream asBuffered(InputStream in)
in is a BufferedInputStream, cast it as such; otherwise, wrap it in a
BufferedInputStream.
public static BufferedOutputStream asBuffered(OutputStream out)
out is a BufferedOutputStream, cast it as such; otherwise, wrap it in a
BufferedOutputStream.
public static void closeOnExit(Closeable c)
Closeable.close() will be invoked from a
shutdown hook.
public static void attemptClose(Closeable c)
public static FilePredicate asFilePredicate(Predicate<? super File> p)
FileFilter in terms of a Predicate.
public static FilePredicate asFilePredicate(FileFilter filter)
FilePredicate in terms of a FileFilter (this provides access
to predicate operations like and and or).
public static FilePredicate regexFilePredicate(String regex)
FilePredicate that accepts files whose (simple) names match
a regular expression.
public static FilePredicate regexFilePredicate(Pattern regex)
FilePredicate that accepts files whose (simple) names match
a regular expression.
public static FilePredicate regexCanonicalCaseFilePredicate(String regex)
FilePredicate that accepts files whose (simple) names in the
canonical case (see canonicalCase(java.io.File)) match a regular expression.
public static FilePredicate extensionFilePredicate(String extension)
FilePredicate that accepts file objects with the given extension (that is,
for extension txt, file objects whose canonical-case names (see canonicalCase(java.io.File))
end in .txt).
public static FilePredicate sameNameFilePredicate(String name)
FilePredicate that only accepts files with the given name (where both names
are converted to the canonical case; see canonicalCase(java.io.File)).
public static FilePredicate samePathFilePredicate(File path)
FilePredicate that only accepts files with the given path and name (where
both paths are converted to the canonical case; see canonicalCase(java.io.File)). If path is
relative, any file with an absolute path that ends with path will be accepted.
Otherwise, only a file with the exact same path is accepted.
public static FilePredicate sameAttributesFilePredicate(File f)
throws FileNotFoundException
FilePredicate that only accepts a file with the same attributes as
f (at creation time). This is useful in detecting changes being made to a file.
The files' modification dates, lengths, and read/write permissions are compared.
FileNotFoundException - If f is not a normal file, or if access to its attributes
is not available.
public static FilePredicate sameContentsFilePredicate(File f)
throws IOException
FilePredicate that only accepts files with contents matching a CRC-32
hash of f (at creation time). This is useful in detecting changes being made to
a file.
IOException - If f cannot be read.public static FilePredicate and(FileFilter... filters)
public static FilePredicate and(Iterable<? extends FileFilter> filters)
public static FilePredicate or(FileFilter... filters)
public static FilePredicate or(Iterable<? extends FileFilter> filters)
public static FilePredicate negate(FileFilter filter)
public static FilePredicate fileKey(File f)
throws IOException
FilePredicate that acts as a "key" representing the current state of
the given file. A file will match only if it has the same name, absolute path, attributes,
and contents as f. This is useful for detecting background changes to a file.
IOException - If the given file does not exist, is not a normal file, or does
not allow its attributes or contents to be accessed.public static void replaceSystemOut(OutputStream substitute)
System.out with the given stream and remember the current stream. This call
should always be matched by a subsequent call to revertSystemOut().
public static void ignoreSystemOut()
System.out until revertSystemOut() is called.
A matching revert call should be made.
public static void revertSystemOut()
System.out to its value before the last call to replaceSystemOut(java.io.OutputStream). This call
should always follow a call to replaceSystemOut(). Assuming all calls are properly
paired, and that multiple threads do not concurrently invoke these methods, the stream after this
call will be the stream that was replaced by replaceSystemOut().
public static void replaceSystemErr(OutputStream substitute)
System.err with the given stream and remember the current stream. This call
should always be matched by a subsequent call to revertSystemErr().
public static void ignoreSystemErr()
System.err until revertSystemErr() is called. A
matching revert call should be made.
public static void revertSystemErr()
System.err to its value before the last call to replaceSystemErr(java.io.OutputStream). This call
should always follow a call to replaceSystemErr(). Assuming all calls are properly
paired, and that multiple threads do not concurrently invoke these methods, the stream after this
call will be the stream that was replaced by replaceSystemErr().
public static void replaceSystemIn(InputStream substitute)
System.in with the given stream and remember the current stream. This call
should always be matched by a subsequent call to revertSystemIn().
public static void revertSystemIn()
System.in to its value before the last call to replaceSystemIn(java.io.InputStream). This call
should always follow a call to replaceSystemIn(). Assuming all calls are properly
paired, and that multiple threads do not concurrently invoke these methods, the stream after this
call will be the stream that was replaced by replaceSystemIn().
public static Object ensureSerializable(Object obj)
obj.toString(). Note that subsequent (or concurrent) mutation
of the object may prevent successful serialization.
public static Object[] ensureSerializable(Object[] arr)
arr
unchanged. Otherwise, makes a converted copy. Note that subsequent (or concurrent) mutation
of the array may prevent successful serialization.
public static Iterable<?> ensureSerializable(Iterable<?> iter)
"..." string. Note that subsequent (or concurrent) mutation
of the elements may prevent successful serialization.
public static Throwable ensureSerializable(Throwable t)
SerializableException. Note that subsequent (or concurrent) mutation of the cause may prevent
successful serialization.
public static Exception ensureSerializable(Exception e)
SerializableException. Note that subsequent (or concurrent) mutation of the cause may prevent
successful serialization.
public static RuntimeException ensureSerializable(RuntimeException e)
SerializableException. Note that subsequent (or concurrent) mutation of the cause
may prevent successful serialization.
public static Tuple ensureSerializable(Tuple t)
ensureSerializable() to each of the elements of the given tuple. If the elements
are unchanged and the tuple's class is known to serialize safely, returns the tuple unchanged.
public static Option<?> ensureSerializable(Option<?> opt)
ensureSerializable() to the given option value. If the value is
unchanged and the option's class is known to serialize safely, returns the option unchanged.
public static Wrapper<?> ensureSerializable(Wrapper<?> w)
ensureSerializable() to the given wrapped value. If the value is
unchanged and the wrapper's class is known to serialize safely, returns the wrapper unchanged.
public static Pair<?,?> ensureSerializable(Pair<?,?> p)
ensureSerializable() to each of the elements of the given tuple. If the elements
are unchanged and the tuple's class is known to serialize safely, returns the tuple unchanged.
public static Triple<?,?,?> ensureSerializable(Triple<?,?,?> t)
ensureSerializable() to each of the elements of the given tuple. If the elements
are unchanged and the tuple's class is known to serialize safely, returns the tuple unchanged.
public static Quad<?,?,?,?> ensureSerializable(Quad<?,?,?,?> q)
ensureSerializable() to each of the elements of the given tuple. If the elements
are unchanged and the tuple's class is known to serialize safely, returns the tuple unchanged.
public static Quint<?,?,?,?,?> ensureSerializable(Quint<?,?,?,?,?> q)
ensureSerializable() to each of the elements of the given tuple. If the elements
are unchanged and the tuple's class is known to serialize safely, returns the tuple unchanged.
public static Sextet<?,?,?,?,?,?> ensureSerializable(Sextet<?,?,?,?,?,?> s)
ensureSerializable() to each of the elements of the given tuple. If the elements
are unchanged and the tuple's class is known to serialize safely, returns the tuple unchanged.
public static Septet<?,?,?,?,?,?,?> ensureSerializable(Septet<?,?,?,?,?,?,?> s)
ensureSerializable() to each of the elements of the given tuple. If the elements
are unchanged and the tuple's class is known to serialize safely, returns the tuple unchanged.
public static Octet<?,?,?,?,?,?,?,?> ensureSerializable(Octet<?,?,?,?,?,?,?,?> o)
ensureSerializable() to each of the elements of the given tuple. If the elements
are unchanged and the tuple's class is known to serialize safely, returns the tuple unchanged.
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||