public final class IOTools extends Object
Methods are grouped by purpose:
ByteBuffer instances.deleteDirWithFilesOrThrow(File...) before a
test starts to ensure any previous state has been removed.| Modifier and Type | Field and Description |
|---|---|
static int |
IOSTATUS_INTERRUPTED |
| Modifier and Type | Method and Description |
|---|---|
static long |
addressFor(ByteBuffer byteBuffer)
Gets the memory address of the given ByteBuffer.
|
static void |
clean(ByteBuffer bb)
Calls the system's Cleaner Service to clean the given ByteBuffer.
|
static void |
createDirectories(Path dir)
Creates a directory at the specified path, including any necessary
but nonexistent parent directories.
|
static Path |
createTempDirectory(String s)
Creates a temporary directory with the specified name.
|
static File |
createTempFile(String s)
Creates a temporary file in a temporary directory with the specified name.
|
static boolean |
deleteDirWithFiles(@NotNull File dir)
Attempts to delete directories and their files.
|
static boolean |
deleteDirWithFiles(@NotNull File dir,
int maxDepth)
Attempts to delete directories and their files.
|
static boolean |
deleteDirWithFiles(String... dirs)
Deletes each supplied directory and its contents.
|
static boolean |
deleteDirWithFiles(@NotNull String dir,
int maxDepth)
Attempts to delete the specified directory and its files recursively up to a
given depth.
|
static void |
deleteDirWithFilesOrThrow(File... dirs)
Variant accepting
File objects. |
static void |
deleteDirWithFilesOrThrow(String... dirs)
Removes the supplied directories and throws if any remain.
|
static void |
deleteDirWithFilesOrWait(long timeoutMs,
@NotNull File dir)
Polls until the supplied directory disappears or the timeout elapses.
|
static void |
destroyProcess(@NotNull Process process)
Best-effort cleanup of a child
Process: waits up to one second
for the process to exit, calls Process.destroy(), waits up to
another second, and finally escalates to Process.destroyForcibly()
if the child is still alive. |
static boolean |
isClosedException(Exception e)
Tests whether an exception was thrown because a network channel was
already closed.
|
static boolean |
isDirectBuffer(ByteBuffer byteBuffer)
Checks if a ByteBuffer is a direct buffer.
|
static int |
normaliseIOStatus(int n)
Normalizes an I/O operation status code.
|
static InputStream |
open(URL url)
Creates and returns a new InputStream from the provided
url. |
static byte[] |
readAsBytes(InputStream is)
Reads data from an input stream and returns it as a byte array.
|
static byte[] |
readFile(Class<?> clazz,
@NotNull String name)
This method first looks for the file in the classpath.
|
static boolean |
shallowDeleteDirWithFiles(@NotNull File dir)
Attempts to delete a directory with its files.
|
static boolean |
shallowDeleteDirWithFiles(@NotNull String directory)
Attempts to delete a directory with its files.
|
static @NotNull String |
tempName(@NotNull String filename)
Creates a temporary name for a file by appending the system's current
nanosecond time to the file name.
|
static void |
unmonitor(Object t)
Stops the monitoring of the specified object.
|
static @NotNull URL |
urlFor(Class<?> clazz,
String name)
Ensures that directory is absent or deleted, awaits for given timeout if necessary.
|
static @NotNull URL |
urlFor(ClassLoader classLoader,
String name)
Ensures that directory is absent or deleted, awaits for given timeout if necessary.
|
static void |
writeFile(@NotNull String filename,
byte[] bytes)
Writes the given byte array to a file with the specified name.
|
public static final int IOSTATUS_INTERRUPTED
public static boolean isClosedException(Exception e)
The check compares the message against a set of operating system specific
texts. It is therefore best-efforts and may return false even when
the connection is gone. It is intended only to quieten logging once the
application has detected a disconnect.
e - candidate exceptiontrue if the message matches a known closed connection errorpublic static boolean shallowDeleteDirWithFiles(@NotNull
@NotNull String directory)
throws IORuntimeException
directory - The directory to be deletedIORuntimeException - if an I/O error occurspublic static boolean shallowDeleteDirWithFiles(@NotNull
@NotNull File dir)
throws IORuntimeException
dir - The directory to be deletedIORuntimeException - if an I/O error occurspublic static boolean deleteDirWithFiles(@NotNull
String... dirs)
throws IORuntimeException
IORuntimeExceptionpublic static boolean deleteDirWithFiles(@NotNull
@NotNull String dir,
int maxDepth)
throws IORuntimeException
AssertionError is thrown.dir - The directory to be deletedmaxDepth - The maximum depth of directories to be deletedIORuntimeException - if an I/O error occurspublic static boolean deleteDirWithFiles(@NotNull
@NotNull File dir)
throws IORuntimeException
AssertionError.dir - The directories to be deletedIORuntimeException - if an I/O error occurspublic static boolean deleteDirWithFiles(@NotNull
@NotNull File dir,
int maxDepth)
throws IORuntimeException
AssertionError.dir - The directories to be deletedmaxDepth - The maximum depth of directories to be deletedIORuntimeException - if an I/O error occurspublic static void deleteDirWithFilesOrThrow(@NotNull
String... dirs)
throws IORuntimeException
dirs - directories to removeIORuntimeExceptionpublic static void deleteDirWithFilesOrThrow(@NotNull
File... dirs)
throws IORuntimeException
File objects. Behaviour matches the
string-based method.dirs - directories to removeIORuntimeExceptionpublic static void deleteDirWithFilesOrWait(long timeoutMs,
@NotNull
@NotNull File dir)
timeoutMs - maximum time in milliseconds to waitdir - directory to removeAssertionError - if the directory remains after the timeoutpublic static void writeFile(@NotNull
@NotNull String filename,
byte[] bytes)
throws IOException
filename - The name of the file to write tobytes - The byte array to write to the fileIOException - If an I/O error occurs@NotNull public static @NotNull URL urlFor(Class<?> clazz, String name) throws FileNotFoundException
clazz - file to use to determine the class loader.name - Name of the file to find.FileNotFoundException - if the file is not found.@NotNull public static @NotNull URL urlFor(ClassLoader classLoader, String name) throws FileNotFoundException
classLoader - Class loader to use to find the file.name - Name of the file to find.FileNotFoundException - if the file is not found.public static InputStream open(URL url) throws IOException
url.
It is up to the caller to close the returned InputStream after being used. If this method throws, no resources remain open.
Gzip detection uses URL.getPath() so that a query string
(for example, .../foo.gz?token=abc) does not defeat the suffix
match. Unexpected throwables from GZIPInputStream's constructor
(for example, OutOfMemoryError) are deliberately allowed to
propagate after the underlying stream has been closed; they are not
silently swallowed.
url - to create an InputStream fromIOException - if the URL cannot be openedpublic static byte[] readFile(Class<?> clazz, @NotNull @NotNull String name) throws IOException
clazz - file to use to determine the class loader.name - Name of the fileIOException - FileNotFoundException thrown if file is not foundpublic static byte[] readAsBytes(InputStream is) throws IOException
is - The input stream to read data fromIOException - If an I/O error occurs@NotNull public static @NotNull String tempName(@NotNull @NotNull String filename)
filename - The original file namepublic static void createDirectories(Path dir) throws IOException
dir - The path of the directory to createIOException - If an I/O error occurspublic static File createTempFile(String s)
s - The name of the temporary file to createpublic static Path createTempDirectory(String s)
s - The name of the temporary directory to createpublic static void unmonitor(Object t)
t - The object to stop monitoringpublic static void clean(ByteBuffer bb)
bb - The ByteBuffer to cleanpublic static boolean isDirectBuffer(ByteBuffer byteBuffer)
byteBuffer - The ByteBuffer to checkpublic static long addressFor(ByteBuffer byteBuffer)
byteBuffer - The ByteBuffer for which to get the memory addresspublic static int normaliseIOStatus(int n)
n - The I/O status code to normalizepublic static void destroyProcess(@NotNull
@NotNull Process process)
Process: waits up to one second
for the process to exit, calls Process.destroy(), waits up to
another second, and finally escalates to Process.destroyForcibly()
if the child is still alive. When destruction is needed the pipe streams
are closed so file descriptors are not held until the Process
object is reaped. If the current thread is interrupted while waiting,
the interrupt flag is restored before destruction is attempted.process - the child process to tear down; must not be nullCopyright © 2026 Chronicle Software Ltd. All rights reserved.