Class AtomicFiles

java.lang.Object
be.imgn.gaia.core.io.AtomicFiles

public final class AtomicFiles extends Object
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
  • Method Details

    • write

      public static void write(Path file, byte[] bytes) throws IOException
      Writes bytes to a file atomically, creating it or replacing its content.
      Parameters:
      file - the file to write
      bytes - the new content of the file
      Throws:
      IOException - if the file cannot be written, which leaves it as it was
    • write

      public static void write(Path file, Bytes bytes) throws IOException
      Writes bytes to a file atomically, creating it or replacing its content.
      Parameters:
      file - the file to write
      bytes - 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

      public static void write(Path file, AtomicFiles.Output output) throws IOException
      Writes a file atomically, creating it or replacing its content with what output writes. The stream given to output is buffered, and closing it has no effect.
      Parameters:
      file - the file to write
      output - writes the new content of the file
      Throws:
      FileSystemException - if file is a directory
      IOException - if the file cannot be written or output fails, which leaves the file as it was
    • writeString

      public static void writeString(Path file, CharSequence text) throws IOException
      Writes text to a file atomically in the current default charset, creating it or replacing its content.
      Parameters:
      file - the file to write
      text - the new content of the file
      Throws:
      IOException - if the file cannot be written, which leaves it as it was
      CharacterCodingException - if the text contains a character that charset cannot encode, or a malformed surrogate pair
    • writeString

      public static void writeString(Path file, CharSequence text, Charset charset) throws IOException
      Writes text to a file atomically in a charset, creating it or replacing its content.
      Parameters:
      file - the file to write
      text - the new content of the file
      charset - the charset to encode the text in
      Throws:
      IOException - if the file cannot be written, which leaves it as it was
      IllegalArgumentException - if charset does not support encoding
      CharacterCodingException - if the text contains a character the charset cannot encode, or a malformed surrogate pair