Example usage for com.google.common.io ByteSource openStream

List of usage examples for com.google.common.io ByteSource openStream

Introduction

In this page you can find the example usage for com.google.common.io ByteSource openStream.

Prototype

public abstract InputStream openStream() throws IOException;

Source Link

Document

Opens a new InputStream for reading from this source.

Usage

From source file:com.bouncestorage.chaoshttpproxy.Main.java

public static void main(String[] args) throws Exception {
    Options options = new Options();
    CmdLineParser parser = new CmdLineParser(options);
    try {/*from  www  .j a va2  s .co m*/
        parser.parseArgument(args);
    } catch (CmdLineException cle) {
        usage(parser);
    }

    if (options.version) {
        System.err.println(Main.class.getPackage().getImplementationVersion());
        System.exit(0);
    }

    ByteSource byteSource;
    if (options.propertiesFile == null) {
        byteSource = Resources.asByteSource(Resources.getResource("chaos-http-proxy.conf"));
    } else {
        byteSource = Files.asByteSource(options.propertiesFile);
    }

    ChaosConfig config;
    try (InputStream is = byteSource.openStream()) {
        config = ChaosConfig.loadFromPropertyStream(is);
    } catch (IOException ioe) {
        System.err.println(ioe.getMessage());
        System.exit(1);
        return;
    }

    URI proxyEndpoint = new URI("http", null, options.address, options.port, null, null, null);
    ChaosHttpProxy proxy = new ChaosHttpProxy(proxyEndpoint, config);
    try {
        proxy.start();
    } catch (Exception e) {
        System.err.println(e.getMessage());
        System.exit(1);
    }
}

From source file:google.registry.tmch.TmchData.java

@SuppressWarnings("deprecation")
static PGPPublicKey loadPublicKey(ByteSource pgpPublicKeyFile) {
    try (InputStream input = pgpPublicKeyFile.openStream();
            InputStream decoder = PGPUtil.getDecoderStream(input)) {
        return new BcPGPPublicKeyRing(decoder).getPublicKey();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }//from w ww .  jav a 2 s  .c o  m
}

From source file:eu.lp0.cursus.test.xml.ValidateResultsXML.java

private static void validate(Collection<ByteSource> pages) throws Exception {
    for (ByteSource page : pages) {
        Validator validator = Namespace.RESULTS_V1.newValidator();
        validator.validate(new StreamSource(page.openStream()));
    }/*from   w  w  w  .j a va2 s  .co  m*/
}

From source file:be.fror.password.vault.core.VaultFormat.java

public static Vault read(ByteSource source) throws IOException, UnsupportedFormatException {
    VaultFormat[] formats = { Vault1Format.INSTANCE };
    try (PushbackInputStream in = new PushbackInputStream(source.openStream())) {
        for (VaultFormat format : formats) {
            if (format.accept(in)) {
                return format.read(in);
            }//from   ww w  .j a  v  a 2  s  .c  o  m
        }
    }
    throw new UnsupportedFormatException(source.toString());
}

From source file:org.jclouds.digitalocean2.ssh.DSAKeys.java

/**
 * Returns {@link java.security.spec.DSAPublicKeySpec} which was OpenSSH Base64 Encoded {@code id_rsa.pub}
 *
 * @param supplier the input stream factory, formatted {@code ssh-dss AAAAB3NzaC1yc2EAAAADAQABAAAB...}
 *
 * @return the {@link java.security.spec.DSAPublicKeySpec} which was OpenSSH Base64 Encoded {@code id_rsa.pub}
 * @throws java.io.IOException if an I/O error occurs
 *///from   w ww  .j av a  2 s  .com
public static DSAPublicKeySpec publicKeySpecFromOpenSSH(ByteSource supplier) throws IOException {
    InputStream stream = supplier.openStream();
    Iterable<String> parts = Splitter.on(' ').split(toStringAndClose(stream).trim());
    checkArgument(size(parts) >= 2 && "ssh-dss".equals(get(parts, 0)),
            "bad format, should be: ssh-dss AAAAB3...");
    stream = new ByteArrayInputStream(base64().decode(get(parts, 1)));
    String marker = new String(readLengthFirst(stream));
    checkArgument("ssh-dss".equals(marker), "looking for marker ssh-dss but got %s", marker);
    BigInteger p = new BigInteger(readLengthFirst(stream));
    BigInteger q = new BigInteger(readLengthFirst(stream));
    BigInteger g = new BigInteger(readLengthFirst(stream));
    BigInteger y = new BigInteger(readLengthFirst(stream));
    return new DSAPublicKeySpec(y, p, q, g);
}

From source file:com.google.caliper.util.Util.java

public static ImmutableMap<String, String> loadProperties(ByteSource is) throws IOException {
    Properties props = new Properties();
    Closer closer = Closer.create();/*from  ww w .  j  ava2  s.  c  o m*/
    InputStream in = closer.register(is.openStream());
    try {
        props.load(in);
    } finally {
        closer.close();
    }
    return Maps.fromProperties(props);
}

From source file:com.facebook.buck.artifact_cache.ThriftArtifactCacheProtocol.java

private static String computeHash(ByteSource source, HashFunction hashFunction) throws IOException {
    try (InputStream inputStream = source.openStream();
            HashingOutputStream outputStream = new HashingOutputStream(hashFunction, new OutputStream() {
                @Override//from  w w  w  .ja v  a2s. co  m
                public void write(int b) throws IOException {
                    // Do nothing.
                }
            })) {
        ByteStreams.copy(inputStream, outputStream);
        return outputStream.hash().toString();
    }
}

From source file:org.apache.druid.java.util.common.StreamUtils.java

/**
 * Retry copy attempts from input stream to output stream. Does *not* check to make sure data was intact during the transfer
 *
 * @param byteSource  Supplier for input streams to copy from. The stream is closed on every retry.
 * @param byteSink    Supplier for output streams. The stream is closed on every retry.
 * @param shouldRetry Predicate to determine if the throwable is recoverable for a retry
 * @param maxAttempts Maximum number of retries before failing
 *//*from ww w.  j a  va2 s. c o m*/
public static long retryCopy(final ByteSource byteSource, final ByteSink byteSink,
        final Predicate<Throwable> shouldRetry, final int maxAttempts) {
    try {
        return RetryUtils.retry(() -> {
            try (InputStream inputStream = byteSource.openStream()) {
                try (OutputStream outputStream = byteSink.openStream()) {
                    final long retval = ByteStreams.copy(inputStream, outputStream);
                    // Workarround for http://hg.openjdk.java.net/jdk8/jdk8/jdk/rev/759aa847dcaf
                    outputStream.flush();
                    return retval;
                }
            }
        }, shouldRetry, maxAttempts);
    } catch (Exception e) {
        throw Throwables.propagate(e);
    }
}

From source file:org.apache.isis.core.commons.lang.ClassExtensions.java

public static Properties resourceProperties(final Class<?> extendee, final String suffix) {
    try {/* w  w  w . j a v  a  2 s .co  m*/
        final URL url = Resources.getResource(extendee, extendee.getSimpleName() + suffix);
        final ByteSource byteSource = Resources.asByteSource(url);
        final Properties properties = new Properties();
        properties.load(byteSource.openStream());
        return properties;
    } catch (Exception e) {
        return null;
    }
}

From source file:org.opendaylight.controller.cluster.messaging.MessageAssembler.java

private static Object reAssembleMessage(final AssembledMessageState state) throws MessageSliceException {
    try {/*w w  w  .java  2s  . com*/
        final ByteSource assembledBytes = state.getAssembledBytes();
        try (ObjectInputStream in = new ObjectInputStream(assembledBytes.openStream())) {
            return in.readObject();
        }

    } catch (IOException | ClassNotFoundException e) {
        throw new MessageSliceException(
                String.format("Error re-assembling bytes for identifier %s", state.getIdentifier()), e);
    }
}