Example usage for java.io Reader mark

List of usage examples for java.io Reader mark

Introduction

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

Prototype

public void mark(int readAheadLimit) throws IOException 

Source Link

Document

Marks the present position in the stream.

Usage

From source file:org.mule.util.ClassUtils.java

private static String classNameHelper(Reader encodedName) {
    // I did consider separating this data from the code, but I could not find a
    // solution that was as clear to read, or clearly motivated (these data are not
    // used elsewhere).

    try {//from   ww  w . j  av a2s.c  om
        encodedName.mark(1);
        switch (encodedName.read()) {
        case -1:
            return "null";
        case 'Z':
            return "boolean";
        case 'B':
            return "byte";
        case 'C':
            return "char";
        case 'D':
            return "double";
        case 'F':
            return "float";
        case 'I':
            return "int";
        case 'J':
            return "long";
        case 'S':
            return "short";
        case '[':
            return classNameHelper(encodedName) + "[]";
        case 'L':
            return shorten(new BufferedReader(encodedName).readLine());
        default:
            encodedName.reset();
            return shorten(new BufferedReader(encodedName).readLine());
        }
    } catch (IOException e) {
        return "unknown type: " + e.getMessage();
    }
}

From source file:org.regenstrief.util.Util.java

/**
 * Retrieves a Reader that can be marked and marks it
 * //  w  w w.jav a2 s. co m
 * @param in the Reader
 * @return the markable Reader
 **/
public static final Reader getMarkableReader(Reader in) {
    in = in.markSupported() ? in : new BufferedReader(in);
    try {
        in.mark(MAX_MARK_BUFFER);
    } catch (final IOException e) {
        throw new RuntimeException(e);
    }
    return in;
}

From source file:org.transitime.utils.csv.CsvBaseReader.java

/**
 * Parse the CSV file. Reads in the header info and then each line. Calls
 * the abstract handleRecord() method for each record. Adds each resulting
 * CSV object to the gtfsObjecgts array.
 */// w  w  w.j  a v a  2s.  co m
private void parse() {
    CSVRecord record = null;
    try {
        IntervalTimer timer = new IntervalTimer();

        logger.debug("Parsing CSV file {} ...", fileName);

        // Open the file for reading. Use UTF-8 format since that will work
        // for both regular ASCII format and UTF-8 extended format files 
        // since UTF-8 was designed to be backwards compatible with ASCII. 
        // This way will work for Chinese and other character sets. Use
        // InputStreamReader so can specify that using UTF-8 format. Use
        // BufferedReader so that can determine if first character is an
        // optional BOM (Byte Order Mark) character used to indicate that 
        // file is in UTF-8 format. BufferedReader allows us to read in
        // first character and then discard if it is a BOM character or
        // reset the reader to back to the beginning if it is not. This
        // way the CSV parser will process the file starting with the first
        // true character.         
        Reader in = new BufferedReader(new InputStreamReader(new FileInputStream(fileName), "UTF-8"));

        // Deal with the possible BOM character at the beginning of the file
        in.mark(1);
        int firstRead = in.read();
        final int BOM_CHARACTER = 0xFEFF;
        if (firstRead != BOM_CHARACTER)
            in.reset();

        // Get ready to parse the CSV file.
        // Allow lines to be comments if they start with "-" so that can
        // easily comment out problems and also test what happens when
        // certain data is missing. Using the '-' character so can
        // comment out line that starts with "--", which is what is 
        // used for SQL. 
        CSVFormat formatter = CSVFormat.DEFAULT.withHeader().withCommentMarker('-');

        // Parse the file
        Iterable<CSVRecord> records = formatter.parse(in);

        logger.debug("Finished CSV parsing of file {}. Took {} msec.", fileName, timer.elapsedMsec());

        int lineNumberWhenLogged = 0;
        timer = new IntervalTimer();
        IntervalTimer loggingTimer = new IntervalTimer();

        Iterator<CSVRecord> iterator = records.iterator();
        while (iterator.hasNext()) {
            // Determine the record to process
            record = iterator.next();

            // If blank line then skip it. This way avoid error messages since
            // expected data column won't exist
            if (record.size() == 0)
                continue;

            // Process the record using appropriate handler
            // and create the corresponding CSV object
            T gtfsObject;
            try {
                gtfsObject = handleRecord(record, supplemental);
            } catch (ParseException e) {
                logger.error("ParseException occurred for record {} "
                        + "(comment lines not included when determing record #) for " + "filename {} . {}",
                        record.getRecordNumber(), fileName, e.getMessage());

                // Continue even though there was an error so that all errors 
                // logged at once.               
                continue;
            } catch (NumberFormatException e) {
                logger.error("NumberFormatException occurred for record {} "
                        + "(comment lines not included when determing record #) " + "for filename {} . {}",
                        record.getRecordNumber(), fileName, e.getMessage());

                // Continue even though there was an error so that all errors 
                // logged at once.               
                continue;
            }

            // Add the newly created CSV object to the object list
            if (gtfsObject != null)
                gtfsObjects.add(gtfsObject);

            // Log info if it has been a while. Check only every 20,000
            // lines to see if the 10 seconds has gone by. If so, then log
            // number of lines. By only looking at timer every 20,000 lines
            // not slowing things down by for every line doing system call 
            // for to get current time.
            final int LINES_TO_PROCESS_BEFORE_CHECKING_IF_SHOULD_LOG = 20000;
            final long SECONDS_ELSAPSED_UNTIL_SHOULD_LOG = 5;
            if (record.getRecordNumber() >= lineNumberWhenLogged
                    + LINES_TO_PROCESS_BEFORE_CHECKING_IF_SHOULD_LOG) {
                lineNumberWhenLogged = (int) record.getRecordNumber();
                if (loggingTimer.elapsedMsec() > SECONDS_ELSAPSED_UNTIL_SHOULD_LOG * Time.MS_PER_SEC) {
                    logger.info("  Processed {} lines. Took {} msec...", lineNumberWhenLogged,
                            timer.elapsedMsec());
                    loggingTimer = new IntervalTimer();
                }
            }
        } // End of while iterating over records

        // Close up the file reader
        in.close();

        // Determine number of records for logging message
        long numberRecords = 0;
        if (record != null)
            numberRecords = record.getRecordNumber();

        logger.info("Finished parsing {} records from file {} . Took {} msec.", numberRecords, fileName,
                timer.elapsedMsec());
    } catch (FileNotFoundException e) {
        if (required)
            logger.error("Required CSV file {} not found.", fileName);
        else
            logger.info("CSV file {} not found but OK because this file " + "not required.", fileName);
    } catch (IOException e) {
        logger.error("IOException occurred when reading in filename {}.", fileName, e);
    }
}