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

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

Introduction

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

Prototype

public static List<String> readLines(Readable r) throws IOException 

Source Link

Document

Reads all of the lines from a Readable object.

Usage

From source file:cpw.mods.fml.common.asm.transformers.deobf.FMLDeobfuscatingRemapper.java

public void setup(File mcDir, LaunchClassLoader classLoader, String deobfFileName) {
    this.classLoader = classLoader;
    try {//w w  w .j a  v a2s.  co  m
        InputStream classData = getClass().getResourceAsStream(deobfFileName);
        LZMAInputSupplier zis = new LZMAInputSupplier(classData);
        InputSupplier<InputStreamReader> srgSupplier = CharStreams.newReaderSupplier(zis, Charsets.UTF_8);
        List<String> srgList = CharStreams.readLines(srgSupplier);
        rawMethodMaps = Maps.newHashMap();
        rawFieldMaps = Maps.newHashMap();
        Builder<String, String> builder = ImmutableBiMap.<String, String>builder();
        Splitter splitter = Splitter.on(CharMatcher.anyOf(": ")).omitEmptyStrings().trimResults();
        for (String line : srgList) {
            String[] parts = Iterables.toArray(splitter.split(line), String.class);
            String typ = parts[0];
            if ("CL".equals(typ)) {
                parseClass(builder, parts);
            } else if ("MD".equals(typ)) {
                parseMethod(parts);
            } else if ("FD".equals(typ)) {
                parseField(parts);
            }
        }
        classNameBiMap = builder.build();
    } catch (IOException ioe) {
        FMLRelaunchLog.log(Level.ERROR, ioe, "An error occurred loading the deobfuscation map data");
    }
    methodNameMaps = Maps.newHashMapWithExpectedSize(rawMethodMaps.size());
    fieldNameMaps = Maps.newHashMapWithExpectedSize(rawFieldMaps.size());
}

From source file:com.opengamma.strata.collect.io.CsvFile.java

/**
 * Parses the specified reader as a CSV file where the separator is specified and might not be a comma.
 * <p>/*from   w w w  .ja v a2 s. c  o  m*/
 * This factory method takes a {@link Reader}.
 * Callers are encouraged to use {@link CharSource} instead of {@code Reader}
 * as it allows the resource to be safely managed.
 * <p>
 * This factory method allows the separator to be controlled.
 * For example, a tab-separated file is very similar to a CSV file, the only difference is the separator.
 * 
 * @param reader  the file resource
 * @param headerRow  whether the source has a header row, an empty source must still contain the header
 * @param separator  the separator used to separate each field, typically a comma, but a tab is sometimes used
 * @return the CSV file
 * @throws UncheckedIOException if an IO exception occurs
 * @throws IllegalArgumentException if the file cannot be parsed
 */
public static CsvFile of(Reader reader, boolean headerRow, char separator) {
    ArgChecker.notNull(reader, "source");
    List<String> lines = Unchecked.wrap(() -> CharStreams.readLines(reader));
    return create(lines, headerRow, separator);
}

From source file:org.apache.beam.sdk.util.FilePatternMatchingShardedFile.java

/**
 * Reads all the lines of all the files.
 *
 * <p>Not suitable for use except in testing of small data, since the data size may be far more
 * than can be reasonably processed serially, in-memory, by a single thread.
 *//* w w w. ja  v  a2 s . c  om*/
@VisibleForTesting
List<String> readLines(Collection<Metadata> files) throws IOException {
    List<String> allLines = Lists.newArrayList();
    int i = 1;
    for (Metadata file : files) {
        try (Reader reader = Channels.newReader(FileSystems.open(file.resourceId()),
                StandardCharsets.UTF_8.name())) {
            List<String> lines = CharStreams.readLines(reader);
            allLines.addAll(lines);
            LOG.debug("[{} of {}] Read {} lines from file: {}", i, files.size(), lines.size(), file);
        }
        i++;
    }
    return allLines;
}

From source file:org.kitesdk.morphline.stdlib.GrokDictionaries.java

private void loadDictionary(Reader reader) throws IOException {
    for (String line : CharStreams.readLines(reader)) {
        line = line.trim();//w  ww .j ava  2 s .c om
        if (line.length() == 0) {
            continue; // ignore empty lines
        }
        if (line.startsWith("#")) {
            continue; // ignore comment lines
        }
        int i = line.indexOf(" ");
        if (i < 0) {
            throw new MorphlineCompilationException(
                    "Dictionary entry line must contain a space to separate name and value: " + line,
                    getConfig());
        }
        if (i == 0) {
            throw new MorphlineCompilationException("Dictionary entry line must contain a name: " + line,
                    getConfig());
        }
        String name = line.substring(0, i);
        String value = line.substring(i + 1, line.length()).trim();
        if (value.length() == 0) {
            throw new MorphlineCompilationException("Dictionary entry line must contain a value: " + line,
                    getConfig());
        }
        dictionary.put(name, value);
    }
}

From source file:edu.cmu.cs.lti.ark.fn.wordnet.WordNetRelations.java

public WordNetRelations(String stopWordFile, String configFile) {
    try {/*ww  w .  j a  va  2s  . com*/
        final InputSupplier<InputStreamReader> stopwordSupplier = Files
                .newReaderSupplier(new File(stopWordFile), Charsets.UTF_8);
        stopwords = ImmutableSet.copyOf(CharStreams.readLines(stopwordSupplier));
        mWN = WordNetAPI.getInstance(new FileInputStream(configFile));
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:com.cinchapi.common.process.Processes.java

/**
 * Execute {@link Process#waitFor()} while reading everything from the
 * {@code process}'s standard out and error to prevent the process from
 * hanging.//from  ww w.  j  a  va 2  s  .  co m
 * 
 * @param process the {@link Process} for which to wait
 * @return a map containing all the {@link ProcessData} (e.g. exit code,
 *         stdout and stderr)
 */
public static ProcessResult waitFor(Process process) {
    AtomicBoolean finished = new AtomicBoolean(false);
    List<String> stdout = Lists.newArrayList();
    List<String> stderr = Lists.newArrayList();
    CountDownLatch latch = new CountDownLatch(2);
    try {
        // Asynchronously exhaust stdout so process doesn't hang
        executor.execute(() -> {
            try {
                InputStreamReader reader = new InputStreamReader(process.getInputStream());
                while (!finished.get()) {
                    stdout.addAll(CharStreams.readLines(reader));
                }
                reader.close();
                latch.countDown();
            } catch (IOException e) {
                throw CheckedExceptions.wrapAsRuntimeException(e);
            }
        });

        // Asynchronously exhaust stderr so process doesn't hang
        executor.execute(() -> {
            try {
                InputStreamReader reader = new InputStreamReader(process.getErrorStream());
                while (!finished.get()) {
                    stderr.addAll(CharStreams.readLines(reader));
                }
                reader.close();
                latch.countDown();
            } catch (IOException e) {
                throw CheckedExceptions.wrapAsRuntimeException(e);
            }
        });
        int code = process.waitFor();
        finished.set(true);
        latch.await();
        return new ProcessResult(code, stdout, stderr);
    } catch (InterruptedException e) {
        throw CheckedExceptions.wrapAsRuntimeException(e);
    }
}

From source file:org.languagetool.dev.eval.LanguageDetectionMinLengthEval.java

private List<String> getLines(InputStream stream) throws IOException {
    List<String> lines = CharStreams.readLines(new InputStreamReader(stream));
    List<String> result = new ArrayList<>();
    for (String line : lines) {
        if (!line.startsWith("#")) {
            result.add(line);/*from   w w  w. ja  v  a  2  s . com*/
        }
    }
    return result;
}

From source file:util.mybatis.comment.ClassCommentPlugin.java

/**
 * ?./*from  ww w .ja  v a 2  s  .  c  o  m*/
 *
 * @param method the method
 * @param interfaze the interfaze
 * @param introspectedTable the introspected table
 * @param body the body
 */
public void methodGenerated(Method method, Interface interfaze, IntrospectedTable introspectedTable,
        String body) {

    Map<String, Object> local = Maps.newLinkedHashMap();
    local.put("todo", (Strings.isNullOrEmpty(body) ? "TODO" : body));
    local.put("tags", new MethodWrapper(method));

    String methodComment = CodetemplatesLoader.getInstance().get("methodcomment_context");
    methodComment = VelocityUtils.evaluate(methodComment, local);
    try {
        List<String> lines = CharStreams.readLines(CharStreams.newReaderSupplier(methodComment));
        if (lines == null) {
            return;
        }
        boolean isfirst = true;
        for (String line : lines) {
            line = line.trim();
            if (Strings.isNullOrEmpty(line)) {
                continue;
            }
            if (!isfirst) {
                line = " " + line;
            }
            method.addJavaDocLine(line);
            isfirst = false;
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.googlesource.gerrit.plugins.github.notification.WebhookServlet.java

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    if (Strings.emptyToNull(config.webhookUser) == null) {
        logger.error("No webhookUser defined: cannot process GitHub events");
        resp.sendError(SC_INTERNAL_SERVER_ERROR);
        return;/*from  w ww .  j  ava  2s.co  m*/
    }

    WebhookEventHandler<?> handler = getWebhookHandler(req.getHeader("X-Github-Event"));
    if (handler == null) {
        resp.sendError(SC_NOT_FOUND);
        return;
    }

    try (BufferedReader reader = req.getReader()) {
        String body = Joiner.on("\n").join(CharStreams.readLines(reader));
        if (!validateSignature(req.getHeader("X-Hub-Signature"), body, req.getCharacterEncoding())) {
            logger.error("Signature mismatch to the payload");
            resp.sendError(SC_FORBIDDEN);
            return;
        }

        session.get().setUserAccountId(Account.Id.fromRef(config.webhookUser));
        GitHubLogin login = loginProvider.get(config.webhookUser);
        if (login == null || !login.isLoggedIn()) {
            logger.error("Cannot login to github as {}. {}.webhookUser is not correctly configured?",
                    config.webhookUser, GitHubConfig.CONF_SECTION);
            resp.setStatus(SC_INTERNAL_SERVER_ERROR);
            return;
        }
        requestScopedLoginProvider.get(req).login(login.getToken());

        if (callHander(handler, body)) {
            resp.setStatus(SC_NO_CONTENT);
        } else {
            resp.sendError(SC_INTERNAL_SERVER_ERROR);
        }
    }
}

From source file:org.openscience.cdk.io.FormatFactory.java

public IChemFormat guessFormat(InputStream input) throws IOException {
    if (input == null) {
        throw new IllegalArgumentException("input cannot be null");
    }//from  ww w . j ava 2 s  .c  o m

    // make a copy of the header
    byte[] header = new byte[this.headerLength];
    if (!input.markSupported()) {
        throw new IllegalArgumentException("input must support mark");
    }
    input.mark(this.headerLength);
    input.read(header, 0, this.headerLength);
    input.reset();

    BufferedReader buffer = new BufferedReader(new StringReader(new String(header)));

    /* Search file for a line containing an identifying keyword */
    List<String> lines = Collections.unmodifiableList(CharStreams.readLines(buffer));
    Set<MatchResult> results = new TreeSet<MatchResult>();

    for (IChemFormatMatcher format : formats) {
        results.add(format.matches(lines));
    }

    // best result is first element (sorted set)
    if (results.size() > 1) {
        MatchResult best = results.iterator().next();
        if (best.matched())
            return best.format();
    }

    buffer = new BufferedReader(new StringReader(new String(header)));

    String line = buffer.readLine();
    // is it a XYZ file?
    StringTokenizer tokenizer = new StringTokenizer(line.trim());
    try {
        int tokenCount = tokenizer.countTokens();
        if (tokenCount == 1) {
            Integer.parseInt(tokenizer.nextToken());
            // if not failed, then it is a XYZ file
            return (IChemFormat) XYZFormat.getInstance();
        } else if (tokenCount == 2) {
            Integer.parseInt(tokenizer.nextToken());
            if ("Bohr".equalsIgnoreCase(tokenizer.nextToken())) {
                return (IChemFormat) XYZFormat.getInstance();
            }
        }
    } catch (NumberFormatException exception) {
    }

    return null;
}