List of usage examples for com.google.common.io Closer register
public <C extends Closeable> C register(@Nullable C closeable)
From source file:org.glowroot.agent.weaving.ClassLoaders.java
static void defineClassesInBootstrapClassLoader(Collection<LazyDefinedClass> lazyDefinedClasses, Instrumentation instrumentation, File generatedJarFile) throws IOException { Closer closer = Closer.create(); try {//from ww w. ja v a2 s .co m FileOutputStream out = closer.register(new FileOutputStream(generatedJarFile)); JarOutputStream jarOut = closer.register(new JarOutputStream(out)); generate(lazyDefinedClasses, jarOut); } catch (Throwable t) { throw closer.rethrow(t); } finally { closer.close(); } instrumentation.appendToBootstrapClassLoaderSearch(new JarFile(generatedJarFile)); // appendToBootstrapClassLoaderSearch() line above does not add to the bootstrap resource // search path, only to the bootstrap class search path (this is different from // appendToSystemClassLoaderSearch() which adds to both the system resource search path and // the system class search path) // // adding the generated jar file to the bootstrap resource search path is probably needed // more generally, but it is at least needed to support jboss 4.2.0 - 4.2.3 because // org.jboss.mx.loading.LoadMgr3.beginLoadTask() checks that the class loader has the class // as a resource before loading it, so without adding the generated jar file to the // bootstrap resource search path, jboss ends up throwing ClassNotFoundException for the // glowroot generated classes that have been added to the bootstrap class loader search path // (see issue #101 for more info on this particular jboss issue) appendToBootstrapResourcePath(generatedJarFile); }
From source file:org.ow2.proactive_grid_cloud_portal.dataspace.FileSystem.java
public static void copy(InputStream is, FileObject outFile) throws IOException { outFile.refresh();/* w w w .j a v a 2 s.c o m*/ Closer closer = Closer.create(); closer.register(is); try { OutputStream os = outFile.getContent().getOutputStream(); closer.register(os); ByteStreams.copy(is, os); } catch (IOException ioe) { throw closer.rethrow(ioe); } finally { closer.close(); } }
From source file:org.ow2.proactive_grid_cloud_portal.dataspace.FileSystem.java
public static void copy(FileObject fo, OutputStream os) throws IOException { fo.refresh();// w w w .j a va 2 s . com Closer closer = Closer.create(); closer.register(os); try { InputStream is = fo.getContent().getInputStream(); closer.register(is); ByteStreams.copy(is, os); } catch (IOException ioe) { throw closer.rethrow(ioe); } finally { closer.close(); } }
From source file:com.skcraft.launcher.persistence.Persistence.java
/** * Save an object to file./* ww w. j a v a 2 s. co m*/ * * @param object the object * @throws java.io.IOException on save error */ public static void commit(@NonNull Object object) throws IOException { ByteSink sink; synchronized (bound) { sink = bound.get(object); if (sink == null) { throw new IOException("Cannot persist unbound object: " + object); } } Closer closer = Closer.create(); try { OutputStream os = closer.register(sink.openBufferedStream()); mapper.writeValue(os, object); } finally { closer.close(); } }
From source file:org.pantsbuild.tools.jar.JarEntryCopier.java
/** * Copy a a jar entry to an output file without decompressing and re-compressing the entry when * it is {@link ZipEntry#DEFLATED}.//w ww . j av a 2s .com * * @param jarOut The jar file being created or appended to. * @param name The resource name to write. * @param jarIn The input JarFile. * @param jarEntry The entry extracted from <code>jarIn</code>. The compression method passed in * to this entry is preserved in the output file. * @throws IOException if there is a problem reading from {@code jarIn} or writing to * {@code jarOut}. */ static void copyEntry(JarOutputStream jarOut, String name, JarFile jarIn, JarEntry jarEntry) throws IOException { JarEntry outEntry = new JarEntry(jarEntry); ZE_NAME.set(outEntry, name); if (outEntry.isDirectory()) { outEntry.setMethod(ZipEntry.STORED); outEntry.setSize(0); outEntry.setCompressedSize(0); outEntry.setCrc(0); jarOut.putNextEntry(outEntry); jarOut.closeEntry(); } else if (jarEntry.getMethod() == ZipEntry.STORED) { Closer closer = Closer.create(); try { InputStream is = closer.register(jarIn.getInputStream(jarEntry)); jarOut.putNextEntry(outEntry); ByteStreams.copy(is, jarOut); } catch (IOException e) { throw closer.rethrow(e); } finally { closer.close(); } jarOut.closeEntry(); } else { Closer closer = Closer.create(); try { // Grab the underlying stream so we can read the compressed bytes. FilterInputStream zis = (FilterInputStream) closer.register(jarIn.getInputStream(jarEntry)); InputStream is = FIS_IN.get(zis); // Start it as a DEFLATE.... jarOut.putNextEntry(outEntry); // But swap out the method to STORE to the bytes don't get compressed. // This works because ZipFile doesn't make a defensive copy. outEntry.setMethod(ZipEntry.STORED); outEntry.setSize(jarEntry.getCompressedSize()); ByteStreams.copy(is, jarOut); } catch (IOException e) { throw closer.rethrow(e); } finally { closer.close(); } // The internal CRC is now wrong, so hack it before we close the entry. CRC_VALUE.set(ZOS_CRC.get(jarOut), (int) jarEntry.getCrc()); jarOut.closeEntry(); // Restore entry back to normal, so it will be written out correctly at the end. outEntry.setMethod(ZipEntry.DEFLATED); outEntry.setSize(jarEntry.getSize()); } }
From source file:com.android.build.gradle.internal.transforms.MultiStreamJarTransform.java
private static void processFolder(@NonNull JarOutputStream jos, @NonNull String path, @NonNull File folder, @NonNull byte[] buffer) throws IOException { File[] files = folder.listFiles(); if (files != null) { for (File file : files) { if (file.isFile()) { if (file.getName().endsWith(DOT_CLASS)) { // new entry jos.putNextEntry(new JarEntry(path + file.getName())); // put the file content Closer closer = Closer.create(); try { FileInputStream fis = closer.register(new FileInputStream(file)); int count; while ((count = fis.read(buffer)) != -1) { jos.write(buffer, 0, count); }//from w ww. j av a 2 s. c o m } finally { closer.close(); } // close the entry jos.closeEntry(); } } else if (file.isDirectory()) { processFolder(jos, path + file.getName() + "/", file, buffer); } } } }
From source file:com.skcraft.launcher.persistence.Persistence.java
/** * Read an object from a byte source, without binding it. * * @param source byte source/*from w ww . j a v a2s . co m*/ * @param cls the class * @param returnNull true to return null if the object could not be loaded * @param <V> the type of class * @return an object */ public static <V> V read(ByteSource source, Class<V> cls, boolean returnNull) { V object; Closer closer = Closer.create(); try { object = mapper.readValue(closer.register(source.openBufferedStream()), cls); } catch (IOException e) { if (!(e instanceof FileNotFoundException)) { log.log(Level.INFO, "Failed to load" + cls.getCanonicalName(), e); } if (returnNull) { return null; } try { object = cls.newInstance(); } catch (InstantiationException e1) { throw new RuntimeException("Failed to construct object with no-arg constructor", e1); } catch (IllegalAccessException e1) { throw new RuntimeException("Failed to construct object with no-arg constructor", e1); } } finally { try { closer.close(); } catch (IOException e) { } } return object; }
From source file:com.github.fge.jackson.JsonLoader.java
/** * Read a {@link JsonNode} from a file on the local filesystem. * * @param path the path (relative or absolute) to the file * @return the document in the file/*from ww w . j a va 2s .com*/ * @throws IOException if this is not a file, if it cannot be read, etc. */ public static JsonNode fromPath(final String path) throws IOException { final Closer closer = Closer.create(); final JsonNode ret; final FileInputStream in; try { in = closer.register(new FileInputStream(path)); ret = READER.fromInputStream(in); } finally { closer.close(); } return ret; }
From source file:com.github.fge.jackson.JsonLoader.java
/** * Same as {@link #fromPath(String)}, but this time the user supplies the * {@link File} object instead//ww w .ja v a 2s . c o m * * @param file the File object * @return The document * @throws IOException in many cases! */ public static JsonNode fromFile(final File file) throws IOException { final Closer closer = Closer.create(); final JsonNode ret; final FileInputStream in; try { in = closer.register(new FileInputStream(file)); ret = READER.fromInputStream(in); } finally { closer.close(); } return ret; }
From source file:alluxio.client.block.RemoteBlockInStream.java
/** * Creates a new remote block input stream. * * @param blockId the block id//from w w w. j a v a 2 s.co m * @param blockSize the block size * @param workerNetAddress the worker address * @param context the file system context to use for acquiring worker and master clients * @param options the instream options * @return the {@link RemoteBlockInStream} created * @throws IOException if the block is not available on the remote worker */ // TODO(peis): Use options idiom (ALLUXIO-2579). public static RemoteBlockInStream 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(); return new RemoteBlockInStream(context, client, blockId, blockSize, result.getLockId(), closer, options); } catch (AlluxioException | IOException e) { CommonUtils.closeQuitely(closer); throw CommonUtils.castToIOException(e); } }