public class IoUtil extends Object
Modifier and Type | Field and Description |
---|---|
static int |
BLOCK_SIZE
Size in bytes of a file page.
|
Constructor and Description |
---|
IoUtil() |
Modifier and Type | Method and Description |
---|---|
static 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 the
FileChannel . |
static FileChannel |
createEmptyFile(File file,
long length,
boolean fillWithZeros)
Create an empty file, and optionally fill with 0s, and return the
FileChannel . |
static void |
delete(File file,
boolean ignoreFailures)
Recursively delete a file or directory tree.
|
static void |
deleteIfExists(File file)
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 a region of a file with a given byte value.
|
static long |
map(FileChannel fileChannel,
FileChannel.MapMode mode,
long offset,
long length)
Map a range of a file and return the address at which the range begins.
|
static MappedByteBuffer |
mapExistingFile(File location,
FileChannel.MapMode mapMode,
String descriptionLabel)
Check that file exists, open file, and return MappedByteBuffer for entire file for a given
FileChannel.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 given
FileChannel.MapMode . |
static MappedByteBuffer |
mapExistingFile(File location,
String descriptionLabel)
Check that file exists, open file, and return MappedByteBuffer for entire file as
FileChannel.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 as
FileChannel.MapMode.READ_WRITE . |
static MappedByteBuffer |
mapNewFile(File location,
long length)
Create a new file, fill with 0s, and return a
MappedByteBuffer for the file. |
static MappedByteBuffer |
mapNewFile(File location,
long length,
boolean fillWithZeros)
Create a new file, and optionally fill with 0s, and return a
MappedByteBuffer for the file. |
static String |
tmpDirName()
Return the system property for java.io.tmpdir ensuring a
File.separator is at the end. |
static void |
unmap(ByteBuffer buffer)
Unmap a
ByteBuffer without waiting for the next GC cycle if its memory mapped. |
static void |
unmap(FileChannel fileChannel,
long address,
long length)
Unmap a region of a file.
|
static void |
unmap(MappedByteBuffer buffer)
Unmap a
MappedByteBuffer without waiting for the next GC cycle. |
public static final int BLOCK_SIZE
public static void fill(FileChannel fileChannel, long position, long length, byte value)
fileChannel
- to fill.position
- at which to start writing.length
- of the region to write.value
- to fill the region with.public static void delete(File file, boolean ignoreFailures)
file
- to be deleted.ignoreFailures
- don't throw an exception if a delete fails.public static void ensureDirectoryExists(File directory, String descriptionLabel)
directory
- the directory which definitely exists after this method call.descriptionLabel
- to associate with the directory for any exceptions.public static void ensureDirectoryIsRecreated(File directory, String descriptionLabel, BiConsumer<String,String> callback)
Call callback if it does exist.
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.public static void deleteIfExists(File file)
file
- to delete.public static FileChannel createEmptyFile(File file, long length)
FileChannel
.file
- to create.length
- of the file to create.FileChannel
for the file.public static FileChannel createEmptyFile(File file, long length, boolean fillWithZeros)
FileChannel
.file
- to create.length
- of the file to create.fillWithZeros
- to the length of the file to force allocation.FileChannel
for the file.public static MappedByteBuffer mapExistingFile(File location, String descriptionLabel)
FileChannel.MapMode.READ_WRITE
.
The file itself will be closed, but the mapping will persist.
location
- of the file to map.descriptionLabel
- to be associated for any exceptions.MappedByteBuffer
for the file.public static MappedByteBuffer mapExistingFile(File location, String descriptionLabel, long offset, long length)
FileChannel.MapMode.READ_WRITE
.
The file itself will be closed, but the mapping will persist.
location
- of the file to map.descriptionLabel
- to be associated for an exceptions.offset
- offset to start mapping at.length
- length to map region.MappedByteBuffer
for the file.public static MappedByteBuffer mapExistingFile(File location, FileChannel.MapMode mapMode, String descriptionLabel)
FileChannel.MapMode
.
The file itself will be closed, but the mapping will persist.
location
- of the file to map.mapMode
- for the mapping.descriptionLabel
- to be associated for any exceptions.MappedByteBuffer
for the file.public static MappedByteBuffer mapExistingFile(File location, FileChannel.MapMode mapMode, String descriptionLabel, long offset, long length)
FileChannel.MapMode
.
The file itself will be closed, but the mapping will persist.
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.MappedByteBuffer
for the file.public static MappedByteBuffer mapNewFile(File location, long length)
MappedByteBuffer
for the file.
The file itself will be closed, but the mapping will persist.
location
- of the file to create and map.length
- of the file to create and map.MappedByteBuffer
for the file.public static MappedByteBuffer mapNewFile(File location, long length, boolean fillWithZeros)
MappedByteBuffer
for the file.
The file itself will be closed, but the mapping will persist.
location
- of the file to create and map.length
- of the file to create and map.fillWithZeros
- to force allocation..MappedByteBuffer
for the file.public static void checkFileExists(File file, String name)
file
- to check existence of.name
- to associate for the exception.public static void unmap(MappedByteBuffer buffer)
MappedByteBuffer
without waiting for the next GC cycle.buffer
- to be unmapped.public static long map(FileChannel fileChannel, FileChannel.MapMode mode, long offset, long length)
fileChannel
- to be mapped.mode
- for the mapped region.offset
- within the file the mapped region should start.length
- of the mapped region.public static void unmap(FileChannel fileChannel, long address, long length)
fileChannel
- which has been mapped.address
- at which the mapping begins.length
- of the mapped region.public static void unmap(ByteBuffer buffer)
ByteBuffer
without waiting for the next GC cycle if its memory mapped.buffer
- to be unmapped.public static String tmpDirName()
File.separator
is at the end.Copyright © 2014-2019 Real Logic Ltd. All Rights Reserved.