List of usage examples for com.google.common.io Closeables closeQuietly
public static void closeQuietly(@Nullable Reader reader)
From source file:org.sonar.colorizer.HtmlDecorator.java
public static String getCss() { InputStream input = null;/*from ww w. java 2s . co m*/ try { input = HtmlRenderer.class.getResourceAsStream(CSS_PATH); return new String(ByteStreams.toByteArray(input)); } catch (IOException e) { throw new SynhtaxHighlightingException("Sonar Colorizer CSS file not found: " + CSS_PATH, e); } finally { Closeables.closeQuietly(input); } }
From source file:com.geico.tfs.matcher.TfsMatcherReader.java
private void parse(File file) { InputStreamReader reader = null; XMLInputFactory xmlFactory = XMLInputFactory.newInstance(); try {//w w w . j a v a 2 s . c om reader = new InputStreamReader(new FileInputStream(file), Charsets.UTF_8); stream = xmlFactory.createXMLStreamReader(reader); while (stream.hasNext()) { if (stream.next() == XMLStreamConstants.START_ELEMENT) { String tagName = stream.getLocalName(); if ("Match".equals(tagName)) { handleMatchTag(); } } } } catch (IOException e) { throw Throwables.propagate(e); } catch (XMLStreamException e) { throw Throwables.propagate(e); } finally { closeXmlStream(); Closeables.closeQuietly(reader); } return; }
From source file:org.jclouds.examples.blobstore.hdfs.io.HdfsPayloadSlicer.java
protected Payload doSlice(final FSDataInputStream inputStream, final long offset, final long length) { return new InputStreamSupplierPayload(new InputSupplier<InputStream>() { public InputStream getInput() throws IOException { if (offset > 0) { try { inputStream.seek(offset); } catch (IOException e) { Closeables.closeQuietly(inputStream); throw e; }//w w w . j a v a 2 s . c o m } return new LimitInputStream(inputStream, length); } }); }
From source file:com.netflix.governator.guice.Grapher.java
/** * Writes the "Dot" graph to a given file. * * @param file file to write to//from w ww .j a v a2 s . c om */ public void toFile(File file) throws Exception { PrintWriter out = new PrintWriter(file, "UTF-8"); try { out.write(graph()); } finally { Closeables.closeQuietly(out); } }
From source file:com.netflix.exhibitor.core.index.ZooKeeperLogFiles.java
private boolean isLogFile(File f) throws Exception { InputStream log = new BufferedInputStream(new FileInputStream(f)); try {//from w ww . j a va2 s .c o m ZooKeeperLogParser logParser = new ZooKeeperLogParser(log); return logParser.isValid(); } finally { Closeables.closeQuietly(log); } }
From source file:org.renyan.leveldb.impl.FileChannelLogWriter.java
@Override public synchronized void close() { closed.set(true);/*from www . j a v a2 s. co m*/ // try to forces the log to disk try { fileChannel.force(true); } catch (IOException ignored) { } // close the channel Closeables.closeQuietly(fileChannel); }
From source file:com.nesscomputing.httpclient.response.StringContentConverter.java
@Override public String convert(HttpClientResponse httpClientResponse, InputStream inputStream) throws IOException { final int responseCode = httpClientResponse.getStatusCode(); switch (responseCode) { case 200:/*from w ww .ja va 2s . com*/ case 201: final Charset charset = Charset.forName(Objects.firstNonNull(httpClientResponse.getCharset(), "UTF-8")); final InputStreamReader reader = new InputStreamReader(inputStream, charset); try { return CharStreams.toString(reader); } finally { Closeables.closeQuietly(reader); } case 204: return ""; case 404: if (ignore404) { return ""; } throw throwHttpResponseException(httpClientResponse); default: throw throwHttpResponseException(httpClientResponse); } }
From source file:org.sonar.fortify.rule.RulePackParser.java
public List<RulePack> parse() { List<File> files = new ArrayList<File>(); for (String location : this.settings.getStringArray(FortifyConstants.RULEPACK_PATHS_PROPERTY)) { File file = new File(location); if (file.isDirectory()) { files.addAll(FileUtils.listFiles(file, new String[] { "xml" }, false)); } else if (file.exists()) { files.add(file);//from w ww. j a va 2 s . c o m } else { LOG.warn("Ignore rulepack location: \"{}\", file is not found.", file); } } List<RulePack> rulePacks = new ArrayList<RulePack>(); for (File file : files) { InputStream stream = null; try { stream = new FileInputStream(file); rulePacks.add(new RulePackStAXParser().parse(stream)); } catch (Exception e) { LOG.error("Unexpected error during the parse of " + file + ".", e); } finally { Closeables.closeQuietly(stream); } } return rulePacks; }
From source file:com.metamx.druid.index.v1.CompressedObjectStrategy.java
@Override public ResourceHolder<T> fromByteBuffer(ByteBuffer buffer, int numBytes) { byte[] bytes = new byte[numBytes]; buffer.get(bytes);//from ww w .ja v a 2s. c o m final ResourceHolder<ByteBuffer> bufHolder = CompressedPools.getByteBuf(order); final ByteBuffer buf = bufHolder.get(); buf.position(0); buf.limit(buf.capacity()); try { final ResourceHolder<byte[]> outputBytesHolder = CompressedPools.getOutputBytes(); byte[] outputBytes = outputBytesHolder.get(); int numDecompressedBytes = LZFDecoder.decode(bytes, outputBytes); buf.put(outputBytes, 0, numDecompressedBytes); buf.flip(); Closeables.closeQuietly(outputBytesHolder); return new ResourceHolder<T>() { @Override public T get() { return converter.convert(buf); } @Override public void close() throws IOException { bufHolder.close(); } }; } catch (IOException e) { throw Throwables.propagate(e); } }
From source file:org.stjs.generator.executor.RhinoExecutor.java
public ExecutionResult run(Collection<File> srcFiles, boolean mainClassDisabled) throws ScriptException { ScriptEngineManager factory = new ScriptEngineManager(); ScriptEngine engine = factory.getEngineByName("JavaScript"); Reader reader = null;/* www.j a v a 2 s. c o m*/ try { reader = new InputStreamReader( Thread.currentThread().getContextClassLoader().getResourceAsStream("stjs.js"), "UTF-8"); engine.eval(reader); if (mainClassDisabled) { engine.eval("stjs.mainCallDisabled=true;"); } Object result = null; for (File srcFile : srcFiles) { // keep the result of last evaluation result = addScript(engine, srcFile); } return new ExecutionResult(result, null, null, 0); } catch (UnsupportedEncodingException e) { throw new ScriptException(e); } finally { Closeables.closeQuietly(reader); } }