Example usage for com.google.common.io Closer rethrow

List of usage examples for com.google.common.io Closer rethrow

Introduction

In this page you can find the example usage for com.google.common.io Closer rethrow.

Prototype

public RuntimeException rethrow(Throwable e) throws IOException 

Source Link

Document

Stores the given throwable and rethrows it.

Usage

From source file:org.glowroot.weaving.ClassLoaders.java

public static void defineClassesInBootstrapClassLoader(Collection<LazyDefinedClass> lazyDefinedClasses,
        Instrumentation instrumentation, File generatedJarFile) throws IOException {
    Closer closer = Closer.create();
    try {//from   w ww.j a v a  2 s. c o m
        FileOutputStream out = closer.register(new FileOutputStream(generatedJarFile));
        JarOutputStream jarOut = closer.register(new JarOutputStream(out));
        generate(lazyDefinedClasses, jarOut);
    } catch (Throwable t) {
        closer.rethrow(t);
    } finally {
        closer.close();
    }
    instrumentation.appendToBootstrapClassLoaderSearch(new JarFile(generatedJarFile));
}

From source file:com.github.autermann.sockets.ssl.SSLUtils.java

public static PrivateKey readKey(InputSupplier<? extends InputStream> in)
        throws IOException, NoSuchAlgorithmException, InvalidKeySpecException {
    Closer closer = Closer.create();
    try {/*from   ww w .j a v  a2  s.  c om*/
        Reader reader = closer.register(CharStreams.newReaderSupplier(in, Charsets.UTF_8).getInput());
        return createPrivateKey(new PemReader(reader).readPemObject());
    } catch (IOException e) {
        throw closer.rethrow(e);
    } finally {
        closer.close();
    }
}

From source file:org.ow2.proactive_grid_cloud_portal.dataspace.util.VFSZipper.java

private static void copyFileContents(FileObject fo, ZipOutputStream zos) throws IOException {
    Closer closer = Closer.create();
    try {/*www  . j  a  v  a  2s  . c o m*/
        InputStream inputStream = fo.getContent().getInputStream();
        closer.register(inputStream);
        ByteStreams.copy(inputStream, zos);
    } catch (IOException ioe) {
        throw closer.rethrow(ioe);
    } finally {
        closer.close();
    }
}

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  w w w  . java 2  s.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: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 a v a 2  s.c o m
 *
 * @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:org.apache.gobblin.aws.Log4jConfigHelper.java

/**
 * Update the log4j configuration./* w w  w.j av a  2  s. c  o m*/
 *
 * @param targetClass the target class used to get the original log4j configuration file as a resource
 * @param log4jFileName the custom log4j configuration properties file name
 * @throws IOException if there's something wrong with updating the log4j configuration
 */
public static void updateLog4jConfiguration(Class<?> targetClass, String log4jFileName) throws IOException {
    final Closer closer = Closer.create();
    try {
        final InputStream inputStream = closer.register(targetClass.getResourceAsStream("/" + log4jFileName));
        final Properties originalProperties = new Properties();
        originalProperties.load(inputStream);

        LogManager.resetConfiguration();
        PropertyConfigurator.configure(originalProperties);
    } catch (Throwable t) {
        throw closer.rethrow(t);
    } 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  ww .ja  va 2s  .  co  m*/
    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:org.ow2.proactive_grid_cloud_portal.dataspace.FileSystem.java

public static void copy(InputStream is, FileObject outFile) throws IOException {
    outFile.refresh();// ww  w  . jav a2s .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:tachyon.util.io.FileUtils.java

/**
 * Blocking operation that copies the processes stdout/stderr to this JVM's stdout/stderr.
 *
 * @param process process whose stdout/stderr to copy
 * @throws IOException when operation fails
 */// w ww .  j a  v  a2 s.co m
private static void redirectIO(final Process process) throws IOException {
    Preconditions.checkNotNull(process);
    /*
     * Because chmod doesn't have a lot of error or output messages, it is safe to process the
     * output after the process is done. As of java 7, you can have the process redirect to
     * System.out and System.err without forking a process.
     *
     * TODO(cc): When java 6 support is dropped switch to ProcessBuilder.html#inheritIO().
     */
    Closer closer = Closer.create();
    try {
        ByteStreams.copy(closer.register(process.getInputStream()), System.out);
        ByteStreams.copy(closer.register(process.getErrorStream()), System.err);
    } catch (Exception e) {
        throw closer.rethrow(e);
    } finally {
        closer.close();
    }
}

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   w ww.j a va 2s. c o 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);
}