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:ca.travelagency.utils.ZipUtils.java

public static byte[] compress(String source) throws IOException {
    ByteArrayOutputStream byteArrayOutputStream = null;
    try {//  w w w . java2s  .com
        byte[] bytes = source.getBytes(Charsets.UTF_8);
        Deflater deflater = new Deflater();
        deflater.setInput(bytes);
        deflater.finish();

        byteArrayOutputStream = new ByteArrayOutputStream(bytes.length);
        byte[] buffer = new byte[1024];
        while (!deflater.finished()) {
            int count = deflater.deflate(buffer);
            byteArrayOutputStream.write(buffer, 0, count);
        }
        return byteArrayOutputStream.toByteArray();
    } finally {
        Closeables.close(byteArrayOutputStream, true);
    }
}

From source file:gmusic.api.comm.Util.java

public static String toString(final InputStream is, final Charset cs) throws IOException {
    Closeable closeMe = is;//w  ww  . j  a v  a  2  s . c  om
    try {
        final InputStreamReader isr = new InputStreamReader(is, cs);
        closeMe = isr;
        return CharStreams.toString(isr);
    } finally {
        Closeables.close(closeMe, true);
    }
}

From source file:sonia.maven.native2utf8.Native2UTF8.java

/**
 * Method description//w ww.  j  av  a  2  s  . co  m
 *
 *
 * @param src
 * @param dst
 * @param encoding
 *
 * @throws IOException
 */
public static void nativeToUTF8(File src, File dst, String encoding) throws IOException {
    BufferedReader input = null;
    BufferedWriter output = null;

    try {
        input = new BufferedReader(new InputStreamReader(new FileInputStream(src), encoding));
        output = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(dst), "UTF8"));

        char[] buffer = new char[65536];
        int len;

        while ((len = input.read(buffer)) != -1) {
            if (len > 0) {
                output.write(StringEscapeUtils.unescapeJava((CharBuffer.wrap(buffer, 0, len)).toString()));
            }
        }
    } finally {
        Closeables.close(input, true);
        Closeables.close(output, true);
    }
}

From source file:com.google.dart.compiler.backend.dart.DartBackend.java

private static void packageLibs(Collection<LibraryUnit> libraries, Writer w, DartCompilerContext context)
        throws IOException {
    for (LibraryUnit libUnit : libraries) {
        for (DartUnit unit : libUnit.getUnits()) {
            DartSource src = unit.getSource();
            if (src != null) {
                Reader r = context.getArtifactReader(src, "", EXTENSION_DART);
                boolean failed = true;
                try {
                    CharStreams.copy(r, w);
                    failed = false;/*  ww  w.jav  a2s. c o m*/
                } finally {
                    Closeables.close(r, failed);
                }
            }
        }
    }
}

From source file:org.apache.mahout.classifier.sgd.ModelSerializer.java

public static void writeBinary(String path, OnlineLogisticRegression model) throws IOException {
    DataOutputStream out = new DataOutputStream(new FileOutputStream(path));
    try {//  w  ww.  j  av a  2 s. c om
        PolymorphicWritable.write(out, model);
    } finally {
        Closeables.close(out, false);
    }
}

From source file:defrac.intellij.util.DefaultSettings.java

public static void write(@NotNull final String name, @NotNull final String package$,
        @NotNull final String identifier, @NotNull final String version,
        @NotNull final Set<DefracPlatform> platforms, @NotNull final File file) throws IOException {
    PrintWriter writer = null;//from w w w .j  a va  2  s .c o m

    try {
        writer = new PrintWriter(Files.newWriter(file, Charsets.UTF_8));

        writer.println("{\n" + "  \"name\": \"" + name + "\",\n" + "  \"package\": \"" + package$ + "\",\n"
                + "  \"identifier\": \"" + identifier + "\",\n" + "  \"version\": \"" + version + "\",\n"
                + "  \"targets\": [");

        for (final DefracPlatform platform : platforms) {
            writer.println("    \"" + platform.name + "\",");
        }

        writer.println("  ],\n" + "  \"web\": {\n" + "    \"js\": \"" + identifier + ".js\"\n" + "  }\n" + "}");
    } finally {
        Closeables.close(writer, /*swallowException=*/false);
    }
}

From source file:eu.redzoo.reactive.sse.SSEOutputStream.java

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

From source file:org.calrissian.mango.collect.mock.MockIterable.java

@Override
public void closeQuietly() {
    try {//from   www .j a v  a 2  s.  com
        Closeables.close(this, true);
    } catch (IOException e) {
        // IOException should not have been thrown
    }
}

From source file:net.derquinse.common.io.DurableFiles.java

/**
 * Copies to a file all bytes from an {@link InputStream} supplied by a factory. The file is
 * sync'd before being closed.//from w w  w. j a  v a2s.  co m
 * @param from the input factory
 * @param to the destination file
 * @throws IOException if an I/O error occurs
 */
public static void copy(ByteSource from, File to) throws IOException {
    boolean threw = true;
    FileOutputStream os = new FileOutputStream(to);
    try {
        from.copyTo(os);
        os.flush();
        os.getFD().sync();
        threw = false;
    } finally {
        Closeables.close(os, threw);
    }
}

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

public static Schema fromAvsc(FileSystem fs, Path path) throws IOException {
    InputStream in = null;/*from  ww  w.j av a  2  s .  c o m*/
    boolean threw = true;

    try {
        in = fs.open(path);
        Schema schema = new Schema.Parser().parse(in);
        threw = false;
        return schema;
    } finally {
        Closeables.close(in, threw);
    }
}