Class IOUtils
java.lang.Object
org.elasticsearch.core.internal.io.IOUtils
public final class IOUtils
extends java.lang.Object
Utilities for common I/O methods. Borrowed heavily from Lucene (org.apache.lucene.util.IOUtils).
-
Field Summary
-
Method Summary
Modifier and Type Method Description static voidclose(java.io.Closeable... objects)Closes all givenCloseables.static voidclose(java.lang.Exception e, java.io.Closeable... objects)Closes all givenCloseables.static voidclose(java.lang.Exception ex, java.lang.Iterable<? extends java.io.Closeable> objects)Closes all givenCloseables.static voidclose(java.lang.Iterable<? extends java.io.Closeable> objects)Closes all givenCloseables.static voidcloseWhileHandlingException(java.io.Closeable... objects)Closes all givenCloseables, suppressing all thrown exceptions.static voidcloseWhileHandlingException(java.lang.Iterable<? extends java.io.Closeable> objects)Closes all givenCloseables, suppressing all thrown exceptions.static voiddeleteFilesIgnoringExceptions(java.nio.file.Path... files)Deletes all given files, suppressing all thrownIOExceptions.static voiddeleteFilesIgnoringExceptions(java.util.Collection<? extends java.nio.file.Path> files)Deletes all given files, suppressing all thrownIOExceptions.static voidfsync(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 voidrm(java.nio.file.Path... locations)Deletes one or more files or directories (and everything underneath it).
-
Field Details
-
UTF_8
public static final java.lang.String UTF_8UTF-8 charset string.Where possible, use
StandardCharsets.UTF_8instead, as using the String constant may slow things down.- See Also:
StandardCharsets.UTF_8
-
WINDOWS
public static final boolean WINDOWS -
LINUX
public static final boolean LINUX -
MAC_OS_X
public static final boolean MAC_OS_X
-
-
Method Details
-
close
public static void close(java.io.Closeable... objects) throws java.io.IOExceptionCloses all givenCloseables. 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 with other exceptions added as suppressed, or completes normally if there were no exceptions.- Parameters:
objects- objects to close- Throws:
java.io.IOException
-
close
public static void close(java.lang.Exception e, java.io.Closeable... objects) throws java.io.IOExceptionCloses all givenCloseables. Some of the Closeables may be null; they are ignored. After everything is closed, the method adds any exceptions as suppressed to the original exception, or throws the first exception it hit ifExceptionis null. If no exceptions are encountered and the passed in exception is null, it completes normally.- Parameters:
objects- objects to close- Throws:
java.io.IOException
-
close
public static void close(java.lang.Iterable<? extends java.io.Closeable> objects) throws java.io.IOExceptionCloses all givenCloseables. 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 with other exceptions added as suppressed, or completes normally if there were no exceptions.- Parameters:
objects- objects to close- Throws:
java.io.IOException
-
close
public static void close(java.lang.Exception ex, java.lang.Iterable<? extends java.io.Closeable> objects) throws java.io.IOExceptionCloses all givenCloseables. If a non-null exception is passed in, or closing a stream causes an exception, throws the exception with otherRuntimeExceptionorIOExceptionexceptions added as suppressed.- Parameters:
ex- existing Exception to add exceptions occurring during close toobjects- objects to close- Throws:
java.io.IOException- See Also:
close(Closeable...)
-
closeWhileHandlingException
public static void closeWhileHandlingException(java.io.Closeable... objects)Closes all givenCloseables, suppressing all thrown exceptions. Some of theCloseables may be null, they are ignored.- Parameters:
objects- objects to close
-
closeWhileHandlingException
public static void closeWhileHandlingException(java.lang.Iterable<? extends java.io.Closeable> objects)Closes all givenCloseables, suppressing all thrown exceptions.- Parameters:
objects- objects to close- See Also:
closeWhileHandlingException(Closeable...)
-
deleteFilesIgnoringExceptions
public static void deleteFilesIgnoringExceptions(java.nio.file.Path... files)Deletes all given files, suppressing all thrownIOExceptions. Some of the files may be null, if so they are ignored.- Parameters:
files- the paths of files to delete
-
deleteFilesIgnoringExceptions
public static void deleteFilesIgnoringExceptions(java.util.Collection<? extends java.nio.file.Path> files)Deletes all given files, suppressing all thrownIOExceptions. Some of the files may be null, if so they are ignored.- Parameters:
files- the paths of files to delete
-
rm
public static void rm(java.nio.file.Path... locations) throws java.io.IOExceptionDeletes one or more files or directories (and everything underneath it).- Throws:
java.io.IOException- if any of the given files (or their sub-hierarchy files in case of directories) cannot be removed.
-
fsync
public static void fsync(java.nio.file.Path fileToSync, boolean isDir) throws java.io.IOExceptionEnsure that any writes to the given file is written to the storage device that contains it. TheisDirparameter specifies whether or not the path to sync is a directory. This is needed because we open for read and ignore anIOExceptionsince not all filesystems and operating systems support fsyncing on a directory. For regular files we must open for write for the fsync to have an effect.- Parameters:
fileToSync- the file to fsyncisDir- if true, the given file is a directory (we open for read and ignoreIOExceptions, because not all file systems and operating systems allow to fsync on a directory)- Throws:
java.io.IOException
-