List of usage examples for com.google.common.io Closeables close
public static void close(@Nullable Closeable closeable, boolean swallowIOException) throws IOException
From source file:org.apache.mahout.classifier.sgd.ModelSerializer.java
public static void writeBinary(String path, AdaptiveLogisticRegression model) throws IOException { DataOutputStream out = new DataOutputStream(new FileOutputStream(path)); try {//from w w w . j a va 2s.co m PolymorphicWritable.write(out, model); } finally { Closeables.close(out, false); } }
From source file:com.wonders.diamond.core.utils.CloseableUtils.java
/** * <p>/*from w w w . j a va2 s. c om*/ * This method has been added because Guava has removed the * {@code closeQuietly()} method from {@code Closeables} in v16.0. It's * tempting simply to replace calls to {@code closeQuietly(closeable)} * with calls to {@code close(closeable, true)} to close * {@code Closeable}s while swallowing {@code IOException}s, but * {@code close()} is declared as {@code throws IOException} whereas * {@code closeQuietly()} is not, so it's not a drop-in replacement. * </p> * <p> * On the whole, Guava is very backwards compatible. By fixing this nit, * Curator can continue to support newer versions of Guava without having * to bump its own dependency version. * </p> * <p> * See <a href="https://issues.apache.org/jira/browse/CURATOR-85">https://issues.apache.org/jira/browse/CURATOR-85</a> * </p> */ public static void closeQuietly(Closeable closeable) { try { // Here we've instructed Guava to swallow the IOException Closeables.close(closeable, true); } catch (IOException e) { // We instructed Guava to swallow the IOException, so this should // never happen. Since it did, log it. log.error("IOException should not have been thrown.", e); } }
From source file:eu.interedition.collatex.cli.PluginScript.java
public static PluginScript read(URL source) throws ScriptException, IOException { InputStream sourceStream = null; try {/*from ww w . j a va 2 s. c o m*/ return read(source.toString(), new InputStreamReader(sourceStream = source.openStream(), SCRIPT_CHARSET)); } finally { Closeables.close(sourceStream, false); } }
From source file:org.jooby.internal.URLAsset.java
private static long lastModified(final URL resource) throws IOException { URLConnection uc = null;/*ww w . j a v a 2s . c om*/ try { uc = resource.openConnection(); return uc.getLastModified(); } catch (IOException ex) { return -1; } finally { if (uc != null) { // http://stackoverflow.com/questions/2057351/how-do-i-get-the-last-modification-time-of // -a-java-resource Closeables.close(uc.getInputStream(), true); } } }
From source file:org.apache.mahout.classifier.sgd.ModelSerializer.java
public static <T extends Writable> T readBinary(InputStream in, Class<T> clazz) throws IOException { DataInput dataIn = new DataInputStream(in); try {/*from w w w.ja v a2s . co m*/ return PolymorphicWritable.read(dataIn, clazz); } finally { Closeables.close(in, false); } }
From source file:org.apache.mahout.clustering.ClusteringTestUtils.java
public static void writePointsToFile(Iterable<VectorWritable> points, boolean intWritable, Path path, FileSystem fs, Configuration conf) throws IOException { SequenceFile.Writer writer = new SequenceFile.Writer(fs, conf, path, intWritable ? IntWritable.class : LongWritable.class, VectorWritable.class); try {// w ww .j a v a 2s . c om int recNum = 0; for (VectorWritable point : points) { writer.append(intWritable ? new IntWritable(recNum++) : new LongWritable(recNum++), point); } } finally { Closeables.close(writer, false); } }
From source file:ca.travelagency.utils.ZipUtils.java
public static String uncompress(byte[] source) throws IOException { ByteArrayOutputStream byteArrayOutputStream = null; try {//from www. jav a 2 s.c om Inflater inflater = new Inflater(); inflater.setInput(source); byteArrayOutputStream = new ByteArrayOutputStream(source.length); byte[] buffer = new byte[BUFFER_SIZE]; while (!inflater.finished()) { int count = inflater.inflate(buffer); byteArrayOutputStream.write(buffer, 0, count); } return new String(byteArrayOutputStream.toByteArray(), Charsets.UTF_8); } catch (DataFormatException e) { throw new IOException(e); } finally { Closeables.close(byteArrayOutputStream, true); } }
From source file:org.kitesdk.data.spi.Schemas.java
public static Schema fromAvsc(Configuration conf, URI location) throws IOException { InputStream in = null;/* w w w .j av a 2 s . c o m*/ boolean threw = true; try { in = open(conf, location); Schema schema = fromAvsc(in); threw = false; return schema; } finally { Closeables.close(in, threw); } }
From source file:org.asoem.greyfish.utils.persistence.JavaPersister.java
private <T> T deserialize(final ByteSource byteSource, final Class<T> clazz) throws IOException, ClassNotFoundException { final InputStream input = byteSource.openBufferedStream(); boolean threw = true; try {/*from w w w .j a v a2 s . co m*/ final T object = deserialize(input, clazz); threw = false; return object; } finally { Closeables.close(input, threw); } }
From source file:org.calrissian.mango.collect.AbstractCloseableIterable.java
/** * {@inheritDoc}//from www . j a va 2 s. c o m */ @Override public void closeQuietly() { try { Closeables.close(this, true); } catch (IOException e) { // IOException should not have been thrown } }