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

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

Introduction

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

Prototype

public static CharSource asCharSource(URL url, Charset charset) 

Source Link

Document

Returns a CharSource that reads from the given URL using the given character set.

Usage

From source file:org.gradle.configuration.DefaultImportsReader.java

public DefaultImportsReader() {
    try {//from ww w  . j ava  2s  .  co m
        URL url = getClass().getResource(RESOURCE);
        if (url == null) {
            throw new IllegalStateException("Could not load default imports resource: " + RESOURCE);
        }
        this.importPackages = Resources.asCharSource(url, Charsets.UTF_8)
                .readLines(new LineProcessor<String[]>() {
                    private final List<String> packages = Lists.newLinkedList();

                    @Override
                    public boolean processLine(@SuppressWarnings("NullableProblems") String line)
                            throws IOException {
                        packages.add(line.substring(7, line.length() - 2));
                        return true;
                    }

                    @Override
                    public String[] getResult() {
                        return packages.toArray(new String[packages.size()]);
                    }
                });
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
}

From source file:org.anarres.dblx.core.model.ModelLoader.java

@Nonnull
public Model load() throws IOException {
    Model model = new Model(modelName);

    Splitter splitter = Splitter.on(CharMatcher.BREAKING_WHITESPACE);

    NODES: {/*from  w ww . ja  v  a  2  s .co m*/
        URL url = Resources.getResource("models/" + modelName + "/nodes.csv");
        CharSource source = Resources.asCharSource(url, StandardCharsets.UTF_8);
        try (Reader in = source.openBufferedStream()) {
            CSVReader reader = newSVReader(in, '\t', 1);
            for (String[] line : reader) {
                String name = line[0];
                long x = (long) LengthUnit.INCH.toMillimetres(Double.parseDouble(line[1]));
                long y = (long) LengthUnit.INCH.toMillimetres(Double.parseDouble(line[2]));
                long z = (long) LengthUnit.INCH.toMillimetres(Double.parseDouble(line[3]));
                List<String> tags = splitter.splitToList(line[4]);
                model.addNode(new Node(name, x, y, z, tags));
            }
        }
    }

    BARS: {
        URL url = Resources.getResource("models/" + modelName + "/bars.csv");
        CharSource source = Resources.asCharSource(url, StandardCharsets.UTF_8);
        try (Reader in = source.openBufferedStream()) {
            CSVReader reader = newSVReader(in, '\t', 1);
            for (String[] line : reader) {
                List<String> tags = splitter.splitToList(line[2]);
                Bar bar = new Bar(line[0], line[1], tags);
                model.addEdge(bar);
            }
        }
    }

    return model;
}

From source file:com.teradata.tpcds.distribution.DistributionUtils.java

protected static Iterator<List<String>> getDistributionIterator(String filename) {
    URL resource = Resources.getResource(DistributionUtils.class, filename);
    checkState(resource != null, "Distribution file '%s' not found", filename);
    try {// w  w w .j av a 2s .  c om
        // get an iterator that iterates over lists of the colon separated values from the distribution files
        return transform(filter(
                Resources.asCharSource(resource, StandardCharsets.ISO_8859_1).readLines().iterator(), line -> {
                    line = line.trim();
                    return !line.isEmpty() && !line.startsWith("--");
                }), line -> ImmutableList
                        .copyOf(Splitter.on(Pattern.compile("(?<!\\\\):")).trimResults().split(line)));
    } catch (IOException e) {
        throw Throwables.propagate(e);
    }
}

From source file:com.lyndir.masterpassword.GUI.java

private static void checkUpdate() {
    try {/*w  w w. jav a 2  s . com*/
        Enumeration<URL> manifestURLs = Thread.currentThread().getContextClassLoader()
                .getResources(JarFile.MANIFEST_NAME);
        while (manifestURLs.hasMoreElements()) {
            InputStream manifestStream = manifestURLs.nextElement().openStream();
            Attributes attributes = new Manifest(manifestStream).getMainAttributes();
            if (!GUI.class.getCanonicalName().equals(attributes.getValue(Attributes.Name.MAIN_CLASS)))
                continue;

            String manifestRevision = attributes.getValue(Attributes.Name.IMPLEMENTATION_VERSION);
            String upstreamRevisionURL = "http://masterpasswordapp.com/masterpassword-gui.jar.rev";
            CharSource upstream = Resources.asCharSource(URI.create(upstreamRevisionURL).toURL(),
                    Charsets.UTF_8);
            String upstreamRevision = upstream.readFirstLine();
            logger.inf("Local Revision:    <%s>", manifestRevision);
            logger.inf("Upstream Revision: <%s>", upstreamRevision);
            if (!manifestRevision.equalsIgnoreCase(upstreamRevision)) {
                logger.wrn("You are not running the current official version.  Please update from:\n"
                        + "http://masterpasswordapp.com/masterpassword-gui.jar");
                JOptionPane.showMessageDialog(null,
                        "A new version of Master Password is available.\n"
                                + "Please download the latest version from http://masterpasswordapp.com",
                        "Update Available", JOptionPane.WARNING_MESSAGE);
            }
        }
    } catch (IOException e) {
        logger.wrn(e, "Couldn't check for version update.");
    }
}

From source file:com.lyndir.masterpassword.gui.GUI.java

private static void checkUpdate() {
    try {/*w w  w  . jav a 2  s . c  o m*/
        Enumeration<URL> manifestURLs = Thread.currentThread().getContextClassLoader()
                .getResources(JarFile.MANIFEST_NAME);
        while (manifestURLs.hasMoreElements()) {
            InputStream manifestStream = manifestURLs.nextElement().openStream();
            Attributes attributes = new Manifest(manifestStream).getMainAttributes();
            if (!GUI.class.getCanonicalName().equals(attributes.getValue(Attributes.Name.MAIN_CLASS)))
                continue;

            String manifestRevision = attributes.getValue(Attributes.Name.IMPLEMENTATION_VERSION);
            String upstreamRevisionURL = "http://masterpasswordapp.com/masterpassword-gui.jar.rev";
            CharSource upstream = Resources.asCharSource(URI.create(upstreamRevisionURL).toURL(),
                    Charsets.UTF_8);
            String upstreamRevision = upstream.readFirstLine();
            logger.inf("Local Revision:    <%s>", manifestRevision);
            logger.inf("Upstream Revision: <%s>", upstreamRevision);
            if (manifestRevision != null && !manifestRevision.equalsIgnoreCase(upstreamRevision)) {
                logger.wrn("You are not running the current official version.  Please update from:\n"
                        + "http://masterpasswordapp.com/masterpassword-gui.jar");
                JOptionPane.showMessageDialog(null,
                        "A new version of Master Password is available.\n"
                                + "Please download the latest version from http://masterpasswordapp.com",
                        "Update Available", JOptionPane.WARNING_MESSAGE);
            }
        }
    } catch (IOException e) {
        logger.wrn(e, "Couldn't check for version update.");
    }
}

From source file:org.gradle.buildinit.plugins.internal.SimpleTemplateOperation.java

@Override
public void generate() {
    try {/*from   www. j a  v a  2  s  .co m*/
        target.getParentFile().mkdirs();
        SimpleTemplateEngine templateEngine = new SimpleTemplateEngine();
        String templateText = Resources.asCharSource(templateURL, CharsetToolkit.getDefaultSystemCharset())
                .read();
        Template template = templateEngine.createTemplate(templateText);
        Writer writer = Files.asCharSink(target, Charsets.UTF_8).openStream();
        try {
            template.make(bindings).writeTo(writer);
        } finally {
            writer.close();
        }
    } catch (Exception ex) {
        throw new GradleException("Could not generate file " + target + ".", ex);
    }
}

From source file:com.facebook.swift.thrift.generator.SwiftDocumentContext.java

public void build() throws IOException {
    this.document = ThriftIdlParser.parseThriftIdl(Resources.asCharSource(thriftUri.toURL(), Charsets.UTF_8));
    this.typeConverter = new TypeToJavaConverter(typeRegistry, typedefRegistry, codeModel, namespace,
            getJavaPackage());//from   w w  w . ja va  2  s .c  om
    this.constantRenderer = new ConstantRenderer(typeConverter, namespace, typeRegistry, typedefRegistry);
}

From source file:org.isisaddons.module.fakedata.dom.IsisClobs.java

private static Clob asClob(final String fileName) {
    final URL resource = Resources.getResource(IsisBlobs.class, "clobs/" + fileName);
    final CharSource charSource = Resources.asCharSource(resource, Charsets.US_ASCII);
    final String chars;
    try {/*from w ww . jav  a2  s . com*/
        chars = charSource.read();
        return new Clob(fileName, mimeTypeFor(fileName), chars);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:br.com.objectos.way.core.code.jdt.AstReaderResource.java

@Override
void setSource(ASTParser parser) throws Exception {
    String name = resourceName.substring(resourceName.lastIndexOf('/'));
    parser.setUnitName(name);/*  w  w  w .j a va2 s  .  co  m*/

    URL url = Resources.getResource(getClass(), resourceName);
    Reader reader = Resources.asCharSource(url, Charsets.UTF_8).openStream();
    String source = CharStreams.toString(reader);
    char[] chars = source.toCharArray();
    parser.setSource(chars);
}

From source file:com.github.steveash.jg2p.align.InputReader.java

public List<InputRecord> readFromClasspath(String resource) throws IOException {
    return read(Resources.asCharSource(Resources.getResource(resource), Charsets.UTF_8));
}