List of usage examples for com.google.common.io Closer register
public <C extends Closeable> C register(@Nullable C closeable)
From source file:com.sk89q.worldguard.six2five.util.SwingHelper.java
public static BufferedImage readIconImage(Class<?> clazz, String path) { Closer closer = Closer.create(); try {//from w w w .j a va 2 s .c om try { InputStream in = closer.register(clazz.getResourceAsStream(path)); if (in != null) { return ImageIO.read(in); } } finally { closer.close(); } } catch (IOException ignored) { } return null; }
From source file:org.ow2.proactive_grid_cloud_portal.dataspace.util.VFSZipper.java
public static void zip(FileObject file, OutputStream out) throws IOException { Closer closer = Closer.create(); try {//from www. ja v a 2s . c o m closer.register(out); InputStream in = file.getContent().getInputStream(); closer.register(in); ByteStreams.copy(in, out); } catch (IOException ioe) { throw closer.rethrow(ioe); } finally { closer.close(); } }
From source file:alluxio.client.block.LocalBlockInStream.java
/** * Creates a new local block input stream. * * @param blockId the block id//from ww w. ja va 2 s . c om * @param blockSize the size of the block * @param workerNetAddress the address of the local worker * @param context the file system context * @param options the instream options * @return the {@link LocalBlockInStream} instance * @throws IOException if I/O error occurs */ // TODO(peis): Use options idiom (ALLUXIO-2579). public static LocalBlockInStream create(long blockId, long blockSize, WorkerNetAddress workerNetAddress, FileSystemContext context, InStreamOptions options) throws IOException { Closer closer = Closer.create(); try { BlockWorkerClient client = closer.register(context.createBlockWorkerClient(workerNetAddress)); LockBlockResult result = closer.register(client.lockBlock(blockId, LockBlockOptions.defaults())) .getResult(); LocalFileBlockReader reader = closer.register(new LocalFileBlockReader(result.getBlockPath())); return new LocalBlockInStream(client, blockId, blockSize, reader, closer, options); } catch (AlluxioException | IOException e) { CommonUtils.closeQuitely(closer); throw CommonUtils.castToIOException(e); } }
From source file:com.android.build.gradle.internal.transforms.MultiStreamJarTransform.java
private static void jarFolder(@NonNull File folder, @NonNull File jarFile) throws IOException { Closer closer = Closer.create(); try {/*from www .j av a 2 s.c om*/ FileOutputStream fos = closer.register(new FileOutputStream(jarFile)); JarOutputStream jos = closer.register(new JarOutputStream(fos)); final byte[] buffer = new byte[8192]; processFolder(jos, "", folder, buffer); } finally { closer.close(); } }
From source file:org.glowroot.agent.live.JvmTool.java
private static <T> T processAndClose(InputStream in, InputStreamProcessor<T> processor) throws IOException { Closer closer = Closer.create(); try {/*from w w w.ja v a 2s . c om*/ closer.register(in); return processor.process(in); } catch (Throwable t) { throw closer.rethrow(t); } finally { closer.close(); } }
From source file:org.asoem.greyfish.utils.persistence.Persisters.java
/** * Create a copy of the given object {@code o} by serializing it with the given {@code Persister}. What copy means * in this context, dependents fully on the {@code Persister} implementation. * * @param o the object you wish to copy * @param persister the {@code Persister} to use for the serialization process * @return a copy of {@code o}// www . j a v a2 s. c o m * @throws Exception if some errors occur during the serialization process */ public static <T> T copyAsync(final T o, final Persister persister) throws Exception { checkNotNull(o); checkNotNull(persister); final Closer closer = Closer.create(); try { final PipedOutputStream pipedOutputStream = closer.register(new PipedOutputStream()); final PipedInputStream pipedInputStream = closer.register(new PipedInputStream(pipedOutputStream)); final Future<T> deserializeFuture = Executors.newSingleThreadExecutor().submit(new Callable<T>() { @SuppressWarnings("unchecked") // safe if persister is implemented correctly @Override public T call() throws Exception { try { return (T) persister.deserialize(pipedInputStream, o.getClass()); } catch (Exception e) { pipedInputStream.close(); throw e; } } }); try { persister.serialize(o, pipedOutputStream); } catch (Exception e) { if (!deserializeFuture.isDone()) { deserializeFuture.cancel(true); throw e; } else { // the future task had an exception and closed the stream, which caused this exception deserializeFuture.get(); // throws the exception throw new AssertionError("unreachable"); } } return deserializeFuture.get(3, TimeUnit.SECONDS); } finally { closer.close(); } }
From source file:feign.TrustingSSLSocketFactory.java
private static KeyStore loadKeyStore(InputSupplier<InputStream> inputStreamSupplier) throws IOException { Closer closer = Closer.create(); try {//w w w . j ava2s .com InputStream inputStream = closer.register(inputStreamSupplier.getInput()); KeyStore keyStore = KeyStore.getInstance("JKS"); keyStore.load(inputStream, KEYSTORE_PASSWORD); return keyStore; } catch (Throwable e) { throw closer.rethrow(e); } finally { closer.close(); } }
From source file:alluxio.client.block.stream.BlockOutStream.java
/** * Creates a new local block output stream. * * @param blockId the block id// w w w .ja v a 2s . c om * @param blockSize the block size * @param workerNetAddress the worker network address * @param context the file system context * @param options the options * @throws IOException if an I/O error occurs * @return the {@link BlockOutStream} instance created */ public static BlockOutStream createLocalBlockOutStream(long blockId, long blockSize, WorkerNetAddress workerNetAddress, FileSystemContext context, OutStreamOptions options) throws IOException { Closer closer = Closer.create(); try { BlockWorkerClient client = closer.register(context.createBlockWorkerClient(workerNetAddress)); PacketOutStream outStream = PacketOutStream.createLocalPacketOutStream(client, blockId, blockSize, options.getWriteTier()); closer.register(outStream); return new BlockOutStream(outStream, blockId, blockSize, client, options); } catch (IOException e) { CommonUtils.closeQuietly(closer); throw e; } }
From source file:com.j2swift.util.ProGuardUsageParser.java
public static DeadCodeMap parse(CharSource listing) throws IOException { LineProcessor<DeadCodeMap> processor = new LineProcessor<DeadCodeMap>() { DeadCodeMap.Builder dead = DeadCodeMap.builder(); String lastClass;/*from w ww.j a v a2 s. c om*/ @Override public DeadCodeMap getResult() { return dead.build(); } private void handleClass(String line) { if (line.endsWith(":")) { // Class, but not completely dead; save to read its dead methods lastClass = line.substring(0, line.length() - 1); } else { dead.addDeadClass(line); } } private void handleMethod(String line) throws IOException { Matcher methodMatcher = proGuardMethodPattern.matcher(line); if (!methodMatcher.matches()) { throw new AssertionError("Line doesn't match expected ProGuard format!"); } if (lastClass == null) { throw new IOException("Bad listing format: method not attached to a class"); } String returnType = methodMatcher.group(5); String methodName = methodMatcher.group(6); String arguments = methodMatcher.group(7); String signature = buildMethodSignature(returnType, arguments); dead.addDeadMethod(lastClass, methodName, signature); } private void handleField(String line) throws IOException { String name = line.substring(line.lastIndexOf(" ") + 1); dead.addDeadField(lastClass, name); } @Override public boolean processLine(String line) throws IOException { if (line.startsWith("ProGuard, version") || line.startsWith("Reading ")) { // ignore output header } else if (!line.startsWith(" ")) { handleClass(line); } else if (line.startsWith(" ") && !line.contains("(")) { handleField(line); } else { handleMethod(line); } return true; } }; // TODO(cgdecker): Just use listing.readLines(processor) once guava_jdk5 is upgraded to a newer // version. Closer closer = Closer.create(); try { BufferedReader reader = closer.register(listing.openBufferedStream()); String line; while ((line = reader.readLine()) != null) { processor.processLine(line); } return processor.getResult(); } catch (Throwable e) { throw closer.rethrow(e); } finally { closer.close(); } }
From source file:alluxio.client.block.stream.BlockOutStream.java
/** * Creates a new remote block output stream. * * @param blockId the block id/* www . ja v a 2s. com*/ * @param blockSize the block size * @param workerNetAddress the worker network address * @param context the file system context * @param options the options * @throws IOException if an I/O error occurs * @return the {@link BlockOutStream} instance created */ public static BlockOutStream createRemoteBlockOutStream(long blockId, long blockSize, WorkerNetAddress workerNetAddress, FileSystemContext context, OutStreamOptions options) throws IOException { Closer closer = Closer.create(); try { BlockWorkerClient client = closer.register(context.createBlockWorkerClient(workerNetAddress)); PacketOutStream outStream = PacketOutStream.createNettyPacketOutStream(context, client.getDataServerAddress(), client.getSessionId(), blockId, blockSize, options.getWriteTier(), Protocol.RequestType.ALLUXIO_BLOCK); closer.register(outStream); return new BlockOutStream(outStream, blockId, blockSize, client, options); } catch (IOException e) { CommonUtils.closeQuietly(closer); throw e; } }