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

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

Introduction

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

Prototype

public static void closeQuietly(@Nullable Reader reader) 

Source Link

Document

Closes the given Reader , logging any IOException that's thrown rather than propagating it.

Usage

From source file:com.netflix.exhibitor.core.config.none.NoneConfigProvider.java

@Override
public LoadedInstanceConfig storeConfig(ConfigCollection config, long compareVersion) throws Exception {
    File propertiesFile = new File(directory, FILE_NAME);
    PropertyBasedInstanceConfig propertyBasedInstanceConfig = new PropertyBasedInstanceConfig(config);

    long lastModified = 0;
    OutputStream out = new BufferedOutputStream(new FileOutputStream(propertiesFile));
    try {/* ww  w . jav  a 2 s.c o  m*/
        propertyBasedInstanceConfig.getProperties().store(out, "Auto-generated by Exhibitor");
        lastModified = propertiesFile.lastModified();
    } finally {
        Closeables.closeQuietly(out);
    }

    return new LoadedInstanceConfig(propertyBasedInstanceConfig, lastModified);
}

From source file:com.b2international.snowowl.snomed.core.refset.automap.RefSetAutoMapPersisterUtil.java

/**
 * Persists the given model to XML file at the given location. Uses <code>UTF-8</code> character encoding.<br>
 * If the file already exists, it <b>overrides</b> with the new values, creates a new file otherwise.
 * // w w  w  .  j av  a 2  s  .c o m
 * @param model {@link RefSetAutoMapperModel} to persist
 * @param file {@link IFile} the file to persist the serialized model
 * 
 * @return the newly created file, <b>null</b> if there was error during the creation
 */
public static File persist(RefSetAutoMapperModel model, File file) {
    InputStream is = null;
    try {
        String builtXml = serializeToXml(model);
        File savedFile = saveFile(builtXml, file);

        return savedFile;
    } catch (SnowowlServiceException e) {
        ApplicationContext.handleException(SnomedDatastoreActivator.getContext().getBundle(), e,
                e.getMessage());
    } finally {
        Closeables.closeQuietly(is);
    }
    return null;
}

From source file:com.github.zhongl.io.DirectBuffer.java

private DirectBuffer readFrom(File file) throws IOException {
    FileInputStream stream = new FileInputStream(file);

    try {// w w w  . jav  a  2 s . c  o m
        byteBuffer.clear();
        stream.getChannel().read(byteBuffer);
        byteBuffer.flip();

        return this;
    } finally {
        Closeables.closeQuietly(stream);
    }
}

From source file:org.richfaces.cdk.resource.writer.impl.JavaScriptCompressingProcessor.java

@Override
public void process(String resourceName, InputStream in, OutputStream out, boolean closeAtFinish)
        throws IOException {

    Reader reader = null;//from  w  w w.  java  2 s. co  m
    Writer writer = null;

    try {
        reader = new InputStreamReader(in, charset);
        writer = new OutputStreamWriter(out, charset);

        MavenLogErrorReporter reporter = new MavenLogErrorReporter(resourceName);
        new JavaScriptCompressor(reader, reporter).compress(writer, 0, true, true, false, false);

        if (!closeAtFinish) {
            // add semicolon to satisfy end of context of each script when packing files
            writer.write(";");
            writer.flush();
        }

        if (reporter.hasErrors() && log.isErrorEnabled()) {
            log.error(reporter.getErrorsLog());
        }

        if (reporter.hasWarnings() && log.isDebugEnabled()) {
            log.debug(reporter.getWarningsLog());
        }
    } finally {
        Closeables.closeQuietly(reader);
        if (closeAtFinish) {
            Closeables.closeQuietly(writer);
        } else {
            writer.flush();
        }
    }
}

From source file:co.cask.cdap.internal.app.runtime.worker.WorkerDriver.java

@Override
protected void shutDown() throws Exception {
    if (worker == null) {
        return;//from  w  ww . j  ava2 s . c o  m
    }
    ClassLoader classLoader = setContextCombinedClassLoader();
    try {
        worker.destroy();
    } finally {
        ClassLoaders.setContextClassLoader(classLoader);
        if (context.getPluginInstantiator() != null) {
            Closeables.closeQuietly(context.getPluginInstantiator());
        }
        context.close();
    }
}

From source file:interactivespaces.workbench.project.test.JunitTestClassDetector.java

private JunitTestClassVisitor classVisitor(File testClassFile) {
    JunitTestClassVisitor classVisitor = new JunitTestClassVisitor();

    InputStream classStream = null;
    try {/*from   w w w .j  a  v  a 2  s .  c om*/
        classStream = new BufferedInputStream(new FileInputStream(testClassFile));

        ClassReader classReader = new ClassReader(classStream);
        classReader.accept(classVisitor,
                ClassReader.SKIP_DEBUG | ClassReader.SKIP_CODE | ClassReader.SKIP_FRAMES);
    } catch (Throwable e) {
        throw new InteractiveSpacesException(
                String.format("Could not process class file %s", testClassFile.getAbsolutePath()), e);
    } finally {
        Closeables.closeQuietly(classStream);
    }

    return classVisitor;
}

From source file:org.sonar.batch.report.SonarReport.java

private void exportResults() {
    File exportFile = new File(fileSystem.workingDir(), settings.getString("sonar.report.export.path"));

    LOG.info("Exporting report results to " + exportFile.getAbsolutePath());
    Writer output = null;//w w w.  jav a2  s  .  c  o  m
    try {
        output = new BufferedWriter(new FileWriter(exportFile));
        createJson().writeJSONString(output);
    } catch (IOException e) {
        throw new SonarException("Unable to write report results in file " + exportFile.getAbsolutePath(), e);
    } finally {
        Closeables.closeQuietly(output);
    }
}

From source file:org.envirocar.server.mongo.guice.MongoConnectionModule.java

private Properties getProperties() throws IOException {
    InputStream is = MongoDB.class.getResourceAsStream(PROPERTIES_FILE);
    Properties p = new Properties();
    if (is != null) {
        try {//from  w  ww .j a va  2  s.  c om
            p.load(is);
        } finally {
            Closeables.closeQuietly(is);
        }
    }
    return p;
}

From source file:uk.co.unclealex.process.gobblers.StreamGobbler.java

/**
 * Consume the output of a stream and push it to the {@link ProcessCallback}.
 * /*from   w ww.  ja  v a  2  s  .  c  o  m*/
 * @return The entire output of the stream.
 * @throws IOException
 *           Signals that an I/O exception has occurred.
 */
@Override
public String call() throws IOException {
    StringBuilder stringBuilder = new StringBuilder();
    Thread.currentThread().setName(getThreadName());
    BufferedReader reader = new BufferedReader(new InputStreamReader(getInputStream()));
    try {
        String line;
        ProcessCallback processCallback = getProcessCallback();
        while ((line = reader.readLine()) != null) {
            stringBuilder.append(line).append('\n');
            executeCallback(processCallback, line);
        }
    } finally {
        Closeables.closeQuietly(reader);
    }
    return stringBuilder.toString();
}

From source file:org.trancecode.xproc.step.EscapeMarkupStepProcessor.java

@Override
protected void execute(final StepInput input, final StepOutput output) {
    final XdmNode node = input.readNode(XProcPorts.SOURCE);
    final Map<QName, Object> serializationOptions = Steps.getSerializationOptions(input,
            DEFAULT_SERIALIZATION_OPTIONS);
    LOG.trace("  options = {}", serializationOptions);

    final XdmNode root = SaxonAxis.childElement(node);
    final ByteArrayOutputStream targetOutputStream = new ByteArrayOutputStream();
    final Serializer serializer = Steps.getSerializer(targetOutputStream, serializationOptions,
            input.getPipelineContext().getProcessor());

    try {/*from   w  w  w.java 2s. co m*/
        input.getPipelineContext().getProcessor().writeXdmValue(SaxonAxis.childElement(root), serializer);
        targetOutputStream.close();
    } catch (final Exception e) {
        throw new PipelineException("Error while trying to write document", e);
    } finally {
        Closeables.closeQuietly(targetOutputStream);
    }

    final SaxonBuilder builder = new SaxonBuilder(
            input.getPipelineContext().getProcessor().getUnderlyingConfiguration());
    builder.startDocument();
    builder.startElement(root.getNodeName());
    builder.startContent();
    builder.text(new String(targetOutputStream.toByteArray()));
    builder.endElement();
    builder.endDocument();
    output.writeNodes(XProcPorts.RESULT, builder.getNode());
}