AtomicFileOutputStream out = new AtomicFileOutputStream(file); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(out)); boolean aborted = false; bw.flush(); out.flush(); } catch (IOException e) { LOG.error("Failed to write new file " + file, e); out.abort(); throw e; } finally { out.close();
/** * Ensure the tmp file is cleaned up and dstFile is not created when * aborting a new file. */ @Test public void testAbortNewFileAfterFlush() throws IOException { AtomicFileOutputStream fos = new AtomicFileOutputStream(dstFile); fos.write(TEST_STRING.getBytes()); fos.flush(); fos.abort(); assertEquals(0, testDir.list().length); }
boolean error = true; try { out = new AtomicFileOutputStream(targetFile); if (wStmt == null) { bw.flush(); out.flush(); out.abort(); } else {
/** * Ensure the tmp file is cleaned up and dstFile is not created when * aborting a new file. */ @Test public void testAbortNewFile() throws IOException { AtomicFileOutputStream fos = new AtomicFileOutputStream(dstFile); fos.abort(); assertEquals(0, testDir.list().length); }
/** * serialize the datatree and session into the file snapshot * @param dt the datatree to be serialized * @param sessions the sessions to be serialized * @param snapShot the file to store snapshot into * @param fsync sync the file immediately after write */ public synchronized void serialize(DataTree dt, Map<Long, Integer> sessions, File snapShot, boolean fsync) throws IOException { if (!close) { try (CheckedOutputStream crcOut = new CheckedOutputStream(new BufferedOutputStream(fsync ? new AtomicFileOutputStream(snapShot) : new FileOutputStream(snapShot)), new Adler32())) { //CheckedOutputStream cout = new CheckedOutputStream() OutputArchive oa = BinaryOutputArchive.getArchive(crcOut); FileHeader header = new FileHeader(SNAP_MAGIC, VERSION, dbId); serialize(dt, sessions, oa, header); long val = crcOut.getChecksum().getValue(); oa.writeLong(val, "val"); oa.writeString("/", "path"); crcOut.flush(); } } }
boolean triedToClose = false, success = false; try { flush(); ((FileOutputStream) out).getFD().sync();
/** * Ensure the tmp file is cleaned up and dstFile is untouched when * aborting an existing file overwrite. */ @Test public void testAbortExistingFile() throws IOException { FileOutputStream fos1 = new FileOutputStream(dstFile); fos1.write(TEST_STRING.getBytes()); fos1.close(); AtomicFileOutputStream fos2 = new AtomicFileOutputStream(dstFile); fos2.abort(); // Should not have touched original file assertEquals(TEST_STRING, ClientBase.readFile(dstFile)); assertEquals(1, testDir.list().length); }
/** * Test case where there is no existing file */ @Test public void testWriteNewFile() throws IOException { OutputStream fos = new AtomicFileOutputStream(dstFile); assertFalse(dstFile.exists()); fos.write(TEST_STRING.getBytes()); fos.flush(); assertFalse(dstFile.exists()); fos.close(); assertTrue(dstFile.exists()); String readBackData = ClientBase.readFile(dstFile); assertEquals(TEST_STRING, readBackData); }
boolean triedToClose = false, success = false; try { flush(); ((FileOutputStream) out).getChannel().force(true);
AtomicFileOutputStream out = new AtomicFileOutputStream(file); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(out)); boolean aborted = false; bw.flush(); out.flush(); } catch (IOException e) { LOG.error("Failed to write new file " + file, e); out.abort(); throw e; } finally { out.close();
/** * Ensure the tmp file is cleaned up and dstFile is untouched when * aborting an existing file overwrite. */ @Test public void testAbortExistingFileAfterFlush() throws IOException { FileOutputStream fos1 = new FileOutputStream(dstFile); fos1.write(TEST_STRING.getBytes()); fos1.close(); AtomicFileOutputStream fos2 = new AtomicFileOutputStream(dstFile); fos2.write(TEST_STRING_2.getBytes()); fos2.flush(); fos2.abort(); // Should not have touched original file assertEquals(TEST_STRING, ClientBase.readFile(dstFile)); assertEquals(1, testDir.list().length); } }
/** * Test case where there is no existing file */ @Test public void testOverwriteFile() throws IOException { assertTrue("Creating empty dst file", dstFile.createNewFile()); OutputStream fos = new AtomicFileOutputStream(dstFile); assertTrue("Empty file still exists", dstFile.exists()); fos.write(TEST_STRING.getBytes()); fos.flush(); // Original contents still in place assertEquals("", ClientBase.readFile(dstFile)); fos.close(); // New contents replace original file String readBackData = ClientBase.readFile(dstFile); assertEquals(TEST_STRING, readBackData); }