当前位置: 首页>>代码示例>>Java>>正文


Java AtomicFileOutputStream类代码示例

本文整理汇总了Java中org.apache.zookeeper.common.AtomicFileOutputStream的典型用法代码示例。如果您正苦于以下问题:Java AtomicFileOutputStream类的具体用法?Java AtomicFileOutputStream怎么用?Java AtomicFileOutputStream使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


AtomicFileOutputStream类属于org.apache.zookeeper.common包,在下文中一共展示了AtomicFileOutputStream类的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: writeLongToFile

import org.apache.zookeeper.common.AtomicFileOutputStream; //导入依赖的package包/类
/**
* Write a long value to disk atomically. Either succeeds or an exception
* is thrown.
* @param name file name to write the long to
* @param value the long value to write to the named file
* @throws IOException if the file cannot be written atomically
*/
  private void writeLongToFile(String name, long value) throws IOException {
      File file = new File(logFactory.getSnapDir(), name);
      AtomicFileOutputStream out = new AtomicFileOutputStream(file);
      BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(out));
      boolean aborted = false;
      try {
          bw.write(Long.toString(value));
          bw.flush();
          
          out.flush();
      } catch (IOException e) {
          LOG.error("Failed to write new file " + file, e);
          // worst case here the tmp file/resources(fd) are not cleaned up
          //   and the caller will be notified (IOException)
          aborted = true;
          out.abort();
          throw e;
      } finally {
          if (!aborted) {
              // if the close operation (rename) fails we'll get notified.
              // worst case the tmp file may still exist
              out.close();
          }
      }
  }
 
开发者ID:l294265421,项目名称:ZooKeeper,代码行数:33,代码来源:QuorumPeer.java

示例2: testOverwriteFile

import org.apache.zookeeper.common.AtomicFileOutputStream; //导入依赖的package包/类
/**
 * 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);
}
 
开发者ID:maoling,项目名称:fuck_zookeeper,代码行数:23,代码来源:AtomicFileOutputStreamTest.java

示例3: testAbortExistingFileAfterFlush

import org.apache.zookeeper.common.AtomicFileOutputStream; //导入依赖的package包/类
/**
 * 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);
}
 
开发者ID:maoling,项目名称:fuck_zookeeper,代码行数:21,代码来源:AtomicFileOutputStreamTest.java

示例4: serialize

import org.apache.zookeeper.common.AtomicFileOutputStream; //导入依赖的package包/类
/**
 * 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();
        }
    }
}
 
开发者ID:didichuxing2,项目名称:https-github.com-apache-zookeeper,代码行数:26,代码来源:FileSnap.java

示例5: writeLongToFile

import org.apache.zookeeper.common.AtomicFileOutputStream; //导入依赖的package包/类
static void writeLongToFile(File file, long value) throws IOException {
    AtomicFileOutputStream out = new AtomicFileOutputStream(file);
    BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(out));
    try {
        bw.write(Long.toString(value));
        bw.flush();
        out.flush();
        out.close();
    } catch (IOException e) {
        LOG.error("Failed to write new file " + file, e);
        out.abort();
        throw e;
    }
}
 
开发者ID:l294265421,项目名称:ZooKeeper,代码行数:15,代码来源:QuorumPeerMainTest.java

示例6: testWriteNewFile

import org.apache.zookeeper.common.AtomicFileOutputStream; //导入依赖的package包/类
/**
 * 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);
}
 
开发者ID:maoling,项目名称:fuck_zookeeper,代码行数:17,代码来源:AtomicFileOutputStreamTest.java

示例7: createFailingStream

import org.apache.zookeeper.common.AtomicFileOutputStream; //导入依赖的package包/类
/**
 * Create a stream that fails to flush at close time
 */
private OutputStream createFailingStream() throws FileNotFoundException {
    return new AtomicFileOutputStream(dstFile) {
        @Override
        public void flush() throws IOException {
            throw new IOException("injected failure");
        }
    };
}
 
开发者ID:maoling,项目名称:fuck_zookeeper,代码行数:12,代码来源:AtomicFileOutputStreamTest.java


注:本文中的org.apache.zookeeper.common.AtomicFileOutputStream类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。