/** * Write to a file atomically, by first writing to a temporary file in the same directory and then moving it to * the target location. This function attempts to clean up its temporary files when possible, but they may stick * around (for example, if the JVM crashes partway through executing the function). In any case, the target file * should be unharmed. * * The OutputStream passed to the consumer is uncloseable; calling close on it will do nothing. This is to ensure * that the stream stays open so we can fsync it here before closing. Hopefully, this doesn't cause any problems * for callers. * * This method is not just thread-safe, but is also safe to use from multiple processes on the same machine. */ public static void writeAtomically(final File file, OutputStreamConsumer f) throws IOException { writeAtomically(file, file.getParentFile(), f); }
public synchronized void takeSnapshot(String tier, List<LookupBean> lookups) { final File persistFile = getPersistFile(tier); try { FileUtils.writeAtomically(persistFile, out -> objectMapper.writeValue(out, lookups)); } catch (IOException e) { throw new ISE(e, "Exception during serialization of lookups using file [%s]", persistFile.getAbsolutePath()); } }
private void writeMapToDisk(String prefix, byte[] userMapBytes) throws IOException { File cacheDir = new File(commonCacheConfig.getCacheDirectory()); cacheDir.mkdirs(); File userMapFile = new File(commonCacheConfig.getCacheDirectory(), getUserRoleMapFilename(prefix)); FileUtils.writeAtomically(userMapFile, out -> out.write(userMapBytes)); }
private void writeUserMapToDisk(String prefix, byte[] userMapBytes) throws IOException { File cacheDir = new File(commonCacheConfig.getCacheDirectory()); cacheDir.mkdirs(); File userMapFile = new File(commonCacheConfig.getCacheDirectory(), getUserMapFilename(prefix)); FileUtils.writeAtomically(userMapFile, out -> out.write(userMapBytes)); }