FileOutputStream, except that all writes are first
redirected to a temporary file. When the stream is closed, the temporary file (atomically) replaces the target file.
In detail, the strategy is to:
Errors are handled as follows:
Implementation inspired by code from Marty Lamb and Apache.
| Modifier and Type | Field and Description |
|---|---|
| private final Path | backupFile
A backup of the target file (if it exists), created when the stream is closed |
| private boolean | |
| private final boolean | |
| private static final Logger | |
| private static final String | |
| private final Path | targetFile
The file we want to create/replace. |
| private static final String | |
| private final Path | temporaryFile
The file to which writes are redirected to. |
| private FileLock |
| Access | Constructor and Description |
|---|---|
| public | AtomicFileOutputStream(Path
the path of the file to write to or replace path, boolean whether to keep the backup file (.sav) after a successful write process keepBackup)Creates a new output stream to write to or replace the file at the specified path. |
| public | AtomicFileOutputStream(Path
the path of the file to write to or replace path)Creates a new output stream to write to or replace the file at the specified path. |
| pack-priv | AtomicFileOutputStream(Path path, Path pathOfTemporaryFile, OutputStream temporaryFileOutputStream, boolean keepBackup)
Required for proper testing |
| Modifier and Type | Method and Description |
|---|---|
| public void | |
| private void | |
| public void | close()
Overrides java. Implements java. perform the final operations to move the temporary file to its final destination |
| public void | flush()
Overrides java. Implements java. Flushes this output stream and forces any buffered output bytes to be written out to the stream. |
| public Path | |
| private static Path | |
| private static Path | |
| public void | write(byte[]
the data. b, int the start offset in the data. off, int the number of bytes to write. len)Overrides java. Overridden because of cleanup actions in case of an error |
| public void | write(int
the b)byte.Overrides java. Writes the specified |
| backupFile | back to summary |
|---|---|
| private final Path backupFile A backup of the target file (if it exists), created when the stream is closed | |
| errorDuringWrite | back to summary |
|---|---|
| private boolean errorDuringWrite | |
| keepBackup | back to summary |
|---|---|
| private final boolean keepBackup | |
| LOGGER | back to summary |
|---|---|
| private static final Logger LOGGER | |
| SAVE_EXTENSION | back to summary |
|---|---|
| private static final String SAVE_EXTENSION | |
| targetFile | back to summary |
|---|---|
| private final Path targetFile The file we want to create/replace. | |
| TEMPORARY_EXTENSION | back to summary |
|---|---|
| private static final String TEMPORARY_EXTENSION | |
| temporaryFile | back to summary |
|---|---|
| private final Path temporaryFile The file to which writes are redirected to. | |
| temporaryFileLock | back to summary |
|---|---|
| private FileLock temporaryFileLock | |
| AtomicFileOutputStream | back to summary |
|---|---|
| public AtomicFileOutputStream(Path path, boolean keepBackup) throws IOException Creates a new output stream to write to or replace the file at the specified path.
| |
| AtomicFileOutputStream | back to summary |
|---|---|
| public AtomicFileOutputStream(Path path) throws IOException Creates a new output stream to write to or replace the file at the specified path. The backup file (.sav) is deleted when write was successful.
| |
| AtomicFileOutputStream | back to summary |
|---|---|
| pack-priv AtomicFileOutputStream(Path path, Path pathOfTemporaryFile, OutputStream temporaryFileOutputStream, boolean keepBackup) throws IOException Required for proper testing | |
| abort | back to summary |
|---|---|
| public void abort() Closes the write process to the temporary file but does not commit to the target file. | |
| cleanup | back to summary |
|---|---|
| private void cleanup() | |
| close | back to summary |
|---|---|
| public void close() throws IOException Overrides java. Implements java. perform the final operations to move the temporary file to its final destination
| |
| flush | back to summary |
|---|---|
| public void flush() throws IOException Overrides java. Implements java. Doc from java. Flushes this output stream and forces any buffered output bytes to be written out to the stream.
| |
| getBackup | back to summary |
|---|---|
| public Path getBackup() Returns the path of the backup copy of the original file (may not exist) | |
| getPathOfSaveBackupFile | back to summary |
|---|---|
| private static Path getPathOfSaveBackupFile(Path targetFile) | |
| getPathOfTemporaryFile | back to summary |
|---|---|
| private static Path getPathOfTemporaryFile(Path targetFile) | |
| write | back to summary |
|---|---|
| public void write(byte[] b, int off, int len) throws IOException Overrides java. Overridden because of cleanup actions in case of an error
| |
| write | back to summary |
|---|---|
| public void write(int b) throws IOException Overrides java. Doc from java. Writes the specified
The
Implements the abstract
| |