
com.puresoltechnologies.streaming.csv.CSVRecordReader Maven / Gradle / Ivy
The newest version!
package com.puresoltechnologies.streaming.csv;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import com.puresoltechnologies.streaming.streams.InputStreamIterator.InputStreamPartReader;
/**
* This method reads a single CSVRecord from the input stream.
*
* @author Rick-Rainer Ludwig
*/
public class CSVRecordReader implements InputStreamPartReader {
private final Charset charset;
public CSVRecordReader() {
this(Charset.defaultCharset());
}
public CSVRecordReader(Charset charset) {
this.charset = charset;
}
@Override
public CSVRecord readPart(InputStream inputStream) {
try {
String line = readLine(inputStream);
if (line == null) {
return null;
}
CSVRecord record = new CSVRecord();
String[] fields = line.split(",");
for (int fieldId = 0; fieldId < fields.length; ++fieldId) {
record.addField(fields[fieldId]);
}
return record;
} catch (IOException e) {
return null;
}
}
private String readLine(InputStream inputStreamReader) throws IOException {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
int quoteCount = 0;
while (true) {
int c = inputStreamReader.read();
if (c == -1) {
return null;
}
if ((c == '\n') || (c == '\r')) {
if ((byteArrayOutputStream.size() == 0)) {
continue;
}
if (quoteCount % 2 == 0) {
break;
}
}
byteArrayOutputStream.write(c);
if (c == '"') {
++quoteCount;
}
}
return byteArrayOutputStream.toString(charset.name());
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy