public class IoUtil
extends java.lang.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(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 the
FileChannel |
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 the
FileChannel |
static void |
delete(java.io.File file,
boolean ignoreFailures)
Recursively delete a file or directory tree.
|
static void |
deleteIfExists(java.io.File file)
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 a region of a file with a given byte value.
|
static long |
map(java.nio.channels.FileChannel fileChannel,
java.nio.channels.FileChannel.MapMode mode,
long offset,
long length)
Map a range of a file and return the address at which the range begins.
|
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
|
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
|
static java.nio.MappedByteBuffer |
mapNewFile(java.io.File location,
long length)
Create a new file, fill with 0s, and return a
MappedByteBuffer 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 a
MappedByteBuffer for the file. |
static java.lang.String |
tmpDirName()
Return the system property for java.io.tmpdir ensuring a
File.separator is at the end. |
static void |
unmap(java.nio.ByteBuffer buffer)
Unmap a
ByteBuffer without waiting for the next GC cycle if its memory mapped. |
static void |
unmap(java.nio.channels.FileChannel fileChannel,
long address,
long length)
Unmap a region of a file.
|
static void |
unmap(java.nio.MappedByteBuffer buffer)
Unmap a
MappedByteBuffer without waiting for the next GC cycle. |
public static final int BLOCK_SIZE
public static void fill(java.nio.channels.FileChannel fileChannel, long position, long length, byte value)
fileChannel
- to fillposition
- at which to start writing.length
- of the region to write.value
- to fill the region with.public static void delete(java.io.File file, boolean ignoreFailures)
file
- to be deleted.ignoreFailures
- don't throw an exception if a delete fails.public static void ensureDirectoryExists(java.io.File directory, java.lang.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(java.io.File directory, java.lang.String descriptionLabel, java.util.function.BiConsumer<java.lang.String,java.lang.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(java.io.File file)
file
- to deletepublic static java.nio.channels.FileChannel createEmptyFile(java.io.File file, long length)
FileChannel
file
- to createlength
- of the file to createFileChannel
for the filepublic static java.nio.channels.FileChannel createEmptyFile(java.io.File file, long length, boolean fillWithZeros)
FileChannel
file
- to createlength
- of the file to createfillWithZeros
- to the length of the file to force allocation.FileChannel
for the filepublic static java.nio.MappedByteBuffer mapExistingFile(java.io.File location, java.lang.String descriptionLabel)
The file itself will be closed, but the mapping will persist.
location
- of the file to mapdescriptionLabel
- to be associated for any exceptionsMappedByteBuffer
for the filepublic static java.nio.MappedByteBuffer mapExistingFile(java.io.File location, java.lang.String descriptionLabel, long offset, long length)
The file itself will be closed, but the mapping will persist.
location
- of the file to mapdescriptionLabel
- to be associated for an exceptionsoffset
- offset to start mapping atlength
- length to map regionMappedByteBuffer
for the filepublic static java.nio.MappedByteBuffer mapNewFile(java.io.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 maplength
- of the file to create and mapMappedByteBuffer
for the filepublic static java.nio.MappedByteBuffer mapNewFile(java.io.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 maplength
- of the file to create and mapfillWithZeros
- to force allocation.MappedByteBuffer
for the filepublic static void checkFileExists(java.io.File file, java.lang.String name)
file
- to check existence of.name
- to associate for the exceptionpublic static void unmap(java.nio.MappedByteBuffer buffer)
MappedByteBuffer
without waiting for the next GC cycle.buffer
- to be unmapped.public static long map(java.nio.channels.FileChannel fileChannel, java.nio.channels.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(java.nio.channels.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(java.nio.ByteBuffer buffer)
ByteBuffer
without waiting for the next GC cycle if its memory mapped.buffer
- to be unmapped.public static java.lang.String tmpDirName()
File.separator
is at the end.Copyright © 2014-2018 Real Logic Ltd. All Rights Reserved.