All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.opencsv.stream.reader.LineReader Maven / Gradle / Ivy

There is a newer version: 5.9
Show newest version
package com.opencsv.stream.reader;

import com.opencsv.ICSVParser;

import java.io.BufferedReader;
import java.io.IOException;

/**
 * This class was created for
 * issue #106
 * where carriage returns were being removed.
 * This class allows the user to determine if they wish to keep or
 * remove them from the data being read.
 *
 * @author Scott Conway
 * @since 3.3
 */

public class LineReader {
    private final BufferedReader reader;
    private final boolean keepCarriageReturns;

    /**
     * LineReader constructor.
     *
     * @param reader              Reader that data will be read from.
     * @param keepCarriageReturns True if carriage returns should remain in the data, false to remove them.
     */
    public LineReader(BufferedReader reader, boolean keepCarriageReturns) {
        this.reader = reader;
        this.keepCarriageReturns = keepCarriageReturns;
    }

    /**
     * Reads the next line from the Reader.
     *
     * @return Line read from reader.
     * @throws IOException On error from BufferedReader
     */
    public String readLine() throws IOException {
        return keepCarriageReturns ? readUntilNewline() : reader.readLine();
    }

    private String readUntilNewline() throws IOException {
        StringBuilder sb = new StringBuilder(ICSVParser.INITIAL_READ_SIZE);
        for (int c = reader.read(); c > -1 && c != '\n'; c = reader.read()) {
            sb.append((char) c);
        }

        return sb.length() > 0 ? sb.toString() : null;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy