Example usage for org.apache.zookeeper.common IOUtils closeStream

List of usage examples for org.apache.zookeeper.common IOUtils closeStream

Introduction

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

Prototype

public static void closeStream(Closeable stream) 

Source Link

Document

Closes the stream ignoring IOException .

Usage

From source file:cn.lhfei.hadoop.ch04.SequenceFileReadDemo.java

License:Apache License

public static void main(String[] args) {
    String uri = args[0];//  w ww  .  jav a 2 s . c o  m
    Configuration conf = new Configuration();
    FileSystem fs = null;
    Path path = new Path(uri);

    SequenceFile.Reader reader = null;

    try {
        fs = FileSystem.get(URI.create(uri), conf);

        reader = new SequenceFile.Reader(fs, path, conf);
        Writable key = (Writable) ReflectionUtils.newInstance(reader.getKeyClass(), conf);
        Writable value = (Writable) ReflectionUtils.newInstance(reader.getValueClass(), conf);
        long position = reader.getPosition();
        while (reader.next(key, value)) {
            String syncSeen = reader.syncSeen() ? "*" : "";
            System.out.printf("[%s%s]\t%s\t%s\n", position, syncSeen, key, value);
            position = reader.getPosition(); // beginning of next record
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        IOUtils.closeStream(reader);
    }

}

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

License:Apache License

@Override
public void close() throws IOException {
    boolean triedToClose = false, success = false;
    try {//  ww w . j av  a  2 s  . c  o m
        flush();
        ((FileOutputStream) out).getFD().sync();

        triedToClose = true;
        super.close();
        success = true;
    } finally {
        if (success) {
            boolean renamed = tmpFile.renameTo(origFile);
            if (!renamed) {
                // On windows, renameTo does not replace.
                if (!origFile.delete() || !tmpFile.renameTo(origFile)) {
                    throw new IOException("Could not rename temporary file " + tmpFile + " to " + origFile);
                }
            }
        } else {
            if (!triedToClose) {
                // If we failed when flushing, try to close it to not leak
                // an FD
                IOUtils.closeStream(out);
            }
            // close wasn't successful, try to delete the tmp file
            if (!tmpFile.delete()) {
                LOG.warn("Unable to delete tmp file " + tmpFile);
            }
        }
    }
}

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  .ja  v  a  2 s  . co 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);
            }
        }
    }
}

From source file:ml.shifu.guagua.yarn.GuaguaSplitWriter.java

License:Apache License

private static void writeJobSplitMetaInfo(FileSystem fs, Path filename, FsPermission p,
        int splitMetaInfoVersion, JobSplit.SplitMetaInfo[] allSplitMetaInfo) throws IOException {
    // write the splits meta-info to a file for the job tracker
    FSDataOutputStream out = null;/* w  w  w. ja va2s .  com*/
    try {
        out = FileSystem.create(fs, filename, p);
        out.write(META_SPLIT_FILE_HEADER);
        WritableUtils.writeVInt(out, splitMetaInfoVersion);
        WritableUtils.writeVInt(out, allSplitMetaInfo.length);
        for (JobSplit.SplitMetaInfo splitMetaInfo : allSplitMetaInfo) {
            splitMetaInfo.write(out);
        }
    } finally {
        IOUtils.closeStream(out);
    }
}

From source file:ml.shifu.guagua.yarn.GuaguaYarnTask.java

License:Apache License

@SuppressWarnings({ "unchecked", "unused" })
private <T> T getSplitDetails(Path file, long offset) throws IOException {
    FileSystem fs = file.getFileSystem(getYarnConf());
    FSDataInputStream inFile = null;/*from www  .j a  va 2  s.c  o m*/
    T split = null;
    try {
        inFile = fs.open(file);
        inFile.seek(offset);
        String className = Text.readString(inFile);
        Class<T> cls;
        try {
            cls = (Class<T>) getYarnConf().getClassByName(className);
        } catch (ClassNotFoundException ce) {
            IOException wrap = new IOException(String.format("Split class %s not found", className));
            wrap.initCause(ce);
            throw wrap;
        }
        SerializationFactory factory = new SerializationFactory(getYarnConf());
        Deserializer<T> deserializer = (Deserializer<T>) factory.getDeserializer(cls);
        deserializer.open(inFile);
        split = deserializer.deserialize(null);
    } finally {
        IOUtils.closeStream(inFile);
    }
    return split;
}

From source file:tlfdetail.query.TLFDetailQuery.java

void showResult(String path, Configuration conf) throws IOException {
    FileSystem fs = FileSystem.get(URI.create(path), conf);
    FSDataInputStream in = null;/*ww  w  .j av a 2 s.  c  o  m*/
    try {
        in = fs.open(new Path(path));
        while (in.available() > 0)
            IOUtils.copyBytes(in, System.out, 217, true);
    } finally {
        IOUtils.closeStream(in);
    }
}