Example usage for org.apache.commons.io IOUtils readLines

List of usage examples for org.apache.commons.io IOUtils readLines

Introduction

In this page you can find the example usage for org.apache.commons.io IOUtils readLines.

Prototype

public static List readLines(InputStream input, String encoding) throws IOException 

Source Link

Document

Get the contents of an InputStream as a list of Strings, one entry per line, using the specified character encoding.

Usage

From source file:com.github.frapontillo.pulse.crowd.sentiment.sentiwordnet.SentiWordNet.java

private HashMap<String, SentiWord> getDictionary() {
    if (dict == null) {
        dict = new HashMap<>();
        InputStream model = getClass().getClassLoader().getResourceAsStream("sentiwordnet");
        try {/*from   w  w  w  .  j a  v a  2 s .com*/
            List<String> lines = IOUtils.readLines(model, Charset.forName("UTF-8"));
            lines.forEach(s -> {
                SentiWord word = fromLine(s);
                if (word != null) {
                    dict.put(word.getId(), word);
                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return dict;
}

From source file:com.mammothdata.apex.example.ApplicationTest.java

@Test
public void testApplication() throws Exception {
    try {/*w ww  .ja  v a2  s.  c  o m*/
        // Pull in sample data and shovel it into kafka
        KafkaTestProducer p = new KafkaTestProducer(TOPIC);
        List<String> lines = IOUtils.readLines(this.getClass().getResourceAsStream("/sample.short.json"),
                "UTF-8");
        p.setMessages(lines);

        new Thread(p).start();

        LocalMode lma = LocalMode.newInstance();
        Configuration conf = new Configuration(false);
        conf.addResource(this.getClass().getResourceAsStream("/META-INF/properties.xml"));
        conf.set("dt.operator.KafkaInput.prop.topic", TOPIC);
        conf.set("dt.operator.KafkaInput.prop.zookeeper",
                "localhost:" + KafkaOperatorTestBase.TEST_ZOOKEEPER_PORT[0]);
        conf.set("dt.operator.KafkaInput.prop.maxTuplesPerWindow", "1"); // consume one string per window

        lma.prepareDAG(new Application(), conf);
        LocalMode.Controller lc = lma.getController();

        LOG.info("Application run started");
        lc.run(10000);
        LOG.info("Application run finished");

        lc.shutdown();

    } catch (ConstraintViolationException e) {
        Assert.fail("constraint violations: " + e.getConstraintViolations());
    }
}

From source file:com.adaptris.core.common.PayloadStreamInputParameterTest.java

@Test
public void testExtract() throws Exception {
    PayloadStreamInputParameter p = new PayloadStreamInputParameter();
    AdaptrisMessage msg = AdaptrisMessageFactory.getDefaultInstance().newMessage(TEXT.getBytes());
    try (InputStream in = p.extract(msg)) {
        List<String> strings = IOUtils.readLines(in, Charset.defaultCharset());
        assertEquals(1, strings.size());
        assertEquals(TEXT, strings.get(0));
    }/*ww  w .  ja  va  2s  .com*/
}

From source file:gate.corpora.twitter.Population.java

/**
 * /*from   w  ww.  j  a  va2s .co  m*/
 * @param corpus
 * @param inputUrl
 * @param encoding
 * @param contentKeys
 * @param featureKeys
 * @param tweetsPerDoc 0 = put them all in one document; otherwise the number per document
 * @throws ResourceInstantiationException
 */
public static void populateCorpus(final Corpus corpus, URL inputUrl, String encoding, List<String> contentKeys,
        List<String> featureKeys, int tweetsPerDoc) throws ResourceInstantiationException {
    try {
        InputStream input = inputUrl.openStream();
        List<String> lines = IOUtils.readLines(input, encoding);
        IOUtils.closeQuietly(input);

        // TODO: sort this out so it processes one at a time instead of reading the
        // whole hog into memory

        // For now, we assume the streaming API format (concatenated maps, not in a list)
        List<Tweet> tweets = TweetUtils.readTweetStrings(lines, contentKeys, featureKeys);

        int digits = (int) Math.ceil(Math.log10(tweets.size()));
        int tweetCounter = 0;
        Document document = newDocument(inputUrl, tweetCounter, digits);
        StringBuilder content = new StringBuilder();
        Map<PreAnnotation, Integer> annotandaOffsets = new HashMap<PreAnnotation, Integer>();

        for (Tweet tweet : tweets) {
            if ((tweetsPerDoc > 0) && (tweetCounter > 0) && ((tweetCounter % tweetsPerDoc) == 0)) {
                closeDocument(document, content, annotandaOffsets, corpus);
                document = newDocument(inputUrl, tweetCounter, digits);
                content = new StringBuilder();
                annotandaOffsets = new HashMap<PreAnnotation, Integer>();
            }

            int startOffset = content.length();
            content.append(tweet.getString());
            for (PreAnnotation preAnn : tweet.getAnnotations()) {
                annotandaOffsets.put(preAnn, startOffset);
            }

            content.append('\n');
            tweetCounter++;
        } // end of Tweet loop

        if (content.length() > 0) {
            closeDocument(document, content, annotandaOffsets, corpus);
        } else {
            Factory.deleteResource(document);
        }

        if (corpus.getDataStore() != null) {
            corpus.getDataStore().sync(corpus);
        }

    } catch (Exception e) {
        throw new ResourceInstantiationException(e);
    }
}

From source file:fi.ilmoeuro.membertrack.TestBase.java

private void initSchema() {
    try (final InputStream schemaFileStream = ResourceRoot.class.getResourceAsStream(SCHEMA_LIST_FILE)) {
        if (schemaFileStream == null) {
            throw new RuntimeException("Schema list not found");
        } else {//from w  ww.  jav  a 2  s.c o  m
            for (final String schemaFile : IOUtils.readLines(schemaFileStream, Charsets.UTF_8)) {
                try (final InputStream sqlStream = ResourceRoot.class.getResourceAsStream(schemaFile)) {
                    if (sqlStream == null) {
                        throw new RuntimeException(String.format("Schema file '%s' not found", schemaFile));
                    } else {
                        final String sql = IOUtils.toString(sqlStream, Charsets.US_ASCII);
                        for (String part : sql.split(";")) {
                            if (jooq == null) {
                                throw new IllegalStateException("jooq must be initialized");
                            } else {
                                jooq.execute(part);
                            }
                        }
                    }
                }
            }
        }
    } catch (IOException ex) {
        throw new RuntimeException("Couldn't retrieve schema file", ex);
    }
}

From source file:$.ChangesController.java

@RequestMapping(value = CHANGES_URI, method = RequestMethod.GET)
    @ResponseBody//from   w  ww . j av  a2s  .c  om
    public String getChangesContent() throws IOException {
        ClassPathResource resource = new ClassPathResource("changes.txt");

        // add end of lines
        String resStr = "";
        List<String> lines = IOUtils.readLines(resource.getInputStream(), "utf-8");
        for (String line : lines) {
            resStr += line;
            resStr += "<br/>${symbol_escape}n";
        }

        return resStr;
    }

From source file:jenkins.security.ClassFilterImplSanityTest.java

@Test
public void whitelistSanity() throws Exception {
    try (InputStream is = ClassFilterImpl.class.getResourceAsStream("whitelisted-classes.txt")) {
        List<String> lines = IOUtils.readLines(is, StandardCharsets.UTF_8).stream()
                .filter(line -> !line.matches("#.*|\\s*")).collect(Collectors.toList());
        assertThat("whitelist is NOT ordered", new TreeSet<>(lines),
                contains(lines.toArray(MemoryReductionUtil.EMPTY_STRING_ARRAY)));
        for (String line : lines) {
            try {
                Class.forName(line);
            } catch (ClassNotFoundException x) {
                System.err.println("skipping checks of unknown class " + line);
            }//w  w  w. jav a 2 s .c o  m
        }
    }
}

From source file:de.weltraumschaf.registermachine.asm.Assembler.java

/**
 * Assembles a whole input stream in one call to byte code file representation.
 *
 * @param input input stream with assembly string
 * @param sourceFileName file name used in the byte code header
 * @return a byte code file representation
 * @throws IOException if, I/O errors on the input stream occurs
 * @throws AssemblerSyntaxException if, syntax errors in the assembly occurs
 *//*from   w w  w .ja  v  a2 s . c o m*/
public ByteCodeFile assamble(final InputStream input, final String sourceFileName)
        throws IOException, AssemblerSyntaxException {
    final List<String> lines = IOUtils.readLines(input, App.ENCODING);
    IOUtils.closeQuietly(input);
    final List<Byte> bytecode = createByteCodeWithHeader(sourceFileName);
    final Function main = parser.parseLines(lines);
    bytecode.addAll(main.asByteList());
    return new ByteCodeFile(bytecode);
}

From source file:com.ms.commons.test.integration.apachexmlparse.internal.LocalResourceLoader.java

public Resource getResource(String location) {

    System.out.println("Get resource: " + location);
    try {//from  w  w  w .j  ava 2s  .co m
        boolean isFromCache = true;
        String dtdFileName = IntlTestGlobalConstants.TESTCASE_DTD_DIR + File.separator
                + StringUtil.replaceNoWordChars(location) + ".dtd";
        if (DTD_MAP.get(dtdFileName) == null) {
            File dtdFile = new File(dtdFileName);
            if (!dtdFile.exists()) {
                // load dtd from net
                isFromCache = false;
                String dtdContent = StringUtils.join(
                        IOUtils.readLines((new URL(location)).openStream(), "UTF-8"),
                        IntlTestGlobalConstants.LINE_SEPARATOR);
                FileUtils.writeStringToFile(dtdFile, dtdContent, "UTF-8");

                DTD_MAP.put(dtdFileName, dtdContent);
            } else {
                DTD_MAP.put(dtdFileName, FileUtils.readFileToString(dtdFile, "UTF-8"));
            }
        }

        if (isFromCache) {
            System.out.println("Get resource from cache: " + location);
        } else {
            System.out.println("Get resource from net: " + location);
        }

        return new ByteArrayResource(DTD_MAP.get(dtdFileName).getBytes("UTF-8"));
    } catch (Exception e) {
        return parent.getResource(location);
    }
}

From source file:com.bazaarvoice.jless.DiffTest.java

private void diffOutput(String fileName, String output) {
    InputStream referenceStream = getClass()
            .getResourceAsStream("/expected/" + getGeneratedDirName() + "/" + fileName + ".css");
    List<String> referenceLines = null;

    try {/*w ww . j  a va2  s. c  o m*/
        //noinspection unchecked
        referenceLines = IOUtils.readLines(referenceStream, "UTF-8");
    } catch (IOException e) {
        TestUtils.getLog().println("Unable to read " + fileName + ".css");
        e.printStackTrace();
    }

    List<String> outputLines = null;

    try {
        //noinspection unchecked
        outputLines = IOUtils.readLines(new StringReader(output));
    } catch (IOException e) {
        TestUtils.getLog().println("Unable to break up output into lines");
        e.printStackTrace();
    }

    Patch diff = DiffUtils.diff(referenceLines, outputLines);

    if (!diff.getDeltas().isEmpty()) {
        TestUtils.getLog().println("Reference output diff:");
        List<String> diffOutput = DiffUtils.generateUnifiedDiff(fileName + ".css", fileName + ".css",
                referenceLines, diff, 3);
        for (String diffOutputLine : diffOutput) {
            TestUtils.getLog().println(diffOutputLine);
        }
    }

    Assert.assertEquals(diff.getDeltas().size(), 0);
}