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:org.kurento.test.utils.Shell.java

public static String runAndWaitNoLog(final String... command) {
    Process p;/*  w  w  w . j a v  a 2 s. c  om*/
    try {
        p = new ProcessBuilder(command).redirectErrorStream(true).start();

        String output = CharStreams.toString(new InputStreamReader(p.getInputStream(), "UTF-8"));

        p.destroy();

        return output;

    } catch (IOException e) {
        throw new KurentoException("Exception executing command on the shell: " + Arrays.toString(command), e);
    }
}

From source file:io.macgyver.ssh.LineResponseCallback.java

@Override
public Iterable<String> handle(Command cmd) throws IOException {
    InputStream inputStream = cmd.getInputStream();
    ArrayList<String> list = Lists.newArrayList();
    String line = null;//w w w .  j a  v a  2s. c o m
    BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
    while ((line = br.readLine()) != null) {
        list.add(line);
    }

    cmd.join(5, TimeUnit.SECONDS);

    if (cmd.getExitStatus() != 0) {
        String x = CharStreams.toString(new InputStreamReader(cmd.getErrorStream(), "UTF-8"));
        throw new SshException(cmd.getExitStatus(), x);

    }
    return list;
}

From source file:com.google.dart.tools.debug.core.sourcemaps.SourceMap.java

public static SourceMap createFrom(IFile file) throws IOException, CoreException {
    Reader reader = new InputStreamReader(file.getContents(), file.getCharset());

    try {/*from ww w. ja  v  a2  s  . co  m*/
        String contents = CharStreams.toString(reader);

        return createFrom(file.getFullPath(), contents);
    } catch (JSONException e) {
        throw new IOException(e);
    } finally {
        reader.close();
    }
}

From source file:ext.deployit.community.cli.mustachify.transform.TextEntryAsStringTransformer.java

@Override
protected void transform(@Nonnull Reader entryContentStream, @Nonnull Writer newEntryContentStream)
        throws IOException {
    newEntryContentStream.write(transform(CharStreams.toString(entryContentStream)));
}

From source file:org.jetbrains.jet.objc.ObjCTestUtil.java

@NotNull
@SuppressWarnings("IOResourceOpenedButNotSafelyClosed")
public static String runProcess(@NotNull String command) {
    try {//from   ww  w  .  j  a  v a  2  s .c o m
        Process process = Runtime.getRuntime().exec(command);
        process.waitFor();

        InputStreamReader output = new InputStreamReader(process.getInputStream());
        String result = CharStreams.toString(output);
        Closeables.closeQuietly(output);

        InputStreamReader errorStream = new InputStreamReader(process.getErrorStream());
        String error = CharStreams.toString(errorStream);
        Closeables.closeQuietly(errorStream);
        System.err.print(error);

        int exitCode = process.exitValue();
        assert exitCode == 0 : "Process exited with code " + exitCode + ", result: " + result;

        return result;
    } catch (Exception e) {
        throw ExceptionUtils.rethrow(e);
    }
}

From source file:com.xebialabs.overthere.util.OverthereUtils.java

/**
 * Reads the contents of an {@link OverthereFile} into a string.
 *
 * @param from        the file to read from.
 * @param charsetName the {@link java.nio.charset.Charset charset} to use.
 * @returns the string.//from   w ww. j a v  a  2s  .c o m
 */
public static String read(final OverthereFile from, final String charsetName) {
    try {
        return CharStreams.toString(new InputSupplier<Reader>() {
            @Override
            public Reader getInput() throws IOException {
                return new InputStreamReader(from.getInputStream(), charsetName);
            }
        });
    } catch (IOException exc) {
        throw new RuntimeException(exc);
    }
}

From source file:com.google.cloud.tools.eclipse.appengine.validation.ValidationUtils.java

static String convertStreamToString(InputStream is, String charset) throws IOException {
    String result = CharStreams.toString(new InputStreamReader(is, charset));
    return result;
}

From source file:org.obiba.git.command.FetchBlobCommand.java

@Override
public String execute(Git git) {
    try {//from w  w w.  j  av  a 2 s  . c  o  m
        ReadFileCommand readFileCommand = new ReadFileCommand.Builder(getRepositoryPath(), path)
                .commitId(commitId).tag(tag).build();
        InputStream inputStream = readFileCommand.execute(git);
        return CharStreams.toString(new InputStreamReader(inputStream, getEncoding()));
    } catch (IOException e) {
        throw new GitException(e);
    }
}

From source file:dev.meng.wikidata.util.string.StringUtils.java

public static String inputStreamToString(InputStream input, String encoding) throws StringConvertionException {
    try {/*from   ww  w . j  a v a  2s. co  m*/
        InputStreamReader reader = new InputStreamReader(input, encoding);
        String result = CharStreams.toString(reader);
        reader.close();
        return result;
    } catch (IOException ex) {
        throw new StringConvertionException(ex);
    }
}

From source file:net.thecodersbreakfast.restangular.server.rest.resource.StaticResource.java

@GET
@Path("{path : .*}")
public Response staticResource(@PathParam("path") String path) throws IOException {
    File file = file(path);/*from w  ww  . jav a 2  s . co m*/
    if (file != null) {
        String mimeType = mimeType(file);
        return Response.ok(file, mimeType).build();
    } else {
        // manage the case when the resource is inside a jar
        try (InputStream is = is(path)) {
            String mimeType = mimeType(path);
            return Response.ok(CharStreams.toString(new InputStreamReader(is, Charsets.UTF_8)), mimeType)
                    .build();
        }
    }
}