public final class IOUtils extends Object
Modifier and Type | Class and Description |
---|---|
static interface |
IOUtils.IOConsumer<T>
An IO operation with a single input.
|
Modifier and Type | Field and Description |
---|---|
static String |
UTF_8
UTF-8 charset string.
|
Modifier and Type | Method and Description |
---|---|
static <T> void |
applyToAll(Collection<T> collection,
IOUtils.IOConsumer<T> consumer)
Applies the consumer to all non-null elements in the collection even if an exception is thrown.
|
static void |
close(Closeable... objects)
Closes all given Closeables.
|
static void |
close(Iterable<? extends Closeable> objects)
Closes all given Closeables.
|
static void |
closeWhileHandlingException(Closeable... objects)
Closes all given Closeables, suppressing all thrown exceptions.
|
static void |
closeWhileHandlingException(Iterable<? extends Closeable> objects)
Closes all given Closeables, suppressing all thrown non
VirtualMachineError exceptions. |
static void |
deleteFiles(Directory dir,
Collection<String> names)
Deletes all given file names.
|
static void |
deleteFilesIfExist(Collection<? extends Path> files)
Deletes all given Paths, if they exist.
|
static void |
deleteFilesIfExist(Path... files)
Deletes all given Paths, if they exist.
|
static void |
deleteFilesIgnoringExceptions(Collection<? extends Path> files)
Deletes all given files, suppressing all thrown IOExceptions.
|
static void |
deleteFilesIgnoringExceptions(Directory dir,
Collection<String> files)
Deletes all given files, suppressing all thrown IOExceptions.
|
static void |
deleteFilesIgnoringExceptions(Directory dir,
String... files) |
static void |
deleteFilesIgnoringExceptions(Path... files)
Deletes all given files, suppressing all thrown IOExceptions.
|
static void |
fsync(Path fileToSync,
boolean isDir)
Ensure that any writes to the given file is written to the storage device that contains it.
|
static Reader |
getDecodingReader(Class<?> clazz,
String resource,
Charset charSet)
Opens a Reader for the given resource using a
CharsetDecoder . |
static Reader |
getDecodingReader(InputStream stream,
Charset charSet)
Wrapping the given
InputStream in a reader using a CharsetDecoder . |
static void |
reThrow(Throwable th)
Deprecated.
This method is deprecated in favor of
rethrowAlways(java.lang.Throwable) . Code should
be updated to rethrowAlways(java.lang.Throwable) and guarded with an additional null-argument check
(because rethrowAlways(java.lang.Throwable) is not accepting null arguments). |
static Error |
rethrowAlways(Throwable th)
This utility method takes a previously caught (non-null)
Throwable and rethrows either the original argument
if it was a subclass of the IOException or an
RuntimeException with the cause set to the argument. |
static void |
reThrowUnchecked(Throwable th)
Deprecated.
This method is deprecated in favor of
rethrowAlways(java.lang.Throwable) . Code should
be updated to rethrowAlways(java.lang.Throwable) and guarded with an additional null-argument check
(because rethrowAlways(java.lang.Throwable) is not accepting null arguments). |
static void |
rm(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(Path path)
Rough Linux-only heuristics to determine whether the provided
Path is backed by spinning storage. |
static <T extends Throwable> |
useOrSuppress(T first,
T second)
Returns the second throwable if the first is null otherwise adds the second as suppressed to the first
and returns it.
|
public static final 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(Closeable... objects) throws IOException
objects
- objects to call close() onIOException
public static void close(Iterable<? extends Closeable> objects) throws IOException
IOException
close(Closeable...)
public static void closeWhileHandlingException(Closeable... objects)
objects
- objects to call close() onpublic static void closeWhileHandlingException(Iterable<? extends Closeable> objects)
VirtualMachineError
exceptions.
Even if a VirtualMachineError
is thrown all given closeable are closed.public static Reader getDecodingReader(InputStream stream, 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 Reader getDecodingReader(Class<?> clazz, String resource, Charset charSet) throws 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 charsetIOException
public static void deleteFilesIgnoringExceptions(Directory dir, Collection<String> files)
Note that the files should not be null.
public static void deleteFilesIgnoringExceptions(Directory dir, String... files)
public static void deleteFiles(Directory dir, Collection<String> names) throws IOException
dir
- Directory to delete files fromnames
- file names to deleteIOException
public static void deleteFilesIgnoringExceptions(Path... files)
Some of the files may be null, if so they are ignored.
public static void deleteFilesIgnoringExceptions(Collection<? extends Path> files)
Some of the files may be null, if so they are ignored.
public static void deleteFilesIfExist(Path... files) throws IOException
files
- files to deleteIOException
public static void deleteFilesIfExist(Collection<? extends Path> files) throws IOException
files
- files to deleteIOException
public static void rm(Path... locations) throws IOException
IOException
- if any of the given files (or their subhierarchy files in case
of directories) cannot be removed.public static Error rethrowAlways(Throwable th) throws IOException, RuntimeException
Throwable
and rethrows either the original argument
if it was a subclass of the IOException
or an
RuntimeException
with the cause set to the argument.
This method never returns any value, even though it declares
a return value of type Error
. The return value declaration
is very useful to let the compiler know that the code path following
the invocation of this method is unreachable. So in most cases the
invocation of this method will be guarded by an if
and
used together with a throw
statement, as in:
if (t != null) throw IOUtils.rethrowAlways(t)
th
- The throwable to rethrow, must not be null.IOException
- if the argument was an instance of IOExceptionRuntimeException
- with the Throwable.getCause()
set
to the argument, if it was not an instance of IOException.@Deprecated public static void reThrow(Throwable th) throws IOException
rethrowAlways(java.lang.Throwable)
. Code should
be updated to rethrowAlways(java.lang.Throwable)
and guarded with an additional null-argument check
(because rethrowAlways(java.lang.Throwable)
is not accepting null arguments).IOException
or RuntimeException
if it's not null.IOException
@Deprecated public static void reThrowUnchecked(Throwable th)
rethrowAlways(java.lang.Throwable)
. Code should
be updated to rethrowAlways(java.lang.Throwable)
and guarded with an additional null-argument check
(because rethrowAlways(java.lang.Throwable)
is not accepting null arguments).public static void fsync(Path fileToSync, boolean isDir) throws 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)IOException
public static boolean spins(Directory dir) throws IOException
FSDirectory
or wraps one via possibly
nested FilterDirectory
or FileSwitchDirectory
,
this returns spins(Path)
for the wrapped directory,
else, true.IOException
- if path
does not exist.public static boolean spins(Path path) throws 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.IOException
- if path
does not exist.public static <T extends Throwable> T useOrSuppress(T first, T second)
public static <T> void applyToAll(Collection<T> collection, IOUtils.IOConsumer<T> consumer) throws IOException
IOException
Copyright © 2000-2019 Apache Software Foundation. All Rights Reserved.