Example usage for com.google.common.io InputSupplier getInput

List of usage examples for com.google.common.io InputSupplier getInput

Introduction

In this page you can find the example usage for com.google.common.io InputSupplier getInput.

Prototype

T getInput() throws IOException;

Source Link

Document

Returns an object that encapsulates a readable resource.

Usage

From source file:org.eclipse.recommenders.utils.Zips.java

public static void unzip(File zipFile, File destFolder) throws IOException {
    ZipInputStream zis = null;//from   ww  w. jav a  2 s . c o m
    try {
        InputSupplier<FileInputStream> fis = Files.newInputStreamSupplier(zipFile);
        zis = new ZipInputStream(fis.getInput());
        ZipEntry entry;
        while ((entry = zis.getNextEntry()) != null) {
            if (!entry.isDirectory()) {
                final File file = new File(destFolder, entry.getName());
                Files.createParentDirs(file);
                Files.write(ByteStreams.toByteArray(zis), file);
            }
        }
    } finally {
        Closeables.closeQuietly(zis);
    }
}

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

/**
 * Returns {@link DSAPublicKeySpec} which was OpenSSH Base64 Encoded
 * {@code id_rsa.pub}//  www. ja  va2  s  .co  m
 * 
 * @param supplier the input stream factory, formatted
 *        {@code ssh-dss AAAAB3NzaC1yc2EAAAADAQABAAAB...}
 * 
 * @return the {@link DSAPublicKeySpec} which was OpenSSH Base64 Encoded
 *         {@code id_rsa.pub}
 * @throws IOException if an I/O error occurs
 */
public static DSAPublicKeySpec publicKeySpecFromOpenSSH(InputSupplier<? extends InputStream> supplier)
        throws IOException {
    InputStream stream = supplier.getInput();
    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:org.eclipse.recommenders.utils.gson.GsonUtil.java

public static <T> List<T> deserializeZip(File zip, Class<T> classOfT) throws IOException {

    List<T> res = Lists.newLinkedList();
    ZipInputStream zis = null;//from ww  w  .j a  v a 2 s . co m
    try {
        InputSupplier<FileInputStream> fis = Files.newInputStreamSupplier(zip);
        zis = new ZipInputStream(fis.getInput());
        ZipEntry entry;
        while ((entry = zis.getNextEntry()) != null) {
            if (!entry.isDirectory()) {
                res.add(GsonUtil.<T>deserialize(zis, classOfT));
            }
        }
    } finally {
        Closeables.closeQuietly(zis);
    }
    return res;
}

From source file:com.metamx.druid.index.v1.MetricHolder.java

public static void writeComplexMetric(OutputSupplier<? extends OutputStream> outSupplier, String name,
        String typeName, GenericIndexedWriter column) throws IOException {
    OutputStream out = null;/*from w  w w  .j  a v a  2  s.  c  om*/
    InputStream in = null;

    try {
        out = outSupplier.getOutput();

        out.write(version);
        serializerUtils.writeString(out, name);
        serializerUtils.writeString(out, typeName);

        final InputSupplier<InputStream> supplier = column.combineStreams();
        in = supplier.getInput();

        ByteStreams.copy(in, out);
    } finally {
        Closeables.closeQuietly(out);
        Closeables.closeQuietly(in);
    }
}

From source file:cc.recommenders.utils.gson.GsonUtil.java

public static <T> List<T> deserializeZip(File zip, Class<T> classOfT) throws IOException {

    List<T> res = Lists.newLinkedList();
    ZipInputStream zis = null;// w w w .j a v  a 2s . c  o m
    try {
        InputSupplier<FileInputStream> fis = Files.newInputStreamSupplier(zip);
        zis = new ZipInputStream(fis.getInput());
        ZipEntry entry;
        while ((entry = zis.getNextEntry()) != null) {
            if (!entry.isDirectory()) {
                final InputStreamReader reader = new InputStreamReader(zis);
                final T data = getInstance().fromJson(reader, classOfT);
                res.add(data);
            }
        }
    } finally {
        Closeables.closeQuietly(zis);
    }
    return res;
}

From source file:io.druid.segment.MetricHolder.java

public static void writeComplexMetric(OutputSupplier<? extends OutputStream> outSupplier, String name,
        String typeName, GenericIndexedWriter column) throws IOException {
    try (OutputStream out = outSupplier.getOutput()) {
        out.write(version);/*w ww . ja v a  2 s.c o m*/
        serializerUtils.writeString(out, name);
        serializerUtils.writeString(out, typeName);

        final InputSupplier<InputStream> supplier = column.combineStreams();
        try (InputStream in = supplier.getInput()) {
            ByteStreams.copy(in, out);
        }
    }
}

From source file:org.asoem.greyfish.utils.persistence.Persisters.java

public static <T> T deserialize(final Persister persister,
        final InputSupplier<? extends InputStream> inputSupplier, final Class<T> clazz)
        throws IOException, ClassCastException, ClassNotFoundException {
    final InputStream input = inputSupplier.getInput();
    boolean threw = true;
    try {//from   ww  w.j a  va  2s. c  o  m
        final T object = persister.deserialize(input, clazz);
        threw = false;
        return object;
    } finally {
        Closeables.close(input, threw);
    }
}

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

public static List<X509Certificate> readCertificates(InputSupplier<? extends InputStream> in)
        throws CertificateException, IOException {
    InputStream is = null;//w w w.  ja  v  a  2s  . co m
    try {
        is = in.getInput();
        List<X509Certificate> certs = new LinkedList<X509Certificate>();
        CertificateFactory cf = CertificateFactory.getInstance(SSLConstants.CERTIFICATE_TYPE_X509);
        while (is.available() > 0) {
            X509Certificate cert = (X509Certificate) cf.generateCertificate(is);
            certs.add(cert);
            log.info("Read {}", cert.getSubjectX500Principal().getName());
        }
        return certs;
    } catch (CertificateParsingException ex) {
        // FIXME check compatibility of X509CertificateFactory in Java 6/7
        if (Java.v6 && ex.getMessage() != null
                && ex.getMessage().equals("invalid DER-encoded certificate data")) {
            log.warn("X509CertificateFactory was not able to parse certificate. "
                    + "Consider switching to Java 7 to overcome this issue.");
        }
        throw ex;
    } finally {
        Closeables.close(is, true);
    }
}

From source file:co.cask.cdap.internal.app.runtime.webapp.WebappProgramRunner.java

public static Set<String> getServingHostNames(InputSupplier<? extends InputStream> inputSupplier)
        throws Exception {
    try (JarInputStream jarInput = new JarInputStream(inputSupplier.getInput())) {
        Set<String> hostNames = Sets.newHashSet();
        JarEntry jarEntry;/*from www  .j a  va2  s.  c  o  m*/
        String webappDir = Constants.Webapp.WEBAPP_DIR + "/";
        while ((jarEntry = jarInput.getNextJarEntry()) != null) {
            if (!jarEntry.isDirectory() && jarEntry.getName().startsWith(webappDir)
                    && jarEntry.getName().contains(ServePathGenerator.SRC_PATH)) {
                // Format is - webapp/host:port/[path/]src/files
                String webappHostName = Iterables.get(Splitter.on("/src/").split(jarEntry.getName()), 0);
                String hostName = Iterables.get(Splitter.on('/').limit(2).split(webappHostName), 1);

                hostNames.add(hostName);
            }
        }

        Set<String> registerNames = Sets.newHashSetWithExpectedSize(hostNames.size());
        for (String hostName : hostNames) {
            if (hostName.equals(ServePathGenerator.DEFAULT_DIR_NAME)) {
                LOG.warn("Not registering default service name; default service needs to have a routable path");
                continue;
            } else if (hostName.startsWith(DEFAULT_DIR_NAME_COLON)) {
                LOG.warn("Not registering default service name with explicit port - {}", hostName);
                continue;
            }

            registerNames.add(Networks.normalizeWebappDiscoveryName(hostName));
        }

        return registerNames;
    }
}

From source file:feign.TrustingSSLSocketFactory.java

private static KeyStore loadKeyStore(InputSupplier<InputStream> inputStreamSupplier) throws IOException {
    Closer closer = Closer.create();//w  ww  . ja  v a2s .c o m
    try {
        InputStream inputStream = closer.register(inputStreamSupplier.getInput());
        KeyStore keyStore = KeyStore.getInstance("JKS");
        keyStore.load(inputStream, KEYSTORE_PASSWORD);
        return keyStore;
    } catch (Throwable e) {
        throw closer.rethrow(e);
    } finally {
        closer.close();
    }
}