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.waveprotocol.pst.style.PstStyler.java

@Override
public void style(File f, boolean saveBackup) {
    List<String> lines = null;
    try {/*from   ww  w.  j  av  a2 s  . c  om*/
        lines = CharStreams.readLines(new FileReader(f));
    } catch (IOException e) {
        System.err.println("Couldn't find file " + f.getName() + " to style: " + e.getMessage());
        return;
    }

    Joiner newlineJoiner = Joiner.on('\n');

    if (saveBackup) {
        File backup = new File(f.getAbsolutePath() + BACKUP_SUFFIX);
        try {
            Files.write(newlineJoiner.join(lines), backup, Charset.defaultCharset());
        } catch (IOException e) {
            System.err.println("Couldn't write backup " + backup.getName() + ": " + e.getMessage());
            return;
        }
    }

    try {
        Files.write(newlineJoiner.join(styleLines(lines)), f, Charset.defaultCharset());
    } catch (IOException e) {
        System.err.println("Couldn't write styled file " + f.getName() + ": " + e.getMessage());
        return;
    }
}

From source file:org.terasology.assets.module.ModuleAssetDataProducer.java

private void processRedirectFile(Path file, Name moduleId, Map<ResourceUrn, ResourceUrn> rawRedirects) {
    Path filename = file.getFileName();
    if (filename != null) {
        Name assetName = new Name(com.google.common.io.Files.getNameWithoutExtension(filename.toString()));
        try (BufferedReader reader = Files.newBufferedReader(file, Charsets.UTF_8)) {
            List<String> contents = CharStreams.readLines(reader);
            if (contents.isEmpty()) {
                logger.error("Failed to read redirect '{}:{}' - empty", moduleId, assetName);
            } else if (!ResourceUrn.isValid(contents.get(0))) {
                logger.error("Failed to read redirect '{}:{}' - '{}' is not a valid urn", moduleId, assetName,
                        contents.get(0));
            } else {
                rawRedirects.put(new ResourceUrn(moduleId, assetName), new ResourceUrn(contents.get(0)));
                resolutionMap.put(assetName, moduleId);
            }/*w ww  .  j  av  a 2s  .  c  o m*/
        } catch (IOException e) {
            logger.error("Failed to read redirect '{}:{}'", moduleId, assetName, e);
        }
    } else {
        logger.error("Missing file name for redirect");
    }
}

From source file:org.obm.push.mail.ReplyEmail.java

private String quoteOnLineBreaks(String toQuote) throws IOException {
    StringBuilder stringBuilder = new StringBuilder();
    stringBuilder.append(EMAIL_LINEBREAKER);

    List<String> linesWithoutTermination = CharStreams.readLines(new StringReader(toQuote));
    for (String line : linesWithoutTermination) {
        stringBuilder.append(EMAIL_LINEBREAKER).append("> ").append(line);
    }//from ww  w. java  2s  .  c o m
    return stringBuilder.toString();
}

From source file:org.obm.push.mail.ReplyEmail.java

private Reader textToHtmlReader(Reader reader) throws IOException {
    return new StringReader(encodeTxtLinesInHtml(CharStreams.readLines(reader)));
}

From source file:org.eclipse.xtext.generator.trace.AbstractTraceRegionToString.java

protected List<String> render(final AbstractTraceRegionToString.File file, final int width) {
    try {//from ww  w  .  ja va  2s  .co  m
        String _xifexpression = null;
        if ((file.uri == null)) {
            _xifexpression = this.getLocalText();
        } else {
            _xifexpression = this.getRemoteText(file.uri);
        }
        final String text = _xifexpression;
        ITextRegion _elvis = null;
        ITextRegion _xifexpression_1 = null;
        if ((file.uri == null)) {
            _xifexpression_1 = this.getLocalFrame();
        } else {
            _xifexpression_1 = this.getRemoteFrame(file.uri);
        }
        if (_xifexpression_1 != null) {
            _elvis = _xifexpression_1;
        } else {
            int _length = text.length();
            TextRegion _textRegion = new TextRegion(0, _length);
            _elvis = _textRegion;
        }
        final ITextRegion frame = _elvis;
        final Function1<AbstractTraceRegionToString.Insert, Boolean> _function = (
                AbstractTraceRegionToString.Insert it) -> {
            return Boolean.valueOf(((it.offset >= frame.getOffset())
                    && (it.offset <= (frame.getOffset() + frame.getLength()))));
        };
        final Iterable<AbstractTraceRegionToString.Insert> inframe = IterableExtensions.<AbstractTraceRegionToString.Insert>filter(
                file.inserts, _function);
        final Function<AbstractTraceRegionToString.Insert, Integer> _function_1 = (
                AbstractTraceRegionToString.Insert it) -> {
            return Integer.valueOf(it.offset);
        };
        final Function1<Map.Entry<Integer, Collection<AbstractTraceRegionToString.Insert>>, Integer> _function_2 = (
                Map.Entry<Integer, Collection<AbstractTraceRegionToString.Insert>> it) -> {
            return it.getKey();
        };
        final List<Map.Entry<Integer, Collection<AbstractTraceRegionToString.Insert>>> offsets = IterableExtensions.<Map.Entry<Integer, Collection<AbstractTraceRegionToString.Insert>>, Integer>sortBy(
                IterableExtensions.<Map.Entry<Integer, Collection<AbstractTraceRegionToString.Insert>>>toList(
                        Multimaps.<Integer, AbstractTraceRegionToString.Insert>index(inframe, _function_1)
                                .asMap().entrySet()),
                _function_2);
        int last = frame.getOffset();
        final StringBuilder result = new StringBuilder();
        for (final Map.Entry<Integer, Collection<AbstractTraceRegionToString.Insert>> e : offsets) {
            {
                final Integer offset = e.getKey();
                final String insert = this.render(e.getValue(), width);
                final String prefix = text.substring(last, (offset).intValue());
                result.append(prefix);
                result.append(insert);
                last = (offset).intValue();
            }
        }
        int _offset = frame.getOffset();
        int _length_1 = frame.getLength();
        final int end = (_offset + _length_1);
        if ((last < end)) {
            result.append(text.substring(last, end));
        }
        String _string = result.toString();
        StringReader _stringReader = new StringReader(_string);
        return CharStreams.readLines(_stringReader);
    } catch (Throwable _e) {
        throw Exceptions.sneakyThrow(_e);
    }
}

From source file:com.google.devtools.build.lib.rules.cpp.IncludeParser.java

/**
 * Processes the output generated by an auxiliary include-scanning binary. Closes the stream upon
 * completion.//from w ww.  j  a va2s.c  o  m
 *
 * <p>If a source file has the following include statements:
 * <pre>
 *   #include &lt;string&gt;
 *   #include "directory/header.h"
 * </pre>
 *
 * <p>Then the output file has the following contents:
 * <pre>
 *   "directory/header.h
 *   &lt;string
 * </pre>
 * <p>Each line of the output is translated into an Inclusion object.
 */
public static List<Inclusion> processIncludes(Object streamName, InputStream is) throws IOException {
    List<Inclusion> inclusions = new ArrayList<>();
    InputStreamReader reader = new InputStreamReader(is, ISO_8859_1);
    try {
        for (String line : CharStreams.readLines(reader)) {
            char qchar = line.charAt(0);
            String name = line.substring(1);
            Inclusion.Kind kind = KIND_MAP.get(qchar);
            if (kind == null) {
                throw new IOException("Illegal inclusion kind '" + qchar + "'");
            }
            inclusions.add(new Inclusion(name, kind));
        }
    } catch (IOException e) {
        throw new IOException("Error reading include file " + streamName + ": " + e.getMessage());
    } finally {
        reader.close();
    }
    return inclusions;
}

From source file:io.prestosql.plugin.hive.BackgroundHiveSplitLoader.java

private static List<Path> getTargetPathsFromSymlink(FileSystem fileSystem, Path symlinkDir) {
    try {//from  w ww .java 2s  .  co m
        FileStatus[] symlinks = fileSystem.listStatus(symlinkDir, HIDDEN_FILES_PATH_FILTER);
        List<Path> targets = new ArrayList<>();

        for (FileStatus symlink : symlinks) {
            try (BufferedReader reader = new BufferedReader(
                    new InputStreamReader(fileSystem.open(symlink.getPath()), StandardCharsets.UTF_8))) {
                CharStreams.readLines(reader).stream().map(Path::new).forEach(targets::add);
            }
        }
        return targets;
    } catch (IOException e) {
        throw new PrestoException(HIVE_BAD_DATA, "Error parsing symlinks from: " + symlinkDir, e);
    }
}

From source file:nars.util.data.Util.java

public static List<String> inputToStrings(InputStream is) throws IOException {
    List<String> x = CharStreams.readLines(new InputStreamReader(is, Charsets.UTF_8));
    Closeables.closeQuietly(is);/*from w w  w. j  a  v a  2 s.  co m*/
    return x;
}