RecordReader extends WrappedReader { public String readRecord() throws IOException { sb.setLength(0); delimIndex = 0; delimChar = firstChar; foundRecord = false; while (!foundRecord && (c = read()) != -1) { if (c != '\r') { sb.append((char) c); if (c == delimChar) { if (delimIndex == lastIndex) { delimIndex = 0; delimChar = firstChar; foundRecord = true; } else { delimIndex++; delimChar = delimiter[delimIndex]; } } else { delimIndex = 0; delimChar = firstChar; } if (foundRecord) { sb.setLength(sb.length() - delimiter.length); if (sb.length() > 0) { recordCount++; } else { foundRecord = false; } } } } if (sb.length() == 0) { return null; } return sb.toString(); } RecordReader(final Reader reader);  RecordReader(final Reader reader, final String recordDelimiter); String readRecord(); long getRecordCount();  }
@Test void testSimple() throws IOException { final RecordReader recordReader = createTest("line1\nline2\nline3", null); assertThat(recordReader.readRecord()).isEqualTo("line1"); assertThat(recordReader.readRecord()).isEqualTo("line2"); assertThat(recordReader.readRecord()).isEqualTo("line3"); assertThat(recordReader.readRecord()).isNull(); }
@Test void testBlankLineBreak() throws IOException { final RecordReader recordReader = createTest("line1\nline2\n\nline3", "\n\n"); assertThat(recordReader.readRecord()).isEqualTo("line1\nline2"); assertThat(recordReader.readRecord()).isEqualTo("line3"); assertThat(recordReader.readRecord()).isNull(); }
@Test void testBlankLineBreak2() throws IOException { final RecordReader recordReader = createTest("line1\nline2\n\nline3\n", "\n\n"); assertThat(recordReader.readRecord()).isEqualTo("line1\nline2"); assertThat(recordReader.readRecord()).isEqualTo("line3\n"); assertThat(recordReader.readRecord()).isNull(); }
@Test void testBlankLineBreak3() throws IOException { final RecordReader recordReader = createTest("line1\nline2\n\nline3\n\n", "\n\n"); assertThat(recordReader.readRecord()).isEqualTo("line1\nline2"); assertThat(recordReader.readRecord()).isEqualTo("line3"); assertThat(recordReader.readRecord()).isNull(); }
@Test void testBlankLineBreak4StartingWithBlankLine() throws IOException { final RecordReader recordReader = createTest("\nline1\nline2\n\nline3\n\n", "\n\n"); assertThat(recordReader.readRecord()).isEqualTo("\nline1\nline2"); assertThat(recordReader.readRecord()).isEqualTo("line3"); assertThat(recordReader.readRecord()).isNull(); }
@Test void testUnixMultiLineFormat1() throws IOException { final RecordReader recordReader = createTest("----\nline1\nline2\n----\nline3\n----\n", "----\n"); assertThat(recordReader.readRecord()).isEqualTo("line1\nline2\n"); assertThat(recordReader.readRecord()).isEqualTo("line3\n"); assertThat(recordReader.readRecord()).isNull(); }
@Test void testUnixMultiLineFormat2() throws IOException { final RecordReader recordReader = createTest("line1\nline2\n----\nline3\n----\n", "\n----\n"); assertThat(recordReader.readRecord()).isEqualTo("line1\nline2"); assertThat(recordReader.readRecord()).isEqualTo("line3"); assertThat(recordReader.readRecord()).isNull(); }
@Test void testUnixMultiLineFormat3OddDelimiters() throws IOException { final RecordReader recordReader = createTest("line1\nline2\n----\nline3\n----\n----\n----\n", "----\n"); assertThat(recordReader.readRecord()).isEqualTo("line1\nline2\n"); assertThat(recordReader.readRecord()).isEqualTo("line3\n"); assertThat(recordReader.readRecord()).isNull(); }