@Test public void tmpFileRemoved() throws Exception { try { AtomicSave.execute(DESTINATION_FILE, "AtomicSaveTest", out -> { throw new CodedException(ErrorCodes.X_IO_ERROR); }); } catch (CodedException e) { Assert.assertFalse("Temporary file not deleted", tmpFileExists()); return; } finally { Files.deleteIfExists(Paths.get(DESTINATION_FILE)); } fail(); }
public static void execute(String fileName, String tmpPrefix, Callback callback, CopyOption... options) throws Exception { Path target = Paths.get(fileName); Path parentPath = target.getParent(); Path tempFile = DefaultFilepaths.createTempFile(parentPath, tmpPrefix, null); try { SeekableByteChannel channel = Files.newByteChannel(tempFile, CREATE, WRITE, TRUNCATE_EXISTING, DSYNC); try (OutputStream out = Channels.newOutputStream(channel)) { callback.save(out); } if (options.length == 0) { Files.move(tempFile, target, StandardCopyOption.REPLACE_EXISTING); } else { Files.move(tempFile, target, options); } } finally { if (Files.exists(tempFile)) { Files.delete(tempFile); } } }
AtomicSave { public static void execute(String fileName, String tmpPrefix, Callback callback, CopyOption... options) throws Exception { Path target = Paths.get(fileName); Path parentPath = target.getParent(); Path tempFile = DefaultFilepaths.createTempFile(parentPath, tmpPrefix, null); try { SeekableByteChannel channel = Files.newByteChannel(tempFile, CREATE, WRITE, TRUNCATE_EXISTING, DSYNC); try (OutputStream out = Channels.newOutputStream(channel)) { callback.save(out); } if (options.length == 0) { Files.move(tempFile, target, StandardCopyOption.REPLACE_EXISTING); } else { Files.move(tempFile, target, options); } } finally { if (Files.exists(tempFile)) { Files.delete(tempFile); } } } }
AtomicSave { public static void execute(String fileName, String tmpPrefix, Callback callback, CopyOption... options) throws Exception { Path target = Paths.get(fileName); Path parentPath = target.getParent(); Path tempFile = DefaultFilepaths.createTempFile(parentPath, tmpPrefix, null); try { SeekableByteChannel channel = Files.newByteChannel(tempFile, CREATE, WRITE, TRUNCATE_EXISTING, DSYNC); try (OutputStream out = Channels.newOutputStream(channel)) { callback.save(out); } if (options.length == 0) { Files.move(tempFile, target, StandardCopyOption.REPLACE_EXISTING); } else { Files.move(tempFile, target, options); } } finally { if (Files.exists(tempFile)) { Files.delete(tempFile); } } } private  AtomicSave(); }
AtomicSave { public static void execute(String fileName, String tmpPrefix, Callback callback, CopyOption... options) throws Exception { Path target = Paths.get(fileName); Path parentPath = target.getParent(); Path tempFile = DefaultFilepaths.createTempFile(parentPath, tmpPrefix, null); try { SeekableByteChannel channel = Files.newByteChannel(tempFile, CREATE, WRITE, TRUNCATE_EXISTING, DSYNC); try (OutputStream out = Channels.newOutputStream(channel)) { callback.save(out); } if (options.length == 0) { Files.move(tempFile, target, StandardCopyOption.REPLACE_EXISTING); } else { Files.move(tempFile, target, options); } } finally { if (Files.exists(tempFile)) { Files.delete(tempFile); } } } private  AtomicSave(); static void execute(String fileName, String tmpPrefix, Callback callback, CopyOption... options); static void execute(String fileName, String tmpPrefix, final byte[] data, CopyOption... options); static void moveBetweenFilesystems(String sourceFileName, String targetFileName); }
AtomicSave { public static void execute(String fileName, String tmpPrefix, Callback callback, CopyOption... options) throws Exception { Path target = Paths.get(fileName); Path parentPath = target.getParent(); Path tempFile = DefaultFilepaths.createTempFile(parentPath, tmpPrefix, null); try { SeekableByteChannel channel = Files.newByteChannel(tempFile, CREATE, WRITE, TRUNCATE_EXISTING, DSYNC); try (OutputStream out = Channels.newOutputStream(channel)) { callback.save(out); } if (options.length == 0) { Files.move(tempFile, target, StandardCopyOption.REPLACE_EXISTING); } else { Files.move(tempFile, target, options); } } finally { if (Files.exists(tempFile)) { Files.delete(tempFile); } } } private  AtomicSave(); static void execute(String fileName, String tmpPrefix, Callback callback, CopyOption... options); static void execute(String fileName, String tmpPrefix, final byte[] data, CopyOption... options); static void moveBetweenFilesystems(String sourceFileName, String targetFileName);  }