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.notifier.desktop.update.impl.UpdateManagerImpl.java

public Version getCurrentVersion() throws IOException {
    InputStream is = getClass().getResourceAsStream(POM_PROPERTIES_NAME);
    if (is == null) {
        return Version.DEV;
    }/*w  ww  .  j  av  a  2 s .c  o m*/
    Properties pom = new Properties();
    try {
        pom.load(is);
    } finally {
        Closeables.closeQuietly(is);
    }
    return Version.parse(pom.getProperty(POM_VERSION_PROPERTY));
}

From source file:org.eclipse.che.plugin.jsonexample.generator.JsonExampleCreateProjectHandler.java

@Override
public void onCreateProject(FolderEntry baseFolder, Map<String, AttributeValue> attributes,
        Map<String, String> options) throws ForbiddenException, ConflictException, ServerException {

    InputStream packageJson = null;
    InputStream personJson = null;
    try {/*from   ww w.jav a2  s  .  c o m*/
        FolderEntry myJsonFiles = baseFolder.createFolder("myJsonFiles");
        packageJson = getClass().getClassLoader().getResourceAsStream("files/default_package");
        personJson = getClass().getClassLoader().getResourceAsStream("files/default_person");

        baseFolder.createFile(FILE_NAME, packageJson);
        myJsonFiles.createFile("person.json", personJson);
    } finally {
        Closeables.closeQuietly(packageJson);
        Closeables.closeQuietly(personJson);
    }
}

From source file:org.codehaus.httpcache4j.cache.FileManager.java

public synchronized File createFile(Key key, InputStream stream) throws IOException {
    File file = resolve(key);/*from  w  w  w  .j a  va 2 s.  c o m*/
    if (!file.getParentFile().exists()) {
        ensureDirectoryExists(file.getParentFile());
    }
    OutputSupplier<FileOutputStream> outputStream = Files.newOutputStreamSupplier(file);
    try {
        ByteStreams.copy(stream, outputStream);
    } finally {
        Closeables.closeQuietly(stream);
    }
    if (file.length() == 0) {
        file.delete();
        file = null;
    }
    if (file != null && !file.exists()) {
        throw new IOException(String.format("Failed to create File '%s' for Key: %s", file.getName(), key));
    }

    return file;
}

From source file:net.sourceforge.docfetcher.model.parse.MSExcelParser.java

protected String renderText(File file, String filename) throws ParseException {
    InputStream in = null;/*from w ww.  j  a  v  a 2s  .  c o m*/
    try {
        in = new FileInputStream(file);
        ExcelExtractor extractor = null;
        try {
            POIFSFileSystem fs = new POIFSFileSystem(in);
            extractor = new ExcelExtractor(fs);
        } catch (OldExcelFormatException e) {
            /*
             * POI doesn't support the old Excel 5.0/7.0 (BIFF5) format,
             * only the BIFF8 format from Excel 97/2000/XP/2003. Thus, we
             * fall back to another Excel library.
             */
            Closeables.closeQuietly(in);
            return extractWithJexcelAPI(file);
        }
        extractor.setFormulasNotResults(ProgramConf.Bool.IndexExcelFormulas.get());
        return extractor.getText();
    } catch (IOException e) {
        throw new ParseException(e);
    } catch (RuntimeException e) {
        // POI can throw NullPointerExceptions on some odd Excel files
        throw new ParseException(e);
    } finally {
        Closeables.closeQuietly(in);
    }
}

From source file:co.cask.cdap.explore.service.FileWriterHelper.java

/**
 * Generate a text file of schema (key String, value Int) containing the records ("<prefix>i", i)
 * for start <= i < end, using the given delimiter. The file is written using the passed-in output stream.
 *///ww w. j  a  va  2  s  .  c  om
public static void generateTextFile(OutputStream out, String delim, String prefix, int start, int end)
        throws IOException {
    try {
        for (int i = start; i < end; i++) {
            String line = String.format("%s%d%s%d\n", prefix, i, delim, i);
            out.write(Bytes.toBytes(line));
        }
    } finally {
        Closeables.closeQuietly(out);
    }
}

From source file:org.sonar.plugins.objectivec.violations.OCLintProfile.java

@Override
public RulesProfile createProfile(final ValidationMessages messages) {
    LoggerFactory.getLogger(getClass()).info("Creating OCLint Profile");
    Reader config = null;/*from w  w  w  .j  a  va 2  s .  co m*/

    try {
        config = new InputStreamReader(getClass().getResourceAsStream(DEFAULT_PROFILE));
        final RulesProfile profile = profileImporter.importProfile(config, messages);
        profile.setName(OCLintRuleRepository.REPOSITORY_KEY);
        profile.setLanguage(ObjectiveC.KEY);
        profile.setDefaultProfile(true);

        return profile;
    } finally {
        Closeables.closeQuietly(config);
    }
}

From source file:seaclouds.matchmaker.Matchmaker.java

public Map<String, Object> match(String reqName, String reqValue, Map reqDescription)
        throws FileNotFoundException {
    // TODO Auto-generated method stub

    System.out.println("-----\nMatchmaking start");

    //TODO: ask the discoverer to find services with reqName and reqValue

    InputStream cloudInput;/* ww w  .java2 s. c om*/
    cloudInput = getClass().getResourceAsStream("/computeServices.yaml");

    String nuroCaseYaml = null;
    try {
        nuroCaseYaml = CharStreams.toString(new InputStreamReader(cloudInput, Charsets.UTF_8));
    } catch (IOException e) {
        e.printStackTrace();
    }
    Closeables.closeQuietly(cloudInput);
    TOSCAYamlParser cloudModel = new TOSCAYamlParser(nuroCaseYaml);

    Map<String, Object> cloudOfferedServiceList = cloudModel.getNodeTemplates();
    Map<String, Object> suitableServiceList = new LinkedHashMap<>();
    log.info("\n" + cloudOfferedServiceList.size() + " service(s) available");

    for (Entry<String, Object> service : cloudOfferedServiceList.entrySet()) {

        String serviceName = (String) service.getKey();
        Map<String, Object> serviceDescription = (Map) service.getValue();

        //1. check if the service offers a capability equals to reqName and check if the service is of type included in reqValue
        //note: a service can offer more than a single capability, thus they are stored into a map
        Map<String, Object> capabilitiesList = (Map) serviceDescription.get("capabilities");
        String offServiceType = (String) serviceDescription.get("type");

        if (capabilitiesList.containsKey(reqName) && offServiceType.equals(reqValue)) {
            if (matchProperties(reqDescription, serviceDescription)) {
                log.info("Service suitable");
                suitableServiceList.put(serviceName, serviceDescription);
                //put serviceDescription somewhere or return its unique identifier
            } else {
                log.info("Service not suitable");
            }
        } else {
            log.info("Service not suitable");
        }
    }

    //TODO: return a map with matching services for each module/requirement

    log.info("-----\nMatchmaking end");
    return suitableServiceList;
}

From source file:org.apache.mahout.text.ChunkedWriter.java

public void write(String key, String value) throws IOException {
    if (currentChunkSize > maxChunkSizeInBytes) {
        Closeables.closeQuietly(writer);
        writer = new SequenceFile.Writer(fs, conf, getPath(currentChunkID++), Text.class, Text.class);
        currentChunkSize = 0;/* w  w  w. j a v a2s  .c  o m*/
    }

    Text keyT = new Text(key);
    Text valueT = new Text(value);
    currentChunkSize += keyT.getBytes().length + valueT.getBytes().length; // Overhead
    writer.append(keyT, valueT);
}

From source file:org.jclouds.loadbalancer.internal.LoadBalancerServiceContextImpl.java

@Override
public void close() {
    Closeables.closeQuietly(delegate());
}

From source file:tv.icntv.sender.decompress.AbstractDeCompress.java

@Override
public void close(BufferedReader in) {
    Closeables.closeQuietly(in);
}