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
Fields -
Method Summary
Modifier and TypeMethodDescriptionstatic voidcheckFileExists(File file, String name) Check that a file exists and throw an exception if not.static FileChannelcreateEmptyFile(File file, long length) Create an empty file, fill with 0s, and return theFileChannel.static FileChannelcreateEmptyFile(File file, long length, boolean fillWithZeros) Create an empty file, and optionally fill with 0s, and return theFileChannel.static voidRecursively delete a file or directory tree.static voiddelete(File file, ErrorHandler errorHandler) Recursively delete a file or directory tree.static voiddeleteIfExists(File file) Delete file only if it already exists.static voiddeleteIfExists(File file, ErrorHandler errorHandler) Delete file only if it already exists.static voidensureDirectoryExists(File directory, String descriptionLabel) Create a directory if it doesn't already exist.static voidensureDirectoryIsRecreated(File directory, String descriptionLabel, BiConsumer<String, String> callback) Create a directory, removing previous directory if it already exists.static voidfill(FileChannel fileChannel, long position, long length, byte value) Fill region of a file with a given byte value.static MappedByteBuffermapExistingFile(File location, String descriptionLabel) Check that file exists, open file, and return MappedByteBuffer for entire file asFileChannel.MapMode.READ_WRITE.static MappedByteBuffermapExistingFile(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 MappedByteBuffermapExistingFile(File location, FileChannel.MapMode mapMode, String descriptionLabel) Check that file exists, open file, and return MappedByteBuffer for entire file for a givenFileChannel.MapMode.static MappedByteBuffermapExistingFile(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 MappedByteBuffermapNewFile(File location, long length) Create a new file, fill with 0s, and return aMappedByteBufferfor the file.static MappedByteBuffermapNewFile(File location, long length, boolean fillWithZeros) Create a new file, and optionally fill with 0s, and return aMappedByteBufferfor the file.static voidremoveTrailingSlashes(StringBuilder builder) Remove trailing slash characters from a builder leaving the remaining characters.static StringReturn the system property for java.io.tmpdir ensuring aFile.separatoris at the end.static voidunmap(ByteBuffer buffer) Unmap aByteBufferwithout waiting for the next GC cycle if its memory mapped.static voidunmap(MappedByteBuffer buffer) Unmap aMappedByteBufferwithout waiting for the next GC cycle.
-
Field Details
-
BLOCK_SIZE
public static final int BLOCK_SIZESize in bytes of a file page.- See Also:
-
-
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:
FileChannelfor 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:
FileChannelfor 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:
MappedByteBufferfor 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:
MappedByteBufferfor 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:
MappedByteBufferfor 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:
MappedByteBufferfor the file.
-
mapNewFile
Create a new file, fill with 0s, and return aMappedByteBufferfor 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:
MappedByteBufferfor the file.
-
mapNewFile
Create a new file, and optionally fill with 0s, and return aMappedByteBufferfor 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:
MappedByteBufferfor 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 aMappedByteBufferwithout waiting for the next GC cycle.- Parameters:
buffer- to be unmapped.- See Also:
-
unmap
Unmap aByteBufferwithout 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.separatoris 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.
-