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.apache.aurora.scheduler.configuration.executor.ExecutorSettingsLoader.java

/**
 * Reads an executor configuration from a JSON-encoded source.
 *
 * @param input The configuration data source.
 * @return A map of executor configurations.
 * @throws ExecutorConfigException If the input cannot be read or is not properly formatted.
 *///from   w w  w.  ja  v  a 2  s. c  o m
public static Map<String, ExecutorConfig> read(Readable input) throws ExecutorConfigException {
    String configContents;
    try {
        configContents = CharStreams.toString(input);
    } catch (IOException e) {
        throw new ExecutorConfigException(e);
    }

    ObjectMapper mapper = new ObjectMapper().registerModule(new ProtobufModule())
            .setPropertyNamingStrategy(CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES);
    List<Schema> parsed;
    try {
        parsed = mapper.readValue(configContents, new TypeReference<List<Schema>>() {
        });
    } catch (IOException e) {
        throw new ExecutorConfigException(e);
    }

    Map<String, ExecutorConfig> customExecutors;
    try {
        // We apply a placeholder value for the executor ID so that we can construct and validate
        // the protobuf schema.  This allows us to catch many validation errors here rather than
        // later on when launching tasks.
        customExecutors = parsed.stream()
                .collect(GuavaUtils.toImmutableMap(m -> m.executor.getName(),
                        m -> new ExecutorConfig(m.executor.setExecutorId(PLACEHOLDER_EXECUTOR_ID).build(),
                                Optional.fromNullable(m.volumeMounts).or(ImmutableList.of()), m.taskPrefix)));

    } catch (RuntimeException e) {
        throw new ExecutorConfigException(e);
    }

    return customExecutors;
}

From source file:com.github.tmyroadctfig.icloud4j.util.StringResponseHandler.java

@Override
public String handleResponse(HttpResponse response) throws IOException {
    HttpEntity respEntity = response.getEntity();
    if (respEntity != null) {
        Reader reader = new InputStreamReader(respEntity.getContent(), Charset.forName("UTF-8"));
        return CharStreams.toString(reader);
    }//from   www  .j  av  a  2 s.  c o m

    return null;
}

From source file:cl.monsoon.s1next.binding.TextViewBindingAdapter.java

@BindingAdapter("textPath")
public static void loadTextAsset(TextView textView, String textPath) {
    try {//from   ww  w  .j  av a2  s  . c  o  m
        InputStream inputStream = textView.getContext().getAssets().open(textPath);
        textView.setText(CharStreams.toString(new InputStreamReader(inputStream, Charsets.UTF_8)));
    } catch (IOException e) {
        throw new IllegalStateException("Can't find license.", e);
    }
}

From source file:com.mymita.vaadlets.demo.AddonDemoApplication.java

private static void fillEditorWithDefaultXML(final VaadletsBuilder vaadlets) {
    vaadlets.<Panel>getComponent("content").getContent().removeAllComponents();
    final TextField editor = vaadlets.getComponent("editor");
    try {//from  w w  w.j  a  v  a 2s . c  o m
        editor.setValue(CharStreams.toString(new InputStreamReader(
                new ClassPathResource("startingPoint.xml", AddonDemoApplication.class).getInputStream(),
                "UTF-8")));
    } catch (final IOException e) {
    }
}

From source file:org.kurento.test.Shell.java

public static String runAndWaitNoLog(final String... command) {
    Process p;//from  w  w  w  .ja v a 2 s .co m
    try {
        p = new ProcessBuilder(command).redirectErrorStream(true).start();

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

        return output;

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

From source file:TrustEngineExample.java

public void runExample() {

    try {// ww w  .  j a v  a 2s  . c  om
        //create trust manager
        TrustManager trustManager = Factory.createInstance(TrustManager.class);
        //load (from json file) and set trust criteria 
        InputStream is = TrustEngineExample.class.getResourceAsStream("/criteria/criteria1.json");
        String criteria = CharStreams.toString(new InputStreamReader(is));
        trustManager.setGlobalTrustCriteria(criteria);
        is.close();

        //add some descriptions (trust profiles)
        URI service_a = URI.create("http://localhost/services/CITY_TRAFFIC_SERVICE_A");
        URI service_b = URI.create("http://localhost/services/CITY_TRAFFIC_SERVICE_B");
        URI service_d = URI.create("http://localhost/services/CITY_TRAFFIC_SERVICE_D");
        InputStream r1is = TrustEngineExample.class
                .getResourceAsStream("/modelrepo/city_traffic_service_A.ttl");
        trustManager.addResourceDescription(service_a, r1is);
        r1is.close();
        InputStream r2is = TrustEngineExample.class
                .getResourceAsStream("/modelrepo/city_traffic_service_B.ttl");
        trustManager.addResourceDescription(service_b, r2is);
        r2is.close();
        InputStream r3is = TrustEngineExample.class
                .getResourceAsStream("/modelrepo/city_traffic_service_D.ttl");
        trustManager.addResourceDescription(service_d, r3is);
        r3is.close();
        /*
         * SCORING
         */
        //create trust scorer and pass trustManager
        TrustScorer s = new TrustScorer(trustManager);
        //obtain and print trust indexes for resources
        System.out.println(service_a.toASCIIString() + " has trust index value:" + s.apply(service_a));
        System.out.println(service_b.toASCIIString() + " has trust index value:" + s.apply(service_b));
        System.out.println(service_b.toASCIIString() + " has trust index value:" + s.apply(service_d));

        /*
         * FILTERING
         */
        //create trust filer to filter out those not trusted
        TrustFilterByExclusion f = new TrustFilterByExclusion(trustManager);
        //obtain trust indexes for resources
        System.out.println(service_a.toASCIIString() + " is trusted = " + f.apply(service_a));
        System.out.println(service_b.toASCIIString() + " is trusted = " + f.apply(service_b));
        System.out.println(service_b.toASCIIString() + " is trusted = " + f.apply(service_d));

    } catch (IOException e) {
        e.printStackTrace();
    }

}

From source file:com.example.managedvms.websockets.WebsocketServlet.java

public static String getExternalIp() {
    try {/* w ww  .  ja v  a  2  s  .co  m*/
        URL url = new URL(METADATA_NETWORK_INTERFACE_URL);
        HttpURLConnection con = (HttpURLConnection) url.openConnection();
        con.setRequestMethod("GET");
        con.setRequestProperty("Metadata-Flavor", "Google");
        try (InputStreamReader in = new InputStreamReader(con.getInputStream(), "UTF-8")) {
            return CharStreams.toString(in);
        }
    } catch (IOException e) {
        return "localhost";
    }
}

From source file:org.haiku.haikudepotserver.singlepage.markup.EmeddedSinglePageTemplateTag.java

@Override
protected int doStartTagInternal() throws Exception {

    TagWriter tagWriter = new TagWriter(pageContext.getOut());

    pageContext.getOut().newLine();//from w  w  w.  j  a v a  2 s .c  om

    tagWriter.startTag("script");
    tagWriter.writeAttribute("type", "text/ng-template");
    tagWriter.writeAttribute("id", template);

    ServletContext servletContext = getRequestContext().getWebApplicationContext().getServletContext();

    try (InputStream inputStream = servletContext.getResourceAsStream(template)) {
        String templateString = CharStreams.toString(new InputStreamReader(inputStream, Charsets.UTF_8));
        tagWriter.appendValue(templateString);
    }

    tagWriter.endTag();

    pageContext.getOut().newLine();

    return SKIP_BODY;
}

From source file:com.eightkdata.mongowp.bson.netty.MongoBsonUtils.java

public static BsonValue read(InputStream is) throws IOException {
    String allText = CharStreams.toString(new BufferedReader(new InputStreamReader(is, Charsets.UTF_8)));
    JsonReader reader = new JsonReader(allText);

    DecoderContext context = DecoderContext.builder().build();

    return BSON_CODEC.decode(reader, context);
}

From source file:org.eclipse.packagedrone.repo.manage.system.internal.SystemServiceImpl.java

private static String discoverHostname() {
    String hostname = System.getenv("HOSTNAME");

    if (hostname == null) {
        hostname = System.getenv("COMPUTERNAME");
    }/*from w w  w. j  av  a 2s . c om*/

    if (hostname == null) {
        try (Reader reader = new FileReader("/etc/hostname")) {
            hostname = CharStreams.toString(reader).trim();
        } catch (final Exception e) {
        }
    }

    if (hostname == null) {
        try {
            hostname = InetAddress.getLocalHost().getHostName();
        } catch (final UnknownHostException e) {
        }
    }

    if (hostname == null) {
        // last chance
        hostname = "localhost";
    }

    return hostname;
}