List of usage examples for com.google.common.io Closeables closeQuietly
public static void closeQuietly(@Nullable Reader reader)
From source file:org.apache.twill.yarn.ResourceReportClient.java
/** * Returns the resource usage of the application fetched from the resource endpoint URL. * @return A {@link ResourceReport} or {@code null} if failed to fetch the report. *///from w ww.ja va 2 s.c o m public ResourceReport get() { for (URL url : resourceUrls) { try { Reader reader = new BufferedReader(new InputStreamReader(url.openStream(), Charsets.UTF_8)); try { LOG.trace("Report returned by {}", url); return reportAdapter.fromJson(reader); } finally { Closeables.closeQuietly(reader); } } catch (IOException e) { // Just log a trace as it's ok to not able to fetch resource report LOG.trace("Exception raised when getting resource report from {}.", url, e); } } return null; }
From source file:ch.bd.qv.quiz.SimplePropertyResourceLoader.java
private String lookup(String key, Locale locale) { FileInputStream fis = null;/* w w w .j a v a 2 s. com*/ Properties props = new Properties(); try { File file = new File(path + "/QuizTranslation_" + locale.getLanguage() + ".properties"); fis = new FileInputStream(file); props.load(fis); // LOGGER.debug("looking for "+key + " in file: "+file.getAbsolutePath()); return (String) props.get(key); } catch (IOException ioex) { LOGGER.warn("cannot access file with locale " + locale, ioex); return null; } finally { Closeables.closeQuietly(fis); } }
From source file:org.richfaces.demo.TreeNodeParser.java
public void parse(URL url) throws IOException, SAXException { InputStream is = null;/*from w w w. j a v a2 s.c om*/ try { is = url.openStream(); reader.setContentHandler(this); reader.parse(new InputSource(is)); } finally { Closeables.closeQuietly(is); } }
From source file:com.anjuke.romar.mahout.similarity.file.DataFileIterator.java
@Override public void close() { endOfData(); Closeables.closeQuietly(_inputStream); }
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> * This method creates a new file.//from w w w . j a v a 2s . c om * * @param model {@link RefSetAutoMapperModel} to persist * @param targetPath {@link IPath} the location where to persist the model * @param fileName String the name of the persisted XML file * * @return the newly created file, <b>null</b> if there was error during the process * @throws SnowowlServiceException */ public static IFile persist(RefSetAutoMapperModel model, IPath targetPath, String fileName) throws SnowowlServiceException { InputStream is = null; try { String builtXml = serializeToXml(model); is = new ByteArrayInputStream(builtXml.toString().getBytes("UTF-8")); IFile file = saveFile(is, targetPath, fileName); return file; } catch (UnsupportedEncodingException e) { ApplicationContext.handleException(SnomedDatastoreActivator.getContext().getBundle(), e, e.getMessage()); } finally { Closeables.closeQuietly(is); } return null; }
From source file:org.cretz.sbnstat.scrape.Cache.java
public void add(String url, String html) throws IOException { //create a UUID that will define this document String uuid;//from w w w. ja v a 2s. c o m File file; do { uuid = UUID.randomUUID().toString(); file = new File(directory, uuid + ".html"); } while (file.exists()); //save HTML Files.write(html, file, Charset.forName("UTF8")); //add to URL property list urls.setProperty(url, uuid); //write the urls file Writer writer = new FileWriter(urlsFile); try { urls.store(writer, "Cache"); } finally { Closeables.closeQuietly(writer); } }
From source file:org.iglootools.hchelpers.core.mime.InputSupplierSourceBody.java
public void writeTo(final OutputStream out) throws IOException { if (out == null) { throw new IllegalArgumentException("Output stream may not be null"); }//from w w w.j a v a2 s. co m InputStream in = null; try { in = this.inputSupplier.getInput(); ByteStreams.copy(in, out); out.flush(); } finally { Closeables.closeQuietly(in); } }
From source file:net.awired.visuwall.hudsonclient.loader.DocumentLoader.java
public Document loadFromUrl(String strUrl) throws DocumentNotLoadedException { Preconditions.checkNotNull(strUrl, "strUrl is mandatory"); InputStream stream = null;/*from w ww . ja v a 2 s . c o m*/ try { URL url = new URL(strUrl); stream = url.openStream(); DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder documentBuilder = builderFactory.newDocumentBuilder(); return documentBuilder.parse(stream); } catch (MalformedURLException e) { throw new DocumentNotLoadedException("Can't load document from url: " + strUrl, e); } catch (IOException e) { throw new DocumentNotLoadedException("Can't load document from url: " + strUrl, e); } catch (SAXException e) { throw new DocumentNotLoadedException("Can't load document from url: " + strUrl, e); } catch (ParserConfigurationException e) { throw new DocumentNotLoadedException("Can't load document from url: " + strUrl, e); } finally { Closeables.closeQuietly(stream); } }
From source file:org.richfaces.request.BaseUploadedFile.java
public byte[] getData() { long size = getSize(); if (size > Integer.MAX_VALUE) { throw new FileUploadException("Resource content is too long to be allocated as byte[]"); }//from w w w. j av a 2 s .co m InputStream is = null; try { is = getInputStream(); byte[] result = new byte[(int) size]; ByteStreams.readFully(is, result); return result; } catch (IOException e) { throw new FileUploadException(e.getMessage(), e); } finally { Closeables.closeQuietly(is); } }
From source file:org.kohsuke.stapler.AbstractAPT6.java
/** * Loads the given content from a location under the class output directory. */// ww w. ja v a 2 s .c om protected final Content load(final Content content) { InputStream is = null; try { final String path = content.location(); final FileObject f = processingEnv.getFiler().getResource(StandardLocation.CLASS_OUTPUT, "", path); content.load(is = f.openInputStream()); } catch (final FilerException e) { // someone else is already processing this file! } catch (final FileNotFoundException e) { // nothing to load } catch (final Exception e) { processingEnv.getMessager().printMessage(Kind.ERROR, e.toString()); } finally { Closeables.closeQuietly(is); } return content; }