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

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

Introduction

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

Prototype

public AtomicFileOutputStream(File f) throws FileNotFoundException 

Source Link

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 {// w w w  . ja  va 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);
            }
        }
    }
}