Class AtomicFiles
java.lang.Object
be.imgn.gaia.core.io.AtomicFiles
Writes files atomically: a reader sees either the previous content of a file or its new content, never a part of it,
and so does the next run of a program after a crash.
The content is written to a temporary file in the same directory, forced to the storage device, then moved over the file in one atomic step. If anything fails, the file is left as it was and the temporary file is deleted.
- A file that exists keeps its POSIX permissions, which the temporary file has from its creation, along with
reading by its owner before anything is written to it; a new one gets the default permissions, as with
Files.write(Path, byte[], java.nio.file.OpenOption...). Other attributes, such as the owner, ACLs or extended attributes, are not kept. - A symbolic link to a file that exists is followed: the file it points to is replaced, and the link stays. A symbolic link to a file that does not exist is not followed: it is replaced with the new file.
- On Windows, the file cannot be replaced while another program has it open: the write then fails with an
AccessDeniedException. - A file system that cannot move a file atomically within a directory makes every write fail with an
AtomicMoveNotSupportedException, rather than replace the file in a way that is not atomic. - In a directory that other users can write to and that has no sticky bit, they can rename the temporary file and put another one in its place before it is moved. Write only to directories other users cannot change.
AtomicFiles.writeString(settingsFile, toml);
AtomicFiles.write(exportFile, out -> ImageIO.write(image, "png", out));
- Since:
- 0.1.1
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceWrites the content of a file to a stream. -
Method Summary
Modifier and TypeMethodDescriptionstatic voidWrites bytes to a file atomically, creating it or replacing its content.static voidwrite(Path file, AtomicFiles.Output output) Writes a file atomically, creating it or replacing its content with whatoutputwrites.static voidWrites bytes to a file atomically, creating it or replacing its content.static voidwriteString(Path file, CharSequence text) Writes text to a file atomically in the current default charset, creating it or replacing its content.static voidwriteString(Path file, CharSequence text, Charset charset) Writes text to a file atomically in a charset, creating it or replacing its content.
-
Method Details
-
write
Writes bytes to a file atomically, creating it or replacing its content.- Parameters:
file- the file to writebytes- the new content of the file- Throws:
IOException- if the file cannot be written, which leaves it as it was
-
write
Writes bytes to a file atomically, creating it or replacing its content.- Parameters:
file- the file to writebytes- the new content of the file- Throws:
IOException- if the file cannot be written, which leaves it as it was- Since:
- 0.2.0
-
write
Writes a file atomically, creating it or replacing its content with whatoutputwrites. The stream given tooutputis buffered, and closing it has no effect.- Parameters:
file- the file to writeoutput- writes the new content of the file- Throws:
FileSystemException- iffileis a directoryIOException- if the file cannot be written oroutputfails, which leaves the file as it was
-
writeString
Writes text to a file atomically in the current default charset, creating it or replacing its content.- Parameters:
file- the file to writetext- the new content of the file- Throws:
IOException- if the file cannot be written, which leaves it as it wasCharacterCodingException- if the text contains a character that charset cannot encode, or a malformed surrogate pair
-
writeString
Writes text to a file atomically in a charset, creating it or replacing its content.- Parameters:
file- the file to writetext- the new content of the filecharset- the charset to encode the text in- Throws:
IOException- if the file cannot be written, which leaves it as it wasIllegalArgumentException- ifcharsetdoes not support encodingCharacterCodingException- if the text contains a character the charset cannot encode, or a malformed surrogate pair
-