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

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

Introduction

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

Prototype

@Beta
public <T> T readLines(LineProcessor<T> processor) throws IOException 

Source Link

Document

Reads lines of text from this source, processing each line as it is read using the given LineProcessor processor .

Usage

From source file:com.acme.scramble.ScrambleScorerMain.java

public static void main(String[] args) throws IOException, URISyntaxException {
    ScrambleScorerMain scorerMain = new ScrambleScorerMain(new ScrambleScorer(), new ImmutableList.Builder<>());

    if (args == null || args.length == 1) {
        File file = new File(args[0]);
        if (file.exists()) {
            Files.readLines(file, UTF_8, scorerMain);
            return;
        }/* w ww.  ja va2 s  . co  m*/

        System.err.println("Input file '" + file + "' not found.");
    } else {
        URL resource = Resources.getResource("sample_input.txt");
        CharSource charSource = Resources.asCharSource(resource, UTF_8);
        charSource.readLines(scorerMain);
    }

}

From source file:com.github.steveash.jg2p.seq.SeqInputReader.java

public List<AlignGroup> readInput(CharSource source) throws IOException {
    return source.readLines(new LineProcessor<List<AlignGroup>>() {

        final List<AlignGroup> groups = Lists.newArrayList();
        String lastGroup = "";
        List<Alignment> aligns = Lists.newArrayList();

        @Override/*  w  w  w.  j a  v a  2s.  c  o m*/
        public boolean processLine(String line) throws IOException {
            List<String> fields = caretSplit.splitToList(line);
            if (!lastGroup.equals(fields.get(0))) {
                lastGroup = fields.get(0);
                if (!aligns.isEmpty()) {
                    groups.add(new AlignGroup(aligns));
                }
                aligns.clear();
            }
            Double score = Double.parseDouble(fields.get(1));
            Word input = Word.fromSpaceSeparated(fields.get(2));
            Iterable<String> graphs = pipeSplit.split(fields.get(3));
            Iterable<String> phones = pipeSplit.split(fields.get(4));

            aligns.add(new Alignment(input, Zipper.up(graphs, phones), score));
            return true;
        }

        @Override
        public List<AlignGroup> getResult() {
            if (!aligns.isEmpty()) {
                groups.add(new AlignGroup(aligns));
            }
            return groups;
        }
    });
}

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

public List<InputRecord> read(CharSource source) throws IOException {
    return source.readLines(new LineProcessor<List<InputRecord>>() {
        private final List<InputRecord> recs = Lists.newArrayList();

        @Override//  ww w  .j a v  a 2  s .  co m
        public boolean processLine(String line) throws IOException {
            if (isBlank(line)) {
                return true;
            }
            if (isComment(line)) {
                return true;
            }

            InputRecord maybe = reader.parse(line);
            if (maybe != null) {
                recs.add(maybe);
            }
            return true;
        }

        @Override
        public List<InputRecord> getResult() {
            return recs;
        }
    });
}

From source file:org.apache.druid.cli.validate.DruidJsonValidator.java

private Void readData(final StringInputRowParser parser, final CharSource source) throws IOException {
    return source.readLines(new LineProcessor<Void>() {
        private final StringBuilder builder = new StringBuilder();

        @Override/*from  w w  w.  j av  a  2s  .  c o m*/
        public boolean processLine(String line) throws IOException {
            InputRow parsed = parser.parse(line);
            builder.append(parsed.getTimestamp());
            for (String dimension : parsed.getDimensions()) {
                builder.append('\t');
                builder.append(parsed.getRaw(dimension));
            }
            logWriter.write(builder.toString());
            builder.setLength(0);
            return true;
        }

        @Override
        public Void getResult() {
            return null;
        }
    });
}

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

protected void processATFile(CharSource rulesResource) throws IOException {
    rulesResource.readLines(new LineProcessor<Void>() {
        @Override//from   w ww.  j av a 2  s  .  com
        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() > 3) {
                throw new RuntimeException("Invalid config file line " + input);
            }
            Modifier m = new Modifier();
            m.setTargetAccess(parts.get(0));

            if (parts.size() == 2) {
                m.modifyClassVisibility = true;
            } else {
                String nameReference = parts.get(2);
                int parenIdx = nameReference.indexOf('(');
                if (parenIdx > 0) {
                    m.desc = nameReference.substring(parenIdx);
                    m.name = nameReference.substring(0, parenIdx);
                } else {
                    m.name = nameReference;
                }
            }
            String className = parts.get(1).replace('/', '.');
            modifiers.put(className, m);
            if (DEBUG)
                System.out.printf("AT RULE: %s %s %s (type %s)\n", toBinary(m.targetAccess), m.name, m.desc,
                        className);
            return true;
        }
    });
}

From source file:com.google.devtools.j2objc.util.ProGuardUsageParser.java

public static DeadCodeMap parse(CharSource listing) throws IOException {
    LineProcessor<DeadCodeMap> processor = new LineProcessor<DeadCodeMap>() {
        DeadCodeMap.Builder dead = DeadCodeMap.builder();
        String lastClass;/*  w ww  .ja  v a2  s.  co  m*/

        @Override
        public DeadCodeMap getResult() {
            return dead.build();
        }

        private void handleClass(String line) {
            if (line.endsWith(":")) {
                // Class, but not completely dead; save to read its dead methods
                lastClass = line.substring(0, line.length() - 1);
            } else {
                dead.addDeadClass(line);
            }
        }

        private void handleMethod(String line) throws IOException {
            Matcher methodMatcher = proGuardMethodPattern.matcher(line);
            if (!methodMatcher.matches()) {
                throw new AssertionError("Line doesn't match expected ProGuard format!");
            }
            if (lastClass == null) {
                throw new IOException("Bad listing format: method not attached to a class");
            }
            String returnType = methodMatcher.group(5);
            String methodName = methodMatcher.group(6);
            String arguments = methodMatcher.group(7);
            String signature = buildMethodSignature(returnType, arguments);
            dead.addDeadMethod(lastClass, methodName, signature);
        }

        private void handleField(String line) throws IOException {
            String name = line.substring(line.lastIndexOf(" ") + 1);
            dead.addDeadField(lastClass, name);
        }

        @Override
        public boolean processLine(String line) throws IOException {
            if (line.startsWith("ProGuard, version") || line.startsWith("Reading ")) {
                // ignore output header
            } else if (!line.startsWith("    ")) {
                handleClass(line);
            } else if (line.startsWith("    ") && !line.contains("(")) {
                handleField(line);
            } else {
                handleMethod(line);
            }
            return true;
        }
    };

    return listing.readLines(processor);
}