public final class IOTools extends Object
It provides functionality such as:
It also handles some specific byte buffer operations and manages a
ConcurrentHashMap for tracking object counts of specific types.
Note: This class is designed to be thread-safe and is intended to be used concurrently.
| 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) |
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)
Canonical usage is to call this *before* your test, so you fail fast if you can't delete
|
static void |
deleteDirWithFilesOrThrow(String... dirs)
Canonical usage is to call this *before* your test, so you fail fast if you can't delete
|
static void |
deleteDirWithFilesOrWait(long timeoutMs,
@NotNull File dir)
Ensures that directory is absent or deleted, awaits for given timeout if necessary.
|
static boolean |
isClosedException(Exception e)
Is the passed exception one that results from reading from or writing to
a reset or closed connection?
|
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)
NOTE: This is not reliable and shouldn't be used for anything critical. We use it to make logging less noisy, false negatives are acceptable and expected. It should not produce false positives, but there's no guarantees it doesn't.
e - The exceptionpublic 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 - dirsIORuntimeExceptionpublic static void deleteDirWithFilesOrThrow(@NotNull
File... dirs)
throws IORuntimeException
dirs - dirsIORuntimeExceptionpublic static void deleteDirWithFilesOrWait(long timeoutMs,
@NotNull
@NotNull File dir)
timeoutMs - Time to ensure that the directory is absent.dir - Dir to delete.AssertionError - If timeout passed and the directory is still present.@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.
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 occurspublic 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 String tempName(@NotNull @NotNull String filename)
filename - The original file namepublic static void clean(ByteBuffer bb)
bb - The ByteBuffer to cleanpublic 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 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 normalizeCopyright © 2024. All rights reserved.