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

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

Introduction

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

Prototype

public static String toString(URL url, Charset charset) throws IOException 

Source Link

Document

Reads all characters from a URL into a String , using the given character set.

Usage

From source file:com.dgraf.nashornplayground.Performance.java

public Performance() {
    super();//from   w  ww  .ja  v a  2s.  c om
    runner = new nashornScriptRunner();
    scriptset = new nashornScriptset();
    try {
        scriptset.setName("Performance");
        URL url = Resources.getResource("com/dgraf/nashornplayground/examples/performance.js");
        String script = Resources.toString(url, Charsets.UTF_8);
        scriptset.setScript(script);
    } catch (IOException ex) {
        Logger.getLogger(HelloWorld.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:org.openhab.binding.netatmo.internal.weather.GetStationsDataRequestStub.java

private GetStationsDataRequestStub(final String response) throws Exception {
    super(ACCESS_TOKEN);

    final URL resource = getClass().getResource(response);

    if (resource == null) {
        throw new IOException("Resource '" + response + "' not found!");
    }//from  w w  w.ja  v  a 2s  . c o m

    this.response = Resources.toString(resource, Charsets.UTF_8);
}

From source file:com.dgraf.nashornplayground.HelloWorld.java

public HelloWorld() {
    super();//  ww w  .ja v  a2 s  .c  o  m
    runner = new nashornScriptRunner();
    scriptset = new nashornScriptset();
    try {
        scriptset.setName("Hello World");
        URL url = Resources.getResource("com/dgraf/nashornplayground/examples/helloworld.js");
        String script = Resources.toString(url, Charsets.UTF_8);
        scriptset.setScript(script);
    } catch (IOException ex) {
        Logger.getLogger(HelloWorld.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:org.openhab.binding.netatmo.internal.camera.GetHomeDataRequestStub.java

GetHomeDataRequestStub(final String response) throws Exception {
    super(ACCESS_TOKEN);

    final URL resource = getClass().getResource(response);

    if (resource == null) {
        throw new IOException("Resource '" + response + "' not found!");
    }/*  ww  w.  j av a2  s .co m*/

    this.response = Resources.toString(resource, Charsets.UTF_8);
}

From source file:com.fractal.facebooksentiment.processor.FacebookSentimentCalculator.java

private FacebookSentimentCalculator() {
    commentSentimentMap = Maps.newTreeMap();
    afinnSentimentMap = Maps.newTreeMap();

    try {/*from w w w .  j  a v a 2  s . c  om*/
        final URL url = Resources.getResource(Constants.AFINN_SENTIMENT_FILE_NAME);
        final String text = Resources.toString(url, Charsets.UTF_8);
        final Iterable<String> lineSplit = Splitter.on("\n").trimResults().omitEmptyStrings().split(text);
        List<String> tabSplit;
        for (final String str : lineSplit) {
            tabSplit = Lists.newArrayList(Splitter.on("\t").trimResults().omitEmptyStrings().split(str));
            afinnSentimentMap.put(tabSplit.get(0), Integer.parseInt(tabSplit.get(1)));
        }
    } catch (final IOException ioException) {
        ioException.printStackTrace();
        // Should not occur. If it occurs, we cant continue. So, exiting at this point itself.
        // System.exit(1);
    }
}

From source file:com.facebook.buck.core.module.impl.BuckModuleJarHashProvider.java

/** @return the hash of a jar that contains a Buck module. */
public String getModuleHash(PluginWrapper modulePluginWrapper) {
    URL binaryHashLocation = modulePluginWrapper.getPluginClassLoader()
            .getResource(MODULE_BINARY_HASH_LOCATION);

    if (binaryHashLocation == null) {
        throw new IllegalStateException(createFailureMessage(modulePluginWrapper));
    }//from  www.ja  v  a 2  s . c  om

    try {
        return Resources.toString(binaryHashLocation, Charset.defaultCharset());
    } catch (IOException e) {
        throw new RuntimeException(createFailureMessage(modulePluginWrapper), e);
    }
}

From source file:com.microsoft.kafkaavailability.PropertiesManager.java

/***
 *
 * @param propFileName json file containing properties
 * @param typeParameterClass The class object associated with the type T
 * @throws IOException if property file is not found in classpath
 *//*www.  jav  a 2 s  . c om*/
public PropertiesManager(String propFileName, Class<T> typeParameterClass) throws IOException {
    this.m_propFileName = propFileName;
    m_typeParameterClass = typeParameterClass;
    Gson gson = new Gson();
    URL url = Thread.currentThread().getContextClassLoader().getResource(propFileName);

    if (url != null) {
        String text = Resources.toString(url, Charsets.UTF_8);
        m_prop = gson.fromJson(text, m_typeParameterClass);
    } else {
        throw new FileNotFoundException("property file '" + propFileName + "' not found in the classpath");
    }
}

From source file:com.cognifide.aet.common.RedirectWriter.java

private String getContent(String reportUrl) throws IOException {
    String result;/*from w ww  .  j  a v  a2  s  . c  o m*/
    String template = Resources.toString(Resources.getResource(getClass(), TEMPLATE_RESOURCE_PATH),
            Charsets.UTF_8);
    result = template.replaceAll(REPORT_URL_PLACEHOLDER, reportUrl);
    return result;
}

From source file:org.graylog2.rest.GraylogErrorPageGenerator.java

@Inject
public GraylogErrorPageGenerator(Engine templateEngine) throws IOException {
    this(Resources.toString(Resources.getResource("error.html.template"), StandardCharsets.UTF_8),
            templateEngine);/*from   w  ww.jav a  2s .  co  m*/
}

From source file:fr.rjoakim.app.checklink.utils.FileResource.java

@Override
public String getContent(String filename) throws FileResourceServiceException {
    if (Strings.isNullOrEmpty(filename)) {
        throw new IllegalArgumentException("filename argument is required.");
    }/*from   w  w  w  .  j  a  v a  2  s.com*/

    try {
        URL url = Resources.getResource(filename);
        return Resources.toString(url, Charsets.UTF_8);
    } catch (IOException e) {
        throw new FileResourceServiceException(e.getMessage(), e);
    }
}