Package org.agrona
Class IoUtil
- java.lang.Object
-
- org.agrona.IoUtil
-
public final class IoUtil extends java.lang.Object
Collection of IO utilities for dealing with files, especially mapping and un-mapping.
-
-
Field Summary
Fields Modifier and Type Field Description static int
BLOCK_SIZE
Size in bytes of a file page.private static byte[]
FILLER
private static int
MAP_PRIVATE
private static int
MAP_READ_ONLY
private static int
MAP_READ_WRITE
-
Constructor Summary
Constructors Modifier Constructor Description private
IoUtil()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static void
checkFileExists(java.io.File file, java.lang.String name)
Check that a file exists and throw an exception if not.static java.nio.channels.FileChannel
createEmptyFile(java.io.File file, long length)
Create an empty file, fill with 0s, and return theFileChannel
.static java.nio.channels.FileChannel
createEmptyFile(java.io.File file, long length, boolean fillWithZeros)
Create an empty file, and optionally fill with 0s, and return theFileChannel
.static void
delete(java.io.File file, boolean ignoreFailures)
Recursively delete a file or directory tree.static void
delete(java.io.File file, ErrorHandler errorHandler)
Recursively delete a file or directory tree.static void
deleteIfExists(java.io.File file)
Delete file only if it already exists.static void
deleteIfExists(java.io.File file, ErrorHandler errorHandler)
Delete file only if it already exists.static void
ensureDirectoryExists(java.io.File directory, java.lang.String descriptionLabel)
Create a directory if it doesn't already exist.static void
ensureDirectoryIsRecreated(java.io.File directory, java.lang.String descriptionLabel, java.util.function.BiConsumer<java.lang.String,java.lang.String> callback)
Create a directory, removing previous directory if it already exists.static void
fill(java.nio.channels.FileChannel fileChannel, long position, long length, byte value)
Fill region of a file with a given byte value.private static java.lang.String
getFileMode(java.nio.channels.FileChannel.MapMode mode)
static java.nio.MappedByteBuffer
mapExistingFile(java.io.File location, java.lang.String descriptionLabel)
Check that file exists, open file, and return MappedByteBuffer for entire file asFileChannel.MapMode.READ_WRITE
.static java.nio.MappedByteBuffer
mapExistingFile(java.io.File location, java.lang.String descriptionLabel, long offset, long length)
Check that file exists, open file, and return MappedByteBuffer for only region specified asFileChannel.MapMode.READ_WRITE
.static java.nio.MappedByteBuffer
mapExistingFile(java.io.File location, java.nio.channels.FileChannel.MapMode mapMode, java.lang.String descriptionLabel)
Check that file exists, open file, and return MappedByteBuffer for entire file for a givenFileChannel.MapMode
.static java.nio.MappedByteBuffer
mapExistingFile(java.io.File location, java.nio.channels.FileChannel.MapMode mapMode, java.lang.String descriptionLabel, long offset, long length)
Check that file exists, open file, and return MappedByteBuffer for only region specified for a givenFileChannel.MapMode
.static java.nio.MappedByteBuffer
mapNewFile(java.io.File location, long length)
Create a new file, fill with 0s, and return aMappedByteBuffer
for the file.static java.nio.MappedByteBuffer
mapNewFile(java.io.File location, long length, boolean fillWithZeros)
Create a new file, and optionally fill with 0s, and return aMappedByteBuffer
for the file.static void
removeTrailingSlashes(java.lang.StringBuilder builder)
Remove trailing slash characters from a builder leaving the remaining characters.static java.lang.String
tmpDirName()
Return the system property for java.io.tmpdir ensuring aFile.separator
is at the end.static void
unmap(java.nio.ByteBuffer buffer)
Unmap aByteBuffer
without waiting for the next GC cycle if its memory mapped.static void
unmap(java.nio.MappedByteBuffer buffer)
Unmap aMappedByteBuffer
without waiting for the next GC cycle.
-
-
-
Field Detail
-
BLOCK_SIZE
public static final int BLOCK_SIZE
Size in bytes of a file page.- See Also:
- Constant Field Values
-
FILLER
private static final byte[] FILLER
-
MAP_READ_ONLY
private static final int MAP_READ_ONLY
- See Also:
- Constant Field Values
-
MAP_READ_WRITE
private static final int MAP_READ_WRITE
- See Also:
- Constant Field Values
-
MAP_PRIVATE
private static final int MAP_PRIVATE
- See Also:
- Constant Field Values
-
-
Method Detail
-
fill
public static void fill(java.nio.channels.FileChannel fileChannel, long position, long length, byte value)
Fill region of a file with a given byte value.- Parameters:
fileChannel
- to fill.position
- at which to start writing.length
- of the region to write.value
- to fill the region with.
-
delete
public static void delete(java.io.File file, boolean ignoreFailures)
Recursively delete a file or directory tree.- Parameters:
file
- to be deleted.ignoreFailures
- don't throw an exception if delete fails.
-
delete
public static void delete(java.io.File file, ErrorHandler errorHandler)
Recursively delete a file or directory tree.- Parameters:
file
- to be deleted.errorHandler
- to delegate errors to on exception.
-
ensureDirectoryExists
public static void ensureDirectoryExists(java.io.File directory, java.lang.String descriptionLabel)
Create a directory if it doesn't already exist.- Parameters:
directory
- the directory which definitely exists after this method call.descriptionLabel
- to associate with the directory for any exceptions.
-
ensureDirectoryIsRecreated
public static void ensureDirectoryIsRecreated(java.io.File directory, java.lang.String descriptionLabel, java.util.function.BiConsumer<java.lang.String,java.lang.String> callback)
Create a directory, removing previous directory if it already exists.Call callback if it does exist.
- Parameters:
directory
- the directory which definitely exists after this method call.descriptionLabel
- to associate with the directory for any exceptions and callback.callback
- to call if directory exists passing back absolute path and descriptionLabel.
-
deleteIfExists
public static void deleteIfExists(java.io.File file)
Delete file only if it already exists.- Parameters:
file
- to delete.
-
deleteIfExists
public static void deleteIfExists(java.io.File file, ErrorHandler errorHandler)
Delete file only if it already exists.- Parameters:
file
- to delete.errorHandler
- to delegate error to on exception.
-
createEmptyFile
public static java.nio.channels.FileChannel createEmptyFile(java.io.File file, long length)
Create an empty file, fill with 0s, and return theFileChannel
.- Parameters:
file
- to create.length
- of the file to create.- Returns:
FileChannel
for the file.
-
createEmptyFile
public static java.nio.channels.FileChannel createEmptyFile(java.io.File file, long length, boolean fillWithZeros)
Create an empty file, and optionally fill with 0s, and return theFileChannel
.- Parameters:
file
- to create.length
- of the file to create.fillWithZeros
- to the length of the file to force allocation.- Returns:
FileChannel
for the file.
-
mapExistingFile
public static java.nio.MappedByteBuffer mapExistingFile(java.io.File location, java.lang.String descriptionLabel)
Check that file exists, open file, and return MappedByteBuffer for entire file asFileChannel.MapMode.READ_WRITE
.The file itself will be closed, but the mapping will persist.
- Parameters:
location
- of the file to map.descriptionLabel
- to be associated for any exceptions.- Returns:
MappedByteBuffer
for the file.
-
mapExistingFile
public static java.nio.MappedByteBuffer mapExistingFile(java.io.File location, java.lang.String descriptionLabel, long offset, long length)
Check that file exists, open file, and return MappedByteBuffer for only region specified asFileChannel.MapMode.READ_WRITE
.The file itself will be closed, but the mapping will persist.
- Parameters:
location
- of the file to map.descriptionLabel
- to be associated for an exceptions.offset
- offset to start mapping at.length
- length to map region.- Returns:
MappedByteBuffer
for the file.
-
mapExistingFile
public static java.nio.MappedByteBuffer mapExistingFile(java.io.File location, java.nio.channels.FileChannel.MapMode mapMode, java.lang.String descriptionLabel)
Check that file exists, open file, and return MappedByteBuffer for entire file for a givenFileChannel.MapMode
.The file itself will be closed, but the mapping will persist.
- Parameters:
location
- of the file to map.mapMode
- for the mapping.descriptionLabel
- to be associated for any exceptions.- Returns:
MappedByteBuffer
for the file.
-
mapExistingFile
public static java.nio.MappedByteBuffer mapExistingFile(java.io.File location, java.nio.channels.FileChannel.MapMode mapMode, java.lang.String descriptionLabel, long offset, long length)
Check that file exists, open file, and return MappedByteBuffer for only region specified for a givenFileChannel.MapMode
.The file itself will be closed, but the mapping will persist.
- Parameters:
location
- of the file to map.mapMode
- for the mapping.descriptionLabel
- to be associated for an exceptions.offset
- offset to start mapping at.length
- length to map region.- Returns:
MappedByteBuffer
for the file.
-
mapNewFile
public static java.nio.MappedByteBuffer mapNewFile(java.io.File location, long length)
Create a new file, fill with 0s, and return aMappedByteBuffer
for the file.The file itself will be closed, but the mapping will persist.
- Parameters:
location
- of the file to create and map.length
- of the file to create and map.- Returns:
MappedByteBuffer
for the file.
-
mapNewFile
public static java.nio.MappedByteBuffer mapNewFile(java.io.File location, long length, boolean fillWithZeros)
Create a new file, and optionally fill with 0s, and return aMappedByteBuffer
for the file.The file itself will be closed, but the mapping will persist.
- Parameters:
location
- of the file to create and map.length
- of the file to create and map.fillWithZeros
- to force allocation.- Returns:
MappedByteBuffer
for the file.
-
checkFileExists
public static void checkFileExists(java.io.File file, java.lang.String name)
Check that a file exists and throw an exception if not.- Parameters:
file
- to check existence of.name
- to associate for the exception.
-
unmap
public static void unmap(java.nio.MappedByteBuffer buffer)
Unmap aMappedByteBuffer
without waiting for the next GC cycle.- Parameters:
buffer
- to be unmapped.- See Also:
BufferUtil.free(ByteBuffer)
-
unmap
public static void unmap(java.nio.ByteBuffer buffer)
Unmap aByteBuffer
without waiting for the next GC cycle if its memory mapped.- Parameters:
buffer
- to be unmapped.
-
tmpDirName
public static java.lang.String tmpDirName()
Return the system property for java.io.tmpdir ensuring aFile.separator
is at the end.- Returns:
- tmp directory for the runtime.
-
removeTrailingSlashes
public static void removeTrailingSlashes(java.lang.StringBuilder builder)
Remove trailing slash characters from a builder leaving the remaining characters.- Parameters:
builder
- to remove trailing slash characters from.
-
getFileMode
private static java.lang.String getFileMode(java.nio.channels.FileChannel.MapMode mode)
-
-