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:io.druid.examples.web.InputSupplierUpdateStream.java

public InputSupplierUpdateStream(final InputSupplier<BufferedReader> supplier, final String timeDimension) {
    addToQueueThread = new Thread() {
        public void run() {
            while (!isInterrupted()) {
                try {
                    BufferedReader reader = supplier.getInput();
                    String line;//  w w  w  . j a v  a2s. c o m
                    while ((line = reader.readLine()) != null) {
                        if (isValid(line)) {
                            HashMap<String, Object> map = mapper.readValue(line, typeRef);
                            if (map.get(timeDimension) != null) {
                                queue.offer(map, queueWaitTime, TimeUnit.SECONDS);
                                log.debug("Successfully added to queue");
                            } else {
                                log.info("missing timestamp");
                            }
                        }
                    }
                }

                catch (InterruptedException e) {
                    log.info(e, "Thread adding events to the queue interrupted");
                    return;
                } catch (JsonMappingException e) {
                    log.info(e, "Error in converting json to map");
                } catch (JsonParseException e) {
                    log.info(e, "Error in parsing json");
                } catch (IOException e) {
                    log.info(e, "Error in connecting to InputStream");
                }
            }
        }
    };
    addToQueueThread.setDaemon(true);

    this.supplier = supplier;
    this.typeRef = new TypeReference<HashMap<String, Object>>() {
    };
    this.timeDimension = timeDimension;
}

From source file:de.cosmocode.palava.mail.attachments.LocalFileAttachment.java

@Override
public MailAttachment generate(final String name, Map<String, String> configuration) {
    final String fileName = configuration.get("file");
    Preconditions.checkNotNull(fileName, "'file' not configured for attachment");

    final File file = new File(fileName);
    final InputSupplier<FileInputStream> supplier = Files.newInputStreamSupplier(file);

    return new MailAttachment() {

        @Override/*  w  w w  . j  a v  a  2 s  .  co  m*/
        public String getName() {
            return name;
        }

        @Override
        public InputStream getContent() {
            try {
                return supplier.getInput();
            } catch (IOException e) {
                throw new IllegalStateException(e);
            }
        }

        @Override
        public String toString() {
            return "LocalFileAttachment [" + name + "]";
        }

    };
}

From source file:org.eclipse.osee.orcs.account.admin.internal.oauth.ClientStorage.java

private void txSetClient(TransactionBuilder tx, ArtifactId artId, OAuthClient data) {
    //@formatter:off
    tx.setSoleAttributeFromString(artId, CoreAttributeTypes.Description, data.getApplicationDescription());
    tx.setSoleAttributeFromString(artId, OAUTH_CLIENT_WEBSITE_URI, data.getApplicationWebUri());
    tx.setSoleAttributeFromString(artId, OAUTH_CLIENT_LOGO_URI, data.getApplicationLogoUri());

    tx.setSoleAttributeValue(artId, OAUTH_CLIENT_IS_CONFIDENTIAL, data.isConfidential());

    tx.setAttributesFromStrings(artId, OAUTH_CLIENT_AUTHORIZED_AUDIENCE, data.getRegisteredAudiences());
    tx.setAttributesFromStrings(artId, OAUTH_CLIENT_AUTHORIZED_GRANT_TYPE, data.getAllowedGrantTypes());
    tx.setAttributesFromStrings(artId, OAUTH_CLIENT_AUTHORIZED_REDIRECT_URI, data.getRedirectUris());
    tx.setAttributesFromStrings(artId, OAUTH_CLIENT_AUTHORIZED_SCOPE, data.getRegisteredScopes());
    //@formatter:on

    InputSupplier<InputStream> supplier = data.getApplicationLogoSupplier();
    if (supplier != null) {
        try {// www .j  a va  2  s .  c om
            tx.setAttributesFromValues(artId, CoreAttributeTypes.ImageContent, supplier.getInput());
        } catch (Exception ex) {
            throw new OseeCoreException(ex, "Error reading logo data for [%s]", artId);
        }
    }

    Map<String, String> props = data.getProperties();
    Gson gson = builder.create();
    String json = gson.toJson(props);
    tx.setSoleAttributeValue(artId, OAUTH_CLIENT_PROPERTIES, json);
}

From source file:com.facebook.presto.serde.PagesSerde.java

public Iterable<Page> readPages(final InputSupplier<SliceInput> sliceInputSupplier) {
    Preconditions.checkNotNull(sliceInputSupplier, "sliceInputSupplier is null");

    return new Iterable<Page>() {
        @Override/*  w w  w  .  jav  a 2 s  .  c  om*/
        public Iterator<Page> iterator() {
            try {
                return readPages(sliceInputSupplier.getInput());
            } catch (IOException e) {
                throw Throwables.propagate(e);
            }
        }
    };
}

From source file:eu.esdihumboldt.hale.io.xsd.reader.internal.HumboldtURIResolver.java

/**
 * @see URIResolver#resolveEntity(String, String, String)
 *//*www  .  jav a 2s . c o  m*/
@Override
public InputSource resolveEntity(String namespace, String schemaLocation, String baseUri) {
    if (baseUri != null) {
        try {
            if (baseUri.startsWith("file:/")) { //$NON-NLS-1$
                baseUri = new URI(baseUri).getPath();
            }

            File baseFile = new File(baseUri);
            if (baseFile.exists()) {
                baseUri = baseFile.toURI().toString();
            } else if (collectionBaseURI != null) {
                baseFile = new File(collectionBaseURI);
                if (baseFile.exists()) {
                    baseUri = baseFile.toURI().toString();
                }
            }

            URI ref = new URI(baseUri).resolve(new URI(schemaLocation));

            // try resolving using (local) Resources
            InputSupplier<? extends InputStream> input = Resources.tryResolve(ref,
                    Resources.RESOURCE_TYPE_XML_SCHEMA);
            if (input != null) {
                try {
                    InputSource is = new InputSource(input.getInput());
                    is.setSystemId(ref.toString());
                    return is;
                } catch (Throwable e) {
                    // ignore
                }
            }

            // try resolving through cache
            try {
                InputSource is = new InputSource(Request.getInstance().get(ref));
                is.setSystemId(ref.toString());
                return is;
            } catch (Throwable e) {
                // ignore
            }

            // fall-back
            return new InputSource(ref.toString());
        } catch (URISyntaxException e1) {
            throw new RuntimeException(e1);
        }
    }

    // try resolving using (local) Resources
    try {
        URI locationUri = new URI(schemaLocation);
        InputSupplier<? extends InputStream> input = Resources.tryResolve(locationUri,
                Resources.RESOURCE_TYPE_XML_SCHEMA);
        if (input != null) {
            InputSource is = new InputSource(input.getInput());
            is.setSystemId(schemaLocation);
            return is;
        }
    } catch (Throwable e) {
        // ignore
    }

    // try resolving through cache
    try {
        InputSource is = new InputSource(Request.getInstance().get(schemaLocation));
        is.setSystemId(schemaLocation);
        return is;
    } catch (Throwable e) {
        // ignore
    }

    // fall-back
    return new InputSource(schemaLocation);
}

From source file:co.cask.cdap.data.stream.StreamDataFileIndex.java

/**
 * Constructs with the given input.//from  ww w  .j av a 2 s . com
 *
 * @param indexInputSupplier Provides {@link InputStream} for reading the index.
 */
StreamDataFileIndex(InputSupplier<? extends InputStream> indexInputSupplier) {
    LongList timestamps;
    LongList positions;

    // Load the whole index into memory.
    try {
        Map.Entry<LongList, LongList> index;
        try (InputStream indexInput = indexInputSupplier.getInput()) {
            index = loadIndex(indexInput);
        }
        timestamps = LongLists.unmodifiable(index.getKey());
        positions = LongLists.unmodifiable(index.getValue());
    } catch (IOException e) {
        LOG.error("Failed to load stream index. Default to empty index.", e);
        timestamps = LongLists.EMPTY_LIST;
        positions = LongLists.EMPTY_LIST;
    }
    this.timestamps = timestamps;
    this.positions = positions;
}

From source file:org.apache.drill.exec.expr.fn.FunctionConverter.java

private String getClassBody(Class<?> c) throws CompileException, IOException {
    String path = c.getName();/*  w  w w  . j  a  v  a 2s . c o  m*/
    path = path.replaceFirst("\\$.*", "");
    path = path.replace(".", FileUtils.separator);
    path = "/" + path + ".java";
    URL u = Resources.getResource(c, path);
    InputSupplier<InputStream> supplier = Resources.newInputStreamSupplier(u);
    try (InputStream is = supplier.getInput()) {
        if (is == null) {
            throw new IOException(String.format(
                    "Failure trying to located source code for Class %s, tried to read on classpath location %s",
                    c.getName(), path));
        }
        String body = IO.toString(is);

        //TODO: Hack to remove annotations so Janino doesn't choke.  Need to reconsider this problem...
        //return body.replaceAll("@(?:Output|Param|Workspace|Override|SuppressWarnings\\([^\\\\]*?\\)|FunctionTemplate\\([^\\\\]*?\\))", "");
        return body.replaceAll("@(?:\\([^\\\\]*?\\))?", "");
    }

}

From source file:org.apache.drill.exec.expr.fn.FunctionConverter.java

private CompilationUnit get(Class<?> c) throws IOException {
    String path = c.getName();/*w  w  w .  j  av a2s . c  o m*/
    path = path.replaceFirst("\\$.*", "");
    path = path.replace(".", FileUtils.separator);
    path = "/" + path + ".java";
    CompilationUnit cu = functionUnits.get(path);
    if (cu != null) {
        return cu;
    }

    URL u = Resources.getResource(c, path);
    InputSupplier<InputStream> supplier = Resources.newInputStreamSupplier(u);
    try (InputStream is = supplier.getInput()) {
        if (is == null) {
            throw new IOException(String.format(
                    "Failure trying to located source code for Class %s, tried to read on classpath location %s",
                    c.getName(), path));
        }
        String body = IO.toString(is);

        //TODO: Hack to remove annotations so Janino doesn't choke.  Need to reconsider this problem...
        body = body.replaceAll("@\\w+(?:\\([^\\\\]*?\\))?", "");
        try {
            cu = new Parser(new Scanner(null, new StringReader(body))).parseCompilationUnit();
            functionUnits.put(path, cu);
            return cu;
        } catch (CompileException e) {
            logger.warn("Failure while parsing function class:\n{}", body, e);
            return null;
        }

    }

}

From source file:org.eclipse.andmore.internal.wizards.templates.CreateFileChange.java

@SuppressWarnings("resource") // Eclipse doesn't know about Guava's Closeables.closeQuietly
@Override//  ww w.  java 2s  . co m
public Change perform(IProgressMonitor pm) throws CoreException {
    InputSupplier<FileInputStream> supplier = Files.newInputStreamSupplier(mSource);
    InputStream is = null;
    try {
        pm.beginTask("Creating file", 3);
        IFile file = (IFile) getModifiedResource();

        IContainer parent = file.getParent();
        if (parent != null && !parent.exists()) {
            IFolder folder = ResourcesPlugin.getWorkspace().getRoot().getFolder(parent.getFullPath());
            AdtUtils.ensureExists(folder);
        }

        is = supplier.getInput();
        file.create(is, false, new SubProgressMonitor(pm, 1));
        pm.worked(1);
    } catch (Exception ioe) {
        AndmoreAndroidPlugin.log(ioe, null);
    } finally {
        Closeables.closeQuietly(is);
        pm.done();
    }
    return null;
}

From source file:com.android.ide.eclipse.auidt.internal.wizards.templates.CreateFileChange.java

@SuppressWarnings("resource") // Eclipse doesn't know about Guava's Closeables.closeQuietly
@Override//w w  w .  j a  v a  2  s.co m
public Change perform(IProgressMonitor pm) throws CoreException {
    InputSupplier<FileInputStream> supplier = Files.newInputStreamSupplier(mSource);
    InputStream is = null;
    try {
        pm.beginTask("Creating file", 3);
        IFile file = (IFile) getModifiedResource();

        IContainer parent = file.getParent();
        if (parent != null && !parent.exists()) {
            IFolder folder = ResourcesPlugin.getWorkspace().getRoot().getFolder(parent.getFullPath());
            AdtUtils.ensureExists(folder);
        }

        is = supplier.getInput();
        file.create(is, false, new SubProgressMonitor(pm, 1));
        pm.worked(1);
    } catch (Exception ioe) {
        AdtPlugin.log(ioe, null);
    } finally {
        Closeables.closeQuietly(is);
        pm.done();
    }
    return null;
}