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

public ImmutableList<String> readLines() throws IOException 

Source Link

Document

Reads all the lines of this source as a list of strings.

Usage

From source file:io.takari.maven.plugins.util.PropertiesWriter.java

private static void write(CharSource charSource, String comment, OutputStream out) throws IOException {
    List<String> lines = new ArrayList<>(charSource.readLines());
    lines.remove(comment != null ? 1 : 0);
    BufferedWriter w = new BufferedWriter(new OutputStreamWriter(out, ENCODING));
    for (String line : lines) {
        w.write(line);//from  w w w.j  a va2 s .  c o  m
        w.newLine();
    }
    w.flush();
}

From source file:ch.ethz.system.mt.tpch.DistributionLoader.java

public static <R extends Readable & Closeable> Map<String, Distribution> loadDistribution(CharSource input)
        throws IOException {
    Iterator<String> iterator = filter(input.readLines().iterator(), new Predicate<String>() {
        @Override//www. j ava  2  s  .  com
        public boolean apply(String line) {
            line = line.trim();
            return !line.isEmpty() && !line.startsWith("#");
        }
    });

    return loadDistributions(iterator);
}

From source file:org.apache.isis.viewer.wicket.viewer.services.TranslationsResolverWicket.java

private static List<String> readLines(final URL url) throws IOException {
    if (url == null) {
        return null;
    }//from w  w  w .j a v a  2 s.co m
    final CharSource charSource = Resources.asCharSource(url, Charsets.UTF_8);
    final ImmutableList<String> strings = charSource.readLines();
    return Collections.unmodifiableList(Lists.newArrayList(Iterables.filter(strings, new Predicate<String>() {
        @Override
        public boolean apply(final String input) {
            return input != null && nonEmpty.matcher(input).matches();
        }
    })));
}

From source file:com.opengamma.collect.io.PropertiesFile.java

private static PropertySet parse(CharSource source) throws IOException {
    ImmutableList<String> lines = source.readLines();
    Multimap<String, String> parsed = ArrayListMultimap.create();
    int lineNum = 0;
    for (String line : lines) {
        lineNum++;/*from   w  ww. j  a  v  a2 s.c om*/
        line = line.trim();
        if (line.length() == 0 || line.startsWith("#") || line.startsWith(";")) {
            continue;
        }
        int equalsPosition = line.indexOf('=');
        if (equalsPosition < 0) {
            throw new IllegalArgumentException(
                    "Invalid properties file, expected key=value property, line " + lineNum);
        }
        String key = line.substring(0, equalsPosition).trim();
        String value = line.substring(equalsPosition + 1).trim();
        if (key.length() == 0) {
            throw new IllegalArgumentException("Invalid properties file, empty key, line " + lineNum);
        }
        parsed.put(key, value);
    }
    return PropertySet.of(parsed);
}

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

/**
 * Parses the specified source as a properties file.
 * <p>/*from  www .j a  v a 2 s .  com*/
 * This parses the specified character source expecting a properties file format.
 * The resulting instance can be queried for each key and value.
 * 
 * @param source  the properties file resource
 * @return the properties file
 * @throws UncheckedIOException if an IO exception occurs
 * @throws IllegalArgumentException if the file cannot be parsed
 */
public static PropertiesFile of(CharSource source) {
    ArgChecker.notNull(source, "source");
    ImmutableList<String> lines = Unchecked.wrap(() -> source.readLines());
    PropertySet keyValues = parse(lines);
    return new PropertiesFile(keyValues);
}

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

/**
 * Parses the specified source as an INI file.
 * <p>/*from   ww  w .  jav  a  2s  .  co m*/
 * This parses the specified character source expecting an INI file format.
 * The resulting instance can be queried for each section in the file.
 * 
 * @param source  the INI file resource
 * @return the INI file
 * @throws UncheckedIOException if an IO exception occurs
 * @throws IllegalArgumentException if the file cannot be parsed
 */
public static IniFile of(CharSource source) {
    ArgChecker.notNull(source, "source");
    ImmutableList<String> lines = Unchecked.wrap(() -> source.readLines());
    Map<String, Multimap<String, String>> parsedIni = parse(lines);
    ImmutableMap.Builder<String, PropertySet> builder = ImmutableMap.builder();
    parsedIni.forEach((sectionName, sectionData) -> builder.put(sectionName, PropertySet.of(sectionData)));
    return new IniFile(builder.build());
}

From source file:org.locationtech.geogig.cli.test.functional.general.GlobalState.java

public static List<String> runAndParseCommand(boolean failFast, String... command) throws Exception {
    runCommand(failFast, command);//  w  ww. j av a 2  s.  co  m
    CharSource reader = CharSource.wrap(stdOut.toString(Charsets.UTF_8.name()));
    ImmutableList<String> lines = reader.readLines();
    return lines;
}

From source file:com.opengamma.collect.io.IniFile.java

private static Map<String, Multimap<String, String>> parse(CharSource source) throws IOException {
    ImmutableList<String> lines = source.readLines();
    Map<String, Multimap<String, String>> ini = new LinkedHashMap<>();
    Multimap<String, String> currentSection = null;
    int lineNum = 0;
    for (String line : lines) {
        lineNum++;/*from   w  w  w  . ja v  a2s.  c  o  m*/
        line = line.trim();
        if (line.length() == 0 || line.startsWith("#") || line.startsWith(";")) {
            continue;
        }
        if (line.startsWith("[") && line.endsWith("]")) {
            String sectionName = line.substring(1, line.length() - 1).trim();
            if (ini.containsKey(sectionName)) {
                throw new IllegalArgumentException(
                        "Invalid INI file, duplicate section not allowed, line " + lineNum);
            }
            currentSection = ArrayListMultimap.create();
            ini.put(sectionName, currentSection);

        } else if (currentSection == null) {
            throw new IllegalArgumentException(
                    "Invalid INI file, properties must be within a [section], line " + lineNum);

        } else {
            int equalsPosition = line.indexOf('=');
            if (equalsPosition < 0) {
                throw new IllegalArgumentException(
                        "Invalid INI file, expected key=value property, line " + lineNum);
            }
            String key = line.substring(0, equalsPosition).trim();
            String value = line.substring(equalsPosition + 1).trim();
            if (key.length() == 0) {
                throw new IllegalArgumentException("Invalid INI file, empty key, line " + lineNum);
            }
            currentSection.put(key, value);
        }
    }
    return ini;
}

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

/**
 * Parses the specified source as a CSV file where the separator is specified and might not be a comma.
 * <p>//from  w  w  w . ja va 2 s  .com
 * This overload 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 source  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(CharSource source, boolean headerRow, char separator) {
    ArgChecker.notNull(source, "source");
    List<String> lines = Unchecked.wrap(() -> source.readLines());
    return create(lines, headerRow, separator);
}

From source file:org.anarres.dblx.core.preset.PresetManager.java

PresetManager(@Nonnull LX lx) throws IOException {
    this.channelState = new ChannelState[lx.engine.getChannels().size()];
    for (int i = 0; i < presets.length; ++i) {
        presets[i] = new Preset(this, i);
    }//from w  w  w .java  2s.  co  m

    File file = new File(FILENAME);
    if (file.exists()) {
        CharSource source = Files.asCharSource(file, StandardCharsets.UTF_8);
        ImmutableList<String> values = source.readLines();
        int i = 0;
        for (String serialized : values) {
            presets[i++].load(serialized);
            if (i >= NUM_PRESETS) {
                break;
            }
        }
    }
    for (LXChannel channel : lx.engine.getChannels()) {
        channelState[channel.getIndex()] = new ChannelState(channel);
    }
}