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

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

Introduction

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

Prototype

public static LineIterator lineIterator(Reader reader) 

Source Link

Document

Return an Iterator for the lines in a Reader.

Usage

From source file:mitm.common.security.smime.SMIMEBuilderImplTest.java

@Test
public void testEncryptBase64EncodeBug() throws Exception {
    MimeMessage message = new MimeMessage(MailSession.getDefaultSession());

    message.setSubject("test");
    message.setContent("test", "text/plain");

    SMIMEBuilder builder = new SMIMEBuilderImpl(message, "to", "subject", "from");

    X509Certificate certificate = TestUtils
            .loadCertificate("test/resources/testdata/certificates/certificate-base64-encode-bug.cer");

    builder.addRecipient(certificate, SMIMERecipientMode.ISSUER_SERIAL);

    builder.encrypt(SMIMEEncryptionAlgorithm.DES_EDE3_CBC);

    MimeMessage newMessage = builder.buildMessage();

    newMessage.saveChanges();//  w  w w.j  av  a2 s  .com

    File file = new File(tempDir, "testEncryptBase64EncodeBug.eml");

    FileOutputStream output = new FileOutputStream(file);

    MailUtils.writeMessage(newMessage, output);

    newMessage = MailUtils.loadMessage(file);

    ByteArrayOutputStream bos = new ByteArrayOutputStream();

    newMessage.writeTo(new SkipHeadersOutputStream(bos));

    String blob = new String(bos.toByteArray(), "us-ascii");

    // check if all lines are not longer than 76 characters
    LineIterator it = IOUtils.lineIterator(new StringReader(blob));

    while (it.hasNext()) {
        String next = it.nextLine();

        if (next.length() > 76) {
            fail("Line length exceeds 76: " + next);
        }
    }
}

From source file:org.apache.jackrabbit.core.Tail.java

/**
 * Returns the lines that were written to the file since
 * <code>Tail.start()</code> or the last call to <code>getLines()</code>.
 *
 * @return the matching lines.//from  ww  w  . jav a  2 s.  co m
 * @throws IOException if an error occurs while reading from the file.
 */
public Iterable<String> getLines() throws IOException {
    return new Iterable<String>() {
        public Iterator<String> iterator() {
            Iterator<String> it = IOUtils.lineIterator(reader);
            if (grep == null || grep.length() == 0) {
                return it;
            } else {
                // filter
                return new FilterIterator<String>(it, new Predicate() {
                    public boolean evaluate(Object o) {
                        return o.toString().contains(grep);
                    }
                });
            }
        }
    };
}

From source file:org.apache.marmotta.loader.rio.GeonamesParser.java

/**
 * Parses the data from the supplied Reader, using the supplied baseURI to
 * resolve any relative URI references./*w  w  w. j  a  v  a  2  s . c  o  m*/
 *
 * @param reader  The Reader from which to read the data.
 * @param baseURI The URI associated with the data in the InputStream.
 * @throws java.io.IOException                 If an I/O error occurred while data was read from the InputStream.
 * @throws org.openrdf.rio.RDFParseException   If the parser has found an unrecoverable parse error.
 * @throws org.openrdf.rio.RDFHandlerException If the configured statement handler has encountered an
 *                                             unrecoverable error.
 */
@Override
public void parse(Reader reader, String baseURI) throws IOException, RDFParseException, RDFHandlerException {
    LineIterator it = IOUtils.lineIterator(reader);
    try {
        while (it.hasNext()) {
            lineNumber++;

            String line = it.nextLine();
            if (lineNumber % 2 == 1) {
                // only odd line numbers contain triples
                StringReader buffer = new StringReader(line);
                lineParser.parse(buffer, baseURI);
            }
        }
    } finally {
        it.close();
    }
}

From source file:org.apache.sling.distribution.queue.impl.simple.SimpleDistributionQueueProvider.java

public void enableQueueProcessing(@Nonnull DistributionQueueProcessor queueProcessor, String... queueNames) {

    if (checkpoint) {
        // recover from checkpoints
        log.debug("recovering from checkpoints if needed");
        for (final String queueName : queueNames) {
            log.debug("recovering for queue {}", queueName);
            DistributionQueue queue = getQueue(queueName);
            FilenameFilter filenameFilter = new FilenameFilter() {
                @Override//from  w  w  w  .  j  av a  2 s . c om
                public boolean accept(File file, String name) {
                    return name.equals(queueName + "-checkpoint");
                }
            };
            for (File qf : checkpointDirectory.listFiles(filenameFilter)) {
                log.info("recovering from checkpoint {}", qf);
                try {
                    LineIterator lineIterator = IOUtils.lineIterator(new FileReader(qf));
                    while (lineIterator.hasNext()) {
                        String s = lineIterator.nextLine();
                        String[] split = s.split(" ");
                        String id = split[0];
                        String infoString = split[1];
                        Map<String, Object> info = new HashMap<String, Object>();
                        JSONTokener jsonTokener = new JSONTokener(infoString);
                        JSONObject jsonObject = new JSONObject(jsonTokener);
                        Iterator<String> keys = jsonObject.keys();
                        while (keys.hasNext()) {
                            String key = keys.next();
                            JSONArray v = jsonObject.optJSONArray(key);
                            if (v != null) {
                                String[] a = new String[v.length()];
                                for (int i = 0; i < a.length; i++) {
                                    a[i] = v.getString(i);
                                }
                                info.put(key, a);
                            } else {
                                info.put(key, jsonObject.getString(key));
                            }
                        }
                        queue.add(new DistributionQueueItem(id, info));
                    }
                    log.info("recovered {} items from queue {}", queue.getStatus().getItemsCount(), queueName);
                } catch (FileNotFoundException e) {
                    log.warn("could not read checkpoint file {}", qf.getAbsolutePath());
                } catch (JSONException e) {
                    log.warn("could not parse info from checkpoint file {}", qf.getAbsolutePath());
                }
            }
        }

        // enable checkpointing
        for (String queueName : queueNames) {
            ScheduleOptions options = scheduler.NOW(-1, 15).canRunConcurrently(false)
                    .name(getJobName(queueName + "-checkpoint"));
            scheduler.schedule(new SimpleDistributionQueueCheckpoint(getQueue(queueName), checkpointDirectory),
                    options);
        }
    }

    // enable processing
    for (String queueName : queueNames) {
        ScheduleOptions options = scheduler.NOW(-1, 1).canRunConcurrently(false).name(getJobName(queueName));
        scheduler.schedule(new SimpleDistributionQueueProcessor(getQueue(queueName), queueProcessor), options);
    }

}

From source file:org.canova.api.records.reader.impl.LineRecordReader.java

@Override
public void initialize(InputSplit split) throws IOException, InterruptedException {
    if (split instanceof StringSplit) {
        StringSplit stringSplit = (StringSplit) split;
        iter = Arrays.asList(stringSplit.getData()).listIterator();
    } else if (split instanceof InputStreamInputSplit) {
        InputStream is = ((InputStreamInputSplit) split).getIs();
        if (is != null) {
            iter = IOUtils.lineIterator(new InputStreamReader(is));
        }/*w w  w  .  j av  a 2  s .  c  om*/
    } else {
        this.locations = split.locations();
        if (locations != null && locations.length > 0) {
            iter = IOUtils.lineIterator(new InputStreamReader(locations[0].toURL().openStream()));
        }
    }
    this.inputSplit = split;
}

From source file:org.canova.api.records.reader.impl.LineRecordReader.java

@Override
public Collection<Writable> next() {
    List<Writable> ret = new ArrayList<>();

    if (iter.hasNext()) {
        ret.add(new Text(iter.next()));
        return ret;
    } else {/*from w  ww  . ja  va  2  s  .c om*/
        if (!(inputSplit instanceof StringSplit) && currIndex < locations.length - 1) {
            currIndex++;
            try {
                close();
                iter = IOUtils.lineIterator(new InputStreamReader(locations[currIndex].toURL().openStream()));
            } catch (IOException e) {
                e.printStackTrace();
            }

            if (iter.hasNext()) {
                ret.add(new Text(iter.next()));
                return ret;
            }
        }

        throw new NoSuchElementException("No more elements found!");
    }
}

From source file:org.canova.api.records.reader.impl.LineRecordReader.java

@Override
public boolean hasNext() {
    if (iter != null && iter.hasNext()) {
        return true;
    } else {//  w w w . j  a  va 2s  .  com
        if (locations != null && !(inputSplit instanceof StringSplit) && currIndex < locations.length - 1) {
            currIndex++;
            try {
                close();
                iter = IOUtils.lineIterator(new InputStreamReader(locations[currIndex].toURL().openStream()));
            } catch (IOException e) {
                e.printStackTrace();
            }

            return iter.hasNext();
        }

        return false;
    }
}

From source file:org.datavec.api.records.reader.impl.csv.CSVSequenceRecordReader.java

private List<List<Writable>> loadAndClose(InputStream inputStream) {
    LineIterator lineIter = null;//from  w w w  .ja  v a 2 s .c om
    try {
        lineIter = IOUtils.lineIterator(new InputStreamReader(inputStream));
        return load(lineIter);
    } finally {
        if (lineIter != null) {
            lineIter.close();
        }
        IOUtils.closeQuietly(inputStream);
    }
}

From source file:org.datavec.api.records.reader.impl.jackson.JacksonLineSequenceRecordReader.java

private List<List<Writable>> loadAndClose(InputStream inputStream) {
    LineIterator lineIter = null;/* ww w .  j  av  a  2  s .c  o  m*/
    try {
        lineIter = IOUtils.lineIterator(new BufferedReader(new InputStreamReader(inputStream)));
        return load(lineIter);
    } finally {
        if (lineIter != null) {
            lineIter.close();
        }
        IOUtils.closeQuietly(inputStream);
    }
}

From source file:org.datavec.api.records.reader.impl.LineRecordReader.java

@Override
public List<Writable> next() {
    List<Writable> ret = new ArrayList<>();

    if (iter.hasNext()) {
        String record = iter.next();
        invokeListeners(record);// w ww .  j  a  va 2  s. c  o m
        ret.add(new Text(record));
        lineIndex++;
        return ret;
    } else {
        if (!(inputSplit instanceof StringSplit) && splitIndex < locations.length - 1) {
            splitIndex++;
            lineIndex = 0;
            try {
                close();
                iter = IOUtils.lineIterator(new InputStreamReader(locations[splitIndex].toURL().openStream()));
                onLocationOpen(locations[splitIndex]);
            } catch (IOException e) {
                e.printStackTrace();
            }
            lineIndex = 0; //New split opened -> reset line index

            if (iter.hasNext()) {
                String record = iter.next();
                invokeListeners(record);
                ret.add(new Text(record));
                return ret;
            }
        }

        throw new NoSuchElementException("No more elements found!");
    }
}