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(InputStream input, String encoding) throws IOException 

Source Link

Document

Return an Iterator for the lines in an InputStream, using the character encoding specified (or default encoding if null).

Usage

From source file:org.eclipse.gyrex.jobs.internal.externalprocess.AsyncLoggingInputStreamReader.java

@Override
public void run() {
    try {//from   www  .java 2s  .  com
        final LineIterator li = IOUtils.lineIterator(in, null);
        while (!closed && li.hasNext()) {
            final String line = lastLine = li.next();
            switch (level) {
            case ERROR:
                log.error(line);
                break;

            case INFO:
            default:
                log.info(line);
                break;
            }
        }
    } catch (final Exception | LinkageError | AssertionError e) {
        log.error("Unable to read from stream: {}", e.getMessage(), e);
    } finally {
        IOUtils.closeQuietly(in);
    }
}

From source file:org.eclipse.smila.utils.scriptexecution.LogHelper.java

/**
 * Log info from input stream with specified level.
 * /*from w  w  w.j  a  v  a 2  s  .c  o m*/
 * @param log
 *          log
 * @param inputStream
 *          input stream
 * @param logLevel
 *          log level
 * 
 * @throws IOException
 *           IOException
 */
private static void log(final Log log, final InputStream inputStream, final LogLevel logLevel)
        throws IOException {

    final LineIterator lineIterator = IOUtils.lineIterator(inputStream, null);

    while (lineIterator.hasNext()) {
        final String string = lineIterator.nextLine();

        log(log, string, logLevel);
    }
}

From source file:org.gridgain.client.impl.GridClientPropertiesConfigurationSelfTest.java

/**
 * Uncomment properties./*from  w w w . j  a  va  2  s  . c o  m*/
 *
 * @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("#gg.client.", "gg.client."));

    GridUtils.closeQuiet(in);

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

    tmp.deleteOnExit();

    FileUtils.writeLines(tmp, lines);

    return tmp;
}

From source file:org.jasig.maven.notice.AbstractNoticeMojo.java

/**
 * Read the template notice file into a string, converting the line ending to the current OS line endings
 *///from   ww  w .  j  ava2s. c  o m
protected String readNoticeTemplate(ResourceFinder finder) throws MojoFailureException {
    final URL inputFile = finder.findResource(this.noticeTemplate);

    final StringBuilder noticeTemplateContents = new StringBuilder();
    InputStream inputStream = null;
    try {
        inputStream = inputFile.openStream();
        for (final LineIterator lineIterator = IOUtils.lineIterator(new BufferedInputStream(inputStream),
                this.encoding); lineIterator.hasNext();) {
            final String line = lineIterator.next();
            noticeTemplateContents.append(line).append(IOUtils.LINE_SEPARATOR);
        }
    } catch (IOException e) {
        throw new MojoFailureException(
                "Failed to open NOTICE Template File '" + this.noticeTemplate + "' from: " + inputFile, e);
    } finally {
        IOUtils.closeQuietly(inputStream);
    }

    return noticeTemplateContents.toString();
}

From source file:org.kalypso.model.wspm.tuhh.schema.simulation.LengthSectionParser.java

private IStatus processLSFile(final String header, final String footer, final LogHelper log)
        throws FileNotFoundException, IOException {
    final Collection<IStatus> result = new ArrayList<>();
    final WspmWaterBody waterBody = m_calculation.getReach().getWaterBody();
    final TuhhStationComparator stationComparator = new TuhhStationComparator(waterBody.isDirectionUpstreams());

    LineIterator lineIterator = null;//from  w w w  . java2 s  .c  om

    ResultLengthSection lsProc = null;
    try {
        lineIterator = IOUtils.lineIterator(new FileInputStream(m_lsFile),
                IWspmTuhhConstants.WSPMTUHH_CODEPAGE);

        BigDecimal firstStation = null; // station of previous line
        while (lineIterator.hasNext()) {
            if (log.checkCanceled())
                return Status.CANCEL_STATUS;

            final String nextLine = lineIterator.nextLine();

            /* Introduce space around 'NaN' and '***' values to make it parseable */

            if (nextLine.contains("NaN")) //$NON-NLS-1$
                log.log(false, Messages.getString("LengthSectionParser.0")); //$NON-NLS-1$

            // TODO: handle NaN-values to keep information alive (unfortunally BigDecimal throws a NumberFormatException)
            final String cleanLine1 = nextLine.replaceAll("-NaN", " null "); //$NON-NLS-1$ //$NON-NLS-2$
            final String cleanLine2 = cleanLine1.replaceAll("NaN", " null "); //$NON-NLS-1$ //$NON-NLS-2$
            final String cleanLine3 = cleanLine2.replaceAll("-999.999", " null "); //$NON-NLS-1$ //$NON-NLS-2$

            final BigDecimal station = NumberUtils.parseQuietDecimal(cleanLine3, 0, 11,
                    IWspmTuhhConstants.STATION_SCALE);
            final BigDecimal runoff = NumberUtils.parseQuietDecimal(cleanLine3, 17, 27, 3);

            /* Any lines where station or runoff cannot be parsed are filtered out */
            if (Objects.isNull(station, runoff))
                continue;

            /* A new section begins, if the new station is lower than the next station */
            final boolean sectionEnd = firstStation == null
                    || stationComparator.compare(firstStation, station) == 0;

            if (sectionEnd) {
                if (lsProc != null) {
                    final IStatus processorResult = closeProcessor(lsProc, footer);
                    if (!processorResult.isOK())
                        result.add(processorResult);
                }

                log.log(false, Messages.getString(
                        "org.kalypso.model.wspm.tuhh.schema.simulation.LengthSectionParser.2"), runoff); //$NON-NLS-1$
                lsProc = new ResultLengthSection(runoff, m_outputDir, m_calculation, m_epsThinning,
                        m_ovwMapURL);
                lsProc.addLine(header);

                lsProc.setTitlePattern(m_titlePattern);
                lsProc.setLsFilePattern(m_lsFilePattern);
            }

            /* clean line */
            lsProc.addLine(cleanLine3);

            if (firstStation == null)
                firstStation = station;
        }

        if (lsProc != null) {
            final IStatus processorResult = closeProcessor(lsProc, footer);
            if (!processorResult.isOK())
                result.add(processorResult);
        }
    } finally {
        LineIterator.closeQuietly(lineIterator);
    }

    if (result.isEmpty())
        return Status.OK_STATUS;

    final IStatus[] children = result.toArray(new IStatus[result.size()]);
    final String msg = String.format(Messages.getString("LengthSectionParser.1")); //$NON-NLS-1$
    return new MultiStatus(KalypsoModelWspmTuhhSchemaPlugin.getID(), 0, children, msg, null);
}

From source file:org.mskcc.cbio.importer.io.internal.FileUtilsImpl.java

/**
 * Helper function to create DataMatrix.
 *
 * @param data InputStream/*from   w w w .  ja v  a 2s .c  o  m*/
 * @return DataMatrix
 */
private DataMatrix getDataMatrix(InputStream data) throws Exception {

    // iterate over all lines in byte[]
    List<String> columnNames = null;
    List<LinkedList<String>> rowData = null;
    LineIterator it = IOUtils.lineIterator(data, null);
    try {
        int count = -1;
        while (it.hasNext()) {
            // first row is our column heading, create column vector
            if (++count == 0) {
                columnNames = new LinkedList(Arrays.asList(it.nextLine().split(Converter.VALUE_DELIMITER, -1)));
            }
            // all other rows are rows in the table
            else {
                rowData = (rowData == null) ? new LinkedList<LinkedList<String>>() : rowData;
                rowData.add(new LinkedList(Arrays.asList(it.nextLine().split(Converter.VALUE_DELIMITER, -1))));
            }
        }
    } finally {
        LineIterator.closeQuietly(it);
    }

    // problem reading from data?
    if (columnNames == null || rowData == null) {
        if (LOG.isInfoEnabled()) {
            LOG.info(
                    "getDataMatrix(), problem creating DataMatrix from file, data file probably missing data, returning null");
        }
        return null;
    }

    // made it here, we can create DataMatrix
    if (LOG.isInfoEnabled()) {
        LOG.info("creating new DataMatrix(), from file data");
    }

    // outta here
    return new DataMatrix(rowData, columnNames);
}

From source file:org.neo4art.sentiment.service.DictionaryBasicService.java

/**
 * @see org.neo4art.sentiment.service.DictionaryService#saveDictionary()
 *///from   w  w  w.j a  v  a 2 s. com
@Override
public void saveDictionary() throws IOException {
    String alphabet[] = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q",
            "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };

    for (int i = 0; i < alphabet.length; i++) {
        String dictionaryFile = "dictionary" + File.separator + this.locale.getLanguage() + File.separator
                + alphabet[i] + " Words.txt";

        logger.info("Importing dictionary file: " + dictionaryFile);

        LineIterator dictionaryFileIterator = IOUtils.lineIterator(
                getClass().getClassLoader().getResourceAsStream(dictionaryFile), Charset.defaultCharset());

        while (dictionaryFileIterator.hasNext()) {
            this.mergeWordWithoutFlushing(dictionaryFileIterator.next());
        }
    }

    Neo4ArtBatchInserterSingleton.flushLegacyNodeIndex(NLPLegacyIndex.WORD_LEGACY_INDEX);
}

From source file:org.neo4art.sentiment.service.DictionaryBasicService.java

/**
 * //www  . j a  v  a2  s.  c om
 * @param file
 * @param polarity
 * @throws IOException
 */
private void addPolarity(String file, int polarity) throws IOException {
    logger.info("Adding polarity from file: " + file);

    LineIterator polarityFile = IOUtils.lineIterator(getClass().getClassLoader().getResourceAsStream(file),
            Charset.defaultCharset());

    while (polarityFile.hasNext()) {
        String word = polarityFile.nextLine();

        if (StringUtils.isNotEmpty(word) && !StringUtils.startsWith(word, ";")) {
            Long wordNodeId = this.mergeWordWithoutFlushing(DictionaryUtils.escapeWordForLuceneSearch(word));

            Neo4ArtBatchInserterSingleton.setNodeProperty(wordNodeId, Word.POLARITY_PROPERTY_NAME, polarity);
        }
    }

    Neo4ArtBatchInserterSingleton.flushLegacyNodeIndex(NLPLegacyIndex.WORD_LEGACY_INDEX);
}

From source file:org.occiware.clouddesigner.occi.docker.connector.dockermachine.util.DockerUtil.java

public static String asString(final InputStream response) {
    final StringWriter logwriter = new StringWriter();
    try {//from  ww  w  . j  a  v  a 2 s .  c o m
        final LineIterator itr = IOUtils.lineIterator(response, "UTF-8");
        while (itr.hasNext()) {
            {
                String line = itr.next();
                boolean _hasNext = itr.hasNext();
                if (_hasNext) {
                    logwriter.write((line + "\n"));
                } else {
                    logwriter.write((line + ""));
                }
            }
        }
        response.close();
        return logwriter.toString();
    } catch (final Throwable _t) {
        if (_t instanceof IOException) {
            final IOException e = (IOException) _t;
            throw new RuntimeException(e);
        } else {
            throw Exceptions.sneakyThrow(_t);
        }
    } finally {
        IOUtils.closeQuietly(response);
    }
}

From source file:org.occiware.clouddesigner.occi.hypervisor.connector.libvirt.util.DomainMarshaller.java

public String asString(final String uuid) {
    try {/* www . j a va 2  s.c  om*/
        final String filename = (("/" + uuid) + ".xml");
        final StringWriter logwriter = new StringWriter();
        final File tmpDir = this.createTempDir(this.xmlDirectory);
        String _plus = (tmpDir + "/");
        String _plus_1 = (_plus + filename);
        InputStream response = new FileInputStream(_plus_1);
        try {
            final LineIterator itr = IOUtils.lineIterator(response, "UTF-8");
            while (itr.hasNext()) {
                {
                    String line = itr.next();
                    boolean _hasNext = itr.hasNext();
                    if (_hasNext) {
                        logwriter.write((line + "\n"));
                    } else {
                        logwriter.write((line + ""));
                    }
                }
            }
            response.close();
            return logwriter.toString();
        } catch (final Throwable _t) {
            if (_t instanceof IOException) {
                final IOException e = (IOException) _t;
                throw new RuntimeException(e);
            } else {
                throw Exceptions.sneakyThrow(_t);
            }
        } finally {
            IOUtils.closeQuietly(response);
        }
    } catch (Throwable _e) {
        throw Exceptions.sneakyThrow(_e);
    }
}