public final class IOUtils
extends java.lang.Object
Modifier and Type | Field and Description |
---|---|
static java.lang.String |
UTF_8
UTF-8 charset string.
|
Modifier | Constructor and Description |
---|---|
private |
IOUtils() |
Modifier and Type | Method and Description |
---|---|
private static void |
addSuppressed(java.lang.Throwable exception,
java.lang.Throwable suppressed)
adds a Throwable to the list of suppressed Exceptions of the first Throwable
|
static void |
close(java.io.Closeable... objects)
Closes all given Closeables.
|
static void |
close(java.lang.Iterable<? extends java.io.Closeable> objects)
Closes all given Closeables.
|
static void |
closeWhileHandlingException(java.io.Closeable... objects)
Closes all given Closeables, suppressing all thrown exceptions.
|
static void |
closeWhileHandlingException(java.lang.Iterable<? extends java.io.Closeable> objects)
Closes all given Closeables, suppressing all thrown exceptions.
|
static void |
deleteFiles(Directory dir,
java.util.Collection<java.lang.String> names)
Deletes all given file names.
|
static void |
deleteFiles(Directory dir,
java.lang.String... files) |
static void |
deleteFilesIfExist(java.util.Collection<? extends java.nio.file.Path> files)
Deletes all given Paths, if they exist.
|
static void |
deleteFilesIfExist(java.nio.file.Path... files)
Deletes all given Paths, if they exist.
|
static void |
deleteFilesIgnoringExceptions(java.util.Collection<? extends java.nio.file.Path> files)
Deletes all given files, suppressing all thrown IOExceptions.
|
static void |
deleteFilesIgnoringExceptions(Directory dir,
java.util.Collection<java.lang.String> files)
Deletes all given files, suppressing all thrown IOExceptions.
|
static void |
deleteFilesIgnoringExceptions(Directory dir,
java.lang.String... files) |
static void |
deleteFilesIgnoringExceptions(java.nio.file.Path... files)
Deletes all given files, suppressing all thrown IOExceptions.
|
static void |
fsync(java.nio.file.Path fileToSync,
boolean isDir)
Ensure that any writes to the given file is written to the storage device that contains it.
|
static java.io.Reader |
getDecodingReader(java.lang.Class<?> clazz,
java.lang.String resource,
java.nio.charset.Charset charSet)
Opens a Reader for the given resource using a
CharsetDecoder . |
static java.io.Reader |
getDecodingReader(java.io.InputStream stream,
java.nio.charset.Charset charSet)
Wrapping the given
InputStream in a reader using a CharsetDecoder . |
(package private) static java.nio.file.FileStore |
getFileStore(java.nio.file.Path path) |
(package private) static java.lang.String |
getMountPoint(java.nio.file.FileStore store) |
static void |
reThrow(java.lang.Throwable th)
Simple utility method that takes a previously caught
Throwable and rethrows either IOException or an unchecked exception. |
static void |
reThrowUnchecked(java.lang.Throwable th)
Simple utility method that takes a previously caught
Throwable and rethrows it as an unchecked exception. |
private static java.util.LinkedHashMap<java.nio.file.Path,java.lang.Throwable> |
rm(java.util.LinkedHashMap<java.nio.file.Path,java.lang.Throwable> unremoved,
java.nio.file.Path... locations) |
static void |
rm(java.nio.file.Path... locations)
Deletes one or more files or directories (and everything underneath it).
|
static boolean |
spins(Directory dir)
If the dir is an
FSDirectory or wraps one via possibly
nested FilterDirectory or FileSwitchDirectory ,
this returns spins(Path) for the wrapped directory,
else, true. |
static boolean |
spins(java.nio.file.Path path)
Rough Linux-only heuristics to determine whether the provided
Path is backed by spinning storage. |
(package private) static boolean |
spinsLinux(java.nio.file.Path path) |
public static final java.lang.String UTF_8
Where possible, use StandardCharsets.UTF_8
instead,
as using the String constant may slow things down.
StandardCharsets.UTF_8
public static void close(java.io.Closeable... objects) throws java.io.IOException
objects
- objects to call close() onjava.io.IOException
public static void close(java.lang.Iterable<? extends java.io.Closeable> objects) throws java.io.IOException
java.io.IOException
close(Closeable...)
public static void closeWhileHandlingException(java.io.Closeable... objects)
objects
- objects to call close() onpublic static void closeWhileHandlingException(java.lang.Iterable<? extends java.io.Closeable> objects)
private static void addSuppressed(java.lang.Throwable exception, java.lang.Throwable suppressed)
exception
- this exception should get the suppressed one addedsuppressed
- the suppressed exceptionpublic static java.io.Reader getDecodingReader(java.io.InputStream stream, java.nio.charset.Charset charSet)
InputStream
in a reader using a CharsetDecoder
.
Unlike Java's defaults this reader will throw an exception if your it detects
the read charset doesn't match the expected Charset
.
Decoding readers are useful to load configuration files, stopword lists or synonym files to detect character set problems. However, it's not recommended to use as a common purpose reader.
stream
- the stream to wrap in a readercharSet
- the expected charsetpublic static java.io.Reader getDecodingReader(java.lang.Class<?> clazz, java.lang.String resource, java.nio.charset.Charset charSet) throws java.io.IOException
CharsetDecoder
.
Unlike Java's defaults this reader will throw an exception if your it detects
the read charset doesn't match the expected Charset
.
Decoding readers are useful to load configuration files, stopword lists or synonym files to detect character set problems. However, it's not recommended to use as a common purpose reader.
clazz
- the class used to locate the resourceresource
- the resource name to loadcharSet
- the expected charsetjava.io.IOException
public static void deleteFilesIgnoringExceptions(Directory dir, java.util.Collection<java.lang.String> files)
Note that the files should not be null.
public static void deleteFilesIgnoringExceptions(Directory dir, java.lang.String... files)
public static void deleteFiles(Directory dir, java.util.Collection<java.lang.String> names) throws java.io.IOException
dir
- Directory to delete files fromnames
- file names to deletejava.io.IOException
public static void deleteFiles(Directory dir, java.lang.String... files) throws java.io.IOException
java.io.IOException
public static void deleteFilesIgnoringExceptions(java.nio.file.Path... files)
Some of the files may be null, if so they are ignored.
public static void deleteFilesIgnoringExceptions(java.util.Collection<? extends java.nio.file.Path> files)
Some of the files may be null, if so they are ignored.
public static void deleteFilesIfExist(java.nio.file.Path... files) throws java.io.IOException
files
- files to deletejava.io.IOException
public static void deleteFilesIfExist(java.util.Collection<? extends java.nio.file.Path> files) throws java.io.IOException
files
- files to deletejava.io.IOException
public static void rm(java.nio.file.Path... locations) throws java.io.IOException
java.io.IOException
- if any of the given files (or their subhierarchy files in case
of directories) cannot be removed.private static java.util.LinkedHashMap<java.nio.file.Path,java.lang.Throwable> rm(java.util.LinkedHashMap<java.nio.file.Path,java.lang.Throwable> unremoved, java.nio.file.Path... locations)
public static void reThrow(java.lang.Throwable th) throws java.io.IOException
Throwable
and rethrows either IOException
or an unchecked exception. If the
argument is null then this method does nothing.java.io.IOException
public static void reThrowUnchecked(java.lang.Throwable th)
Throwable
and rethrows it as an unchecked exception.
If the argument is null then this method does nothing.public static void fsync(java.nio.file.Path fileToSync, boolean isDir) throws java.io.IOException
fileToSync
- the file to fsyncisDir
- if true, the given file is a directory (we open for read and ignore IOExceptions,
because not all file systems and operating systems allow to fsync on a directory)java.io.IOException
public static boolean spins(Directory dir) throws java.io.IOException
FSDirectory
or wraps one via possibly
nested FilterDirectory
or FileSwitchDirectory
,
this returns spins(Path)
for the wrapped directory,
else, true.java.io.IOException
- if path
does not exist.public static boolean spins(java.nio.file.Path path) throws java.io.IOException
Path
is backed by spinning storage. For example, this
returns false if the disk is a solid-state disk.path
- a location to check which must exist. the mount point will be determined from this location.java.io.IOException
- if path
does not exist.static boolean spinsLinux(java.nio.file.Path path) throws java.io.IOException
java.io.IOException
static java.nio.file.FileStore getFileStore(java.nio.file.Path path) throws java.io.IOException
java.io.IOException
static java.lang.String getMountPoint(java.nio.file.FileStore store)