@ParametersAreNonnullByDefault
public final class SafeFiles
extends java.lang.Object
When possible methods on this class should be used over the ones in Files
| Modifier and Type | Method and Description |
|---|---|
static SafeOutputStream |
createAtomicFile(java.nio.file.Path path)
This is just like a lazy variation of
write(byte[], java.nio.file.Path). |
static void |
deleteIfExistsQuietly(java.nio.file.Path path)
Delete a path but do not complain if it fails.
|
static void |
ensureDirectoryExists(java.nio.file.Path path)
Create a directory if it does not already exist.
|
static void |
fsync(java.nio.file.Path path)
Fsync a single path.
|
static int |
fsyncRecursive(java.nio.file.Path root)
Walk a directory tree and Fsync both Directory and File inodes.
|
static void |
rename(java.nio.file.Path oldName,
java.nio.file.Path newName)
Perform an atomic rename of oldName -> newName and fsync the containing directory.
|
static void |
write(byte[] data,
java.nio.file.Path path)
Write the bytes to a temporary file, fsync the file, then atomically rename to the target path.
|
static void |
writeUTF8(java.lang.String value,
java.nio.file.Path path)
Write the string to a temporary file, fsync the file, then atomically rename to the target path.
|
public static void rename(java.nio.file.Path oldName,
java.nio.file.Path newName)
throws java.io.IOException
oldName - original filenewName - new filejava.io.IOException - In the event that we could not rename the path.public static void ensureDirectoryExists(java.nio.file.Path path)
throws java.io.IOException
path - path to ensure is a directory.java.io.IOException - In the event that the path is not a directory.@Nonnegative
public static int fsyncRecursive(java.nio.file.Path root)
throws java.io.IOException
root - directory to start the traversal.java.io.IOException - in the event that we could not fsync the provided directory.public static void fsync(java.nio.file.Path path)
throws java.io.IOException
path - path to fsyncjava.io.IOException - in the event that we could not fsync the provided path.public static void writeUTF8(java.lang.String value,
java.nio.file.Path path)
throws java.io.IOException
value - string value to write to file as UTF8 bytespath - path to write out tojava.io.IOException - in the event that the data could not be written to the path.public static void write(byte[] data,
java.nio.file.Path path)
throws java.io.IOException
data - binary value to write to filepath - path to write out tojava.io.IOException - in the event that the data could not be written to the path.@Nonnull public static SafeOutputStream createAtomicFile(java.nio.file.Path path) throws java.io.IOException
write(byte[], java.nio.file.Path). It opens a temp file
and proxies writes through to the underlying file.
Upon calling SafeOutputStream.commit() the rest of the safety behaviors kick in:
On error it will make a best-effort to erase the temporary file.
If you call SafeOutputStream.close() without calling SafeOutputStream.commit()
the atomic write is aborted and cleaned up.
It is safe to call SafeOutputStream.close()} after SafeOutputStream.commit()
so that try-with-resources works.
The returned SafeOutputStream is NOT safe for calls from multiple threads.
path - final desired output pathjava.io.IOException - in the event that the file could not be created.public static void deleteIfExistsQuietly(java.nio.file.Path path)
path - path to delete