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.eclipse.scada.configuration.item.parser.ScriptResourceImpl.java

@Override
protected void doLoad(final InputStream inputStream, final Map<?, ?> options) throws IOException {
    try {//from  www. j a  v a  2 s  .  c  o m
        final String data = CharStreams.toString(new InputStreamReader(inputStream, this.defaultCharacterSet));
        getContents().add(parse(data));
    } finally {
        inputStream.close();
    }
}

From source file:com.github.sdbg.debug.core.internal.sourcemaps.SourceMap.java

public static SourceMap createFrom(IStorage storage) throws IOException, CoreException {
    Reader reader;//  ww  w  . j av a 2s .  c o  m
    if (storage instanceof IFile) {
        reader = new InputStreamReader(storage.getContents(), ((IFile) storage).getCharset());
    } else {
        reader = new InputStreamReader(storage.getContents());
    }

    try {
        String contents = CharStreams.toString(reader);

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

From source file:google.registry.tools.ExecuteEppCommand.java

@Override
protected void initMutatingEppToolCommand() throws IOException {
    if (mainParameters.isEmpty()) {
        addXmlCommand(clientId, CharStreams.toString(new InputStreamReader(stdin, UTF_8)));
    } else {//from w  ww.j a  va 2s .  c  o  m
        for (String command : mainParameters) {
            addXmlCommand(clientId, Files.toString(new File(command), UTF_8));
        }
    }
}

From source file:org.opennms.newts.cassandra.NewtsInstance.java

public static CQLDataSet getDataSet(String keyspace, int replicationFactor) {
    try {//  w  ww .j  av  a 2s .  c om
        //  Concatenate the schema strings
        String schemasString = "";
        for (Schema schema : schemaLoader) {
            schemasString += CharStreams.toString(new InputStreamReader(schema.getInputStream()));
        }

        // Replace the placeholders
        schemasString = schemasString.replace(KEYSPACE_PLACEHOLDER, keyspace);
        schemasString = schemasString.replace(REPLICATION_FACTOR_PLACEHOLDER,
                Integer.toString(replicationFactor));

        // Split the resulting script back into lines
        String lines[] = schemasString.split("\\r?\\n");

        // Remove duplicate CREATE KEYSPACE statements;
        StringBuffer sb = new StringBuffer();
        boolean foundCreateKeyspace = false;
        boolean skipNextLine = false;
        for (String line : lines) {
            if (line.startsWith("CREATE KEYSPACE")) {
                if (!foundCreateKeyspace) {
                    foundCreateKeyspace = true;
                    sb.append(line);
                    sb.append("\n");
                } else {
                    skipNextLine = true;
                }
            } else if (skipNextLine) {
                skipNextLine = false;
            } else {
                sb.append(line);
                sb.append("\n");
            }
        }

        // Write the results to disk
        File schemaFile = File.createTempFile("schema-", ".cql", new File("target"));
        schemaFile.deleteOnExit();
        Files.write(sb.toString(), schemaFile, Charsets.UTF_8);
        return new FileCQLDataSet(schemaFile.getAbsolutePath(), false, true, keyspace);
    } catch (IOException e) {
        throw Throwables.propagate(e);
    }
}

From source file:com.nesscomputing.migratory.loader.UTF8StringContentConverter.java

@Override
public String convert(HttpRequest httpClientRequest, HttpResponse httpClientResponse, InputStream inputStream)
        throws IOException {
    final int responseCode = httpClientResponse.getStatusLine().getStatusCode();
    switch (responseCode) {
    case 200:/*from w ww.ja  va  2s. c  o m*/
    case 201:
        final InputStreamReader reader = new InputStreamReader(inputStream, Charsets.UTF_8);

        try {
            return CharStreams.toString(reader);
        } finally {
            Closeables.closeQuietly(reader);
        }

    case 204:
        return "";

    default:
        throw new IOException("Could not load file from " + httpClientRequest.getRequestLine().getUri() + " ("
                + httpClientResponse.getStatusLine().getStatusCode() + ")");
    }
}

From source file:io.apiman.cli.management.ManagementApiUtil.java

private static void httpError(int expectedStatus, Response response) throws CommandException {
    if (null == response) {
        throw new IllegalArgumentException("Response was null");
    }//  w w  w.ja v  a2 s.  c om

    // obtain response body
    String body = null;

    if (null != response.getBody()) {
        try (InputStream errStream = response.getBody().in()) {
            body = CharStreams.toString(new InputStreamReader(errStream));
        } catch (IOException ignored) {
        }
    }

    throw new CommandException("HTTP " + response.getStatus() + " " + response.getReason() + " but expected "
            + expectedStatus + ":\n" + body);
}

From source file:org.mitre.jose.keystore.JWKSetKeyStore.java

private void initializeJwkSet() {

    if (jwkSet == null) {
        if (location != null) {

            if (location.exists() && location.isReadable()) {

                try {
                    // read in the file from disk
                    String s = CharStreams
                            .toString(new InputStreamReader(location.getInputStream(), Charsets.UTF_8));

                    // parse it into a jwkSet object
                    jwkSet = JWKSet.parse(s);
                } catch (IOException e) {
                    throw new IllegalArgumentException("Key Set resource could not be read: " + location);
                } catch (ParseException e) {
                    throw new IllegalArgumentException("Key Set resource could not be parsed: " + location);
                }//from www. j  a  v  a2s .c o m

            } else {
                throw new IllegalArgumentException("Key Set resource could not be read: " + location);
            }

        } else {
            throw new IllegalArgumentException(
                    "Key store must be initialized with at least one of a jwkSet or a location.");
        }
    }
}

From source file:org.b1.pack.standard.writer.CompressedFormatDetector.java

private static void readResource(final URL url, ImmutableSet.Builder<String> builder) throws IOException {
    StringTokenizer tokenizer = new StringTokenizer(CharStreams.toString(new InputSupplier<Reader>() {
        @Override/* ww w.  ja va 2 s.c o  m*/
        public Reader getInput() throws IOException {
            return new InputStreamReader(url.openStream(), Charsets.UTF_8);
        }
    }));
    while (tokenizer.hasMoreTokens()) {
        builder.add(tokenizer.nextToken().toLowerCase());
    }
}

From source file:org.fenixedu.academic.ui.spring.controller.teacher.authorization.CsvService.java

public List<Map<String, String>> readCsvFile(InputStream stream, String separator, String encoding)
        throws IOException {
    try (InputStreamReader isr = new InputStreamReader(stream, Charsets.UTF_8)) {
        String content = CharStreams.toString(isr);
        List<String> lines = Splitter.on(System.lineSeparator()).splitToList(content);
        List<String> header = null;
        List<Map<String, String>> csvContent = new Vector<Map<String, String>>(lines.size());
        for (String line : lines) {
            String[] parts = line.split(separator);
            if (header == null) {
                header = new ArrayList<String>();
                for (String part : parts) {
                    header.add(cleanup(part));
                }/* w ww .j a va2 s .c  o m*/
            } else {
                int index = 0;
                Map<String, String> linemap = new HashMap<String, String>(header.size());
                for (String column : header) {
                    linemap.put(column, access(parts, index++));
                }
                csvContent.add(linemap);
            }
        }
        return csvContent;
    }
}

From source file:ratpack.error.internal.ErrorPageRenderer.java

public ErrorPageRenderer() {
    if (style == null) {
        InputStream resourceAsStream = ErrorPageRenderer.class.getResourceAsStream("error-template-style.css");
        if (resourceAsStream == null) {
            throw new IllegalStateException("Couldn't find style resource");
        }/* w ww. j ava 2 s  . co  m*/

        InputStreamReader reader = new InputStreamReader(resourceAsStream, CharsetUtil.UTF_8);
        try {
            style = CharStreams.toString(reader);
        } catch (IOException e) {
            throw new IllegalStateException("Could not read style stream", e);
        }
    }

    render();
}