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:org.kitesdk.data.spi.Schemas.java

public static Schema fromAvro(Configuration conf, URI location) throws IOException {
    InputStream in = null;/*  w  w  w  .j  a va  2s.  c o m*/
    boolean threw = true;

    try {
        in = open(conf, location);
        Schema schema = fromAvro(in);
        threw = false;
        return schema;
    } finally {
        Closeables.close(in, threw);
    }
}

From source file:com.b2international.snowowl.core.api.preferences.io.ConfigurationEntrySerializer.java

/**
 * For internal use only; serializes current contents into the defaults file.
 *///from w ww.jav  a2  s . c  o  m
public void serializeDefaults() throws IOException {

    FileOutputStream stream = null;

    try {
        stream = new FileOutputStream(defaultsFile);
        new XStreamWrapper(this).toXML(computeDefault(), stream);
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        Closeables.close(stream, true);
    }
}

From source file:ch.ledcom.maven.sitespeed.report.ResourceFiles.java

private void concatenate(List<File> files, File target) throws IOException {
    if (!target.getParentFile().exists()) {
        target.getParentFile().mkdirs();
    }//from  w  w  w .  ja va 2s. co  m
    InputStream in = null;
    OutputStream out = new FileOutputStream(target);
    boolean threw = true;
    try {
        for (File file : files) {
            boolean threw2 = true;
            try {
                in = this.getClass().getClassLoader().getResourceAsStream(BASE_DIR + "/" + file.getPath());
                IOUtil.copy(in, out);
            } finally {
                Closeables.close(in, threw2);
            }
        }
    } finally {
        Closeables.close(out, threw);
    }
}

From source file:org.openqa.selenium.io.Zip.java

private void zip(File inputDir, OutputStream writeTo) throws IOException {
    ZipOutputStream zos = null;//from   w w  w . j  av  a2  s  .  c om
    try {
        zos = new ZipOutputStream(writeTo);
        addToZip(inputDir.getAbsolutePath(), zos, inputDir);
    } finally {
        Closeables.close(zos, false);
    }
}

From source file:com.twitter.common.net.http.handlers.pprof.ProfileHandler.java

@Override
protected final void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {

    final int profileDurationSecs = HttpServletRequestParams.getInt(req, "seconds", 10);
    final int profilePollRate = HttpServletRequestParams.getInt(req, "hz", 100);
    LOG.info("Collecting CPU profile for " + profileDurationSecs + " seconds at " + profilePollRate + " Hz");

    Duration sampleDuration = Duration$.MODULE$.fromTimeUnit(profileDurationSecs, TimeUnit.SECONDS);
    CpuProfile profile = CpuProfile.recordInThread(sampleDuration, profilePollRate, stateToProfile).get();
    resp.setHeader("Content-Type", "pprof/raw");
    resp.setStatus(HttpServletResponse.SC_OK);
    OutputStream responseBody = resp.getOutputStream();
    try {//from  ww w.  j a v  a  2  s  .  co m
        profile.writeGoogleProfile(responseBody);
    } finally {
        Closeables.close(responseBody, /* swallowIOException */ true);
    }
}

From source file:rackspace.openstack.Logging.java

public void close() throws IOException {
    Closeables.close(computeService.getContext(), true);
}

From source file:com.android.tools.idea.sdk.VersionCheck.java

/**
 * Verifies that the Android SDK Tools revision is at least {@link #MIN_TOOLS_REV}
 *
 * @param sdkPath the path of the Android SDK.
 * @return the result of the check.//from ww w.java2 s .  c  o m
 */
@NotNull
public static VersionCheckResult checkVersion(@NotNull String sdkPath) {
    File toolsDir = new File(sdkPath, SdkConstants.OS_SDK_TOOLS_FOLDER);
    Revision toolsRevision = new Revision(Integer.MAX_VALUE);
    BufferedReader reader = null;
    try {
        File sourceProperties = new File(toolsDir, SdkConstants.FN_SOURCE_PROP);
        //noinspection IOResourceOpenedButNotSafelyClosed
        reader = new BufferedReader(new FileReader(sourceProperties));
        String line;
        while ((line = reader.readLine()) != null) {
            Matcher m = SOURCE_PROPERTY_PATTERN.matcher(line);
            if (m.matches()) {
                try {
                    toolsRevision = Revision.parseRevision(m.group(1));
                } catch (NumberFormatException ignore) {
                }
                break;
            }
        }
    } catch (IOException e) {
        String msg = String.format("Failed to read file: '%1$s' for Android SDK at '%2$s'",
                SdkConstants.FN_SOURCE_PROP, sdkPath);
        LOG.info(msg, e);
    } finally {
        try {
            Closeables.close(reader, true /* swallowIOException */);
        } catch (IOException e) {
            // Cannot happen
        }
    }
    return new VersionCheckResult(toolsRevision);
}

From source file:org.moe.designer.android.sdk.VersionCheck.java

/**
 * Verifies that the Android SDK Tools revision is at least {@link #MIN_TOOLS_REV}
 *
 * @param sdkPath the path of the Android SDK.
 * @return the result of the check./*from w w  w  .ja v  a 2 s. c o  m*/
 */
@NotNull
public static VersionCheckResult checkVersion(@NotNull String sdkPath) {
    File toolsDir = new File(sdkPath, SdkConstants.OS_SDK_TOOLS_FOLDER);
    FullRevision toolsRevision = new FullRevision(Integer.MAX_VALUE);
    BufferedReader reader = null;
    try {
        File sourceProperties = new File(toolsDir, SdkConstants.FN_SOURCE_PROP);
        //noinspection IOResourceOpenedButNotSafelyClosed
        reader = new BufferedReader(new FileReader(sourceProperties));
        String line;
        while ((line = reader.readLine()) != null) {
            Matcher m = SOURCE_PROPERTY_PATTERN.matcher(line);
            if (m.matches()) {
                try {
                    toolsRevision = FullRevision.parseRevision(m.group(1));
                } catch (NumberFormatException ignore) {
                }
                break;
            }
        }
    } catch (IOException e) {
        String msg = String.format("Failed to read file: '%1$s' for Android SDK at '%2$s'",
                SdkConstants.FN_SOURCE_PROP, sdkPath);
        LOG.info(msg, e);
    } finally {
        try {
            Closeables.close(reader, true /* swallowIOException */);
        } catch (IOException e) {
            // Cannot happen
        }
    }
    return new VersionCheckResult(toolsRevision);
}

From source file:org.apache.parquet.cli.commands.CatCommand.java

@Override
public int run() throws IOException {
    Preconditions.checkArgument(sourceFiles != null && !sourceFiles.isEmpty(), "Missing file name");
    Preconditions.checkArgument(sourceFiles.size() == 1, "Only one file can be given");

    final String source = sourceFiles.get(0);

    Schema schema = getAvroSchema(source);
    Schema projection = Expressions.filterSchema(schema, columns);

    Iterable<Object> reader = openDataFile(source, projection);
    boolean threw = true;
    long count = 0;
    try {//w w w  .j ava 2 s .  c om
        for (Object record : reader) {
            if (numRecords > 0 && count >= numRecords) {
                break;
            }
            if (columns == null || columns.size() != 1) {
                console.info(String.valueOf(record));
            } else {
                console.info(String.valueOf(select(projection, record, columns.get(0))));
            }
            count += 1;
        }
        threw = false;
    } catch (RuntimeException e) {
        throw new RuntimeException("Failed on record " + count, e);
    } finally {
        if (reader instanceof Closeable) {
            Closeables.close((Closeable) reader, threw);
        }
    }

    return 0;
}

From source file:org.kitesdk.data.filesystem.ParquetAppender.java

@Override
public void close() throws IOException {
    Closeables.close(avroParquetWriter, false);
}