Class SafeFileOutputStream

java.lang.Object
java.io.OutputStream
com.inductiveautomation.ignition.common.util.SafeFileOutputStream
All Implemented Interfaces:
Closeable, Flushable, AutoCloseable

public class SafeFileOutputStream extends OutputStream

A FileOutputStream that implements a two-stage write process, and additionaly copies any existing file to a backup location. This ensures that no matter what (software or hardware failures), there is always a valid (non-corrupt) copy of the destination file. More formally, the logic is as follows.

  • As write() is called on the SafeFileOutputStream instance, information is written to a temporary file, obtained by File.createTempFile().
  • When close() is called, the destination file (if it exists), is renamed to the backup file
  • Then the temp file is renamed to the destination file