Package org.agrona
Class IoUtil
java.lang.Object
org.agrona.IoUtil
Collection of IO utilities for dealing with files, especially mapping and un-mapping.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final int
Size in bytes of a file page.private static final byte[]
private static final int
private static final int
private static final int
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic void
checkFileExists
(File file, String name) Check that a file exists and throw an exception if not.static FileChannel
createEmptyFile
(File file, long length) Create an empty file, fill with 0s, and return theFileChannel
.static FileChannel
createEmptyFile
(File file, long length, boolean fillWithZeros) Create an empty file, and optionally fill with 0s, and return theFileChannel
.static void
Recursively delete a file or directory tree.static void
delete
(File file, ErrorHandler errorHandler) Recursively delete a file or directory tree.static void
deleteIfExists
(File file) Delete file only if it already exists.static void
deleteIfExists
(File file, ErrorHandler errorHandler) Delete file only if it already exists.static void
ensureDirectoryExists
(File directory, String descriptionLabel) Create a directory if it doesn't already exist.static void
ensureDirectoryIsRecreated
(File directory, String descriptionLabel, BiConsumer<String, String> callback) Create a directory, removing previous directory if it already exists.static void
fill
(FileChannel fileChannel, long position, long length, byte value) Fill region of a file with a given byte value.private static String
static MappedByteBuffer
mapExistingFile
(File location, String descriptionLabel) Check that file exists, open file, and return MappedByteBuffer for entire file asFileChannel.MapMode.READ_WRITE
.static MappedByteBuffer
mapExistingFile
(File location, String descriptionLabel, long offset, long length) Check that file exists, open file, and return MappedByteBuffer for only region specified asFileChannel.MapMode.READ_WRITE
.static MappedByteBuffer
mapExistingFile
(File location, FileChannel.MapMode mapMode, String descriptionLabel) Check that file exists, open file, and return MappedByteBuffer for entire file for a givenFileChannel.MapMode
.static MappedByteBuffer
mapExistingFile
(File location, FileChannel.MapMode mapMode, String descriptionLabel, long offset, long length) Check that file exists, open file, and return MappedByteBuffer for only region specified for a givenFileChannel.MapMode
.static MappedByteBuffer
mapNewFile
(File location, long length) Create a new file, fill with 0s, and return aMappedByteBuffer
for the file.static MappedByteBuffer
mapNewFile
(File location, long length, boolean fillWithZeros) Create a new file, and optionally fill with 0s, and return aMappedByteBuffer
for the file.static void
removeTrailingSlashes
(StringBuilder builder) Remove trailing slash characters from a builder leaving the remaining characters.static String
Return the system property for java.io.tmpdir ensuring aFile.separator
is at the end.static void
unmap
(ByteBuffer buffer) Unmap aByteBuffer
without waiting for the next GC cycle if its memory mapped.static void
unmap
(MappedByteBuffer buffer) Unmap aMappedByteBuffer
without waiting for the next GC cycle.
-
Field Details
-
BLOCK_SIZE
public static final int BLOCK_SIZESize in bytes of a file page.- See Also:
-
FILLER
private static final byte[] FILLER -
MAP_READ_ONLY
private static final int MAP_READ_ONLY- See Also:
-
MAP_READ_WRITE
private static final int MAP_READ_WRITE- See Also:
-
MAP_PRIVATE
private static final int MAP_PRIVATE- See Also:
-
-
Constructor Details
-
IoUtil
private IoUtil()
-
-
Method Details
-
fill
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
Recursively delete a file or directory tree.- Parameters:
file
- to be deleted.ignoreFailures
- don't throw an exception if delete fails.
-
delete
Recursively delete a file or directory tree.- Parameters:
file
- to be deleted.errorHandler
- to delegate errors to on exception.
-
ensureDirectoryExists
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(File directory, String descriptionLabel, BiConsumer<String, 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
Delete file only if it already exists.- Parameters:
file
- to delete.
-
deleteIfExists
Delete file only if it already exists.- Parameters:
file
- to delete.errorHandler
- to delegate error to on exception.
-
createEmptyFile
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
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
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 MappedByteBuffer mapExistingFile(File location, 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 MappedByteBuffer mapExistingFile(File location, FileChannel.MapMode mapMode, 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 MappedByteBuffer mapExistingFile(File location, FileChannel.MapMode mapMode, 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
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
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
Check that a file exists and throw an exception if not.- Parameters:
file
- to check existence of.name
- to associate for the exception.
-
unmap
Unmap aMappedByteBuffer
without waiting for the next GC cycle.- Parameters:
buffer
- to be unmapped.- See Also:
-
unmap
Unmap aByteBuffer
without waiting for the next GC cycle if its memory mapped.- Parameters:
buffer
- to be unmapped.
-
tmpDirName
Return the system property for java.io.tmpdir ensuring aFile.separator
is at the end.- Returns:
- tmp directory for the runtime.
-
removeTrailingSlashes
Remove trailing slash characters from a builder leaving the remaining characters.- Parameters:
builder
- to remove trailing slash characters from.
-
getFileMode
-