public class SafeFileOutputStream
extends java.io.OutputStream
Attempts to prevent data loss if OS crash happens during write.
The class creates a backup copy before overwriting the target file, and issues fsync()
afterwards. The behavior is based
on an assumption that after a crash either a target remains unmodified (i.e. unfinished write doesn't reach the disc),
or a backup file exists along with a partially overwritten target file.
The class is not thread-safe; expected to be used within try-with-resources or an equivalent statement.
Constructor and Description |
---|
SafeFileOutputStream(java.io.File target) |
SafeFileOutputStream(java.io.File target,
java.lang.String backupExt) |
SafeFileOutputStream(java.nio.file.Path target) |
SafeFileOutputStream(java.nio.file.Path target,
java.lang.String backupExt) |
Modifier and Type | Method and Description |
---|---|
void |
abort() |
void |
close() |
void |
write(byte[] b,
int off,
int len) |
void |
write(int b) |
public SafeFileOutputStream(java.io.File target)
public SafeFileOutputStream(java.io.File target, java.lang.String backupExt)
public SafeFileOutputStream(java.nio.file.Path target)
public SafeFileOutputStream(java.nio.file.Path target, java.lang.String backupExt)
public void write(int b) throws java.io.IOException
write
in class java.io.OutputStream
java.io.IOException
public void write(byte[] b, int off, int len) throws java.io.IOException
write
in class java.io.OutputStream
java.io.IOException
public void abort() throws java.io.IOException
java.io.IOException
public void close() throws java.io.IOException
close
in interface java.io.Closeable
close
in interface java.lang.AutoCloseable
close
in class java.io.OutputStream
java.io.IOException