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 IOException
s 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
Modifier and TypeFieldDescriptionstatic final int
The stream is closed, and the update has been canceled either explicitly viacancel()
or implicitly due to anIOException
having been thrown.static final int
The stream is closed and the update has been successful.static final int
The stream is open for writing. -
Constructor Summary
ConstructorDescriptionAtomicUpdateFileOutputStream
(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 TypeMethodDescriptionboolean
cancel()
Cancel this instance if still open.void
close()
Close this instance if still open.protected void
finalize()
Ensure the temporary file is deleted in cases where this instance never got successfully closed.void
flush()
int
getState()
Get the state of this instanceGet the target file.Get the temporary file.long
Get the last modification timestamp of the target file as it was at the time it was updated by this instance.void
write
(byte[] b) void
write
(byte[] b, int off, int len) void
write
(int b) Methods inherited from class java.io.FileOutputStream
getChannel, getFD
Methods 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 anIOException
having 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
- iftempFile
cannot be opened for any reasonSecurityException
- if a security manager prevents writing totempFile
NullPointerException
- 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
- iftempFile
cannot be opened for any reasonSecurityException
- if a security manager prevents writing totempFile
NullPointerException
- 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
- iftempFile
cannot be opened for any reasonIOException
- if a temporary file could not be createdSecurityException
- if a security manager prevents writing totempFile
NullPointerException
- iftargetFile
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.append
- if true, then bytes will be written to the end of the file rather than the beginning- Throws:
FileNotFoundException
- iftempFile
cannot be opened for any reasonIOException
- if a temporary file could not be createdSecurityException
- if a security manager prevents writing totempFile
NullPointerException
- iftargetFile
is null
-
-
Method Details
-
getTargetFile
Get the target file.- Returns:
- target file, never null
-
getTempFile
Get the temporary file.If this instance is in state
CLOSED
orCANCELED
, 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:
write
in classFileOutputStream
- Throws:
IOException
-
write
- Overrides:
write
in classFileOutputStream
- Throws:
IOException
-
write
- Overrides:
write
in classFileOutputStream
- Throws:
IOException
-
flush
- Specified by:
flush
in interfaceFlushable
- Overrides:
flush
in 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:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Overrides:
close
in 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.
-