Class AtomicUpdateFileOutputStream
- All Implemented Interfaces:
Closeable,Flushable,AutoCloseable
FileOutputStream that atomically updates the target file.
Instances write to a temporary file until close() is invoked, at which time the temporary file
gets renamed to the target file. This rename operation is atomic on most systems
(e.g., all UNIX variants). The result is that the target file always exists, and if opened at any time,
will contain either the previous content or the new content, but never a mix of the two.
Instances therefore represent a "transaction" for rewriting the file. As such, they can be in one of three states:
OPEN, CLOSED, or CANCELED. State CLOSED implies that the file update was
successful (no IOExceptions occurred); state CANCELED implies the file update was either
explicitly canceled (via cancel()) or implicitly canceled due to an IOException being thrown by any method.
When still OPEN, the transaction is "comitted" by invoking close(), or rolled back by invoking cancel().
Once an instance has been closed or canceled, the temporary file will have been deleted, and any subsequent invocations
of close() or cancel() have no effect.
Note: to guarantee that the new content will always be found in the future, even if there is a sudden system crash,
the caller must also FileChannel.force(boolean) this instance (before closing) and the containing
directory (after closing).
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intThe stream is closed, and the update has been canceled either explicitly viacancel()or implicitly due to anIOExceptionhaving been thrown.static final intThe stream is closed and the update has been successful.static final intThe stream is open for writing. -
Constructor Summary
ConstructorsConstructorDescriptionAtomicUpdateFileOutputStream(File targetFile) Convenience constructor.AtomicUpdateFileOutputStream(File targetFile, boolean append) Convenience constructor.AtomicUpdateFileOutputStream(File targetFile, File tempFile) Constructor.AtomicUpdateFileOutputStream(File targetFile, File tempFile, boolean append) Constructor. -
Method Summary
Modifier and TypeMethodDescriptionbooleancancel()Cancel this instance if still open.voidclose()Close this instance if still open.protected voidfinalize()Ensure the temporary file is deleted in cases where this instance never got successfully closed.voidflush()intgetState()Get the state of this instanceGet the target file.Get the temporary file.longGet the last modification timestamp of the target file as it was at the time it was updated by this instance.voidwrite(byte[] b) voidwrite(byte[] b, int off, int len) voidwrite(int b) Methods inherited from class java.io.FileOutputStream
getChannel, getFDMethods inherited from class java.io.OutputStream
nullOutputStream
-
Field Details
-
OPEN
public static final int OPENThe stream is open for writing.- See Also:
-
CLOSED
public static final int CLOSEDThe stream is closed and the update has been successful.- See Also:
-
CANCELED
public static final int CANCELEDThe stream is closed, and the update has been canceled either explicitly viacancel()or implicitly due to anIOExceptionhaving been thrown.- See Also:
-
-
Constructor Details
-
AtomicUpdateFileOutputStream
Constructor.- Parameters:
targetFile- the ultimate destination for the output when closed.tempFile- temporary file that accumulates output until close.- Throws:
FileNotFoundException- iftempFilecannot be opened for any reasonSecurityException- if a security manager prevents writing totempFileNullPointerException- if either parameter is null
-
AtomicUpdateFileOutputStream
public AtomicUpdateFileOutputStream(File targetFile, File tempFile, boolean append) throws FileNotFoundException Constructor.- Parameters:
targetFile- the ultimate destination for the output when closed.tempFile- temporary file that accumulates output until close.append- if true, then bytes will be written to the end of the file rather than the beginning- Throws:
FileNotFoundException- iftempFilecannot be opened for any reasonSecurityException- if a security manager prevents writing totempFileNullPointerException- if either parameter is null
-
AtomicUpdateFileOutputStream
Convenience constructor.This constructor uses a temporary file within the same directory as
targetFile.- Parameters:
targetFile- the ultimate destination for the output when closed.- Throws:
FileNotFoundException- iftempFilecannot be opened for any reasonIOException- if a temporary file could not be createdSecurityException- if a security manager prevents writing totempFileNullPointerException- iftargetFileis null
-
AtomicUpdateFileOutputStream
Convenience constructor.This constructor uses a temporary file within the same directory as
targetFile.- Parameters:
targetFile- the ultimate destination for the output when closed.append- if true, then bytes will be written to the end of the file rather than the beginning- Throws:
FileNotFoundException- iftempFilecannot be opened for any reasonIOException- if a temporary file could not be createdSecurityException- if a security manager prevents writing totempFileNullPointerException- iftargetFileis null
-
-
Method Details
-
getTargetFile
Get the target file.- Returns:
- target file, never null
-
getTempFile
Get the temporary file.If this instance is in state
CLOSEDorCANCELED, the file will no longer exist.- Returns:
- temporary file, never null
-
getState
public int getState()Get the state of this instance- Returns:
- the current state of this instance
-
cancel
public boolean cancel()Cancel this instance if still open. This rolls back the open transaction.This method does nothing (and returns false) if
close()orcancel()has already been invoked.- Returns:
- true if this instance was canceled, false if this instance is already closed or canceled
-
write
- Overrides:
writein classFileOutputStream- Throws:
IOException
-
write
- Overrides:
writein classFileOutputStream- Throws:
IOException
-
write
- Overrides:
writein classFileOutputStream- Throws:
IOException
-
flush
- Specified by:
flushin interfaceFlushable- Overrides:
flushin classOutputStream- Throws:
IOException
-
close
Close this instance if still open. This commits the open transaction.If this instance is still open, this method will close the temporary file and then attempt to atomically rename it onto the target file. In any case, after this method returns (either normally or abnormally), the temporary file will no longer exist.
If this instance has already been closed or canceled, this method does nothing.
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classFileOutputStream- Throws:
IOException- if an I/O error occurs
-
getTimestamp
public long getTimestamp()Get the last modification timestamp of the target file as it was at the time it was updated by this instance.This method only works after
close()has been successfully invoked, otherwise it returns zero.- Returns:
- target file modification time, or zero if
close()has not been successfully invoked
-
finalize
Ensure the temporary file is deleted in cases where this instance never got successfully closed.
-