Class IOUtils
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceAn IO operation with a single input.static interfaceA Function that may throw an IOException -
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> voidapplyToAll(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 voidCloses all given Closeables.static voidCloses all given Closeables.static voidcloseWhileHandlingException(Closeable... objects) Closes all given Closeables, suppressing all thrown exceptions.static voidcloseWhileHandlingException(Iterable<? extends Closeable> objects) Closes all given Closeables, suppressing all thrown nonVirtualMachineErrorexceptions.static voiddeleteFiles(Directory dir, Collection<String> names) Deletes all given file names.static voiddeleteFilesIfExist(Path... files) Deletes all given Paths, if they exist.static voiddeleteFilesIfExist(Collection<? extends Path> files) Deletes all given Paths, if they exist.static voiddeleteFilesIgnoringExceptions(Path... files) Deletes all given files, suppressing all thrown IOExceptions.static voiddeleteFilesIgnoringExceptions(Collection<? extends Path> files) Deletes all given files, suppressing all thrown IOExceptions.static voiddeleteFilesIgnoringExceptions(Directory dir, String... files) static voiddeleteFilesIgnoringExceptions(Directory dir, Collection<String> files) Deletes all given files, suppressing all thrown IOExceptions.static voidEnsure that any writes to the given file is written to the storage device that contains it.static ReadergetDecodingReader(InputStream stream, Charset charSet) Wrapping the givenInputStreamin a reader using aCharsetDecoder.static ReadergetDecodingReader(Class<?> clazz, String resource, Charset charSet) Opens a Reader for the given resource using aCharsetDecoder.(package private) static FileStoregetFileStore(Path path) (package private) static StringgetMountPoint(FileStore store) static voidDeprecated.static ErrorThis utility method takes a previously caught (non-null)Throwableand rethrows either the original argument if it was a subclass of theIOExceptionor anRuntimeExceptionwith the cause set to the argument.static voidDeprecated.This method is deprecated in favor ofrethrowAlways(java.lang.Throwable).static voidDeletes one or more files or directories (and everything underneath it).private static LinkedHashMap<Path, Throwable> rm(LinkedHashMap<Path, Throwable> unremoved, Path... locations) static booleanRough Linux-only heuristics to determine whether the providedPathis backed by spinning storage.static booleanIf the dir is anFSDirectoryor wraps one via possibly nestedFilterDirectoryorFileSwitchDirectory, this returnsspins(Path)for the wrapped directory, else, true.(package private) static booleanspinsLinux(Path path) static <T extends Throwable>
TuseOrSuppress(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.
-
Field Details
-
UTF_8
UTF-8 charset string.Where possible, use
StandardCharsets.UTF_8instead, as using the String constant may slow things down.- See Also:
-
-
Constructor Details
-
IOUtils
private IOUtils()
-
-
Method Details
-
close
Closes all given Closeables. Some of the Closeables may be null; they are ignored. After everything is closed, the method either throws the first exception it hit while closing, or completes normally if there were no exceptions.- Parameters:
objects- objects to call close() on- Throws:
IOException
-
close
Closes all given Closeables.- Throws:
IOException- See Also:
-
closeWhileHandlingException
Closes all given Closeables, suppressing all thrown exceptions. Some of the Closeables may be null, they are ignored.- Parameters:
objects- objects to call close() on
-
closeWhileHandlingException
Closes all given Closeables, suppressing all thrown nonVirtualMachineErrorexceptions. Even if aVirtualMachineErroris thrown all given closeable are closed.- See Also:
-
getDecodingReader
Wrapping the givenInputStreamin a reader using aCharsetDecoder. Unlike Java's defaults this reader will throw an exception if your it detects the read charset doesn't match the expectedCharset.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.
- Parameters:
stream- the stream to wrap in a readercharSet- the expected charset- Returns:
- a wrapping reader
-
getDecodingReader
public static Reader getDecodingReader(Class<?> clazz, String resource, Charset charSet) throws IOException Opens a Reader for the given resource using aCharsetDecoder. Unlike Java's defaults this reader will throw an exception if your it detects the read charset doesn't match the expectedCharset.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.
- Parameters:
clazz- the class used to locate the resourceresource- the resource name to loadcharSet- the expected charset- Returns:
- a reader to read the given file
- Throws:
IOException
-
deleteFilesIgnoringExceptions
Deletes all given files, suppressing all thrown IOExceptions.Note that the files should not be null.
-
deleteFilesIgnoringExceptions
-
deleteFiles
Deletes all given file names. Some of the file names may be null; they are ignored. After everything is deleted, the method either throws the first exception it hit while deleting, or completes normally if there were no exceptions.- Parameters:
dir- Directory to delete files fromnames- file names to delete- Throws:
IOException
-
deleteFilesIgnoringExceptions
Deletes all given files, suppressing all thrown IOExceptions.Some of the files may be null, if so they are ignored.
-
deleteFilesIgnoringExceptions
Deletes all given files, suppressing all thrown IOExceptions.Some of the files may be null, if so they are ignored.
-
deleteFilesIfExist
Deletes all given Paths, if they exist. Some of the Files may be null; they are ignored. After everything is deleted, the method either throws the first exception it hit while deleting, or completes normally if there were no exceptions.- Parameters:
files- files to delete- Throws:
IOException
-
deleteFilesIfExist
Deletes all given Paths, if they exist. Some of the Files may be null; they are ignored. After everything is deleted, the method either throws the first exception it hit while deleting, or completes normally if there were no exceptions.- Parameters:
files- files to delete- Throws:
IOException
-
rm
Deletes one or more files or directories (and everything underneath it).- Throws:
IOException- if any of the given files (or their subhierarchy files in case of directories) cannot be removed.
-
rm
private static LinkedHashMap<Path,Throwable> rm(LinkedHashMap<Path, Throwable> unremoved, Path... locations) -
rethrowAlways
This utility method takes a previously caught (non-null)Throwableand rethrows either the original argument if it was a subclass of theIOExceptionor anRuntimeExceptionwith 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 anifand used together with athrowstatement, as in:if (t != null) throw IOUtils.rethrowAlways(t)- Parameters:
th- The throwable to rethrow, must not be null.- Returns:
- This method always results in an exception, it never returns any value. See method documentation for detailsa and usage example.
- Throws:
IOException- if the argument was an instance of IOExceptionRuntimeException- with theThrowable.getCause()set to the argument, if it was not an instance of IOException.
-
reThrow
Deprecated.This method is deprecated in favor ofrethrowAlways(java.lang.Throwable). Code should be updated torethrowAlways(java.lang.Throwable)and guarded with an additional null-argument check (becauserethrowAlways(java.lang.Throwable)is not accepting null arguments).Rethrows the argument asIOExceptionorRuntimeExceptionif it's not null.- Throws:
IOException
-
reThrowUnchecked
Deprecated.This method is deprecated in favor ofrethrowAlways(java.lang.Throwable). Code should be updated torethrowAlways(java.lang.Throwable)and guarded with an additional null-argument check (becauserethrowAlways(java.lang.Throwable)is not accepting null arguments). -
fsync
Ensure that any writes to the given file is written to the storage device that contains it.- Parameters:
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)- Throws:
IOException
-
spins
If the dir is anFSDirectoryor wraps one via possibly nestedFilterDirectoryorFileSwitchDirectory, this returnsspins(Path)for the wrapped directory, else, true.- Throws:
IOException- ifpathdoes not exist.
-
spins
Rough Linux-only heuristics to determine whether the providedPathis backed by spinning storage. For example, this returns false if the disk is a solid-state disk.- Parameters:
path- a location to check which must exist. the mount point will be determined from this location.- Returns:
- false if the storage is non-rotational (e.g. an SSD), or true if it is spinning or could not be determined
- Throws:
IOException- ifpathdoes not exist.
-
spinsLinux
- Throws:
IOException
-
getFileStore
- Throws:
IOException
-
getMountPoint
-
useOrSuppress
Returns the second throwable if the first is null otherwise adds the second as suppressed to the first and returns it. -
applyToAll
public static <T> void applyToAll(Collection<T> collection, IOUtils.IOConsumer<T> consumer) throws IOException Applies the consumer to all non-null elements in the collection even if an exception is thrown. The first exception thrown by the consumer is re-thrown and subsequent exceptions are suppressed.- Throws:
IOException
-
rethrowAlways(java.lang.Throwable).