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:org.jpmml.lightgbm.LightGBMUtil.java

static public Iterator<String> parseText(InputStream is) throws IOException {
    Reader reader = new InputStreamReader(is, "US-ASCII");

    List<String> lines = CharStreams.readLines(reader);

    return lines.iterator();
}

From source file:com.android.repository.api.License.java

/**
 * Checks whether this license has previously been accepted.
 * @param repositoryRoot The root directory of the repository
 * @return true if this license has already been accepted
 *//*  w w w. j  av a2  s.  com*/
public boolean checkAccepted(@Nullable File repositoryRoot, @NonNull FileOp fop) {
    if (repositoryRoot == null) {
        return false;
    }
    File licenseDir = new File(repositoryRoot, LICENSE_DIR);
    File licenseFile = new File(licenseDir, getId() == null ? getLicenseHash() : getId());
    if (!fop.exists(licenseFile)) {
        return false;
    }
    try {
        for (String hash : CharStreams.readLines(new InputStreamReader(fop.newFileInputStream(licenseFile)))) {
            if (hash.equals(getLicenseHash())) {
                return true;
            }
        }
    } catch (IOException e) {
        return false;
    }
    return false;
}

From source file:org.obm.push.mail.greenmail.GreenMailExternalProcess.java

private List<String> readProcessOutputLinesUntilStartedTag() throws IOException, InterruptedException {
    List<String> outputLines = Lists.newArrayList();
    Stopwatch stopwatch = Stopwatch.createStarted();
    do {//  w  w  w  .  jav a 2  s  .  co  m
        outputLines.addAll(CharStreams.readLines(readProcessOutput()));
        if (outputLines.contains(STARTED_TAG)) {
            return outputLines;
        }
        Thread.sleep(5);
    } while (stopwatch.elapsed(TimeUnit.MILLISECONDS) < MAX_PROCESS_STARTTIME);
    throw new ExternalProcessException(
            "Process started tag not received in accepted delay of : " + MAX_PROCESS_STARTTIME + " ms");
}

From source file:eus.ixa.ixa.pipe.tok.Annotate.java

public static void tokensToKAF(final Reader breader, final KAFDocument kaf) throws IOException {
    int noSents = 0;
    int noParas = 1;
    final List<String> sentences = CharStreams.readLines(breader);
    for (final String sentence : sentences) {
        noSents = noSents + 1;//from   w  w w.  j  a v a  2  s  . c  om
        final String[] tokens = sentence.split(" ");
        for (final String token : tokens) {
            if (token.equals(RuleBasedSegmenter.PARAGRAPH)) {
                ++noParas;
                // TODO sentences without end markers;
                // crap rule
                while (noParas > noSents) {
                    ++noSents;
                }
            } else {
                // TODO add offset
                final WF wf = kaf.newWF(0, token, noSents);
                wf.setPara(noParas);
                //wf.setSent(noSents);
            }
        }
    }
}

From source file:com.cloudera.hive.scd.SQLUpdater.java

private List<String> readLines(FileSystem fs, Path path, long rootScdTime) throws IOException {
    List<String> lines = Lists.newArrayList();
    long currentScdTime = 0L;
    StringBuilder workingLine = null;
    for (String line : CharStreams.readLines(new InputStreamReader(fs.open(path)))) {
        if (line.toLowerCase(Locale.ENGLISH).startsWith(TIME_PREFIX)) {
            currentScdTime = asSCDTime(line.substring(TIME_PREFIX.length()), rootScdTime);
        } else if (currentScdTime <= rootScdTime) {
            // Prune out comments/whitspace
            line = line.trim();//from w  w w  .  ja  v a2  s . c  o m
            int commentIndex = line.indexOf("--");
            if (commentIndex >= 0) {
                line = line.substring(0, commentIndex);
            }
            if (!line.isEmpty()) {
                if (!line.endsWith(";")) {
                    if (workingLine == null) {
                        workingLine = new StringBuilder();
                    }
                    workingLine.append(line).append(' ');
                } else {
                    if (workingLine != null) {
                        workingLine.append(line);
                        lines.add(workingLine.toString());
                        workingLine = null; // working line is completed.
                    } else { // single-line statement
                        lines.add(line);
                    }
                }
            }
        }
    }
    if (workingLine != null) {
        throw new IllegalStateException("Incomplete SQL in updates: " + workingLine.toString());
    }
    return lines;
}

From source file:com.google.javascript.jscomp.WhitelistWarningsGuard.java

/**
 * Loads legacy warnings list from the file.
 * @return The lines of the file.//from   w  ww  .jav  a 2 s  .  c o m
 */
// TODO(nicksantos): This is a weird API.
static Set<String> loadWhitelistedJsWarnings(Reader reader) throws IOException {
    Preconditions.checkNotNull(reader);
    Set<String> result = new HashSet<>();

    result.addAll(CharStreams.readLines(reader));

    return result;
}

From source file:li.klass.fhem.error.ErrorHolder.java

private static File writeApplicationLogToDisk(Context context) throws IOException {
    InputStreamReader reader = null;
    try {/*from w w w .j av  a2  s .c  om*/
        File outputDir = context.getCacheDir();
        File outputFile = File.createTempFile("andFHEM-", "log", outputDir);
        outputFile.deleteOnExit();

        Process process = Runtime.getRuntime().exec("logcat -d");
        reader = new InputStreamReader(process.getInputStream());

        List<String> logLines = CharStreams.readLines(reader);
        String log = Joiner.on("\r\n").join(logLines);
        return writeToDisk(context, log);
    } finally {
        CloseableUtil.close(reader);
    }
}

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

/**
 * Initialize a new WordNetRelations with the default file_properties and stopwords files
 *//*from   w w  w  . j a  v a  2 s .  c  om*/
public WordNetRelations() throws URISyntaxException {
    final ClassLoader classLoader = getClass().getClassLoader();
    final InputStream filePropertiesFile = classLoader.getResourceAsStream(DEFAULT_FILE_PROPERTIES_FILE);
    final InputSupplier<InputStreamReader> stopwordsFile = Resources
            .newReaderSupplier(classLoader.getResource(DEFAULT_STOPWORDS_FILE), Charsets.UTF_8);
    try {
        stopwords = ImmutableSet.copyOf(CharStreams.readLines(stopwordsFile));
        mWN = WordNetAPI.getInstance(filePropertiesFile);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

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

/**
 * Creates a String of the Class name of the <code>IChemObject</code> reader
 * for this file format. The input is read line-by-line
 * until a line containing an identifying string is
 * found.//from w ww.j a  v  a  2 s.c  o  m
 *
 * <p>The ReaderFactory detects more formats than the CDK
 * has Readers for.
 *
 * <p>This method is not able to detect the format of gziped files.
 * Use <code>guessFormat(InputStream)</code> instead for such files.
 *
 * @throws IOException  if an I/O error occurs
 * @throws IllegalArgumentException if the input is null
 * @return The guessed <code>IChemFormat</code> or <code>null</code> if the
 *         file format is not recognized.
 *
 * @see #guessFormat(InputStream)
 */
public IChemFormat guessFormat(Reader input) throws IOException {
    if (input == null) {
        throw new IllegalArgumentException("input cannot be null");
    }

    // make a copy of the header
    char[] header = new char[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 CharArrayReader(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 CharArrayReader(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;
}

From source file:org.attribyte.api.http.util.RobotsTxt.java

@SuppressWarnings("unchecked")
/**// ww w  .j  ava2 s  .  com
 * Parse robots.txt from a character stream.
 * @param r A reader from which the <code>robots.txt</code> is read.
 * @param agents A list of user agents that, if listed in the file, should be preserved.
 * The wildcard (*) is always preserved.
 */
public RobotsTxt(final Reader r, final Set<String> agents) throws IOException {

    Set<String> preserveAgents = null;
    if (agents != null) {
        preserveAgents = Sets.newHashSetWithExpectedSize(agents.size() + 1);
        preserveAgents.add("*");
        for (String agent : agents) {
            preserveAgents.add(agent.toLowerCase().trim());
        }
    }

    List<String>[] currRecordLists = null;
    boolean newAgent = false;
    List<String> lines = CharStreams.readLines(r);
    for (String currLine : lines) {

        currLine = currLine.trim();
        if (currLine.length() == 0 || currLine.startsWith("#")) {
            continue;
        }

        currLine = currLine.toLowerCase();

        if (currLine.startsWith("user-agent")) {
            String currAgent = getValue(currLine);
            if (currAgent != null) {
                if (currRecordLists == null || newAgent) {
                    currRecordLists = (ArrayList<String>[]) new ArrayList[2];
                    newAgent = false;
                }

                if (preserveAgents == null) {
                    agentMap.put(currAgent, currRecordLists);
                } else if (preserveAgents.contains(currAgent)) {
                    agentMap.put(currAgent, currRecordLists);
                }
            }

        } else if (currLine.startsWith("disallow")) {
            newAgent = true;
            String path = getValue(currLine);
            if (path == null || path.length() == 0) {
                path = EMPTY_PATH;

            }
            if (currRecordLists != null) { //Agent must have appeared first
                if (currRecordLists[DISALLOW] == null) {
                    currRecordLists[DISALLOW] = new ArrayList<String>(8);
                }
                currRecordLists[DISALLOW].add(path);
            }

        } else if (currLine.startsWith("allow")) {
            newAgent = true;
            String path = getValue(currLine);
            if (path == null || path.length() == 0) {
                path = EMPTY_PATH;
            }
            if (currRecordLists != null) {
                if (currRecordLists[ALLOW] == null) {
                    currRecordLists[ALLOW] = new ArrayList<String>(8);
                }

                currRecordLists[ALLOW].add(path);
            }

        } else {
            newAgent = true;
            //Ignore
        }
    }
}