Example usage for com.google.common.io Closeables close

List of usage examples for com.google.common.io Closeables close

Introduction

In this page you can find the example usage for com.google.common.io Closeables close.

Prototype

public static void close(@Nullable Closeable closeable, boolean swallowIOException) throws IOException 

Source Link

Document

Closes a Closeable , with control over whether an IOException may be thrown.

Usage

From source file:InstanceServer.java

@Override
public void close() throws IOException {
    Closeables.close(this.serviceDiscovery, true);
}

From source file:org.kitesdk.data.spi.Schemas.java

public static Schema fromAvro(InputStream in) throws IOException {
    GenericDatumReader<GenericRecord> datumReader = new GenericDatumReader<GenericRecord>();
    DataFileStream<GenericRecord> stream = null;
    boolean threw = true;

    try {//from  w  w w.jav  a  2 s  .c om
        stream = new DataFileStream<GenericRecord>(in, datumReader);
        Schema schema = stream.getSchema();
        threw = false;
        return schema;
    } finally {
        Closeables.close(stream, threw);
    }
}

From source file:com.google.idea.blaze.base.util.SerializationUtil.java

@Nullable
public static Object loadFromDisk(@NotNull File file, @NotNull final Iterable<ClassLoader> classLoaders)
        throws IOException {
    try {//ww w  .ja  v a 2 s. co  m
        FileInputStream fin = null;
        try {
            if (!file.exists()) {
                return null;
            }
            fin = new FileInputStream(file);
            ObjectInputStream ois = new ObjectInputStream(fin) {
                @Override
                protected Class<?> resolveClass(ObjectStreamClass desc)
                        throws IOException, ClassNotFoundException {
                    String name = desc.getName();
                    for (ClassLoader loader : classLoaders) {
                        try {
                            return Class.forName(name, false, loader);
                        } catch (ClassNotFoundException e) {
                            // Ignore - will throw eventually in super
                        }
                    }
                    return super.resolveClass(desc);
                }
            };
            try {
                return (Object) ois.readObject();
            } finally {
                Closeables.close(ois, false);
            }
        } finally {
            Closeables.close(fin, false);
        }
    } catch (ClassNotFoundException e) {
        throw new IOException(e);
    } catch (ClassCastException e) {
        throw new IOException(e);
    }
}

From source file:tv.icntv.grade.film.utils.HadoopUtils.java

public static void save(Path path, Long size) throws IOException {
    FileSystem fileSystem = null;
    FSDataOutputStream out = null;/*from w  w w  .j a va 2s .c  om*/
    try {

        fileSystem = FileSystem.get(configuration);
        if (fileSystem.exists(path)) {
            fileSystem.delete(path);
            out = fileSystem.create(path);
            out.writeLong(size);
            out.flush();
        }

    } catch (Exception e) {
        return;
    } finally {
        Closeables.close(out, true);
        if (null != fileSystem) {
            fileSystem.close();
        }
    }
}

From source file:com.jdom.mediadownloader.FileLockApplicationLock.java

/**
 * {@inheritDoc}/*from  ww w  . j  av  a  2s  . c  om*/
 * 
 * @see com.jdom.mediadownloader.ApplicationLock#unlock()
 */
@Override
public void unlock() {
    if (fl != null) {
        try {
            fl.release();
        } catch (IOException ioe) {
            LOG.error("Unable to release file lock!", ioe);
        }
    }
    if (fos != null) {
        try {
            Closeables.close(fos, true);
        } catch (IOException e) {
            LOG.error("Unable to close output stream!", e);
        }
    }
}

From source file:org.apache.spark.util.collection.unsafe.sort.UnsafeSorterSpillReader.java

public UnsafeSorterSpillReader(BlockManager blockManager, File file, BlockId blockId) throws IOException {
    assert (file.length() > 0);
    final BufferedInputStream bs = new BufferedInputStream(new FileInputStream(file));
    try {/*from w  w w  .j a v a  2s.com*/
        this.in = blockManager.wrapForCompression(blockId, bs);
        this.din = new DataInputStream(this.in);
        numRecordsRemaining = din.readInt();
    } catch (IOException e) {
        Closeables.close(bs, /* swallowIOException = */ true);
        throw e;
    }
}

From source file:org.apache.mahout.vectorizer.document.SequenceFileTokenizerMapper.java

@Override
protected void map(Text key, Text value, Context context) throws IOException, InterruptedException {
    TokenStream stream = analyzer.tokenStream(key.toString(), new StringReader(value.toString()));
    CharTermAttribute termAtt = stream.addAttribute(CharTermAttribute.class);
    stream.reset();/*  ww  w  . j  a v a  2s  . com*/
    StringTuple document = new StringTuple();
    while (stream.incrementToken()) {
        if (termAtt.length() > 0) {
            document.add(new String(termAtt.buffer(), 0, termAtt.length()));
        }
    }
    stream.end();
    Closeables.close(stream, true);
    context.write(key, document);
}

From source file:com.metamx.common.io.smoosh.SmooshedFileMapper.java

public static SmooshedFileMapper load(File baseDir) throws IOException {
    File metaFile = FileSmoosher.metaFile(baseDir);

    BufferedReader in = null;/*from ww  w .j  a v  a2 s.  c  om*/
    try {
        in = new BufferedReader(new InputStreamReader(new FileInputStream(metaFile), Charsets.UTF_8));

        String line = in.readLine();
        if (line == null) {
            throw new ISE("First line should be version,maxChunkSize,numChunks, got null.");
        }

        String[] splits = line.split(",");
        if (!"v1".equals(splits[0])) {
            throw new ISE("Unknown version[%s], v1 is all I know.", splits[0]);
        }
        if (splits.length != 3) {
            throw new ISE("Wrong number of splits[%d] in line[%s]", splits.length, line);
        }
        final Integer numFiles = Integer.valueOf(splits[2]);
        List<File> outFiles = Lists.newArrayListWithExpectedSize(numFiles);

        for (int i = 0; i < numFiles; ++i) {
            outFiles.add(FileSmoosher.makeChunkFile(baseDir, i));
        }

        Map<String, Metadata> internalFiles = Maps.newTreeMap();
        while ((line = in.readLine()) != null) {
            splits = line.split(",");

            if (splits.length != 4) {
                throw new ISE("Wrong number of splits[%d] in line[%s]", splits.length, line);
            }
            internalFiles.put(splits[0], new Metadata(Integer.parseInt(splits[1]), Integer.parseInt(splits[2]),
                    Integer.parseInt(splits[3])));
        }

        return new SmooshedFileMapper(outFiles, internalFiles);
    } finally {
        Closeables.close(in, false);
    }
}

From source file:org.eclipse.recommenders.utils.Zips.java

public static void zip(File directory, File out) throws IOException {
    ZipOutputStream zos = null;/*from   w w w .  j a  v  a  2  s .c  o  m*/
    try {
        OutputSupplier<FileOutputStream> s = Files.newOutputStreamSupplier(out);
        zos = new ZipOutputStream(s.getOutput());
        for (File f : FileUtils.listFiles(directory, FILE, DIRECTORY)) {
            String path = removeStart(f.getPath(), directory.getAbsolutePath() + "/");
            ZipEntry e = new ZipEntry(path);
            zos.putNextEntry(e);
            byte[] data = Files.toByteArray(f);
            zos.write(data);
            zos.closeEntry();
        }
    } finally {
        Closeables.close(zos, false);
    }
}

From source file:com.ml.hadoop.nlp.SequenceFileTokenizerMapper.java

@Override
protected void map(Text key, Text value, Context context) throws IOException, InterruptedException {
    TokenStream stream = analyzer.tokenStream(key.toString(), new StringReader(value.toString()));
    stream.reset();//from www .  j a  va 2  s  . c o m
    CharTermAttribute termAtt = stream.addAttribute(CharTermAttribute.class);
    stream.reset();
    StringTuple document = new StringTuple();
    while (stream.incrementToken()) {
        if (termAtt.length() > 0) {
            document.add(new String(termAtt.buffer(), 0, termAtt.length()));
        }
    }
    stream.end();
    Closeables.close(stream, true);

    //drop stop words
    document = StopWordsHandler.dropStopWords(document);
    context.write(key, document);
}