/** * 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); }
/** * A gunzip function to store locally * * @param in The factory to produce input streams * @param outFile The file to store the result into * @param shouldRetry A predicate to indicate if the Throwable is recoverable * * @return The count of bytes written to outFile */ public static FileUtils.FileCopyResult gunzip( final ByteSource in, final File outFile, Predicate<Throwable> shouldRetry ) { return FileUtils.retryCopy( new ByteSource() { @Override public InputStream openStream() throws IOException { return gzipInputStream(in.openStream()); } }, outFile, shouldRetry, DEFAULT_RETRY_COUNT ); }
private static void writeAtomically(final File file, final File tmpDir, OutputStreamConsumer f) throws IOException { final File tmpFile = new File(tmpDir, StringUtils.format(".%s.%s", file.getName(), UUID.randomUUID())); try { try (final FileOutputStream out = new FileOutputStream(tmpFile)) { // Pass f an uncloseable stream so we can fsync before closing. f.accept(uncloseable(out)); // fsync to avoid write-then-rename-then-crash causing empty files on some filesystems. out.getChannel().force(true); } // No exception thrown; do the move. java.nio.file.Files.move( tmpFile.toPath(), file.toPath(), StandardCopyOption.ATOMIC_MOVE, StandardCopyOption.REPLACE_EXISTING ); } finally { tmpFile.delete(); } }
public void add(String name, File fileToAdd) throws IOException { try (MappedByteBufferHandler fileMappingHandler = FileUtils.map(fileToAdd)) { add(name, fileMappingHandler.get()); } }
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()); } }
final ZipEntry entry = enumeration.nextElement(); result.addFiles( FileUtils.retryCopy( new ByteSource()
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)); }
final File tmpFile = File.createTempFile("compressionUtilZipCache", ZIP_SUFFIX); try { FileUtils.retryCopy( byteSource, tmpFile,
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)); }
FileUtils.retryCopy( Files.asByteSource(oldFile), new File(dir, oldFile.getName()),