Package org.apache.flink.util
Class IOUtils
- java.lang.Object
-
- org.apache.flink.util.IOUtils
-
public final class IOUtils extends Object
An utility class for I/O related functionality.
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static void
cleanup(org.slf4j.Logger log, AutoCloseable... closeables)
Close the AutoCloseable objects and ignore anyException
or null pointers.static void
closeAll(AutoCloseable... closeables)
static void
closeAll(Iterable<? extends AutoCloseable> closeables)
Closes allAutoCloseable
objects in the parameter, suppressing exceptions.static <T extends Throwable>
voidcloseAll(Iterable<? extends AutoCloseable> closeables, Class<T> suppressedException)
Closes allAutoCloseable
objects in the parameter, suppressing exceptions.static void
closeAllQuietly(AutoCloseable... closeables)
Closes all elements in the iterable with closeQuietly().static void
closeAllQuietly(Iterable<? extends AutoCloseable> closeables)
Closes all elements in the iterable with closeQuietly().static void
closeQuietly(AutoCloseable closeable)
Closes the given AutoCloseable.static void
closeSocket(Socket sock)
Closes the socket ignoringIOException
.static void
closeStream(Closeable stream)
Closes the stream ignoringIOException
.static void
copyBytes(InputStream in, OutputStream out)
Copies from one stream to another.static void
copyBytes(InputStream in, OutputStream out, boolean close)
Copies from one stream to another.static void
copyBytes(InputStream in, OutputStream out, int buffSize, boolean close)
Copies from one stream to another.static void
deleteFileQuietly(Path path)
Deletes the given file.static void
deleteFilesRecursively(Path path)
Delete the given directory or file recursively.static void
readFully(InputStream in, byte[] buf, int off, int len)
Reads len bytes in a loop.static void
skipFully(InputStream in, long len)
Similar to readFully().static int
tryReadFully(InputStream in, byte[] buf)
Similar toreadFully(InputStream, byte[], int, int)
.
-
-
-
Method Detail
-
copyBytes
public static void copyBytes(InputStream in, OutputStream out, int buffSize, boolean close) throws IOException
Copies from one stream to another.- Parameters:
in
- InputStream to read fromout
- OutputStream to write tobuffSize
- the size of the bufferclose
- whether or not close the InputStream and OutputStream at the end. The streams are closed in the finally clause.- Throws:
IOException
- thrown if an error occurred while writing to the output stream
-
copyBytes
public static void copyBytes(InputStream in, OutputStream out) throws IOException
Copies from one stream to another. closes the input and output streams at the end.- Parameters:
in
- InputStream to read fromout
- OutputStream to write to- Throws:
IOException
- thrown if an I/O error occurs while copying
-
copyBytes
public static void copyBytes(InputStream in, OutputStream out, boolean close) throws IOException
Copies from one stream to another.- Parameters:
in
- InputStream to read fromout
- OutputStream to write toclose
- whether or not close the InputStream and OutputStream at the end. The streams are closed in the finally clause.- Throws:
IOException
- thrown if an I/O error occurs while copying
-
readFully
public static void readFully(InputStream in, byte[] buf, int off, int len) throws IOException
Reads len bytes in a loop.- Parameters:
in
- The InputStream to read frombuf
- The buffer to filloff
- offset from the bufferlen
- the length of bytes to read- Throws:
IOException
- if it could not read requested number of bytes for any reason (including EOF)
-
tryReadFully
public static int tryReadFully(InputStream in, byte[] buf) throws IOException
Similar toreadFully(InputStream, byte[], int, int)
. Returns the total number of bytes read into the buffer.- Parameters:
in
- The InputStream to read frombuf
- The buffer to fill- Returns:
- The total number of bytes read into the buffer
- Throws:
IOException
- If the first byte cannot be read for any reason other than end of file, or if the input stream has been closed, or if some other I/O error occurs.
-
skipFully
public static void skipFully(InputStream in, long len) throws IOException
Similar to readFully(). Skips bytes in a loop.- Parameters:
in
- The InputStream to skip bytes fromlen
- number of bytes to skip- Throws:
IOException
- if it could not skip requested number of bytes for any reason (including EOF)
-
cleanup
public static void cleanup(org.slf4j.Logger log, AutoCloseable... closeables)
Close the AutoCloseable objects and ignore anyException
or null pointers. Must only be used for cleanup in exception handlers.- Parameters:
log
- the log to record problems to at debug level. Can benull
.closeables
- the objects to close
-
closeStream
public static void closeStream(Closeable stream)
Closes the stream ignoringIOException
. Must only be called in cleaning up from exception handlers.- Parameters:
stream
- the stream to close
-
closeSocket
public static void closeSocket(Socket sock)
Closes the socket ignoringIOException
.- Parameters:
sock
- the socket to close
-
closeAll
public static void closeAll(AutoCloseable... closeables) throws Exception
- Throws:
Exception
- See Also:
closeAll(Iterable)
-
closeAll
public static void closeAll(Iterable<? extends AutoCloseable> closeables) throws Exception
Closes allAutoCloseable
objects in the parameter, suppressing exceptions. Exception will be emitted after calling close() on every object.- Parameters:
closeables
- iterable with closeables to close.- Throws:
Exception
- collected exceptions that occurred during closing
-
closeAll
public static <T extends Throwable> void closeAll(Iterable<? extends AutoCloseable> closeables, Class<T> suppressedException) throws Exception
Closes allAutoCloseable
objects in the parameter, suppressing exceptions. Exception will be emitted after calling close() on every object.- Parameters:
closeables
- iterable with closeables to close.suppressedException
- class of exceptions which should be suppressed during the closing.- Throws:
Exception
- collected exceptions that occurred during closing
-
closeAllQuietly
public static void closeAllQuietly(AutoCloseable... closeables)
Closes all elements in the iterable with closeQuietly().
-
closeAllQuietly
public static void closeAllQuietly(Iterable<? extends AutoCloseable> closeables)
Closes all elements in the iterable with closeQuietly().
-
closeQuietly
public static void closeQuietly(AutoCloseable closeable)
Closes the given AutoCloseable.Important: This method is expected to never throw an exception.
-
deleteFilesRecursively
public static void deleteFilesRecursively(Path path) throws Exception
Delete the given directory or file recursively.- Throws:
Exception
-
deleteFileQuietly
public static void deleteFileQuietly(Path path)
Deletes the given file.Important: This method is expected to never throw an exception.
-
-