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

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

Introduction

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

Prototype

public static <T> T readLines(URL url, Charset charset, LineProcessor<T> callback) throws IOException 

Source Link

Document

Streams lines from a URL, stopping when our callback returns false, or we have read all of the lines.

Usage

From source file:com.marvinformatics.querydsl.Keywords.java

private static Set<String> readLines(String path) {
    try {//from  w  w  w  .  j a  v  a 2 s.co m
        return copyOf(Resources.readLines(Keywords.class.getResource("/keywords/" + path), Charsets.UTF_8,
                new CommentDiscardingLineProcessor()));
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.eclipse.scada.protocol.utils.BufferLoader.java

public static List<IoBuffer> loadBuffersFromResource(final Class<?> clazz, final String resourceName)
        throws IOException {
    logger.debug("Loading buffer - {}", resourceName);

    final URL url = Resources.getResource(clazz, resourceName);

    return Resources.readLines(url, Charset.forName("UTF-8"), new LineProcessor<List<IoBuffer>>() {

        private final List<IoBuffer> result = new LinkedList<>();

        private IoBuffer buffer = null;

        protected void pushBuffer() {
            if (this.buffer == null) {
                return;
            }/* w ww  . j  av a 2  s .  c o m*/

            this.buffer.flip();
            this.result.add(this.buffer);

            this.buffer = null;
        }

        @Override
        public boolean processLine(String line) throws IOException {
            line = line.replaceAll("#.*", ""); // clear comments

            if (line.isEmpty()) {
                pushBuffer();
                return true;
            }

            final String[] toks = line.split("\\s+");

            if (toks.length <= 0) {
                pushBuffer();
                return true;
            }

            if (this.buffer == null) {
                // start a new buffer
                this.buffer = IoBuffer.allocate(0);
                this.buffer.setAutoExpand(true);
            }

            for (final String tok : toks) {
                this.buffer.put(Byte.parseByte(tok, 16));
            }
            return true;
        }

        @Override
        public List<IoBuffer> getResult() {
            pushBuffer(); // last chance to add something
            return this.result;
        }
    });
}

From source file:com.google.gapid.glviewer.ShaderSource.java

public static Shader loadShader(URL resource) {
    String version = isAtLeastVersion(3, 2) ? VERSION_150 : VERSION_130;
    try {/*  ww w.  ja  va2 s  .  co  m*/
        Source source = Resources.readLines(resource, Charsets.US_ASCII, new LineProcessor<Source>() {
            private static final int MODE_COMMON = 0;
            private static final int MODE_VERTEX = 1;
            private static final int MODE_FRAGMENT = 2;
            private static final String VERTEX_PREAMBLE = "#define varying out\n";
            private static final String FRAGMENT_PREAMBLE = "#define varying in\n";

            private final StringBuilder vertexSource = new StringBuilder(version + VERTEX_PREAMBLE);
            private final StringBuilder fragmentSource = new StringBuilder(version + FRAGMENT_PREAMBLE);

            private int mode = MODE_COMMON;

            @Override
            public boolean processLine(String line) throws IOException {
                line = line.trim();
                if ("//! COMMON".equals(line)) {
                    mode = MODE_COMMON;
                } else if ("//! VERTEX".equals(line)) {
                    mode = MODE_VERTEX;
                } else if ("//! FRAGMENT".equals(line)) {
                    mode = MODE_FRAGMENT;
                } else if (!line.startsWith("//")) {
                    switch (mode) {
                    case MODE_COMMON:
                        vertexSource.append(line).append('\n');
                        fragmentSource.append(line).append('\n');
                        break;
                    case MODE_VERTEX:
                        vertexSource.append(line).append('\n');
                        break;
                    case MODE_FRAGMENT:
                        fragmentSource.append(line).append('\n');
                        break;
                    }
                }
                return true;
            }

            @Override
            public Source getResult() {
                return new Source(vertexSource.toString(), fragmentSource.toString());
            }
        });

        Shader shader = new Shader();
        if (!shader.link(source.vertex, source.fragment)) {
            shader.delete();
            shader = null;
        }
        return shader;
    } catch (IOException e) {
        LOG.log(WARNING, "Failed to load shader source", e);
        return null;
    }
}

From source file:io.scigraph.lucene.SynonymMapSupplier.java

@Override
public SynonymMap get() {
    try {//  ww w.j  a v  a  2 s  .  c  o  m
        return Resources.readLines(Resources.getResource("lemmatization.txt"), Charsets.UTF_8,
                new LineProcessor<SynonymMap>() {

                    SynonymMap.Builder builder = new SynonymMap.Builder(true);

                    @Override
                    public boolean processLine(String line) throws IOException {
                        List<String> synonyms = newArrayList(Splitter.on(',').trimResults().split(line));
                        for (String term : synonyms) {
                            for (String synonym : synonyms) {
                                if (!term.equals(synonym)) {
                                    builder.add(new CharsRef(term), new CharsRef(synonym), true);
                                }
                            }
                        }
                        return true;
                    }

                    @Override
                    public SynonymMap getResult() {
                        try {
                            return builder.build();
                        } catch (IOException e) {
                            e.printStackTrace();
                            return null;
                        }
                    }
                });
    } catch (Exception e) {
        logger.log(Level.WARNING, "Failed to build synonym map", e);
        return null;
    }
}

From source file:org.fabrician.enabler.util.ExecCmdProcessInjector.java

public ExecCmdProcessInjector(DockerContainer enabler, URL cmdUrl, long cmdInjectionDelay) throws IOException {
    this.enabler = enabler;
    this.cmdInjectionDelay = cmdInjectionDelay;
    this.cmds = Resources.readLines(cmdUrl, Charsets.UTF_8, new ExecCmdLineProcessor());
}

From source file:us.eharning.atomun.mnemonic.spi.electrum.v2.CJKCleanupUtility.java

private static RangeSet<Integer> buildRanges() {
    LineProcessor<RangeSet<Integer>> lineProcess = new LineProcessor<RangeSet<Integer>>() {
        private final RangeSet<Integer> resultBuilder = TreeRangeSet.create();

        @Override/*w  ww  .  j a va2  s . c o m*/
        public boolean processLine(@Nonnull String line) throws IOException {
            /* Skip comments and empty lines */
            int commentIndex = line.indexOf('#');
            if (commentIndex >= 0) {
                line = line.substring(0, commentIndex);
            }
            line = CharMatcher.WHITESPACE.trimFrom(line);
            if (line.isEmpty()) {
                return true;
            }
            /* NOTE: Assuming 0xHEX-0xHEX notation */
            int splitMarker = line.indexOf('-');
            assert splitMarker >= 0;
            int start = Integer.parseInt(line.substring(2, splitMarker), 16);
            int stop = Integer.parseInt(line.substring(splitMarker + 3), 16);
            resultBuilder.add(Range.closed(start, stop));
            return true;
        }

        @Override
        public RangeSet<Integer> getResult() {
            return ImmutableRangeSet.copyOf(resultBuilder);
        }
    };
    try {
        return Resources.readLines(MnemonicUtility.class.getResource("cjk_ranges.dat"), Charsets.UTF_8,
                lineProcess);
    } catch (IOException e) {
        throw new Error(e);
    }
}

From source file:org.netbeans.modules.android.grammars.LayoutClassesParser.java

WidgetData load() {
    try {//from w w w.j  a  v a  2  s . c o m
        WidgetData clzDescriptors = Resources.readLines(url, Charsets.ISO_8859_1, new LineProcessorImpl());
        return clzDescriptors;
    } catch (IOException ex) {
        LOG.log(Level.INFO, null, ex);
        return new WidgetData(Collections.<LayoutElementType, Collection<UIClassDescriptor>>emptyMap(),
                Collections.<UIClassDescriptor>emptySet());
    }
}

From source file:com.zz.langchecker.LangSwitcherTokenizer.java

LangSwitcherTokenizer(LangChecker langChecker, int minTokenLength) {
    this.langChecker = langChecker;

    this.minTokenLength = minTokenLength;

    try {/*from   w w  w  . j a  va 2 s . c  o m*/
        this.exceptions = Resources.readLines(this.getClass().getResource("exceptions.csv"), Charsets.UTF_8,
                new ExceptionsLineProcessor());
    } catch (IOException e) {
        throw Throwables.propagate(e);
    }
}

From source file:net.minecraftforge.fml.common.asm.transformers.MarkerTransformer.java

private void readMapFile(String rulesFile) throws IOException {
    File file = new File(rulesFile);
    URL rulesResource;//from ww w.j  a  v  a  2s .  com
    if (file.exists()) {
        rulesResource = file.toURI().toURL();
    } else {
        rulesResource = Resources.getResource(rulesFile);
    }
    Resources.readLines(rulesResource, Charsets.UTF_8, new LineProcessor<Void>() {
        @Override
        public Void getResult() {
            return null;
        }

        @Override
        public boolean processLine(String input) throws IOException {
            String line = Iterables.getFirst(Splitter.on('#').limit(2).split(input), "").trim();
            if (line.length() == 0) {
                return true;
            }
            List<String> parts = Lists.newArrayList(Splitter.on(" ").trimResults().split(line));
            if (parts.size() != 2) {
                throw new RuntimeException("Invalid config file line " + input);
            }
            List<String> markerInterfaces = Lists
                    .newArrayList(Splitter.on(",").trimResults().split(parts.get(1)));
            for (String marker : markerInterfaces) {
                markers.put(parts.get(0), marker);
            }
            return true;
        }
    });
}

From source file:com.mapr.synth.distributions.WordGenerator.java

public WordGenerator(String seed, String others) {
    // read the common words
    if (seed != null) {
        try {// w w  w.ja  va  2  s . c  o m
            Resources.readLines(Resources.getResource(seed), Charsets.UTF_8, new LineProcessor<Object>() {
                private boolean header = true;
                private final Splitter onTabs = Splitter.on("\t");

                public boolean processLine(String s) throws IOException {
                    if (!s.startsWith("#")) {
                        if (!header) {
                            Iterator<String> fields = onTabs.split(s).iterator();
                            fields.next();
                            String word = fields.next();
                            words.add(word);
                            int count = (int) Math.rint(Double.parseDouble(fields.next()));
                            baseWeights.put(word, count);
                        } else {
                            header = false;
                        }
                    }
                    return true;
                }

                public Object getResult() {
                    return null;
                }
            });
        } catch (IOException e) {
            log.error("Can't read resource \"{}\", will continue without realistic words", seed);
        }
    }

    try {
        wordReader = new BufferedReader(
                Resources.newReaderSupplier(Resources.getResource(others), Charsets.UTF_8).getInput());
    } catch (IOException e) {
        log.error("Can't read resource \"{}\", will continue without realistic words", others);
        wordReader = null;
    }

}