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.jmattr.meta.impl.ServicesFiles.java

/**
 * Reads the set of service classes from a service file.
 *
 * @param input not {@code null}. Closed after use.
 * @return a not {@code null Set} of service class names.
 * @throws IOException//from  w ww .  j  av  a2  s. c  o  m
 */
static Set<String> readServiceFile(InputStream input) throws IOException {
    HashSet<String> serviceClasses = new HashSet<String>();
    Closer closer = Closer.create();
    try {
        // TODO(gak): use CharStreams
        BufferedReader r = closer.register(new BufferedReader(new InputStreamReader(input, Charsets.UTF_8)));
        String line;
        while ((line = r.readLine()) != null) {
            int commentStart = line.indexOf('#');
            if (commentStart >= 0) {
                line = line.substring(0, commentStart);
            }
            line = line.trim();
            if (!line.isEmpty()) {
                serviceClasses.add(line);
            }
        }
        return serviceClasses;
    } catch (Throwable t) {
        throw closer.rethrow(t);
    } finally {
        closer.close();
    }
}

From source file:com.github.sdorra.buildfrontend.AbstractNodeMojo.java

/**
 * Method description/*from w  ww.  j  av a2 s .c  o m*/
 *
 *
 * @param urlString
 * @param target
 *
 * @throws IOException
 */
private static void download(String urlString, File target) throws IOException {
    URL url = new URL(urlString);
    URLConnection connection = url.openConnection();
    Closer closer = Closer.create();

    try {
        InputStream input = closer.register(connection.getInputStream());
        OutputStream output = closer.register(new FileOutputStream(target));

        ByteStreams.copy(input, output);
    } catch (IOException ex) {
        throw closer.rethrow(ex);
    } finally {
        closer.close();
    }
}

From source file:org.glowroot.local.ui.ClasspathCache.java

private static void loadClassNamesFromJarFile(File jarFile, Multimap<String, File> newClassNameLocations)
        throws IOException {
    Closer closer = Closer.create();
    InputStream s = new FileInputStream(jarFile);
    JarInputStream jarIn = closer.register(new JarInputStream(s));
    try {//from   w w  w  .j  a  v a  2  s  .  com
        loadClassNamesFromManifestClassPath(jarIn, jarFile, newClassNameLocations);
        loadClassNamesFromJarInputStream(jarIn, jarFile, newClassNameLocations);
    } catch (Throwable t) {
        throw closer.rethrow(t);
    } finally {
        closer.close();
    }
}

From source file:gobblin.metrics.MetricReportUtils.java

/**
 * Parses a {@link gobblin.metrics.MetricReport} from a byte array Avro serialization.
 * @param reuse MetricReport to reuse.//ww  w .  ja v a2  s .  c o  m
 * @param bytes Input bytes.
 * @return MetricReport.
 * @throws java.io.IOException
 */
public synchronized static MetricReport deserializeReportFromAvroSerialization(MetricReport reuse, byte[] bytes)
        throws IOException {
    if (!READER.isPresent()) {
        READER = Optional.of(new SpecificDatumReader<MetricReport>(MetricReport.class));
    }

    Closer closer = Closer.create();

    try {
        DataInputStream inputStream = closer.register(new DataInputStream(new ByteArrayInputStream(bytes)));

        // Check version byte
        int versionNumber = inputStream.readInt();
        if (versionNumber != SCHEMA_VERSION) {
            throw new IOException(
                    String.format("MetricReport schema version not recognized. Found version %d, expected %d.",
                            versionNumber, SCHEMA_VERSION));
        }

        // Decode the rest
        Decoder decoder = DecoderFactory.get().binaryDecoder(inputStream, null);
        return READER.get().read(reuse, decoder);
    } catch (Throwable t) {
        throw closer.rethrow(t);
    } finally {
        closer.close();
    }
}

From source file:org.apache.gobblin.metrics.reporter.util.MetricReportUtils.java

/**
 * Parses a {@link org.apache.gobblin.metrics.MetricReport} from a byte array Avro serialization.
 * @param reuse MetricReport to reuse.//from  w  ww  .  j av  a  2s.c o  m
 * @param bytes Input bytes.
 * @return MetricReport.
 * @throws java.io.IOException
 */
public synchronized static MetricReport deserializeReportFromAvroSerialization(MetricReport reuse, byte[] bytes)
        throws IOException {
    if (!READER.isPresent()) {
        READER = Optional.of(new SpecificDatumReader<>(MetricReport.class));
    }

    Closer closer = Closer.create();

    try {
        DataInputStream inputStream = closer.register(new DataInputStream(new ByteArrayInputStream(bytes)));

        // Check version byte
        int versionNumber = inputStream.readInt();
        if (versionNumber != SCHEMA_VERSION) {
            throw new IOException(
                    String.format("MetricReport schema version not recognized. Found version %d, expected %d.",
                            versionNumber, SCHEMA_VERSION));
        }

        // Decode the rest
        Decoder decoder = DecoderFactory.get().binaryDecoder(inputStream, null);
        return READER.get().read(reuse, decoder);
    } catch (Throwable t) {
        throw closer.rethrow(t);
    } finally {
        closer.close();
    }
}

From source file:gobblin.metrics.reporter.util.EventUtils.java

/**
 * Parses a {@link gobblin.metrics.MetricReport} from a byte array Avro serialization.
 * @param reuse MetricReport to reuse.// w  w w.  j ava2  s. c om
 * @param bytes Input bytes.
 * @return MetricReport.
 * @throws java.io.IOException
 */
public synchronized static GobblinTrackingEvent deserializeReportFromAvroSerialization(
        GobblinTrackingEvent reuse, byte[] bytes) throws IOException {
    if (!reader.isPresent()) {
        reader = Optional.of(new SpecificDatumReader<>(GobblinTrackingEvent.class));
    }

    Closer closer = Closer.create();

    try {
        DataInputStream inputStream = closer.register(new DataInputStream(new ByteArrayInputStream(bytes)));

        // Check version byte
        int versionNumber = inputStream.readInt();
        if (versionNumber != SCHEMA_VERSION) {
            throw new IOException(
                    String.format("MetricReport schema version not recognized. Found version %d, expected %d.",
                            versionNumber, SCHEMA_VERSION));
        }

        // Decode the rest
        Decoder decoder = DecoderFactory.get().binaryDecoder(inputStream, null);
        return reader.get().read(reuse, decoder);
    } catch (Throwable t) {
        throw closer.rethrow(t);
    } finally {
        closer.close();
    }
}

From source file:gobblin.metrics.MetricReportUtils.java

/**
 * Parses a {@link gobblin.metrics.MetricReport} from a byte array representing a json input.
 * @param reuse MetricReport to reuse.//from w  ww .  ja  v  a2  s. com
 * @param bytes Input bytes.
 * @return MetricReport.
 * @throws java.io.IOException
 */
public synchronized static MetricReport deserializeReportFromJson(MetricReport reuse, byte[] bytes)
        throws IOException {
    if (!READER.isPresent()) {
        READER = Optional.of(new SpecificDatumReader<MetricReport>(MetricReport.class));
    }

    Closer closer = Closer.create();

    try {
        DataInputStream inputStream = closer.register(new DataInputStream(new ByteArrayInputStream(bytes)));

        // Check version byte
        int versionNumber = inputStream.readInt();
        if (versionNumber != SCHEMA_VERSION) {
            throw new IOException(
                    String.format("MetricReport schema version not recognized. Found version %d, expected %d.",
                            versionNumber, SCHEMA_VERSION));
        }

        // Decode the rest
        Decoder decoder = DecoderFactory.get().jsonDecoder(MetricReport.SCHEMA$, inputStream);
        return READER.get().read(reuse, decoder);
    } catch (Throwable t) {
        throw closer.rethrow(t);
    } finally {
        closer.close();
    }
}

From source file:org.apache.gobblin.metrics.reporter.util.MetricReportUtils.java

/**
 * Parses a {@link org.apache.gobblin.metrics.MetricReport} from a byte array representing a json input.
 * @param reuse MetricReport to reuse.// ww  w  . ja  va  2  s  .c o m
 * @param bytes Input bytes.
 * @return MetricReport.
 * @throws java.io.IOException
 */
public synchronized static MetricReport deserializeReportFromJson(MetricReport reuse, byte[] bytes)
        throws IOException {
    if (!READER.isPresent()) {
        READER = Optional.of(new SpecificDatumReader<>(MetricReport.class));
    }

    Closer closer = Closer.create();

    try {
        DataInputStream inputStream = closer.register(new DataInputStream(new ByteArrayInputStream(bytes)));

        // Check version byte
        int versionNumber = inputStream.readInt();
        if (versionNumber != SCHEMA_VERSION) {
            throw new IOException(
                    String.format("MetricReport schema version not recognized. Found version %d, expected %d.",
                            versionNumber, SCHEMA_VERSION));
        }

        // Decode the rest
        Decoder decoder = DecoderFactory.get().jsonDecoder(MetricReport.SCHEMA$, inputStream);
        return READER.get().read(reuse, decoder);
    } catch (Throwable t) {
        throw closer.rethrow(t);
    } finally {
        closer.close();
    }
}

From source file:gobblin.metrics.reporter.util.EventUtils.java

/**
 * Parses a {@link gobblin.metrics.MetricReport} from a byte array representing a json input.
 * @param reuse MetricReport to reuse./* w  ww . j  a  v  a  2s .com*/
 * @param bytes Input bytes.
 * @return MetricReport.
 * @throws java.io.IOException
 */
public synchronized static GobblinTrackingEvent deserializeReportFromJson(GobblinTrackingEvent reuse,
        byte[] bytes) throws IOException {
    if (!reader.isPresent()) {
        reader = Optional.of(new SpecificDatumReader<>(GobblinTrackingEvent.class));
    }

    Closer closer = Closer.create();

    try {
        DataInputStream inputStream = closer.register(new DataInputStream(new ByteArrayInputStream(bytes)));

        // Check version byte
        int versionNumber = inputStream.readInt();
        if (versionNumber != SCHEMA_VERSION) {
            throw new IOException(
                    String.format("MetricReport schema version not recognized. Found version %d, expected %d.",
                            versionNumber, SCHEMA_VERSION));
        }

        // Decode the rest
        Decoder decoder = DecoderFactory.get().jsonDecoder(GobblinTrackingEvent.SCHEMA$, inputStream);
        return reader.get().read(reuse, decoder);
    } catch (Throwable t) {
        throw closer.rethrow(t);
    } finally {
        closer.close();
    }
}

From source file:org.apache.gobblin.util.logs.Log4jConfigurationHelper.java

/**
 * Update the log4j configuration./*from w  w  w .  j  av a2 s.  c om*/
 *
 * @param targetClass the target class used to get the original log4j configuration file as a resource
 * @param log4jPath the custom log4j configuration properties file path
 * @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 log4jPath, String log4jFileName)
        throws IOException {
    Closer closer = Closer.create();
    try {
        InputStream fileInputStream = closer.register(new FileInputStream(log4jPath));
        InputStream inputStream = closer.register(targetClass.getResourceAsStream("/" + log4jFileName));
        Properties customProperties = new Properties();
        customProperties.load(fileInputStream);
        Properties originalProperties = new Properties();
        originalProperties.load(inputStream);

        for (Entry<Object, Object> entry : customProperties.entrySet()) {
            originalProperties.setProperty(entry.getKey().toString(), entry.getValue().toString());
        }

        LogManager.resetConfiguration();
        PropertyConfigurator.configure(originalProperties);
    } catch (Throwable t) {
        throw closer.rethrow(t);
    } finally {
        closer.close();
    }
}