Example usage for org.apache.zookeeper.common AtomicFileOutputStream abort

List of usage examples for org.apache.zookeeper.common AtomicFileOutputStream abort

Introduction

In this page you can find the example usage for org.apache.zookeeper.common AtomicFileOutputStream abort.

Prototype

public void abort() 

Source Link

Document

Close the atomic file, but do not "commit" the temporary file on top of the destination.

Usage

From source file:com.lami.tuomatuo.mq.zookeeper.common.AtomicFileWritingIdiom.java

License:Apache License

private AtomicFileWritingIdiom(File targetFile, OutputStreamStatement osStmt, WriterStatement wStmt)
        throws IOException {
    org.apache.zookeeper.common.AtomicFileOutputStream out = null;
    boolean error = true;
    try {/*from www  .  j ava 2  s  .c o  m*/
        out = new AtomicFileOutputStream(targetFile);
        if (wStmt == null) {
            // execute output stream operation
            osStmt.write(out);
        } else {
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(out));
            // execute writer operation and flush
            wStmt.write(bw);
            bw.flush();
        }
        out.flush();
        // everything went ok
        error = false;
    } finally {
        // nothing interesting to do if out == null
        if (out != null) {
            if (error) {
                // worst case here the tmp file/resources(fd) are not cleaned up
                // and the caller will be notified (IOException)
                out.abort();
            } else {
                // if the close operation (rename) fails we'll get notified.
                // worst case the tmp file may still exist
                IOUtils.closeStream(out);
            }
        }
    }
}