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(Reader input) throws IOException 

Source Link

Document

Get the contents of a Reader as a list of Strings, one entry per line.

Usage

From source file:com.openshift.internal.restclient.capability.resources.OpenshiftBinaryRSyncRetrievalIntegrationTest.java

protected void rsyncAndCheck(final String fileName, Peer source, Peer destination) {
    List<String> logs = pod.accept(new CapabilityVisitor<IRSyncable, List<String>>() {

        @Override/*from   w w  w  .  ja  v a2s. co  m*/
        public List<String> visit(IRSyncable cap) {
            try {
                final BufferedReader reader = new BufferedReader(
                        new InputStreamReader(cap.sync(source, destination, new SkipTlsVerify())));
                List<String> logs = IOUtils.readLines(reader);
                // wait until end of 'rsync'
                cap.await();
                return logs;
            } catch (Exception e) {
                LOG.error("Exception rsyncing to pod:", e);
            }
            return new ArrayList<>();
        }

    }, new ArrayList<>());
    if (LOG.isDebugEnabled()) {
        LOG.debug("**** RSync Logs ****");
        logs.forEach(l -> LOG.debug(l));
    }
    // then verify that the logs contain a message about the dummy file
    assertThat(logs).isNotEmpty();
    assertThat(logs.stream().anyMatch(line -> line.contains(fileName))).isTrue();
}

From source file:au.org.ala.bhl.TaxonGrab.java

private Set<String> loadLexicon() {
    HashSet<String> set = new HashSet<String>();
    String path = "/au/org/ala/bhl/english.txt";
    InputStream is = TaxonGrab.class.getResourceAsStream(path);
    try {//from  w w w.j  a v  a  2s  . c o  m
        @SuppressWarnings("unchecked")
        List<String> lines = IOUtils.readLines(is);
        int count = 0;
        for (String line : lines) {
            StringBuilder word = new StringBuilder();
            for (int i = 0; i < line.length(); ++i) {
                char ch = line.charAt(i);
                if (Character.isLetter(ch)) {
                    word.append(ch);
                }
            }
            count++;
            set.add(word.toString().toLowerCase());
        }

        System.out.println("" + count + " words loaded.");
    } catch (IOException e) {
        e.printStackTrace();
    }

    return set;
}

From source file:info.magnolia.templating.inheritance.DefaultInheritanceContentDecoratorTest.java

private Map<String, String> loadSessionConfigs(String resourceSuffix) throws IOException {

    InputStream stream = getClass()
            .getResourceAsStream(getClass().getSimpleName() + "_" + resourceSuffix + ".txt");
    List<String> lines = IOUtils.readLines(stream);

    HashMap<String, String> sections = new HashMap<String, String>();
    String sectionName = null;//from   w  w  w.j  a  va 2  s. co m
    List<String> sectionLines = new ArrayList<String>();

    for (String line : lines) {
        if (line.equals("")) {
            continue;
        }
        if (line.startsWith("#")) {
            continue;
        }
        if (line.startsWith("[")) {
            if (sectionName != null) {
                sections.put(sectionName, StringUtils.join(sectionLines, "\n"));
            }
            sectionName = StringUtils.substringBetween(line, "[", "]");
            sectionLines.clear();
            continue;
        }
        sectionLines.add(line);
    }
    if (sectionName != null) {
        sections.put(sectionName, StringUtils.join(sectionLines, "\n"));
    }
    return sections;
}

From source file:com.adaptris.core.services.splitter.LineCountSplitterTest.java

public void testSplitMessageWithHeader2() throws Exception {
    LineCountSplitter s = new LineCountSplitter();
    s.setKeepHeaderLines(2);/*from   www . j  a  v a  2 s  . c o m*/
    s.setSplitOnLine(10);
    s.setIgnoreBlankLines(true);

    final String HEADER_LINE_1 = "HEADER LINE 1";
    final String HEADER_LINE_2 = "HEADER LINE 2";
    List<AdaptrisMessage> result = toList(s.splitMessage(
            createLineCountMessageInputWithHeader(new String[] { HEADER_LINE_1, HEADER_LINE_2 })));

    assertEquals("5 split messages", 5, result.size());

    for (AdaptrisMessage m : result) {
        try (Reader reader = m.getReader()) {
            List<String> lines = IOUtils.readLines(reader);
            assertEquals("12 lines per message", 12, lines.size());
            assertEquals("Must be header line 1", HEADER_LINE_1, lines.get(0));
            assertEquals("Must be header line 2", HEADER_LINE_2, lines.get(1));
            for (int i = 2; i < 12; i++) {
                assertEquals("Must be regular line", LINE, lines.get(i));
            }
        }
    }
}

From source file:hu.bme.mit.sette.tools.catg.CatgGenerator.java

private void copyTool(EclipseProject eclipseProject) throws IOException, SetteConfigurationException {
    FileUtils.copyDirectory(getTool().getToolDirectory(), getRunnerProjectSettings().getBaseDirectory());

    // edit build.xml
    // TODO make better

    File buildXml = new File(getRunnerProjectSettings().getBaseDirectory(), "build.xml");
    List<String> newLines = new ArrayList<>();

    InputStream fis = null;/*from   ww w  . j a v  a  2 s.  c o  m*/
    try {
        fis = new FileInputStream(buildXml);
        List<String> lines = IOUtils.readLines(fis);

        for (String line : lines) {
            if (line.contains("[SETTE]")) {
                String indent = "";

                for (int i = 0; i < line.length(); i++) {
                    char ch = line.charAt(i);

                    if (ch == ' ' || ch == '\t') {
                        indent += ch;
                    } else {
                        break;
                    }
                }

                line = line.trim();
                if (line.equals("<!-- [SETTE][Libraries] -->")) {
                    for (EclipseClasspathEntry entry : eclipseProject.getClasspathDescriptor()
                            .classpathEntries()) {
                        if (entry.getKind().equals(Kind.LIBRARY)) {
                            newLines.add(
                                    String.format("%s<pathelement location=\"%s\"/>", indent, entry.getPath()));
                        }
                    }
                } else if (line.equals("<!-- [SETTE][Sources] -->")) {
                    for (EclipseClasspathEntry entry : eclipseProject.getClasspathDescriptor()
                            .classpathEntries()) {
                        if (entry.getKind().equals(Kind.SOURCE)) {
                            newLines.add(String.format("%s<src path=\"%s\"/>", indent, entry.getPath()));
                        }
                    }
                } else {
                    throw new SetteConfigurationException(
                            "Invalid SETTE command (XML comment) in CATG build.xml: " + line);
                }
            } else {
                newLines.add(line);
            }
        }
    } finally {
        IOUtils.closeQuietly(fis);
    }

    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream(buildXml);
        IOUtils.writeLines(newLines, null, fos);
    } finally {
        IOUtils.closeQuietly(fos);
    }
}

From source file:com.streamsets.pipeline.stage.destination.s3.TestAmazonS3Target.java

@Test
public void testWriteTextDataWithCompression() throws Exception {

    String prefix = "textPrefixrCompression";
    AmazonS3Target amazonS3Target = createS3targetWithTextData(prefix, true);
    TargetRunner targetRunner = new TargetRunner.Builder(AmazonS3DTarget.class, amazonS3Target).build();
    targetRunner.runInit();/*w w w  . j  av  a  2  s.co  m*/

    List<Record> logRecords = TestUtil.createStringRecords();

    //Make sure the prefix is empty
    ObjectListing objectListing = s3client.listObjects(BUCKET_NAME, prefix);
    Assert.assertTrue(objectListing.getObjectSummaries().isEmpty());

    targetRunner.runWrite(logRecords);
    targetRunner.runDestroy();

    //check that prefix contains 1 file
    objectListing = s3client.listObjects(BUCKET_NAME, prefix);
    Assert.assertEquals(1, objectListing.getObjectSummaries().size());
    S3ObjectSummary objectSummary = objectListing.getObjectSummaries().get(0);

    //get contents of file and check data - should have 9 lines
    S3Object object = s3client.getObject(BUCKET_NAME, objectSummary.getKey());
    S3ObjectInputStream objectContent = object.getObjectContent();

    Assert.assertTrue(object.getKey().endsWith(".gz"));

    List<String> stringList = IOUtils.readLines(new GZIPInputStream(objectContent));
    Assert.assertEquals(9, stringList.size());
    for (int i = 0; i < 9; i++) {
        Assert.assertEquals(TestUtil.TEST_STRING + i, stringList.get(i));
    }
}

From source file:com.taobao.diamond.server.service.task.processor.UpdateConfigInfoTaskProcessor.java

@SuppressWarnings("unchecked")
private String generateNewContentByIdentity(String oldContent, String appendant, String appendantIdentity) {
    StringBuilder sb = new StringBuilder();
    StringReader reader = null;/*from w  w w.  j  a  va 2  s .co  m*/
    try {
        reader = new StringReader(oldContent);
        List<String> lines = IOUtils.readLines(reader);
        for (String line : lines) {
            if (!line.contains(appendantIdentity)) {
                sb.append(line);
                sb.append(Constants.DIAMOND_LINE_SEPARATOR);
            }
        }
        sb.append(appendant);

        return sb.toString();
    } catch (Exception e) {
        log.error("", e);
        return oldContent;
    } finally {
        reader.close();
    }
}

From source file:de.xwic.sandbox.base.model.util.ConfigurationUtil.java

/**
 * @param domain//  ww w .j  a  v a2 s  .  c o m
 * @return
 * @throws ConfigurationException
 */
public static String getResourceAsString(final Domain domain, String resourceId) throws ConfigurationException {
    if (domain == null) {
        throw new ConfigurationException("Domain should not be null");
    }

    final Resource resource = domain.getResource(resourceId);
    InputStream stream = null;
    try {
        stream = resource.getLocation().openStream();
        final String join = StringUtils.join(IOUtils.readLines(stream), "\n");
        return join;
    } catch (IOException e) {
        throw new ConfigurationException("Error in reading resource file for id " + resourceId, e);
    } finally {
        StreamUtil.close(stream);
    }
}

From source file:com.btoddb.chronicle.FileTestUtils.java

public Matcher<? super File> hasCount(final int count) {
    return new TypeSafeMatcher<File>() {
        String errorDesc;//w w  w .  java 2 s  .  c  o m
        String expected;
        String got;

        @Override
        protected boolean matchesSafely(final File f) {
            FileReader fr = null;
            try {
                fr = new FileReader(f);
                return count == IOUtils.readLines(fr).size();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                return false;
            } catch (IOException e) {
                e.printStackTrace();
                return false;
            } finally {
                if (null != fr) {
                    try {
                        fr.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }

        }

        @Override
        public void describeTo(final Description description) {
            description.appendText(errorDesc).appendValue(expected);
        }

        @Override
        protected void describeMismatchSafely(final File item, final Description mismatchDescription) {
            mismatchDescription.appendText("  was: ").appendValue(got);
        }
    };
}

From source file:com.streamsets.pipeline.stage.executor.s3.TestAmazonS3Executor.java

@Test
public void testCopyObjectDeleteOriginal() throws Exception {
    String newName = UUID.randomUUID().toString();

    AmazonS3ExecutorConfig config = getConfig();
    config.taskConfig.taskType = TaskType.COPY_OBJECT;
    config.taskConfig.dropAfterCopy = true;
    config.taskConfig.copyTargetLocation = newName;

    AmazonS3Executor executor = new AmazonS3Executor(config);
    TargetRunner runner = new TargetRunner.Builder(AmazonS3DExecutor.class, executor).build();
    runner.runInit();//from  w  w  w .j a  v  a 2  s . c  o m

    try {
        s3client.putObject(new PutObjectRequest(BUCKET_NAME, objectName, IOUtils.toInputStream("dropAfterCopy"),
                new ObjectMetadata()));
        runner.runWrite(ImmutableList.of(getTestRecord()));

        S3Object object = s3client.getObject(BUCKET_NAME, newName);
        S3ObjectInputStream objectContent = object.getObjectContent();

        List<String> stringList = IOUtils.readLines(objectContent);
        Assert.assertEquals(1, stringList.size());
        Assert.assertEquals("dropAfterCopy", stringList.get(0));

        Assert.assertFalse(s3client.doesObjectExist(BUCKET_NAME, objectName));

        Assert.assertEquals(1, runner.getEventRecords().size());
        assertEvent(runner.getEventRecords().get(0), newName);
    } finally {
        runner.runDestroy();
    }
}