Class FileUtils
- Author:
- poirigui
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceFileUtils.PathWriter<E extends Exception>Writes a file or a directory at a given path, handed to it bywriteAtomically(Path, PathWriter). -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic InputStreamopenCompressedFile(Path path) Open a GZIP-compressed file safely.static <E extends Exception>
voidwriteAtomically(Path destination, FileUtils.PathWriter<E> writer) Writedestinationthrough a temporary sibling that is moved over it only oncewriterhas returned normally.
-
Constructor Details
-
FileUtils
public FileUtils()
-
-
Method Details
-
openCompressedFile
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 Writedestinationthrough a temporary sibling that is moved over it only oncewriterhas returned normally.A partial result is never at
destination: not when the writer throws, anErrorincluded, and not when the JVM dies or a daemon writer thread is halted mid-write, which no catch block can cover. On failure,destinationkeeps 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
destinationis 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 ondestinationacross this call.- Parameters:
destination- the file or directory to create or replacewriter- writes the content at the temporary path it is given- Throws:
IOExceptionE
-