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.google.gerrit.server.edit.tree.ChangeFileContentModificationSubject.java

public StringSubject newContent() throws IOException {
    isNotNull();/*ww w  . j a  va  2  s .  co m*/
    RawInput newContent = actual().getNewContent();
    Truth.assertThat(newContent).named("newContent").isNotNull();
    String contentString = CharStreams
            .toString(new InputStreamReader(newContent.getInputStream(), StandardCharsets.UTF_8));
    return Truth.assertThat(contentString).named("newContent");
}

From source file:com.fitbur.core.core.el.mvel.MvelExpressionService.java

private CompiledExpression compile(String name, Reader template) {
    CompiledExpression compiled = null;//w  ww.j  a  v  a 2  s. com

    try {
        compiled = cache.get(name, () -> {
            return (CompiledExpression) MVEL.compileExpression(CharStreams.toString(template));
        });
    } catch (ExecutionException ex) {
    }

    return compiled;
}

From source file:com.facebook.buck.jvm.java.ZipEntryJavaFileObject.java

/**
 * Returns the contents of the {@link ZipEntry} as a string. Ensures that the entry is read at
 * most once./*from   w  w  w . java 2  s .  c  o  m*/
 */
private synchronized String getContentsAsString() {
    if (contents != null) {
        return contents;
    }

    try (InputStream inputStream = zipFile.getInputStream(zipEntry);
            InputStreamReader isr = new InputStreamReader(inputStream, Charsets.UTF_8)) {
        contents = CharStreams.toString(isr);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }

    return contents;
}

From source file:org.caleydo.core.view.opengl.layout2.util.GLGraphicsUtils.java

/**
 * Load and compile vertex and fragment shader
 *
 * @param vertexShader/*from   w ww  .j a v a2s. c o m*/
 * @param fragmentShader
 * @return
 * @throws IOException
 */
public static int loadShader(GL2 gl, InputStream vertexShader, InputStream fragmentShader) throws IOException {
    String vsrc = CharStreams.toString(new InputStreamReader(vertexShader));
    String fsrc = CharStreams.toString(new InputStreamReader(fragmentShader));
    return compileShader(gl, vsrc, fsrc);
}

From source file:com.google.firebase.testing.IntegrationTestUtils.java

public static synchronized String getApiKey() {
    if (apiKey == null) {
        try (InputStream stream = new FileInputStream(IT_API_KEY_PATH)) {
            apiKey = CharStreams.toString(new InputStreamReader(stream)).trim();
        } catch (IOException e) {
            String msg = String.format("Failed to read API key from %s. "
                    + "Integration tests require an API key obtained from a Firebase "
                    + "project. See CONTRIBUTING.md for more details.", IT_API_KEY_PATH);
            throw new RuntimeException(msg, e);
        }/*from w  w w .j ava2s . c om*/
    }
    return apiKey;
}

From source file:cherry.goods.command.CommandLauncherImpl.java

/**
 * ??? (??) ??//  w  ww . ja  va2s  . c  o m
 * 
 * @param command ? ()
 * @return? (??)
 * @throws IOException ? (???)?????????
 * @throws InterruptedException ?????????
 */
@Override
public CommandResult launch(String... command) throws IOException, InterruptedException {
    CommandResult result = new CommandResult();
    Process proc = new ProcessBuilder(command).redirectErrorStream(redirectErrorStream).start();
    try (InputStream in = proc.getErrorStream(); Reader r = new InputStreamReader(in, charset)) {
        result.setStderr(CharStreams.toString(r));
    }
    try (InputStream in = proc.getInputStream(); Reader r = new InputStreamReader(in, charset)) {
        result.setStdout(CharStreams.toString(r));
    }
    int exitValue = proc.waitFor();
    result.setExitValue(exitValue);
    return result;
}

From source file:org.eclipse.che.api.debugger.server.DebuggerActionProvider.java

@Override
public ActionDto readFrom(Class<ActionDto> type, Type genericType, Annotation[] annotations,
        MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream)
        throws IOException, WebApplicationException {
    String json = CharStreams.toString(new BufferedReader(new InputStreamReader(entityStream)));

    JsonParser jsonParser = new JsonParser();
    JsonElement jsonElement = jsonParser.parse(json);
    JsonObject jsonObject = jsonElement.getAsJsonObject();

    if (!jsonObject.has("type")) {
        throw new IOException("Json is broken. There is not type key in json object");
    }/*from  w  ww. j  ava  2 s.  c  om*/

    Action.TYPE actionType = Action.TYPE.valueOf(jsonObject.get("type").getAsString());
    switch (actionType) {
    case RESUME:
        return DtoFactory.getInstance().createDtoFromJson(json, ResumeActionDto.class);
    case START:
        return DtoFactory.getInstance().createDtoFromJson(json, StartActionDto.class);
    case STEP_INTO:
        return DtoFactory.getInstance().createDtoFromJson(json, StepIntoActionDto.class);
    case STEP_OUT:
        return DtoFactory.getInstance().createDtoFromJson(json, StepOutActionDto.class);
    case STEP_OVER:
        return DtoFactory.getInstance().createDtoFromJson(json, StepOverActionDto.class);
    case SUSPEND:
        return DtoFactory.getInstance().createDtoFromJson(json, SuspendActionDto.class);
    default:
        throw new IOException("Can't parse json. Unknown action type: " + actionType);
    }
}

From source file:org.gw4e.eclipse.wizard.convert.JSONPostConversionImpl.java

/**
 * Pretty-fy  the json text so that it displays well in an editor
 * @param monitor//from w  w  w  .  j  a  v a2 s. c  om
 */
private void formatSource(IProgressMonitor monitor) {
    SubMonitor subMonitor = SubMonitor.convert(monitor, 100);
    InputStream in = null;
    Reader reader = null;
    try {
        Gson gson = new GsonBuilder().setPrettyPrinting().create();
        in = convertedFile.getContents();
        reader = new InputStreamReader(in);
        subMonitor.split(10);
        String text = CharStreams.toString(reader);
        JsonParser jp = new JsonParser();
        subMonitor.split(20);
        JsonElement je = jp.parse(text);
        String formatted = gson.toJson(je);
        subMonitor.split(40);
        ResourceManager.createFileDeleteIfExists(convertedFile.getParent().getFullPath().toString(),
                convertedFile.getName(), formatted, subMonitor.split(30));
    } catch (Exception e) {
        ResourceManager.logException(e);
    } finally {
        try {
            if (reader != null)
                reader.close();
        } catch (Exception e) {
        }
        try {
            if (in != null)
                in.close();
        } catch (Exception e) {
        }
        subMonitor.done();
    }
}

From source file:com.lyndir.masterpassword.model.impl.MPFlatUnmarshaller.java

@Nonnull
@Override/*from  w ww. j av a  2 s .  c o m*/
public MPFileUser unmarshall(@Nonnull final File file, @Nullable final char[] masterPassword)
        throws IOException, MPMarshalException, MPIncorrectMasterPasswordException, MPKeyUnavailableException,
        MPAlgorithmException {
    try (Reader reader = new InputStreamReader(new FileInputStream(file), Charsets.UTF_8)) {
        return unmarshall(CharStreams.toString(reader), masterPassword);
    }
}

From source file:de.viadee.maven.doxia.modules.textile.TextileParser.java

/**
 * {@inheritDoc}/*w ww. jav a  2 s .co m*/
 */
@SuppressWarnings("nls")
@Override
public void parse(final Reader reader, final Sink sink) throws ParseException {
    // Check Inputs
    Preconditions.checkNotNull(reader);
    Preconditions.checkNotNull(sink);

    this.getLog().info("Parsing Textile document..");

    // Read content of given markup
    String markupContent;

    try {
        markupContent = CharStreams.toString(reader);
        this.getLog().info("Textile content is: \n" + markupContent);
    } catch (final IOException exception) {
        throw new ParseException("Cannot read input", exception);
    }

    // Parse given markup to HTML
    if (markupContent != null && !markupContent.isEmpty()) {
        final MarkupParser markupParser = new MarkupParser();
        markupParser.setMarkupLanguage(new TextileLanguage());

        final String html = markupParser.parseToHtml(markupContent);
        this.getLog().info("HTML content is: \n" + html);

        try {
            LineNumberReader lnr = new LineNumberReader(new StringReader(markupContent));
            String line = null;

            sink.head();
            while ((line = lnr.readLine()) != null) {
                if (line.startsWith(META_PREFIX) && line.endsWith(META_SUFFIX)) {

                    String inBetween = line.substring(META_PREFIX.length(),
                            line.length() - META_SUFFIX.length());

                    if (inBetween.toLowerCase().startsWith(AUTHOR_ATTRIBUTE)) {
                        sink.author();
                        sink.rawText(inBetween.substring(AUTHOR_ATTRIBUTE.length()));
                        sink.author_();
                    }

                    if (inBetween.toLowerCase().startsWith(TITLE_ATTRIBUTE)) {
                        System.out.println("set title to: " + inBetween.substring(TITLE_ATTRIBUTE.length()));
                        sink.title();
                        sink.rawText(inBetween.substring(TITLE_ATTRIBUTE.length()));
                        sink.title_();
                    }

                    if (inBetween.toLowerCase().startsWith(DATE_ATTRIBUTE)) {
                        sink.date();
                        sink.rawText(inBetween.substring(DATE_ATTRIBUTE.length()));
                        sink.date_();
                    }

                }

            }
            sink.head_();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        sink.rawText(html);

        sink.flush();
    }

    // Finally close the sink.
    sink.close();
}