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

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

Introduction

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

Prototype

public static <T> T readLines(URL url, Charset charset, LineProcessor<T> callback) throws IOException 

Source Link

Document

Streams lines from a URL, stopping when our callback returns false, or we have read all of the lines.

Usage

From source file:org.rpmcomparator.service.MimeLookUp.java

public static Multimap<String, String> populateMimeMap(Multimap<String, String> mimeFileMap) {
    MimeLineProcessor mimeLineProcessor = new MimeLineProcessor(mimeFileMap);
    try {//from w  w  w .  j a v a2  s.c  o  m
        return Resources.readLines(Resources.getResource(MIME_TYPE_FILE_PATH), Charset.defaultCharset(),
                mimeLineProcessor);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:us.eharning.atomun.mnemonic.spi.BidirectionalDictionary.java

/**
 * Utility method to convert a resource URL into a list of lines.
 *
 * @param resource/*from  ww  w .  j  a va 2 s .c o m*/
 *         location to generate the line list from.
 *
 * @return list of lines.
 *
 * @throws IOException
 *         on I/O error reading from the resource.
 */
@Nonnull
private static ImmutableList<String> resourceToLines(@Nonnull URL resource) throws IOException {
    LineProcessor<ImmutableList<String>> lineProcess = new LineProcessor<ImmutableList<String>>() {
        final ImmutableList.Builder<String> result = ImmutableList.builder();

        @Override
        public boolean processLine(@Nonnull String line) throws IOException {
            /* Skip comments and empty lines */
            if (line.startsWith("#") || line.isEmpty()) {
                return true;
            }
            /* Binding dictionary handling to NFKD normalization */
            line = Normalizer.normalize(line, Normalizer.Form.NFKD);
            result.add(line);
            return true;
        }

        @Override
        public ImmutableList<String> getResult() {
            return result.build();
        }
    };
    return Resources.readLines(resource, Charsets.UTF_8, lineProcess);
}

From source file:blue.lapis.methodremapper.RemapperConfig.java

/**
 * Loads the mappings from the specified {@link URL} (resource).
 *
 * @param resource The resource to load the mappings from
 * @return The loaded mappings/*w  ww  . j  a  v  a  2s  .c  o  m*/
 * @throws IOException If the mappings couldn't be loaded
 */
public static ImmutableTable<String, String, String> loadMappings(URL resource) throws IOException {
    return Resources.readLines(resource, UTF_8, new RemapperConfig());
}

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

private void readMapFile(String rulesFile) throws IOException {
    File file = new File(rulesFile);
    URL rulesResource;// w  ww  .  java2 s.c  om
    if (file.exists()) {
        rulesResource = file.toURI().toURL();
    } else {
        rulesResource = Resources.getResource(rulesFile);
    }
    Resources.readLines(rulesResource, Charsets.UTF_8, new LineProcessor<Void>() {
        @Override
        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;
        }
    });
    FMLRelaunchLog.fine("Loaded %d rules from AccessTransformer config file %s\n", modifiers.size(), rulesFile);
}

From source file:com.mapr.synth.samplers.VinSampler.java

private static Map<String, String> mapResource(String name) throws IOException {
    final Splitter onTab = Splitter.on("\t");

    return Resources.readLines(Resources.getResource(name), Charsets.UTF_8,
            new LineProcessor<Map<String, String>>() {
                final Map<String, String> r = Maps.newHashMap();

                @Override//from  ww w  . j av a  2  s.co  m
                public boolean processLine(String line) throws IOException {
                    Iterator<String> pieces = onTab.split(line).iterator();
                    String key = pieces.next();
                    r.put(key, pieces.next());
                    return true;
                }

                @Override
                public Map<String, String> getResult() {
                    return r;
                }
            });
}

From source file:com.mapr.synth.samplers.VinSampler.java

private static SetMultimap<String, String> multiMapResource(String name) throws IOException {
    final Splitter onTab = Splitter.on("\t");

    return Resources.readLines(Resources.getResource(name), Charsets.UTF_8,
            new LineProcessor<SetMultimap<String, String>>() {
                final SetMultimap<String, String> r = HashMultimap.create();

                @Override//from  ww  w.j  a  va2  s .co  m
                public boolean processLine(String line) throws IOException {
                    Iterator<String> pieces = onTab.split(line).iterator();
                    String key = pieces.next();
                    r.put(key, pieces.next());
                    return true;
                }

                @Override
                public SetMultimap<String, String> getResult() {
                    return r;
                }
            });
}

From source file:org.jclouds.abiquo.environment.InfrastructureTestEnvironment.java

public static String readLicense() throws IOException {
    URL url = CloudTestEnvironment.class.getResource("/license/expired");
    return Resources.readLines(url, Charset.defaultCharset(), new LineProcessor<String>() {
        StringBuilder sb = new StringBuilder();

        @Override/*  w w w. j  a  v  a  2 s  . c om*/
        public String getResult() {
            return sb.toString();
        }

        @Override
        public boolean processLine(String line) throws IOException {
            if (!line.startsWith("#")) {
                sb.append(line);
            }
            return true;
        }
    });
}