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:com.googlecode.jmxtrans.model.output.support.pool.SocketAllocator.java

@Override
public void deallocate(SocketPoolable poolable) throws Exception {
    Closer closer = Closer.create();
    try {/*  w w  w  .j a v a  2s .  c  o m*/
        closer.register(poolable.getSocket());
        closer.register(poolable.getWriter());
    } catch (Throwable t) {
        throw closer.rethrow(t);
    } finally {
        closer.close();
    }
}

From source file:ch.ledcom.demo.redirect.ImageServlet.java

@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    Closer closer = Closer.create();
    try {/*from  w  ww.  j  ava  2  s  .c o m*/
        InputStream imageStream = closer.register(loadImage());
        ByteStreams.copy(imageStream, resp.getOutputStream());
    } catch (Throwable t) {
        throw closer.rethrow(t);
    } finally {
        closer.close();
    }
}

From source file:gobblin.util.HeapDumpForTaskUtils.java

/**
 * Generate the dumpScript, which is used when OOM error is thrown during task execution.
 * The current content dumpScript puts the .prof files to the DUMP_FOLDER within the same directory of the dumpScript.
 *
 * User needs to add the following options to the task java.opts:
 *
 * -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=./heapFileName.hprof -XX:OnOutOfMemoryError=./dumpScriptFileName
 *
 * @param dumpScript The path to the dumpScript, which needs to be added to the Distributed cache.
 * To use it, simply put the path of dumpScript to the gobblin config: job.hdfs.files.
 * @param fs File system//  w ww .  j a v a 2 s. co  m
 * @param heapFileName the name of the .prof file.
 * @param chmod chmod for the dump script. For hdfs file, e.g, "hadoop fs -chmod 755"
 * @throws IOException
 */
public static void generateDumpScript(Path dumpScript, FileSystem fs, String heapFileName, String chmod)
        throws IOException {
    if (fs.exists(dumpScript)) {
        LOG.info("Heap dump script already exists: " + dumpScript);
        return;
    }

    Closer closer = Closer.create();
    try {
        Path dumpDir = new Path(dumpScript.getParent(), DUMP_FOLDER);
        if (!fs.exists(dumpDir)) {
            fs.mkdirs(dumpDir);
        }
        BufferedWriter scriptWriter = closer.register(new BufferedWriter(
                new OutputStreamWriter(fs.create(dumpScript), ConfigurationKeys.DEFAULT_CHARSET_ENCODING)));

        scriptWriter.write("#!/bin/sh\n");
        scriptWriter.write("if [ -n \"$HADOOP_PREFIX\" ]; then\n");
        scriptWriter.write("  ${HADOOP_PREFIX}/bin/hadoop dfs -put " + heapFileName + " " + dumpDir
                + "/${PWD//\\//_}.hprof\n");
        scriptWriter.write("else\n");
        scriptWriter.write("  ${HADOOP_HOME}/bin/hadoop dfs -put " + heapFileName + " " + dumpDir
                + "/${PWD//\\//_}.hprof\n");
        scriptWriter.write("fi\n");

    } catch (IOException ioe) {
        LOG.error("Heap dump script is not generated successfully.");
        if (fs.exists(dumpScript)) {
            fs.delete(dumpScript, true);
        }
        throw ioe;
    } catch (Throwable t) {
        closer.rethrow(t);
    } finally {
        closer.close();
    }
    Runtime.getRuntime().exec(chmod + " " + dumpScript);
}

From source file:org.glowroot.agent.live.ClasspathCache.java

private static byte[] getBytesFromJarFileInsideJarFile(String name, File jarFile, String jarFileInsideJarFile)
        throws IOException {
    String path = jarFile.getPath();
    URI uri;//from   w  ww.  j ava  2s  . c o  m
    try {
        uri = new URI("jar", "file:" + path + "!/" + jarFileInsideJarFile, "");
    } catch (URISyntaxException e) {
        // this is a programmatic error
        throw new RuntimeException(e);
    }
    Closer closer = Closer.create();
    try {
        InputStream in = closer.register(uri.toURL().openStream());
        JarInputStream jarIn = closer.register(new JarInputStream(in));
        JarEntry jarEntry;
        while ((jarEntry = jarIn.getNextJarEntry()) != null) {
            if (jarEntry.isDirectory()) {
                continue;
            }
            if (jarEntry.getName().equals(name)) {
                return ByteStreams.toByteArray(jarIn);
            }
        }
    } catch (Throwable t) {
        throw closer.rethrow(t);
    } finally {
        closer.close();
    }
    throw new UnsupportedOperationException();
}

From source file:org.ow2.proactive.scheduler.rest.readers.TaskResultReader.java

@Override
public Serializable readFrom(Class<Serializable> type, Type genericType, Annotation[] annotations,
        MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream)
        throws IOException, WebApplicationException {
    Closer closer = Closer.create();
    try {//ww w. j ava2 s  . c o  m
        entityStream = closer.register(entityStream);
        return CharStreams.toString(new InputStreamReader(entityStream));
    } catch (IOException ioe) {
        throw closer.rethrow(ioe);
    } finally {
        closer.close();
    }
}

From source file:ch.ledcom.tomcat.valves.SessionSerializableCheckerValve.java

/**
 * Check if an object is serializable, emit a warning log if it is not.
 *
 * @param attribute/*ww w .ja v  a 2 s.co m*/
 *            the attribute to check
 * @throws IOException
 */
private void checkSerializable(final Object attribute) throws IOException {
    if (!Serializable.class.isAssignableFrom(attribute.getClass())) {
        log.warn(format("Session attribute [%s] of class [%s] is not " + "serializable.", attribute,
                attribute.getClass()));
    }
    final Closer closer = Closer.create();
    try {
        final ObjectOutputStream out = closer.register(new ObjectOutputStream(ByteStreams.nullOutputStream()));
        out.writeObject(attribute);
    } catch (Throwable t) {
        closer.rethrow(t);
    } finally {
        closer.close();
    }
}

From source file:fr.techcode.downloadix.validation.FileValidator.java

/**
 * Validate if the file isn't corrupt/* www.  j  av  a2s. c o m*/
 *
 * @param file File to check
 * @return Valid or Corrupt
 */
@Override
public boolean isValid(File file) {
    // Check if the file exist
    if (!file.exists())
        return false;

    // Get bytes from a file
    byte[] datas = null;
    Closer closer = Closer.create();
    try {
        try {
            FileInputStream stream = closer.register(new FileInputStream(file));
            datas = ByteStreams.toByteArray(stream);
        } catch (Throwable throwable) {
            throw closer.rethrow(throwable);
        } finally {
            closer.close();
        }

    } catch (IOException ex) {
        ex.printStackTrace();
    }

    return datas != null && algorithm.hashBytes(datas).toString().equalsIgnoreCase(hash);
}

From source file:com.jive.myco.seyren.core.service.notification.IrcCatNotificationService.java

private void sendMessage(String ircCatHost, int ircCatPort, String message, String channel) throws IOException {
    Socket socket = new Socket(ircCatHost, ircCatPort);
    Closer closer = Closer.create();
    try {/*from w w w  .  ja v  a  2 s  .  c o m*/
        Writer out = closer.register(new OutputStreamWriter(socket.getOutputStream()));
        out.write(format("%s %s\n", channel, message));
        out.flush();
    } catch (IOException e) {
        socket.close();
        throw closer.rethrow(e);
    } finally {
        closer.close();
    }
}

From source file:org.gradle.caching.internal.controller.service.StoreTarget.java

@Override
public void writeTo(OutputStream output) throws IOException {
    Closer closer = Closer.create();
    closer.register(output);/*from  w  w  w.  jav  a2 s  . c  o m*/
    try {
        if (stored) {
            throw new IllegalStateException("Build cache entry has already been stored");
        }
        stored = true;
        Files.asByteSource(file).copyTo(output);
    } catch (Exception e) {
        throw closer.rethrow(e);
    } finally {
        closer.close();
    }
}

From source file:org.gradle.caching.internal.controller.service.LoadTarget.java

@Override
public void readFrom(InputStream input) throws IOException {
    Closer closer = Closer.create();
    closer.register(input);//from   ww w .  j  ava  2s  .c o m
    try {
        if (loaded) {
            throw new IllegalStateException("Build cache entry has already been read");
        }
        Files.asByteSink(file).writeFrom(input);
        loaded = true;
    } catch (Exception e) {
        throw closer.rethrow(e);
    } finally {
        closer.close();
    }
}