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:com.marklogic.contentpump.DelimitedJSONReader.java

protected void initFileStream(InputSplit inSplit) throws IOException, InterruptedException {
    setFile(((FileSplit) inSplit).getPath());
    configFileNameAsCollection(conf, file);
    fileIn = fs.open(file);//from www.ja va 2  s. co m
    instream = new InputStreamReader(fileIn, encoding);
    reader = new LineNumberReader(instream);
}

From source file:org.kawanfw.sql.jdbc.util.FileBackedList.java

@SuppressWarnings("unchecked")
@Override//w w w .  j  a v  a2 s  .c o m
public E get(int index) {

    if (lineNumberReader == null) {
        throw new IllegalStateException("FileBaskList has been cleared. Can not be used anymore.");
    }

    try {
        int currPos = lineNumberReader.getLineNumber();

        // if the index + 1 < currPos ==> We can go to the line specified

        if (index >= size) {
            throw new IndexOutOfBoundsException("Passed end of ResultSet: " + index);
        }

        index += 1;

        if (currPos - 1 > index) {
            debug("RESET");
            IOUtils.closeQuietly(lineNumberReader);
            lineNumberReader = new LineNumberReader(new FileReader(file));
            currPos = lineNumberReader.getLineNumber();
        }

        if (index == currPos - 1) {
            debug("the line: " + currentLine);
            return (E) currentLine;
        }

        while (currPos - 1 < index) {
            debug("looping...");
            currentLine = lineNumberReader.readLine();
            currPos++;
        }

        // debug("the line: " + currentLine);
        return (E) currentLine;

    } catch (IOException e) {
        throw new IllegalArgumentException(e);
    }

}

From source file:com.datatorrent.stram.StramLocalClusterTest.java

/**
 * Verify test configuration launches and stops after input terminates.
 * Test validates expected output end to end.
 *
 * @throws Exception/*  w  ww .  j  av  a 2 s  .  c om*/
 */
@Test
public void testLocalClusterInitShutdown() throws Exception {
    TestGeneratorInputOperator genNode = dag.addOperator("genNode", TestGeneratorInputOperator.class);
    genNode.setMaxTuples(2);

    GenericTestOperator node1 = dag.addOperator("node1", GenericTestOperator.class);
    node1.setEmitFormat("%s >> node1");

    File outFile = new File(
            "./target/" + StramLocalClusterTest.class.getName() + "-testLocalClusterInitShutdown.out");
    outFile.delete();

    TestOutputOperator outNode = dag.addOperator("outNode", TestOutputOperator.class);
    outNode.pathSpec = outFile.toURI().toString();

    dag.addStream("fromGenNode", genNode.outport, node1.inport1);

    dag.addStream("fromNode1", node1.outport1, outNode.inport);

    dag.getAttributes().put(LogicalPlan.CONTAINERS_MAX_COUNT, 2);

    StramLocalCluster localCluster = new StramLocalCluster(dag);
    localCluster.setHeartbeatMonitoringEnabled(false);
    localCluster.run();

    Assert.assertTrue(outFile + " exists", outFile.exists());
    LineNumberReader lnr = new LineNumberReader(new FileReader(outFile));
    String line;
    while ((line = lnr.readLine()) != null) {
        Assert.assertTrue("line match " + line, line.matches("" + lnr.getLineNumber() + " >> node1"));
    }
    Assert.assertEquals("number lines", 2, lnr.getLineNumber());
    lnr.close();
}

From source file:net.ontopia.topicmaps.db2tm.CSVImport.java

public void importCSV(InputStream csvfile) throws Exception {
    // Execute statements
    try {//from w w  w .j ava  2 s  . c  om
        System.out.println("TABLE: " + table);

        if (cleartable) {
            String delsql = "delete from " + table;
            log.debug("DELETE: {}", delsql);
            Statement delstm = conn.createStatement();
            delstm.executeUpdate(delsql);
            //! conn.commit();
        }

        // get hold of column metadata
        List<String> colnames = new ArrayList<String>();
        List<Integer> coltypes_ = new ArrayList<Integer>();
        ResultSet rs = conn.getMetaData().getColumns(null, null, table, null);
        try {
            while (rs.next()) {
                String colname = rs.getString(4);
                int coltype = rs.getInt(5);
                colnames.add(colname);
                coltypes_.add(new Integer(coltype));
            }
        } finally {
            rs.close();
        }
        int[] coltypes = new int[coltypes_.size()];
        for (int i = 0; i < coltypes.length; i++) {
            coltypes[i] = coltypes_.get(i).intValue();
        }

        String[] qmarks = new String[coltypes.length];
        for (int i = 0; i < qmarks.length; i++) {
            qmarks[i] = "?";
        }

        String sql = "insert into " + table + " (" + StringUtils.join(colnames, ", ") + ") values ("
                + StringUtils.join(qmarks, ", ") + ")";
        log.debug("INSERT: {}", sql);
        PreparedStatement stm = conn.prepareStatement(sql);

        LineNumberReader reader = new LineNumberReader(new InputStreamReader(csvfile));
        CSVReader csvreader = new CSVReader(reader, separator, quoteCharacter);

        // Ignore first X lines
        for (int i = 0; i < ignorelines; i++) {
            String[] tuple = csvreader.readNext();
            if (tuple == null)
                break;
        }

        // HACK: override date+datetime formats
        JDBCUtils.df_date = new SimpleDateFormat("dd.MM.yyyy");
        JDBCUtils.df_datetime = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss");

        // Process input
        log.debug("[{}]", StringUtils.join(colnames, ", "));
        String[] tuple = null;
        try {
            while ((tuple = csvreader.readNext()) != null) {
                for (int i = 0; i < tuple.length; i++) {
                    System.out.println("V:" + (i + 1) + " " + colnames.get(i) + ":" + coltypes[i] + " "
                            + tuple[i].length() + "'" + tuple[i] + "'");
                    JDBCUtils.setObject(stm, i + 1, tuple[i], coltypes[i]);
                }
                stm.execute();
            }
        } catch (Exception e) {
            throw new OntopiaRuntimeException(
                    "Cannot read line " + reader.getLineNumber() + ": " + Arrays.asList(tuple), e);
        }
        conn.commit();

    } catch (Exception e) {
        //conn.rollback();
        throw e;
    }
}

From source file:org.kalypso.ogc.sensor.adapter.NativeObservationDWD5minAdapter.java

@Override
protected List<NativeObservationDataSet> parse(final File source, final TimeZone timeZone,
        final boolean continueWithErrors, final IStatusCollector stati) throws IOException {
    final List<NativeObservationDataSet> datasets = new ArrayList<>();

    final SimpleDateFormat sdf = new SimpleDateFormat("yyMMdd"); //$NON-NLS-1$
    sdf.setTimeZone(timeZone);/*  w ww . j a va 2s .c om*/

    final FileReader fileReader = new FileReader(source);
    final LineNumberReader reader = new LineNumberReader(fileReader);

    try {
        String lineIn = null;
        int valuesLine = 0;
        int step = SEARCH_BLOCK_HEADER;
        final StringBuffer buffer = new StringBuffer();
        long startDate = 0;

        while ((lineIn = reader.readLine()) != null) {
            if (!continueWithErrors && getErrorCount() > getMaxErrorCount())
                return datasets;

            switch (step) {
            case SEARCH_BLOCK_HEADER:
                final Matcher matcher = DWD_BLOCK_PATTERN.matcher(lineIn);
                if (matcher.matches()) {
                    // String DWDID = matcher.group( 1 );
                    final String startDateString = matcher.group(2);

                    final Matcher dateMatcher = DATE_PATTERN.matcher(startDateString);
                    if (dateMatcher.matches()) {
                        // System.out.println( "Startdatum Header:" + startDateString );
                        try {
                            final Date parseDate = sdf.parse(startDateString);
                            startDate = parseDate.getTime();
                        } catch (final ParseException e) {
                            e.printStackTrace();

                            stati.add(IStatus.WARNING,
                                    String.format(Messages.getString("NativeObservationDWD5minAdapter_0"), //$NON-NLS-1$
                                            reader.getLineNumber(), startDateString, DATE_PATTERN.toString()),
                                    e);
                            tickErrorCount();
                        }
                    } else {
                        stati.add(IStatus.INFO,
                                String.format(Messages.getString("NativeObservationDWD5minAdapter_0"), //$NON-NLS-1$
                                        reader.getLineNumber(), startDateString, DATE_PATTERN.toString()));
                    }
                } else {
                    stati.add(IStatus.WARNING,
                            String.format(Messages.getString("NativeObservationDWD5minAdapter_1"), //$NON-NLS-1$
                                    reader.getLineNumber(), lineIn));
                    tickErrorCount();
                }

                step++;
                break;

            case SEARCH_VALUES:
                valuesLine = valuesLine + 1;
                for (int i = 0; i < 16; i++) {
                    final String valueString = lineIn.substring(i * 5, 5 * (i + 1));
                    Double value = new Double(Double.parseDouble(valueString)) / 1000;
                    // TODO: Write status

                    String src = SOURCE_ID;

                    if (value > 99.997) {
                        value = 0.0;
                        src = SOURCE_ID_MISSING_VALUE;
                    }

                    // Datenfilter fr 0.0 - um Datenbank nicht mit unntigen Werten zu fllen (Zur Zeit nicht verwendet, da
                    // Rohdaten bentigt)
                    buffer.append(" "); // separator //$NON-NLS-1$
                    final Date valueDate = new Date(
                            startDate + i * m_timeStep + (valuesLine - 1) * 16 * m_timeStep);
                    buffer.append(valueDate.toString());

                    datasets.add(new NativeObservationDataSet(valueDate, value, toStatus(src), src));
                }
                if (valuesLine == 18) {
                    step = SEARCH_BLOCK_HEADER;
                    valuesLine = 0;
                }
                break;
            default:
                break;
            }
        }
    } finally {
        IOUtils.closeQuietly(reader);
    }

    return datasets;
}

From source file:com.alibaba.jstorm.daemon.supervisor.SandBoxMaker.java

public String generatePolicyFile(Map<String, String> replaceMap) throws IOException {
    // dynamic generate policy file, no static file
    String tmpPolicy = StormConfig.supervisorTmpDir(conf) + File.separator + UUID.randomUUID().toString();

    InputStream inputStream = SandBoxMaker.class.getClassLoader().getResourceAsStream(SANBOX_TEMPLATE_NAME);

    PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(tmpPolicy)));

    try {// w  w  w  . j  a  v a 2  s.  c o  m

        InputStreamReader inputReader = new InputStreamReader(inputStream);

        BufferedReader reader = new BufferedReader(new LineNumberReader(inputReader));

        String line = null;
        while ((line = reader.readLine()) != null) {
            String replaced = replaceLine(line, replaceMap);

            writer.println(replaced);
        }

        return tmpPolicy;
    } catch (Exception e) {
        LOG.error("Failed to generate policy file\n", e);
        throw new IOException(e);
    } finally {
        if (inputStream != null) {
            inputStream.close();
        }
        if (writer != null) {
            writer.close();
        }

    }
}

From source file:edu.stanford.muse.index.DatedDocument.java

public static StringBuilder formatStringForMaxCharsPerLine(String contents, int CHARS_PER_LINE) {
    StringBuilder sb = new StringBuilder();

    // should be in one time setup
    final String punctuationChars = "., \t!)]";
    final Set<Character> punctuationCharSet = new LinkedHashSet<Character>();
    for (int i = 0; i < punctuationChars.length(); i++)
        punctuationCharSet.add(punctuationChars.charAt(i));

    LineNumberReader lnr = new LineNumberReader(new StringReader(contents));
    while (true) {
        String line = null;//from w w  w.  j  a  va  2s  . c om
        try {
            line = lnr.readLine();
        } catch (IOException ioe) {
            sb.append("Unexpected error while formatting line");
            log.warn("Unexpected io exception while formatting max chars per line: " + Util.stackTrace(ioe));
        }
        if (line == null)
            break;

        if (line.length() < CHARS_PER_LINE) {
            // common case, short line, nothing more to be done.
            sb.append(line);
            sb.append("\n");
            //   log.info ("short line: \"" + line + "\"");
            continue;
        }

        // line is longer than CHARS_PER_LINE. try to break it up.
        String remainingLine = line;
        while (true) {
            if (remainingLine.length() <= CHARS_PER_LINE) {
                sb.append(remainingLine);
                break;
            }

            // line is too long, try to break it up, look backwards from CHARS_PER_LINE to see if there's some punctuation
            int i = CHARS_PER_LINE - 1;
            for (; i >= 0; i--) {
                if (punctuationCharSet.contains(remainingLine.charAt(i)))
                    break;
            }

            // i is the index of the punct. char, or -1 if no punct.
            // emit the line, including the punct.
            if (i == -1) {
                // no break found, emit chars as is
                sb.append(remainingLine.substring(0, CHARS_PER_LINE));
                //      log.info ("no break: \"" + remainingLine.substring(0, CHARS_PER_LINE) + "\"");

                remainingLine = remainingLine.substring(CHARS_PER_LINE);
            } else {
                // break found
                sb.append(remainingLine.substring(0, i + 1));
                sb.append("\n");
                //   log.info ("found line: \"" + remainingLine.substring(0, i+1) + "\"");
                if (remainingLine.length() <= (i + 1))
                    break; // nothing remains

                remainingLine = remainingLine.substring(i + 1);
            }
        }
        sb.append("\n");
        //   log.info ("appended newline");
    }
    return sb;
}

From source file:org.apache.hadoop.yarn.server.nodemanager.TestLinuxContainerExecutorWithMocks.java

private List<String> readMockParams() throws IOException {
    LinkedList<String> ret = new LinkedList<String>();
    LineNumberReader reader = new LineNumberReader(new FileReader(mockParamFile));
    String line;/*ww w  . j  a  v a 2s .c  om*/
    while ((line = reader.readLine()) != null) {
        ret.add(line);
    }
    reader.close();
    return ret;
}

From source file:org.opensextant.extractors.test.TestXCoord.java

/**
 * Deprecated: LineNumberReader is deprecated. Very limited as this reads the entire buffer first.
 *
 * @param filepath file to read.//from  w  w w  .ja  va2  s .c o  m
 * @return LineNumberReader obj
 * @throws FileNotFoundException
 * @throws IOException if reader could not be created
 * @deprecated LineNumber reader is deprecated
 */
@Deprecated
public static LineNumberReader getLineReader(String filepath) throws FileNotFoundException, IOException {
    return new LineNumberReader(new StringReader(FileUtility.readFile(filepath)));
}

From source file:com.jkoolcloud.tnt4j.streams.inputs.JavaInputStream.java

@Override
protected void initialize() throws Exception {
    super.initialize();

    inputReader = new LineNumberReader(
            rawReader instanceof BufferedReader ? rawReader : new BufferedReader(rawReader));
}