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:se.sics.datamodel.util.DMKeyFactory.java

private static byte[] serializeLexico(int val) throws IOException {
    Closer closer = Closer.create();
    try {//  www  .  j av  a2  s. c o m
        ByteArrayOutputStream baos = closer.register(new ByteArrayOutputStream());
        DataOutputStream w = closer.register(new DataOutputStream(baos));

        byte sign = (val < 0 ? (byte) 0 : (byte) 1);
        int absVal = Math.abs(val);
        byte[] iVal = Ints.toByteArray(absVal);

        w.write(INDEXVAL_INT);
        w.write(sign);
        w.write(iVal);

        w.flush();

        return baos.toByteArray();
    } catch (Throwable e) {
        throw closer.rethrow(e);
    } finally {
        closer.close();
    }
}

From source file:se.sics.datamodel.util.DMKeyFactory.java

private static byte[] serializeLexico(long val) throws IOException {
    Closer closer = Closer.create();
    try {/*from  ww w .j a  v a 2s .c om*/
        ByteArrayOutputStream baos = closer.register(new ByteArrayOutputStream());
        DataOutputStream w = closer.register(new DataOutputStream(baos));

        byte sign = (val < 0 ? (byte) 0 : (byte) 1);
        long absVal = Math.abs(val);
        byte[] iVal = Longs.toByteArray(absVal);

        w.write(INDEXVAL_LONG);
        w.write(sign);
        w.write(iVal);

        w.flush();

        return baos.toByteArray();
    } catch (Throwable e) {
        throw closer.rethrow(e);
    } finally {
        closer.close();
    }
}

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

private static void loadClassNamesFromDirectoryInsideJarFile(File jarFile, String directoryInsideJarFile,
        Location location, Multimap<String, Location> newClassNameLocations) throws IOException {
    Closer closer = Closer.create();
    try {//from   ww  w.java2 s . c  om
        InputStream in = closer.register(new FileInputStream(jarFile));
        JarInputStream jarIn = closer.register(new JarInputStream(in));
        loadClassNamesFromJarInputStream(jarIn, directoryInsideJarFile, location, newClassNameLocations);
    } catch (Throwable t) {
        throw closer.rethrow(t);
    } finally {
        closer.close();
    }
}

From source file:tachyon.client.file.TachyonFileSystemUtils.java

/**
 * Persist the given file to the under file system.
 *
 * @param tfs {@link TachyonFileSystem} to carry out tachyon operations
 * @param file the file to persist/* w  w  w .j  a va  2 s  . com*/
 * @param fileInfo the file info of the file
 * @param tachyonConf TachyonConf object
 * @return the size of the file persisted
 * @throws IOException if an I/O error occurs
 * @throws FileDoesNotExistException if the given file does not exist
 * @throws TachyonException if an unexpected Tachyon error occurs
 */
public static long persistFile(TachyonFileSystem tfs, TachyonFile file, FileInfo fileInfo,
        TachyonConf tachyonConf) throws IOException, FileDoesNotExistException, TachyonException {
    // TODO(manugoyal) move this logic to the worker, as it deals with the under file system
    Closer closer = Closer.create();
    long ret;
    try {
        InStreamOptions inStreamOptions = new InStreamOptions.Builder(tachyonConf)
                .setTachyonStorageType(TachyonStorageType.NO_STORE).build();
        FileInStream in = closer.register(tfs.getInStream(file, inStreamOptions));
        TachyonURI dstPath = new TachyonURI(fileInfo.getUfsPath());
        UnderFileSystem ufs = UnderFileSystem.get(dstPath.getPath(), tachyonConf);
        String parentPath = dstPath.getParent().getPath();
        if (!ufs.exists(parentPath) && !ufs.mkdirs(parentPath, true)) {
            throw new IOException("Failed to create " + parentPath);
        }
        OutputStream out = closer.register(ufs.create(dstPath.getPath()));
        ret = IOUtils.copyLarge(in, out);
    } catch (Exception e) {
        throw closer.rethrow(e);
    } finally {
        closer.close();
    }
    // Tell the master to mark the file as persisted
    tfs.setState(file, (new SetStateOptions.Builder()).setPersisted(true).build());
    return ret;
}

From source file:se.sics.datamodel.util.DMKeyFactory.java

private static byte[] serializeLexico(String val) throws IOException {
    Closer closer = Closer.create();
    try {/*from www .  java 2  s  .  c o m*/
        ByteArrayOutputStream baos = closer.register(new ByteArrayOutputStream());
        DataOutputStream w = closer.register(new DataOutputStream(baos));
        if (val.length() > MAX_INDEXVAL_SIZE) {
            throw new IOException("KeyFactory - indexValue max 127 chars");
        }
        w.write(INDEXVAL_STRING); //string
        w.write(val.getBytes("UTF8"));
        w.write(STRING_END); //stop
        w.flush();

        return baos.toByteArray();
    } catch (Throwable e) {
        throw closer.rethrow(e);
    } finally {
        closer.close();
    }
}

From source file:org.apache.jackrabbit.oak.run.SegmentUtils.java

static void backup(File source, File target) throws IOException {
    Closer closer = Closer.create();
    try {// w  w w . ja v a 2s  .co m
        FileStore fs;
        if (FileStoreBackup.USE_FAKE_BLOBSTORE) {
            fs = openReadOnlyFileStore(source, newBasicReadOnlyBlobStore());
        } else {
            fs = openReadOnlyFileStore(source);
        }
        closer.register(asCloseable(fs));
        NodeStore store = SegmentNodeStore.builder(fs).build();
        FileStoreBackup.backup(store, target);
    } catch (Throwable e) {
        throw closer.rethrow(e);
    } finally {
        closer.close();
    }
}

From source file:org.apache.jackrabbit.oak.run.SegmentTarUtils.java

static void backup(File source, File target) throws IOException {
    Closer closer = Closer.create();
    try {/*from w  w w  .j  av a2s.  c  o  m*/
        FileStore fs;
        if (FileStoreBackup.USE_FAKE_BLOBSTORE) {
            fs = openReadOnlyFileStore(source, newBasicReadOnlyBlobStore());
        } else {
            fs = openReadOnlyFileStore(source);
        }
        closer.register(fs);
        FileStoreBackup.backup(fs.getReader(), fs.getRevisions(), target);
    } catch (Throwable e) {
        throw closer.rethrow(e);
    } finally {
        closer.close();
    }
}

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

private static <T> T runExternalAttach(long pid, String methodName, InputStreamProcessor<T> processor,
        @Nullable File glowrootJarFile) throws Exception {
    List<String> command = buildCommand(pid, methodName, glowrootJarFile);
    ProcessBuilder processBuilder = new ProcessBuilder(command);
    Process process = processBuilder.start();
    Closer closer = Closer.create();
    ErrorStreamReader errorStreamReader;
    T result = null;//w w  w  .ja v a  2  s . co m
    Exception processingException = null;
    try {
        InputStream in = closer.register(process.getInputStream());
        InputStream err = closer.register(process.getErrorStream());
        errorStreamReader = new ErrorStreamReader(err);
        Thread errorStreamReaderThread = new Thread(errorStreamReader);
        errorStreamReaderThread.setName("Glowroot-JVM-Tool-Error-Stream-Reader");
        errorStreamReaderThread.setDaemon(true);
        errorStreamReaderThread.start();
        try {
            result = processAndClose(in, processor);
        } catch (Exception e) {
            processingException = e;
        } catch (Throwable t) {
            processingException = new RuntimeException(t);
        }
        errorStreamReaderThread.join();
    } catch (Throwable t) {
        throw closer.rethrow(t);
    } finally {
        closer.close();
    }
    int status = process.waitFor();
    if (status == UNAVAILABLE_DUE_TO_RUNNING_IN_JRE_STATUS) {
        throw new UnavailableDueToRunningInJreException();
    } else if (status == UNAVAILABLE_PROBABLY_DUE_TO_DOCKER_PID_ONE_STATUS) {
        throw new UnavailableDueToDockerAlpinePidOneException();
    } else if (status != 0) {
        logger.error("error occurred while trying to run jvm tool:\n{}\n{}", Joiner.on(' ').join(command),
                errorStreamReader.getOutput().trim());
        throw new IllegalStateException("Error occurred while trying to run jvm tool");
    }
    if (result == null) {
        throw checkNotNull(processingException);
    }
    return result;
}

From source file:com.googlecode.jmxtrans.model.output.support.pool.ChannelWriter.java

@Override
public void close() throws IOException {
    Closer closer = Closer.create();
    try {//from w w  w  .ja  va 2 s. co  m
        closer.register(channel);
        flush();
    } catch (Throwable t) {
        throw closer.rethrow(t);
    } finally {
        closer.close();
    }
}

From source file:com.googlecode.jmxtrans.model.output.support.pool.DatagramChannelAllocator.java

@Override
public void deallocate(DatagramChannelPoolable poolable) throws Exception {
    Closer closer = Closer.create();
    try {//from  w  w w . j av  a 2 s  . c o  m
        Writer writer = closer.register(poolable.getWriter());
        writer.flush();
    } catch (Throwable t) {
        throw closer.rethrow(t);
    } finally {
        closer.close();
    }
}