Example usage for org.apache.commons.io LineIterator nextLine

List of usage examples for org.apache.commons.io LineIterator nextLine

Introduction

In this page you can find the example usage for org.apache.commons.io LineIterator nextLine.

Prototype

public String nextLine() 

Source Link

Document

Returns the next line in the wrapped Reader.

Usage

From source file:org.apache.camel.component.exec.ExecutableJavaProgram.java

public static void main(String[] args) throws Exception {
    if (args == null || args.length == 0) {
        throw new IllegalArgumentException("Empty args are not allowed.");
    }//from   ww w .ja v  a  2s  .c om

    if (args[0].equals(PRINT_IN_STDOUT)) {
        System.out.print(PRINT_IN_STDOUT);
        System.exit(0);
    } else if (args[0].equals(PRINT_ARGS_STDOUT)) {
        for (int i = 0; i < args.length; i++) {
            String arg = args[i];
            System.out.println(i + arg);
        }
        System.exit(0);
    } else if (args[0].equals(PRINT_IN_STDERR)) {
        System.err.print(PRINT_IN_STDERR);
        System.exit(1);
    } else if (args[0].equals(String.valueOf(EXIT_WITH_VALUE_0))) {
        System.exit(0);
    } else if (args[0].equals(String.valueOf(EXIT_WITH_VALUE_1))) {
        System.exit(1);
    } else if (args[0].equals(THREADS)) {
        Thread stderrPrinterThread = new Thread(new ErrPrinter());
        Thread stdoutPrinterThread = new Thread(new OutPrinter());

        stderrPrinterThread.start();
        stdoutPrinterThread.start();
        stderrPrinterThread.join();
        stdoutPrinterThread.join();

    } else if (args[0].equals(SLEEP_WITH_TIMEOUT)) {
        doSleep();
        System.exit(0);
    } else if (READ_INPUT_LINES_AND_PRINT_THEM.equals(args[0])) {
        LineIterator iterator = IOUtils.lineIterator(System.in, "UTF-8");
        while (iterator.hasNext()) {
            String line = iterator.nextLine();
            System.out.println(line);

        }
    } else {
        System.out.println(args[0]);
    }

}

From source file:org.apache.flume.source.TestNetcatSource.java

/**
 * Test if an ack is sent for every event in the correct encoding
 *
 * @throws InterruptedException/* w ww . ja v a 2s . co  m*/
 * @throws IOException
 */
@Test
public void testAck() throws InterruptedException, IOException {
    String encoding = "UTF-8";
    String ackEvent = "OK";
    startSource(encoding, "true", "1", "512");
    Socket netcatSocket = new Socket(localhost, selectedPort);
    LineIterator inputLineIterator = IOUtils.lineIterator(netcatSocket.getInputStream(), encoding);
    try {
        // Test on english text snippet
        for (int i = 0; i < 20; i++) {
            sendEvent(netcatSocket, english, encoding);
            Assert.assertArrayEquals("Channel contained our event", english.getBytes(defaultCharset),
                    getFlumeEvent());
            Assert.assertEquals("Socket contained the Ack", ackEvent, inputLineIterator.nextLine());
        }
        // Test on french text snippet
        for (int i = 0; i < 20; i++) {
            sendEvent(netcatSocket, french, encoding);
            Assert.assertArrayEquals("Channel contained our event", french.getBytes(defaultCharset),
                    getFlumeEvent());
            Assert.assertEquals("Socket contained the Ack", ackEvent, inputLineIterator.nextLine());
        }
    } finally {
        netcatSocket.close();
        stopSource();
    }
}

From source file:org.apache.flume.source.TestNetcatSource.java

/**
 * Test that line above MaxLineLength are discarded
 *
 * @throws InterruptedException//  www  . j  av  a  2  s  .  com
 * @throws IOException
 */
@Test
public void testMaxLineLengthwithAck() throws InterruptedException, IOException {
    String encoding = "UTF-8";
    String ackEvent = "OK";
    String ackErrorEvent = "FAILED: Event exceeds the maximum length (10 chars, including newline)";
    startSource(encoding, "true", "1", "10");
    Socket netcatSocket = new Socket(localhost, selectedPort);
    LineIterator inputLineIterator = IOUtils.lineIterator(netcatSocket.getInputStream(), encoding);
    try {
        sendEvent(netcatSocket, "123456789", encoding);
        Assert.assertArrayEquals("Channel contained our event", "123456789".getBytes(defaultCharset),
                getFlumeEvent());
        Assert.assertEquals("Socket contained the Ack", ackEvent, inputLineIterator.nextLine());
        sendEvent(netcatSocket, english, encoding);
        Assert.assertEquals("Channel does not contain an event", null, getRawFlumeEvent());
        Assert.assertEquals("Socket contained the Error Ack", ackErrorEvent, inputLineIterator.nextLine());
    } finally {
        netcatSocket.close();
        stopSource();
    }
}

From source file:org.apache.ignite.internal.client.impl.ClientPropertiesConfigurationSelfTest.java

/**
 * Uncomment properties./* w  w  w  .j av  a2s .c  om*/
 *
 * @param url Source to uncomment client properties for.
 * @return Temporary file with uncommented client properties.
 * @throws IOException In case of IO exception.
 */
private File uncommentProperties(URL url) throws IOException {
    InputStream in = url.openStream();

    assertNotNull(in);

    LineIterator it = IOUtils.lineIterator(in, "UTF-8");
    Collection<String> lines = new ArrayList<>();

    while (it.hasNext())
        lines.add(it.nextLine().replace("#ignite.client.", "ignite.client."));

    IgniteUtils.closeQuiet(in);

    File tmp = File.createTempFile(UUID.randomUUID().toString(), "properties");

    tmp.deleteOnExit();

    FileUtils.writeLines(tmp, lines);

    return tmp;
}

From source file:org.apache.jackrabbit.oak.upgrade.blob.LengthCachingDataStore.java

private static Map<String, Long> loadMappingData(File mappingFile) throws FileNotFoundException {
    Map<String, Long> mapping = new HashMap<String, Long>();
    log.info("Reading mapping data from {}", mappingFile.getAbsolutePath());
    LineIterator itr = new LineIterator(Files.newReader(mappingFile, Charsets.UTF_8));
    try {/*from   w w  w .j  a  va  2 s .  co  m*/
        while (itr.hasNext()) {
            String line = itr.nextLine();
            int indexOfBar = line.indexOf(SEPARATOR);
            checkState(indexOfBar > 0, "Malformed entry found [%s]", line);
            String length = line.substring(0, indexOfBar);
            String id = line.substring(indexOfBar + 1);
            mapping.put(id.trim(), Long.valueOf(length));
        }
        log.info("Total {} mapping entries found", mapping.size());
    } finally {
        itr.close();
    }
    return mapping;
}

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

/**
 * Parses the data from the supplied InputStream, using the supplied baseURI
 * to resolve any relative URI references.
 *
 * @param in      The InputStream 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.
 *//*from ww  w.jav a 2 s.co m*/
@Override
public void parse(InputStream in, String baseURI) throws IOException, RDFParseException, RDFHandlerException {
    LineIterator it = IOUtils.lineIterator(in, RDFFormat.RDFXML.getCharset());
    try {
        while (it.hasNext()) {
            lineNumber++;

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

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.//from   w ww.j  a va2  s .  com
 *
 * @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.nifi.processors.csv.ExtractCSVHeader.java

@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException {
    final FlowFile original = session.get();
    if (original == null) {
        return;//w  w  w .  j  a  v  a2  s  . co m
    }

    final AtomicBoolean lineFound = new AtomicBoolean(false);
    final Map<String, String> attrs = new HashMap<>();

    final AtomicInteger headerLength = new AtomicInteger(0);

    session.read(original, new InputStreamCallback() {
        @Override
        public void process(InputStream inputStream) throws IOException {
            // TODO expose the charset property?
            LineIterator iterator = IOUtils.lineIterator(inputStream, UTF_8);
            if (iterator.hasNext()) {
                lineFound.set(true);
                final String header = iterator.nextLine();

                final String format = context.getProperty(PROP_FORMAT).getValue();
                final String delimiter = context.getProperty(PROP_DELIMITER)
                        .evaluateAttributeExpressions(original).getValue();
                final String prefix = context.getProperty(PROP_SCHEMA_ATTR_PREFIX)
                        .evaluateAttributeExpressions(original).getValue();

                attrs.put(prefix + ATTR_HEADER_ORIGINAL, header);
                // TODO validate delimiter in the callback first
                final CSVFormat csvFormat = buildFormat(format, delimiter, true, // we assume first line is the header
                        null); // no custom header
                final CSVParser parser = csvFormat.parse(new StringReader(header));
                final Map<String, Integer> headers = parser.getHeaderMap();
                final int columnCount = headers.size();
                attrs.put(prefix + ATTR_HEADER_COLUMN_COUNT, String.valueOf(columnCount));
                for (Map.Entry<String, Integer> h : headers.entrySet()) {
                    // CSV columns are 1-based in Excel
                    attrs.put(prefix + (h.getValue() + 1), h.getKey());
                }

                // strip the header and send to the 'content' relationship
                if (StringUtils.isNotBlank(header)) {
                    int hLength = header.length();
                    // move past the new line if there are more lines
                    if (original.getSize() > hLength + 1) {
                        hLength++;
                    }
                    headerLength.set(hLength);
                }
            }
        }
    });

    if (lineFound.get()) {
        FlowFile ff = session.putAllAttributes(original, attrs);

        int offset = headerLength.get();
        if (offset > 0) {
            FlowFile contentOnly = session.clone(ff, offset, original.getSize() - offset);
            session.transfer(contentOnly, REL_CONTENT);
        }

        session.transfer(ff, REL_ORIGINAL);
    } else {
        session.transfer(original, REL_FAILURE);
    }
}

From source file:org.apache.olingo.client.core.communication.request.batch.ODataBatchUtilities.java

/**
 * Reads headers from the batch starting from the given position.
 * <p>/*from  ww  w .  j a  v a 2s.  c o  m*/
 * Retrieved headers will be added to the map given by target parameter.
 *
 * @param iterator batch iterator.
 * @param target destination of the retrieved headers.
 */
public static void readHeaders(final ODataBatchLineIterator iterator,
        final Map<String, Collection<String>> target) {

    try {
        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
        readBatchPart(new ODataBatchController(iterator, null), baos, true);

        final LineIterator headers = IOUtils.lineIterator(new ByteArrayInputStream(baos.toByteArray()),
                Constants.UTF8);
        while (headers.hasNext()) {
            final String line = headers.nextLine().trim();
            if (StringUtils.isNotBlank(line)) {
                addHeaderLine(line, target);
            }
        }
    } catch (Exception e) {
        LOG.error("Error retrieving headers", e);
        throw new IllegalStateException(e);
    }
}

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/* w w  w. ja  v  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);
    }

}