public final class IOUtils extends Object
| Modifier and Type | Method and Description |
|---|---|
static IOException |
closeAndLogException(Closeable closeable,
org.apache.commons.logging.Log logger,
String resourceName,
IOException initialException)
Try to close an IO resource and log and return if there was an exception.
|
static void |
closeQuietly(Closeable closeable)
Null safe close of the given
Closeable suppressing any exception. |
static long |
copy(InputStream input,
OutputStream output)
Copies all the contents from the given input stream to the given output stream.
|
static RandomAccessStreamCache.StreamCacheCreateFunction |
createMemoryOnlyStreamCache()
Provides a function to create an instance of a memory only StreamCache using unrestricted main memory.
|
static Path |
createProtectedTempDir()
Creates a temporary directory in the default temporary-file directory
with owner-only permissions and registers a shutdown hook to delete it on JVM exit.
|
static Path |
createProtectedTempFile(Path dir,
String prefix,
String suffix)
Creates a temporary file in the specified directory (or default temporary-file directory
if null) with owner-only permissions.
|
static RandomAccessStreamCache.StreamCacheCreateFunction |
createTempFileOnlyStreamCache()
Provides a function to create an instance of a temp file only StreamCache using unrestricted size.
|
static long |
populateBuffer(InputStream in,
byte[] buffer)
Populates the given buffer with data read from the input stream.
|
static byte[] |
toByteArray(InputStream in)
Reads the input stream and returns its contents as a byte array.
|
static void |
unmap(ByteBuffer buf)
Unmap memory mapped byte buffers.
|
public static byte[] toByteArray(InputStream in) throws IOException
in - the input stream to read from.IOException - if an I/O error occurspublic static long copy(InputStream input, OutputStream output) throws IOException
input - the input streamoutput - the output streamIOException - if an I/O error occurspublic static long populateBuffer(InputStream in, byte[] buffer) throws IOException
in - the input stream to read frombuffer - the buffer to fillIOException - if an I/O error occurspublic static void closeQuietly(Closeable closeable)
Closeable suppressing any exception.closeable - to be closedpublic static IOException closeAndLogException(Closeable closeable, org.apache.commons.logging.Log logger, String resourceName, IOException initialException)
An exception is only returned if the IOException passed in is null.
closeable - to be closedlogger - the logger to be used so that logging appears under that log instanceresourceName - the name to appear in the log outputinitialException - if set, this exception will be returned even where there is another
exception while closing the IO resourcepublic static void unmap(ByteBuffer buf)
buf - the buffer to be unmappedpublic static RandomAccessStreamCache.StreamCacheCreateFunction createMemoryOnlyStreamCache()
public static RandomAccessStreamCache.StreamCacheCreateFunction createTempFileOnlyStreamCache()
public static Path createProtectedTempDir() throws IOException
Note: This method is designed to be used for storing temporary files that may contain sensitive data in a temporary directories with restricted permissions, to mitigate the risk of unauthorized access by other users or processes on the same system. Used e.g. by PDFDebugger.
IOExceptionpublic static Path createProtectedTempFile(Path dir, String prefix, String suffix) throws IOException
This method attempts to set owner-only permissions at file creation time when supported, to minimize the time window during which the file may have default (world-readable) permissions. On POSIX systems (Linux, macOS), permissions are set during creation. On Windows, permissions are set after file creation via ACL.
Note: This method is designed for storing temporary files that may contain sensitive data
in a temporary directory with restricted permissions, to mitigate the risk of unauthorized
access by other users or processes on the same system. However, unlike createProtectedTempDir(),
this method does NOT automatically delete the file on JVM shutdown. The caller is responsible
for deleting the temporary file when no longer needed. Used e.g. by PDFDebugger.
dir - the directory in which to create the temporary file, or null to use the default
temporary-file directoryprefix - the prefix string to be used in generating the file's name; may be nullsuffix - the suffix string to be used in generating the file's name; may be nullIOException - if an I/O error occurs during file creation or permission settingSecurityException - if a security manager is installed and denies accesscreateProtectedTempDir(),
Files.createTempFile(Path, String, String, FileAttribute[])Copyright © 2002–2026 The Apache Software Foundation. All rights reserved.