public void testCloseQuietlyNull_inputStream() { Closeables.closeQuietly((InputStream) null); }
public void testCloseQuietlyNull_reader() { Closeables.closeQuietly((Reader) null); }
private static String toString(final InputStream in, final Charset charset) throws IOException { try { return new String(ByteStreams.toByteArray(in), charset); } finally { Closeables.closeQuietly(in); } }
public Properties test(Properties props, InputStream stream) { try { if (stream == null) { LOGGER.log(Level.WARNING, String.format("File [%s] not found. Using default properties.", PROPERTIES_FILENAME)); } else { props.load(stream); } } catch (IOException e) { LOGGER.log(Level.WARNING, String.format("Error reading [%s]. Using default properties.", PROPERTIES_FILENAME), e); } finally { Closeables.closeQuietly(stream); } return props; }
/** Returns a stream of each line in the trace file. */ protected Stream<String> lines() throws IOException { InputStream input = readFiles(); Reader reader = new InputStreamReader(input, UTF_8); return new BufferedReader(reader).lines().map(String::trim) .onClose(() -> Closeables.closeQuietly(input)); } }
@Override public LongStream events() throws IOException { DataInputStream input = new DataInputStream(new BufferedInputStream(readFiles())); LongStream stream = StreamSupport.longStream(Spliterators.spliteratorUnknownSize( new TraceIterator(input), Spliterator.ORDERED), /* parallel */ false); return stream.onClose(() -> Closeables.closeQuietly(input)); }
private static Supplier attr(final URL resource, final BiConsumer<Long, Long> attrs) throws Exception { if ("file".equals(resource.getProtocol())) { File file = new File(resource.toURI()); if (file.exists() && file.isFile()) { attrs.accept(file.length(), file.lastModified()); return () -> new FileInputStream(file); } return null; } else { URLConnection cnn = resource.openConnection(); cnn.setUseCaches(false); attrs.accept(cnn.getContentLengthLong(), cnn.getLastModified()); try { Closeables.closeQuietly(cnn.getInputStream()); } catch (NullPointerException ex) { // dir entries throw NPE :S return null; } return () -> resource.openStream(); } }
public String loadContent(String fromFile) { BufferedReader reader = null; StringBuilder sb = new StringBuilder(); try { InputStream is = getResource2(fromFile); reader = new BufferedReader(new InputStreamReader(is)); CharStreams.copy(reader, sb); } catch( IOException e ){ Log.err(e); } finally { Closeables.closeQuietly(reader); } return sb.toString(); } private final String _jarPath;
void loadMapper(Class mapperClass) { String configFile = configFilePath(mapperClass); InputStream input = null; try { input = mapperClass.getResourceAsStream(configFile); checkArgument(input != null, format("Can not find mapper XML file %s", configFile)); new SQXMLMapperBuilder(mapperClass, input, conf, conf.getSqlFragments()).parse(); loadAndConfigureLogger(mapperClass.getName()); } catch (Exception e) { throw new IllegalArgumentException("Unable to load mapper " + mapperClass, e); } finally { Closeables.closeQuietly(input); } }
private SSLContext createServerContext() { InputStream is = this.getKeyStore(); try { KeyStore keyStore = KeyStore.getInstance("JKS"); keyStore.load(is, this.getKeyStorePassword()); KeyManagerFactory factory = KeyManagerFactory.getInstance(getAlgorithm()); factory.init(keyStore, this.getCertPassword()); SSLContext serverContext = SSLContext.getInstance(PROTOCOL); serverContext.init(factory.getKeyManagers(), null, null); return serverContext; } catch (Exception e) { throw new MocoException("Failed to initialize the server-side SSLContext", e); } finally { Closeables.closeQuietly(is); } }
private LessSource source(final String filename) throws StringSourceException { String[] files = {path + filename, filename }; for (String file : files) { InputStream stream = getClass().getResourceAsStream(file); if (stream != null) { try { return new LessStrSource( new String(ByteStreams.toByteArray(stream), StandardCharsets.UTF_8), file); } catch (IOException ex) { log.debug("Can't read file: " + path, ex); throw new StringSourceException(); } finally { Closeables.closeQuietly(stream); } } } throw new StringSourceException(); }
private static Map<String, Object> extractMetadata(final Class<?> owner) { InputStream stream = null; try { Map<String, Object> md = new HashMap<>(); stream = Resources.getResource(owner, classfile(owner)).openStream(); new ClassReader(stream).accept(visitor(md), 0); return md; } catch (Exception ex) { // won't happen, but... throw new IllegalStateException("Can't read class: " + owner.getName(), ex); } finally { Closeables.closeQuietly(stream); } }
public void testCloseQuietly_inputStreamWithEatenException() throws IOException { TestInputStream in = new TestInputStream(new ByteArrayInputStream(new byte[1]), TestOption.CLOSE_THROWS); Closeables.closeQuietly(in); assertTrue(in.closed()); }
private NanoHTTPD.Response getResource(String uri) { byte[] bytes = _cache.get(uri); if( bytes == null ) { InputStream resource = Boot._init.getResource2(uri); if (resource != null) { try { bytes = ByteStreams.toByteArray(resource); } catch( IOException e ) { Log.err(e); } byte[] res = _cache.putIfAbsent(uri,bytes); if( res != null ) bytes = res; // Racey update; take what is in the _cache } Closeables.closeQuietly(resource); } if ((bytes == null) || (bytes.length == 0)) { // make sure that no Exception is ever thrown out from the request Properties parms = new Properties(); parms.setProperty(Request.ERROR,uri); return _http404.serve(this,parms,Request.RequestType.www); } String mime = NanoHTTPD.MIME_DEFAULT_BINARY; if (uri.endsWith(".css")) mime = "text/css"; else if (uri.endsWith(".html")) mime = "text/html"; // return new NanoHTTPD.Response(NanoHTTPD.HTTP_OK,mime,new ByteArrayInputStream(bytes)); NanoHTTPD.Response res = new NanoHTTPD.Response(NanoHTTPD.HTTP_OK,mime,new ByteArrayInputStream(bytes)); res.addHeader("Content-Length", Long.toString(bytes.length)); // res.addHeader("Content-Disposition", "attachment; filename=" + uri); return res; }
props.load(in); } finally { Closeables.closeQuietly(in);
public void testCloseQuietly_readerWithEatenException() throws IOException { TestReader in = new TestReader(TestOption.CLOSE_THROWS); Closeables.closeQuietly(in); assertTrue(in.closed()); }
throw new IllegalStateException(errorMsg, exception); } finally { Closeables.closeQuietly(stringBuffer);
/** * Close the attached spillMap */ @Override public void close() { for (int i = 0; i < spillMaps.size(); i++) { Closeables.closeQuietly(spillMaps.get(i).getSpillFile()); } }
@Override public void close() throws IOException { try { s.close(); } finally { // Always close gbCache and swallow possible Exceptions Closeables.closeQuietly(SpillableGroupByCache.this); } }
@Override public void service(HttpServletRequest request, HttpServletResponse response) { ArgoRequest argoRequest = new ArgoRequest(request, config); try { BeatContext beatContext = bindBeatContext(argoRequest, response); route(beatContext); } finally { Closeables.closeQuietly(argoRequest); } }