public final class IOUtils extends Object
Modifier and Type | Method and Description |
---|---|
static void |
cleanup(org.slf4j.Logger log,
AutoCloseable... closeables)
Close the AutoCloseable objects and ignore any
Exception or null pointers. |
static void |
closeAll(AutoCloseable... closeables) |
static void |
closeAll(Iterable<? extends AutoCloseable> closeables)
Closes all
AutoCloseable objects in the parameter, suppressing exceptions. |
static <T extends Throwable> |
closeAll(Iterable<? extends AutoCloseable> closeables,
Class<T> suppressedException)
Closes all
AutoCloseable 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 ignoring
IOException . |
static void |
closeStream(Closeable stream)
Closes the stream ignoring
IOException . |
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 to
readFully(InputStream, byte[], int, int) . |
public static void copyBytes(InputStream in, OutputStream out, int buffSize, boolean close) throws IOException
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.IOException
- thrown if an error occurred while writing to the output streampublic static void copyBytes(InputStream in, OutputStream out) throws IOException
in
- InputStream to read fromout
- OutputStream to write toIOException
- thrown if an I/O error occurs while copyingpublic static void copyBytes(InputStream in, OutputStream out, boolean close) throws IOException
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.IOException
- thrown if an I/O error occurs while copyingpublic static void readFully(InputStream in, byte[] buf, int off, int len) throws IOException
in
- The InputStream to read frombuf
- The buffer to filloff
- offset from the bufferlen
- the length of bytes to readIOException
- if it could not read requested number of bytes for any reason (including
EOF)public static int tryReadFully(InputStream in, byte[] buf) throws IOException
readFully(InputStream, byte[], int, int)
. Returns the total number of
bytes read into the buffer.in
- The InputStream to read frombuf
- The buffer to fillIOException
- 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.public static void skipFully(InputStream in, long len) throws IOException
in
- The InputStream to skip bytes fromlen
- number of bytes to skipIOException
- if it could not skip requested number of bytes for any reason (including
EOF)public static void cleanup(org.slf4j.Logger log, AutoCloseable... closeables)
Exception
or null pointers.
Must only be used for cleanup in exception handlers.log
- the log to record problems to at debug level. Can be null
.closeables
- the objects to closepublic static void closeStream(Closeable stream)
IOException
. Must only be called in cleaning up from
exception handlers.stream
- the stream to closepublic static void closeSocket(Socket sock)
IOException
.sock
- the socket to closepublic static void closeAll(AutoCloseable... closeables) throws Exception
Exception
closeAll(Iterable)
public static void closeAll(Iterable<? extends AutoCloseable> closeables) throws Exception
AutoCloseable
objects in the parameter, suppressing exceptions. Exception
will be emitted after calling close() on every object.closeables
- iterable with closeables to close.Exception
- collected exceptions that occurred during closingpublic static <T extends Throwable> void closeAll(Iterable<? extends AutoCloseable> closeables, Class<T> suppressedException) throws Exception
AutoCloseable
objects in the parameter, suppressing exceptions. Exception
will be emitted after calling close() on every object.closeables
- iterable with closeables to close.suppressedException
- class of exceptions which should be suppressed during the closing.Exception
- collected exceptions that occurred during closingpublic static void closeAllQuietly(AutoCloseable... closeables)
public static void closeAllQuietly(Iterable<? extends AutoCloseable> closeables)
public static void closeQuietly(AutoCloseable closeable)
Important: This method is expected to never throw an exception.
public static void deleteFilesRecursively(Path path) throws Exception
Exception
public static void deleteFileQuietly(Path path)
Important: This method is expected to never throw an exception.
Copyright © 2014–2024 The Apache Software Foundation. All rights reserved.