Example usage for com.google.common.io CharStreams toString

List of usage examples for com.google.common.io CharStreams toString

Introduction

In this page you can find the example usage for com.google.common.io CharStreams toString.

Prototype

public static String toString(Readable r) throws IOException 

Source Link

Document

Reads all characters from a Readable object into a String .

Usage

From source file:com.axelor.meta.ActionHandler.java

public String template(Templates engine, Reader template) throws IOException {
    return engine.fromText(CharStreams.toString(template)).make(bindings).render();
}

From source file:com.notifier.desktop.transport.usb.impl.Adb.java

protected String runAdb(String... args) throws IOException, InterruptedException {
    ProcessBuilder processBuilder = new ProcessBuilder();
    processBuilder.command(Lists.asList(getAdbPath(), args));
    processBuilder.redirectErrorStream(true);
    final Process process = processBuilder.start();
    String output = CharStreams.toString(new InputSupplier<BufferedReader>() {
        @Override// w  w  w . ja va  2  s.  c om
        public BufferedReader getInput() throws IOException {
            return new BufferedReader(new InputStreamReader(process.getInputStream()));
        }
    });
    logger.trace("adb output:\n{}", output);
    int exitCode = process.waitFor();
    if (exitCode != 0) {
        throw new IOException("adb returned error code: " + exitCode);
    }
    return output;
}

From source file:org.eclipse.xtext.resource.persistence.ResourceStorageLoadable.java

protected void readNodeModel(final StorageAwareResource resource, final InputStream inputStream)
        throws IOException {
    final SerializableNodeModel serializableNodeModel = new SerializableNodeModel(resource);
    boolean _exists = resource.getResourceSet().getURIConverter().exists(resource.getURI(),
            resource.getResourceSet().getLoadOptions());
    boolean _not = (!_exists);
    if (_not) {/*from w w w  .  j a  va2  s.co  m*/
        URI _uRI = resource.getURI();
        String _plus = ("Skipping loading node model for synthetic resource " + _uRI);
        ResourceStorageLoadable.LOG.info(_plus);
        return;
    }
    final InputStream stream = resource.getResourceSet().getURIConverter().createInputStream(resource.getURI());
    String _encoding = resource.getEncoding();
    final InputStreamReader in = new InputStreamReader(stream, _encoding);
    final String completeContent = CharStreams.toString(in);
    final DeserializationConversionContext deserializationContext = new DeserializationConversionContext(
            resource, completeContent);
    final DataInputStream dataIn = new DataInputStream(inputStream);
    serializableNodeModel.readObjectData(dataIn, deserializationContext);
    EObject _head = IterableExtensions.<EObject>head(resource.getContents());
    boolean _hasErrors = deserializationContext.hasErrors();
    ParseResult _parseResult = new ParseResult(_head, serializableNodeModel.root, _hasErrors);
    resource.setParseResult(_parseResult);
}

From source file:org.eclipse.milo.opcua.stack.core.serialization.OpcUaXmlStreamDecoder.java

public OpcUaXmlStreamDecoder setInput(Reader reader) throws IOException, SAXException {
    String s = CharStreams.toString(reader);

    return setInput(new ByteArrayInputStream(s.getBytes()));
}

From source file:zipkin2.storage.cassandra.v1.Schema.java

static void applyCqlFile(String keyspace, Session session, String resource) {
    try (Reader reader = new InputStreamReader(Schema.class.getResourceAsStream(resource), UTF_8)) {
        for (String cmd : CharStreams.toString(reader).split(";")) {
            cmd = cmd.trim().replace(" zipkin", " " + keyspace);
            if (!cmd.isEmpty()) {
                session.execute(cmd);/*from   w  w w .  java 2 s.  c om*/
            }
        }
    } catch (IOException ex) {
        LOG.error(ex.getMessage(), ex);
    }
}

From source file:com.github.cassandra.jdbc.BaseCassandraPreparedStatement.java

public void setCharacterStream(int parameterIndex, Reader reader, int length) throws SQLException {
    // FIXME Slow and will run into OOM issue
    try {/*from   w ww. j  a  v a2s  .co m*/
        setString(parameterIndex, CharStreams.toString(reader).substring(0, length));
    } catch (IOException e) {
        throw new SQLException(e);
    }
}

From source file:com.music.tools.SongDBDownloader.java

private static String getResponseAsString(String urlString, HttpClient client, HttpContext ctx)
        throws IOException {
    HttpUriRequest req = new HttpGet(urlString);
    InputStream is = client.execute(req, ctx).getEntity().getContent();
    String result = CharStreams.toString(new InputStreamReader(is));
    is.close();/*w w  w . j  ava2 s .c o m*/
    req.abort();
    return result;
}

From source file:org.apache.ignite.compatibility.testframework.util.MavenUtils.java

/**
 * Executes given command in operation system.
 *
 * @param cmd Command to execute.//w  w  w  .  ja  v  a2s  .c  o m
 * @return Output of result of executed command.
 * @throws Exception In case of an error.
 */
private static String exec(String cmd) throws Exception {
    ProcessBuilder pb = new ProcessBuilder();
    pb.redirectErrorStream(true);

    pb.command(U.isWindows() ? new String[] { "cmd", "/c", cmd } : new String[] { "/bin/bash", "-c", cmd });

    final Process p = pb.start();

    Future<String> fut = Executors.newSingleThreadExecutor().submit(new Callable<String>() {
        @Override
        public String call() throws Exception {
            String output = CharStreams.toString(new InputStreamReader(p.getInputStream(), Charsets.UTF_8));

            p.waitFor();

            return output;
        }
    });

    String output = null;

    try {
        output = fut.get(5, TimeUnit.MINUTES);

        int exitVal = p.exitValue();

        if (exitVal != 0)
            throw new Exception(String.format("Abnormal exit value of %s for pid %s", exitVal, U.jvmPid()));

        return output;
    } catch (Exception e) {
        p.destroy();

        X.printerrln("Command='" + cmd + "' couldn't be executed: " + output, Charsets.UTF_8, e);

        throw e;
    }
}

From source file:org.geosdi.geoplatform.connector.geoserver.request.datastores.GPGeoserverDeleteDatastoreRequest.java

/**
 * @param reader//w ww .j  ava 2s .  c o  m
 * @return {@link Boolean}
 * @throws Exception
 */
@Override
protected Boolean readInternal(BufferedReader reader) throws Exception {
    String value = CharStreams.toString(reader);
    return ((value != null) && (value.trim().isEmpty()) ? TRUE : FALSE);
}

From source file:natlab.Parse.java

/**
 * Perform the translation from a given Reader containing source code.
 *
 * @param fName    The name of the file to which the source belongs.
 * @param source   The string containing the source code.
 * @param errors  A list of errors for error collection.
 * /*from   w  w w.  j  av a2 s  .  c o  m*/
 * @return A reader object giving access to the translated
 * source.
 */
public static TranslateResult translateFile(String fName, Reader source, List<CompilationProblem> errors) {
    PositionMap prePosMap = null;
    try {
        String sourceAsString = CharStreams.toString(source);
        BufferedReader in = new BufferedReader(new StringReader(sourceAsString));
        FunctionEndScanner.Result result = new FunctionEndScanner(in).translate();
        if (result instanceof NoChangeResult) {
            in = new BufferedReader(new StringReader(sourceAsString));
        } else if (result instanceof TranslationResult) {
            TranslationResult transResult = (TranslationResult) result;
            in = new BufferedReader(new StringReader(transResult.getText()));
            prePosMap = transResult.getPositionMap();
        } else if (result instanceof ProblemResult) {
            for (TranslationProblem problem : ((ProblemResult) result).getProblems()) {
                errors.add(new CompilationProblem(problem.getLine(), problem.getColumn(),
                        problem.getMessage() + "\n"));
            }
            return null; // terminate early since extraction parser can't work without balanced 'end's
        }
        OffsetTracker offsetTracker = new OffsetTracker(new TextPosition(1, 1));
        List<TranslationProblem> problems = new ArrayList<>();
        String destText = MatlabParser.translate(new ANTLRReaderStream(in), 1, 1, offsetTracker, problems);
        if (problems.isEmpty()) {
            PositionMap posMap = offsetTracker.buildPositionMap();
            if (prePosMap != null) {
                posMap = new CompositePositionMap(prePosMap, posMap);
            }
            return new TranslateResult(new StringReader(destText), posMap);
        }
        for (TranslationProblem problem : problems) {
            errors.add(new CompilationProblem(problem.getLine(), problem.getColumn(),
                    problem.getMessage() + "\n"));
        }
        return null;
    } catch (IOException e) {
        errors.add(new CompilationProblem("Error translating %s\n%s", fName, e.getMessage()));
        return null;
    }
}