Example usage for org.apache.hadoop.io IOUtils closeStream

List of usage examples for org.apache.hadoop.io IOUtils closeStream

Introduction

In this page you can find the example usage for org.apache.hadoop.io IOUtils closeStream.

Prototype

public static void closeStream(java.io.Closeable stream) 

Source Link

Document

Closes the stream ignoring Throwable .

Usage

From source file:org.apache.tajo.util.FileUtil.java

License:Apache License

public static void writeProto(File file, Message proto) throws IOException {
    FileOutputStream stream = null;
    try {/*from   w ww.j a  v  a  2  s. co m*/
        stream = new FileOutputStream(file);
        stream.write(proto.toByteArray());
    } finally {
        IOUtils.closeStream(stream);
    }
}

From source file:org.apache.tajo.util.FileUtil.java

License:Apache License

public static void writeProto(FileSystem fs, Path path, Message proto) throws IOException {
    FSDataOutputStream stream = fs.create(path);
    try {//from  w w w .  j  a v  a 2 s  . c o  m
        stream.write(proto.toByteArray());
    } finally {
        IOUtils.closeStream(stream);
    }
}

From source file:org.apache.tajo.util.FileUtil.java

License:Apache License

public static Message loadProto(File file, Message proto) throws IOException {
    FileInputStream in = null;//from   ww w  .jav  a 2s .  com
    try {
        in = new FileInputStream(file);
        Message.Builder builder = proto.newBuilderForType().mergeFrom(in);
        return builder.build();
    } finally {
        IOUtils.closeStream(in);
    }
}

From source file:org.apache.tajo.util.FileUtil.java

License:Apache License

public static Message loadProto(FileSystem fs, Path path, Message proto) throws IOException {
    FSDataInputStream in = null;/*from   w ww. j  a  v a  2 s  .  c  om*/
    try {
        in = new FSDataInputStream(fs.open(path));
        Message.Builder builder = proto.newBuilderForType().mergeFrom(in);
        return builder.build();
    } finally {
        IOUtils.closeStream(in);
    }
}

From source file:org.apache.tajo.util.FileUtil.java

License:Apache License

public static String readTextFromStream(InputStream inputStream) throws IOException {
    try {//w  w w  . j  av a  2s .c  o m
        StringBuilder fileData = new StringBuilder(1000);
        byte[] buf = new byte[1024];
        int numRead;
        while ((numRead = inputStream.read(buf)) != -1) {
            String readData = new String(buf, 0, numRead, Charset.defaultCharset());
            fileData.append(readData);
        }
        return fileData.toString();
    } finally {
        IOUtils.closeStream(inputStream);
    }
}

From source file:org.apache.tajo.util.FileUtil.java

License:Apache License

public static void writeTextToStream(String text, OutputStream outputStream) throws IOException {
    try {//  w ww . j  a v a 2  s .  co  m
        outputStream.write(text.getBytes());
    } finally {
        IOUtils.closeStream(outputStream);
    }
}

From source file:org.apache.tez.common.VersionInfo.java

License:Apache License

protected VersionInfo(String component) {
    this.component = component;
    info = new Properties();
    String versionInfoFile = "/" + component + "-version-info.properties";
    InputStream is = null;/*from  w  w  w  .ja  v a  2 s . c o m*/
    try {
        is = this.getClass().getResourceAsStream(versionInfoFile);
        if (is == null) {
            throw new IOException("Resource not found: " + versionInfoFile);
        }
        info.load(is);
    } catch (IOException ex) {
        LOG.warn("Could not read '" + versionInfoFile + "', " + ex.toString(), ex);
    } finally {
        IOUtils.closeStream(is);
    }
}

From source file:org.apache.tez.http.HttpConnection.java

License:Apache License

/**
 * Cleanup the error stream if any, for keepAlive connections
 *
 * @param errorStream/*from   ww w  .ja v a 2  s .c  om*/
 */
private void readErrorStream(InputStream errorStream) {
    if (errorStream == null) {
        return;
    }
    try {
        DataOutputBuffer errorBuffer = new DataOutputBuffer();
        IOUtils.copyBytes(errorStream, errorBuffer, 4096);
        IOUtils.closeStream(errorBuffer);
        IOUtils.closeStream(errorStream);
    } catch (IOException ioe) {
        // ignore
    }
}

From source file:org.apache.tinkerpop.gremlin.hadoop.structure.hdfs.HDFSTools.java

License:Apache License

public static void decompressFile(final FileSystem fs, final String inFile, final String outFile,
        boolean deletePrevious) throws IOException {
    final Path inPath = new Path(inFile);
    final Path outPath = new Path(outFile);
    final CompressionCodecFactory factory = new CompressionCodecFactory(new Configuration());
    final CompressionCodec codec = factory.getCodec(inPath);
    final OutputStream out = fs.create(outPath);
    final InputStream in = codec.createInputStream(fs.open(inPath));
    IOUtils.copyBytes(in, out, 8192);/*from   w  w  w  . java 2  s  . c  o m*/
    IOUtils.closeStream(in);
    IOUtils.closeStream(out);

    if (deletePrevious)
        fs.delete(new Path(inFile), true);

}

From source file:org.archive.io.hbase.HBaseWriter.java

License:LGPL

/**
 * Read the ReplayInputStream and write it to the given BatchUpdate with the
 * given column./*ww w .  java 2  s  .  com*/
 * 
 * @param replayInputStream
 *            the ris the cell data as a replay input stream
 * @param streamSize
 *            the size
 * 
 * @return the byte array from input stream
 * 
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 */
public static byte[] getByteArrayFromInputStream(final ReplayInputStream replayInputStream,
        final int streamSize) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream(streamSize);
    try {
        // read the InputStream to the ByteArrayOutputStream
        replayInputStream.readFullyTo(baos);
    } finally {
        IOUtils.closeStream(replayInputStream);
        baos.close();
    }
    return baos.toByteArray();
}