Class FileUtils

java.lang.Object
ubic.gemma.core.util.FileUtils

public class FileUtils extends Object
Author:
poirigui
  • Constructor Details

    • FileUtils

      public FileUtils()
  • Method Details

    • openCompressedFile

      public static InputStream openCompressedFile(Path path) throws IOException
      Open a GZIP-compressed file safely.

      This ensures that if an exception is thrown by GZIPInputStream(InputStream), the stream will be closed.

      Throws:
      IOException
    • writeAtomically

      public static <E extends Exception> void writeAtomically(Path destination, FileUtils.PathWriter<E> writer) throws IOException, E
      Write destination through a temporary sibling that is moved over it only once writer has returned normally.

      A partial result is never at destination: not when the writer throws, an Error included, and not when the JVM dies or a daemon writer thread is halted mid-write, which no catch block can cover. On failure, destination keeps whatever it held before.

      The temporary is named .<name>.tmp-<random> and lives in the same directory, so the move is a rename within one file system. It is deleted on failure. One left behind by a JVM that died stays until removed by hand; its name ends in neither the destination's name nor its suffix, so a lookup by name or by suffix does not find it, but code that lists the directory must skip it.

      A directory already at destination is deleted just before the move, because a rename cannot replace a non-empty directory: a crash between the two leaves no directory rather than a partial one.

      The temporary is not synced to disk before the move, so this covers the process dying, not the host crashing. Nothing here locks destination: a caller that coordinates with readers holds its lock on destination across this call.

      Parameters:
      destination - the file or directory to create or replace
      writer - writes the content at the temporary path it is given
      Throws:
      IOException
      E