Example usage for java.io LineNumberReader LineNumberReader

List of usage examples for java.io LineNumberReader LineNumberReader

Introduction

In this page you can find the example usage for java.io LineNumberReader LineNumberReader.

Prototype

public LineNumberReader(Reader in) 

Source Link

Document

Create a new line-numbering reader, using the default input-buffer size.

Usage

From source file:pl.otros.logview.importer.UtilLoggingXmlLogImporter.java

@Override
public void importLogs(InputStream in, LogDataCollector collector, ParsingContext parsingContext) {

    StringBuffer sb = new StringBuffer();
    try {//from   w ww .ja  va 2 s  .c om
        LineNumberReader bin = new LineNumberReader(new InputStreamReader(in, ENCODING));
        String line = null;

        while ((line = bin.readLine()) != null) {
            sb.append(line).append("\n");
            if (bin.getLineNumber() % 30 == 0) {
                decodeEvents(sb.toString(), collector, parsingContext);
                sb.setLength(0);
            }
        }

    } catch (UnsupportedEncodingException e) {
        LOGGER.severe("Cant load codepage " + e.getMessage());
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
        decodeEvents(sb.toString(), collector, parsingContext);
    }

}

From source file:org.apache.streams.rss.test.RssStreamProviderIT.java

@Test
public void testRssStreamProvider() throws Exception {

    final String configfile = "./target/test-classes/RssStreamProviderIT.conf";
    final String outfile = "./target/test-classes/RssStreamProviderIT.stdout.txt";

    InputStream is = RssStreamProviderIT.class.getResourceAsStream("/top100.txt");
    InputStreamReader isr = new InputStreamReader(is);
    BufferedReader br = new BufferedReader(isr);

    RssStreamConfiguration configuration = new RssStreamConfiguration();
    List<FeedDetails> feedArray = new ArrayList<>();
    try {// w ww.  j  a v a  2s . c  o  m
        while (br.ready()) {
            String line = br.readLine();
            if (!StringUtils.isEmpty(line)) {
                feedArray.add(new FeedDetails().withUrl(line).withPollIntervalMillis(5000L));
            }
        }
        configuration.setFeeds(feedArray);
    } catch (Exception ex) {
        ex.printStackTrace();
        Assert.fail();
    }

    org.junit.Assert.assertThat(configuration.getFeeds().size(), greaterThan(70));

    OutputStream os = new FileOutputStream(configfile);
    OutputStreamWriter osw = new OutputStreamWriter(os);
    BufferedWriter bw = new BufferedWriter(osw);

    // write conf
    ObjectNode feedsNode = mapper.convertValue(configuration, ObjectNode.class);
    JsonNode configNode = mapper.createObjectNode().set("rss", feedsNode);

    bw.write(mapper.writeValueAsString(configNode));
    bw.flush();
    bw.close();

    File config = new File(configfile);
    assert (config.exists());
    assert (config.canRead());
    assert (config.isFile());

    RssStreamProvider.main(new String[] { configfile, outfile });

    File out = new File(outfile);
    assert (out.exists());
    assert (out.canRead());
    assert (out.isFile());

    FileReader outReader = new FileReader(out);
    LineNumberReader outCounter = new LineNumberReader(outReader);

    while (outCounter.readLine() != null) {
    }

    assert (outCounter.getLineNumber() >= 200);

}

From source file:de.griffel.confluence.plugins.plantuml.type.UmlSourceBuilder.java

public UmlSourceBuilder append(StringReader stringReader) throws IOException {
    final LineNumberReader reader = new LineNumberReader(stringReader);
    String line = reader.readLine();
    if ("ditaa".equals(line) && DiagramType.UML == diagramType) {
        isDitaa = true;/*w  w  w.  ja  va2 s .com*/
    }
    while (line != null) {
        appendLine(line);
        line = reader.readLine();
    }
    return this;
}

From source file:org.onecmdb.core.utils.transform.csv.CSVDataSource.java

public synchronized List<String> load() throws IOException {
    if (loaded) {
        return (lines);
    }//  www. ja  v a  2  s.c om

    lines = new ArrayList<String>();
    int lineIndex = 0;
    for (URL url : urls) {
        URL nUrl = url;
        if (rootPath != null) {
            nUrl = new URL(nUrl.getProtocol(), nUrl.getHost(), nUrl.getPort(), rootPath + "/" + nUrl.getFile());
        }
        InputStream in = nUrl.openStream();
        try {
            LineNumberReader lin = new LineNumberReader(new InputStreamReader(in));
            boolean eof = false;
            while (!eof) {

                String line = lin.readLine();
                if (line == null) {
                    eof = true;
                    continue;
                }
                // Check if line is not terminated due to nl in fields.
                line = handleEndOfLine(lin, line);
                lineIndex++;
                if (lineIndex < headerLines) {
                    log.debug("Add Header:" + line);
                    headers.add(line);
                    continue;
                }
                log.debug("Add Line:[" + lineIndex + "]" + line);
                lines.add(line);
            }
        } catch (IOException de) {
            IOException e = new IOException("Parse error in <" + url.toExternalForm() + ">, ");
            e.initCause(de);
            throw e;
        } finally {
            if (in != null) {
                in.close();
            }
        }
    }
    loaded = true;

    String header = getHeaderData();
    if (headers != null) {
        String headers[] = header.split(getColDelimiter());
        for (int i = 0; i < headers.length; i++) {
            headerMap.put(headers[i], i);
        }
    }
    return (lines);
}

From source file:org.chenillekit.demo.components.LeftSideMenu.java

/**
 * build the menu item list.//w w  w  . j av  a  2s  .c  o  m
 *
 * @return list of menu items
 */
private List<MenuConfiguration> buildMenuItemList() {
    List<MenuConfiguration> menuEntryList = CollectionFactory.newList();
    LineNumberReader lineNumberReader = null;

    try {
        lineNumberReader = new LineNumberReader(new FileReader(new File(configFile.toURL().toURI())));

        String readedLine;
        while ((readedLine = lineNumberReader.readLine()) != null) {
            String[] values = StringUtils.split(readedLine, '|');
            if (!values[0].equalsIgnoreCase("separator")) {
                Object[] contextParameters = null;
                if (values.length == 4 && parameters != null) {
                    String[] placeHolders = values[3].split(",");
                    contextParameters = new Object[placeHolders.length];
                    for (int i = 0; i < placeHolders.length; i++) {
                        String placeHolder = placeHolders[i];
                        contextParameters[i] = parameters.get(placeHolder);
                    }
                }

                menuEntryList.add(new MenuConfiguration(values[0].trim(), values[1].trim(), values[2].trim(),
                        contextParameters));
            } else
                menuEntryList.add(new MenuConfiguration());
        }

        return menuEntryList;
    } catch (FileNotFoundException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    } catch (URISyntaxException e) {
        throw new RuntimeException(e);
    } finally {
        try {
            if (lineNumberReader != null)
                lineNumberReader.close();
        } catch (IOException e) {
        }
    }
}

From source file:org.kalypso.model.hydrology.internal.postprocessing.BlockTimeSeries.java

/**
 * imports all ts from given blockfile/*from  ww w.ja va  2s  . com*/
 */
public void importBlockFile(final File blockFile) {
    LineNumberReader reader = null;
    try {
        reader = new LineNumberReader(new FileReader(blockFile));

        final BlockTimeStep timeStep = searchTimeoffset(reader);

        while (reader.ready()) {
            final Entry<String, Integer> blockInfo = searchBlockHeader(reader);
            if (blockInfo == null)
                break;

            final String key = blockInfo.getKey();
            final int valuesCount = blockInfo.getValue();

            // Add values to existing block
            final Block block = getOrCreateBlock(key, timeStep);
            block.readValues(reader, valuesCount);
        }
        reader.close();
    } catch (final Exception e) {
        e.printStackTrace();
        System.out.println("could not read blockfile "); //$NON-NLS-1$
    } finally {
        IOUtils.closeQuietly(reader);
    }
}

From source file:edu.umn.msi.tropix.common.io.IOUtilsTest.java

@Test(groups = "unit", expectedExceptions = IORuntimeException.class)
public void lineReaderException() {
    final LineNumberReader reader = new LineNumberReader(new ClosedReader());
    ioUtils.readLine(reader);//  www  . j  a  va  2  s .c  o  m
}

From source file:org.gsoft.admin.ScriptRunner.java

/**
 * Runs an SQL script (read in using the Reader parameter) using the
 * connection passed in//from   w  w  w  . j  av a  2  s .c  o m
 * 
 * @param conn
 *            - the connection to use for the script
 * @param reader
 *            - the source of the script
 * @throws SQLException
 *             if any SQL errors occur
 * @throws IOException
 *             if there is an error reading from the Reader
 */
private void runScript(Connection conn, Reader reader) throws IOException, SQLException {
    StringBuffer command = null;
    try {
        LineNumberReader lineReader = new LineNumberReader(reader);
        String line = null;
        while ((line = lineReader.readLine()) != null) {
            if (command == null) {
                command = new StringBuffer();
            }
            String trimmedLine = line.trim();
            if (trimmedLine.startsWith("--")) {
                println(trimmedLine);
            } else if (trimmedLine.length() < 1 || trimmedLine.startsWith("//")) {
                // Do nothing
            } else if (trimmedLine.length() < 1 || trimmedLine.startsWith("--")) {
                // Do nothing
            } else if (!fullLineDelimiter && trimmedLine.endsWith(getDelimiter())
                    || fullLineDelimiter && trimmedLine.equals(getDelimiter())) {
                command.append(line.substring(0, line.lastIndexOf(getDelimiter())));
                command.append(" ");
                Statement statement = conn.createStatement();

                println(command);

                boolean hasResults = false;
                if (stopOnError) {
                    hasResults = statement.execute(command.toString());
                } else {
                    try {
                        statement.execute(command.toString());
                    } catch (SQLException e) {
                        e.fillInStackTrace();
                        printlnError("Error executing: " + command);
                        printlnError(e);
                    }
                }

                if (autoCommit && !conn.getAutoCommit()) {
                    conn.commit();
                }

                ResultSet rs = statement.getResultSet();
                if (hasResults && rs != null) {
                    ResultSetMetaData md = rs.getMetaData();
                    int cols = md.getColumnCount();
                    for (int i = 0; i < cols; i++) {
                        String name = md.getColumnLabel(i);
                        print(name + "\t");
                    }
                    println("");
                    while (rs.next()) {
                        for (int i = 0; i < cols; i++) {
                            String value = rs.getString(i);
                            print(value + "\t");
                        }
                        println("");
                    }
                }

                command = null;
                try {
                    statement.close();
                } catch (Exception e) {
                    // Ignore to workaround a bug in Jakarta DBCP
                }
                Thread.yield();
            } else {
                command.append(line);
                command.append(" ");
            }
        }
        if (!autoCommit) {
            conn.commit();
        }
    } catch (SQLException e) {
        e.fillInStackTrace();
        printlnError("Error executing: " + command);
        printlnError(e);
        throw e;
    } catch (IOException e) {
        e.fillInStackTrace();
        printlnError("Error executing: " + command);
        printlnError(e);
        throw e;
    } finally {
        conn.rollback();
        flush();
    }
}

From source file:com.fortmoon.utils.CSVDBLoader.java

public void load() throws InterruptedException {
    file = new File(fileName);
    tableName = fileName.substring(0, fileName.indexOf('.'));
    String line = null;/*from w  w w . ja  va  2s. c om*/
    String statement = null;
    int counter = 0;
    try {
        reader = new FileReader(file);
        lr = new LineNumberReader(reader);
        line = lr.readLine();
        if (line != null)
            getColumns(line); //Now we read the header, mark the file position for future resets
        //lr.mark(999999);
        getColumnClasses();
        getInsertPreamble();
        createTable();
        resetReader();

        Runnable r = new Runnable() {
            @Override
            public void run() {
                executeBatch();
            }
        };
        Thread t = new Thread(r, "BatchRunner");
        t.start();
        ArrayList<String> statements = new ArrayList<String>(batchSize);
        line = lr.readLine();
        while (line != null) {
            counter++;
            statement = getStatement(line, counter);
            //this.statements.add(statement);
            statements.add(statement);
            // update every 1000 records
            if (counter % batchSize == 0) {
                log.info("Executing batch for line " + counter + " of " + this.numLines);
                //executeBatch();
                queue.put(statements);
                statements = new ArrayList<String>(batchSize);
                /*               try {
                                  Thread.sleep(100);
                               }
                               catch(InterruptedException ie ) {}
                */ }
            line = lr.readLine();
        }
        // get the last ones
        log.info("Executing final batch");
        queue.put(statements);
        //executeBatch();

    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
        try {
            reader.close();
        } catch (IOException e) {
            log.error("Non-Fatal Exception closing FileReader: " + e.getMessage(), e);
        }
    }
}

From source file:eu.eexcess.diversityasurement.wikipedia.RDFCategoryExtractor.java

private long getTotalNumberOfLines(File file) throws FileNotFoundException, IOException {
    long lines = 0;
    LineNumberReader lineNumberReader = new LineNumberReader(new FileReader(file));
    lineNumberReader.skip(Long.MAX_VALUE);
    lines = lineNumberReader.getLineNumber();
    lineNumberReader.close();//w  w w. ja va  2s . c om
    return lines;
}