List of usage examples for com.google.common.io Closeables closeQuietly
public static void closeQuietly(@Nullable Reader reader)
From source file:org.renyan.leveldb.impl.DbLock.java
public void release() { try {//from www.j a v a2 s .c o m lock.release(); } catch (IOException e) { Throwables.propagate(e); } finally { Closeables.closeQuietly(channel); } }
From source file:com.foundationdb.server.service.plugins.JarPlugin.java
@Override protected Properties readPropertiesRaw() throws IOException { JarFile jar = new JarFile(pluginJar); ZipEntry configsEntry = jar.getEntry(PROPERTY_FILE_PATH); if (configsEntry == null) throw new IOException("couldn't find " + PROPERTY_FILE_PATH + " in " + jar); InputStream propertiesIS = jar.getInputStream(configsEntry); Properties result = new Properties(); try {/*from www .j a v a 2 s . c o m*/ BufferedReader reader = new BufferedReader(new InputStreamReader(propertiesIS)); result.load(reader); } finally { Closeables.closeQuietly(propertiesIS); } return result; }
From source file:org.envirocar.server.rest.SchemaServlet.java
@Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String file = req.getRequestURI().replace(req.getContextPath(), "").replace(req.getServletPath(), ""); InputStream is = null;//from w ww .j a v a2 s. com try { is = SchemaServlet.class.getResourceAsStream("/schema" + file); if (is == null) { resp.sendError(HttpServletResponse.SC_NOT_FOUND); } else { resp.setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON); ByteStreams.copy(is, resp.getOutputStream()); } } finally { Closeables.closeQuietly(is); } }
From source file:tv.icntv.sender.decompress.AbstractDeCompress.java
@Override public void close(InputStream in) { Closeables.closeQuietly(in); }
From source file:com.metamx.druid.index.v1.MetricHolder.java
public static void writeComplexMetric(OutputSupplier<? extends OutputStream> outSupplier, String name, String typeName, GenericIndexedWriter column) throws IOException { OutputStream out = null;//ww w. j a va 2 s .com InputStream in = null; try { out = outSupplier.getOutput(); out.write(version); serializerUtils.writeString(out, name); serializerUtils.writeString(out, typeName); final InputSupplier<InputStream> supplier = column.combineStreams(); in = supplier.getInput(); ByteStreams.copy(in, out); } finally { Closeables.closeQuietly(out); Closeables.closeQuietly(in); } }
From source file:org.jooby.internal.BodyReaderImpl.java
@SuppressWarnings("unchecked") @Override/*from w w w . j av a2 s . c o m*/ public <T> T bytes(final Bytes bin) throws Exception { InputStream in = null; try { in = this.stream.get(); return (T) bin.read(in); } finally { Closeables.closeQuietly(in); } }
From source file:net.sourceforge.docfetcher.model.Daemon.java
public Daemon(@NotNull IndexRegistry indexRegistry) { Util.checkNotNull(indexRegistry); this.indexRegistry = indexRegistry; File indexParentDir = indexRegistry.getIndexParentDir(); indexesFile = new File(indexParentDir, ".indexes.txt"); /*//ww w . j a v a 2 s. c o m * Open a FileOutputStream for writing. This lets the daemon know that * DocFetcher is currently running. */ String lockPath = Util.getAbsPath(indexesFile) + ".lock"; try { final FileOutputStream daemonLock = new FileOutputStream(lockPath); Runtime.getRuntime().addShutdownHook(new Thread() { public void run() { Closeables.closeQuietly(daemonLock); } }); } catch (FileNotFoundException e) { /* * This can occur if two instances are running, if someone is using * the file or if DocFetcher is run from a CD-ROM. */ Util.printErr(e); } }
From source file:nallar.tickthreading.patcher.remapping.Deobfuscator.java
private static byte[] getClassBytesClassPath(String name) { InputStream stream = TickThreading.class.getResourceAsStream(name.replace('.', '/') + ".class"); if (stream == null) { return null; }//from w w w .j av a 2 s. c o m try { return ByteStreams.toByteArray(stream); } catch (IOException e) { Log.severe("Failed to read class " + name, e); } finally { Closeables.closeQuietly(stream); } return null; }
From source file:com.nesscomputing.hbase.spill.SpilledFile.java
SpilledFile(@Nonnull final File file) throws IOException { Preconditions.checkNotNull(file, "the file must not be null!"); this.file = file; InputStream is = null;//from w w w . j av a 2 s.c o m try { is = new FileInputStream(file); version = BinaryConverter.readInt(is); elements = BinaryConverter.readInt(is); timestamp = BinaryConverter.readLong(is); LOG.trace("Opened spill file %s : v%d, count: %d, age: %d", file.getAbsolutePath(), version, elements, timestamp); } finally { Closeables.closeQuietly(is); } }
From source file:com.metamx.druid.kv.GenericIndexed.java
public static <T> GenericIndexed<T> fromIterable(Iterable<T> objectsIterable, ObjectStrategy<T> strategy) { Iterator<T> objects = objectsIterable.iterator(); if (!objects.hasNext()) { final ByteBuffer buffer = ByteBuffer.allocate(4).putInt(0); buffer.flip();/*from www . ja v a 2s . com*/ return new GenericIndexed<T>(buffer, strategy, true); } boolean allowReverseLookup = true; int count = 1; T prevVal = objects.next(); while (objects.hasNext()) { T next = objects.next(); if (!(strategy.compare(prevVal, next) < 0)) { allowReverseLookup = false; } if (prevVal instanceof Closeable) { Closeables.closeQuietly((Closeable) prevVal); } prevVal = next; ++count; } if (prevVal instanceof Closeable) { Closeables.closeQuietly((Closeable) prevVal); } ByteArrayOutputStream headerBytes = new ByteArrayOutputStream(4 + (count * 4)); ByteArrayOutputStream valueBytes = new ByteArrayOutputStream(); int offset = 0; try { headerBytes.write(Ints.toByteArray(count)); for (T object : objectsIterable) { final byte[] bytes = strategy.toBytes(object); offset += 4 + bytes.length; headerBytes.write(Ints.toByteArray(offset)); valueBytes.write(Ints.toByteArray(bytes.length)); valueBytes.write(bytes); if (object instanceof Closeable) { Closeables.closeQuietly((Closeable) object); } } } catch (IOException e) { throw new RuntimeException(e); } ByteBuffer theBuffer = ByteBuffer.allocate(headerBytes.size() + valueBytes.size()); theBuffer.put(headerBytes.toByteArray()); theBuffer.put(valueBytes.toByteArray()); theBuffer.flip(); return new GenericIndexed<T>(theBuffer.asReadOnlyBuffer(), strategy, allowReverseLookup); }