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:com.nearinfinity.honeycomb.config.ConfigurationParser.java

/**
 * Parses the application configuration and returns the XML document.
 * @param configSupplier File supplier containing application configuration
 * @return XML Document/*from www  .  j  av a2 s  .co m*/
 */
private static Document parseDocument(final InputSupplier<? extends InputStream> configSupplier) {
    try {
        return DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(configSupplier.getInput());

    } catch (Exception e) {
        logger.error("Unable to parse honeycomb configuration.", e);
        throw new RuntimeException("Exception while parsing honeycomb configuration.", e);
    }
}

From source file:org.jclouds.ssh.SshKeys.java

/**
 * Returns {@link RSAPublicKeySpec} which was OpenSSH Base64 Encoded {@code id_rsa.pub}
 * // w ww.  ja v a2s .co m
 * @param supplier
 *           the input stream factory, formatted {@code ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAB...}
 * 
 * @return the {@link RSAPublicKeySpec} which was OpenSSH Base64 Encoded {@code id_rsa.pub}
 * @throws IOException
 *            if an I/O error occurs
 */
public static RSAPublicKeySpec 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-rsa".equals(get(parts, 0)),
            "bad format, should be: ssh-rsa AAAAB3...");
    stream = new ByteArrayInputStream(base64().decode(get(parts, 1)));
    String marker = new String(readLengthFirst(stream));
    checkArgument("ssh-rsa".equals(marker), "looking for marker ssh-rsa but got %s", marker);
    BigInteger publicExponent = new BigInteger(readLengthFirst(stream));
    BigInteger modulus = new BigInteger(readLengthFirst(stream));
    return new RSAPublicKeySpec(modulus, publicExponent);
}

From source file:edu.cmu.cs.lti.ark.fn.parsing.FEDict.java

private static Map<String, THashSet<String>> readInput(final InputSupplier<? extends InputStream> inputSupplier)
        throws LoadingException {
    try {//w  ww .j a  v a2s .co  m
        return SerializedObjects.readObject(new InputSupplier<ObjectInputStream>() {
            @Override
            public ObjectInputStream getInput() throws IOException {
                return new ObjectInputStream(new BufferedInputStream(inputSupplier.getInput()));
            }
        });
    } catch (Exception e) {
        throw new LoadingException(e);
    }
}

From source file:com.nearinfinity.honeycomb.config.ConfigurationParser.java

/**
 * Performs validation on the configuration content supplied by the
 * configuration supplier against the schema document provided by the
 * validation supplier.  Throws Runtime exception if validation fails.
 *
 * @param configSupplier The supplier that provides the configuration to inspect, not null
 * @param schemaSupplier The supplier that provides the schema used to inspect the configuration, not null
 *//*w  w w. j a va  2 s.c  o m*/
private static void checkValidConfig(final InputSupplier<? extends InputStream> configSupplier,
        final InputSupplier<? extends InputStream> schemaSupplier) {
    try {
        final SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        final Schema schema = schemaFactory.newSchema(new StreamSource(schemaSupplier.getInput()));
        final Validator validator = schema.newValidator();
        validator.validate(new StreamSource(configSupplier.getInput()));
    } catch (Exception e) {
        logger.error("Unable to validate honeycomb configuration.", e);
        throw new RuntimeException("Exception while validating honeycomb configuration.", e);
    }
}

From source file:co.cask.cdap.common.lang.jar.BundleJarUtil.java

/**
 * Unpack a jar source to a directory.//from w  w w  .  jav a  2s .co m
 *
 * @param inputSupplier Supplier for the jar source
 * @param destinationFolder Directory to expand into
 * @return The {@code destinationFolder}
 * @throws IOException If failed to expand the jar
 */
public static File unpackProgramJar(InputSupplier<? extends InputStream> inputSupplier, File destinationFolder)
        throws IOException {
    Preconditions.checkArgument(inputSupplier != null);
    Preconditions.checkArgument(destinationFolder != null);
    Preconditions.checkArgument(destinationFolder.canWrite());

    destinationFolder.mkdirs();
    Preconditions.checkState(destinationFolder.exists());
    try (ZipInputStream input = new ZipInputStream(inputSupplier.getInput())) {
        unJar(input, destinationFolder);
        return destinationFolder;
    }
}

From source file:com.scottwoodward.rpitems.items.NbtFactory.java

/**
 * Load the content of a file from a stream.
 * <p>/*from   ww  w . j av  a2 s.  c o  m*/
 * Use {@link Files#newInputStreamSupplier(java.io.File)} to provide a stream from a file.
 * @param stream - the stream supplier.
 * @param option - whether or not to decompress the input stream.
 * @return The decoded NBT compound.
 * @throws IOException If anything went wrong.
 */
public static NbtCompound fromStream(InputSupplier<? extends InputStream> stream, StreamOptions option)
        throws IOException {
    InputStream input = null;
    DataInputStream data = null;

    try {
        input = stream.getInput();
        data = new DataInputStream(new BufferedInputStream(
                option == StreamOptions.GZIP_COMPRESSION ? new GZIPInputStream(input) : input));

        return fromCompound(invokeMethod(get().LOAD_COMPOUND, null, data));
    } finally {
        if (data != null)
            Closeables.closeQuietly(data);
        if (input != null)
            Closeables.closeQuietly(input);
    }
}

From source file:org.eclipse.osee.orcs.db.internal.search.tagger.XmlTagger.java

private InputStream getStream(InputSupplier<? extends InputStream> provider) throws IOException {
    return new XmlTextInputStream(provider.getInput());
}

From source file:com.facebook.swift.generator.util.TemplateLoader.java

protected StringTemplateGroup getTemplateGroup() throws IOException {
    if (stg == null) {
        final URL resourceUrl = Resources.getResource(this.getClass(), "/templates/" + templateFileName);
        final InputSupplier<InputStreamReader> is = Resources.newReaderSupplier(resourceUrl, Charsets.UTF_8);
        stg = new StringTemplateGroup(is.getInput(), AngleBracketTemplateLexer.class, ERROR_LISTENER);
    }/*from   ww  w  .ja va  2s  .c  o m*/

    return stg;
}

From source file:org.eclipse.osee.orcs.db.internal.search.tagger.TextStreamTagger.java

@Override
public void tagIt(InputSupplier<? extends InputStream> provider, TagCollector collector) throws Exception {
    InputStream inputStream = null;
    try {/*  w  ww.  j av a2  s .c o m*/
        inputStream = provider.getInput();
        getTagProcessor().collectFromInputStream(inputStream, collector);
    } finally {
        Lib.close(inputStream);
    }
}

From source file:org.sonar.batch.bootstrap.ServerClient.java

public String request(String pathStartingWithSlash, boolean wrapHttpException) {
    InputSupplier<InputStream> inputSupplier = doRequest(pathStartingWithSlash);
    try {//from   ww  w  . j  ava  2  s .  c  om
        return IOUtils.toString(inputSupplier.getInput(), "UTF-8");
    } catch (HttpDownloader.HttpException e) {
        throw (wrapHttpException ? handleHttpException(pathStartingWithSlash, e) : e);
    } catch (IOException e) {
        throw new SonarException(String.format("Unable to request: %s", pathStartingWithSlash), e);
    }
}