com.opencsv.stream.reader.LineReader Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of opencsv Show documentation
Show all versions of opencsv Show documentation
A simple library for reading and writing CSV in Java
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;
}
}