CSVReader implements Closeable, Iterable<String[]> { public String[] readNext() throws IOException { String[] result = null; do { String nextLine = getNextLine(); if (!hasNext) { return result; } String[] r = parser.parseLineMulti(nextLine); if (r.length > 0) { if (result == null) { result = r; } else { String[] t = new String[result.length + r.length]; System.arraycopy(result, 0, t, 0, result.length); System.arraycopy(r, 0, t, result.length, r.length); result = t; } } } while (parser.isPending()); return result; } CSVReader(Reader reader);  CSVReader(Reader reader, char separator);  CSVReader(Reader reader, char separator, char quotechar);  CSVReader(Reader reader, char separator, char quotechar, boolean strictQuotes);  CSVReader(Reader reader, char separator,
                     char quotechar, char escape);  CSVReader(Reader reader, char separator, char quotechar, int line);  CSVReader(Reader reader, char separator, char quotechar, char escape, int line);  CSVReader(Reader reader, char separator, char quotechar, char escape, int line, boolean strictQuotes);  CSVReader(Reader reader, char separator, char quotechar, char escape, int line, boolean strictQuotes, boolean ignoreLeadingWhiteSpace);  CSVReader(Reader reader, int line, CSVParser csvParser); List<String[]> readAll(); String[] readNext(); void close(); Iterator<String[]> iterator(); static final int DEFAULT_SKIP_LINES; }
@Test public void testASingleQuoteAsDataElement() throws IOException { StringBuilder sb = new StringBuilder(CSVParser.INITIAL_READ_SIZE); sb.append("a,'''',c").append("\n"); CSVReader c = new CSVReader(new StringReader(sb.toString()), ',', '\''); String[] nextLine = c.readNext(); assertEquals(3, nextLine.length); assertEquals("a", nextLine[0]); assertEquals(1, nextLine[1].length()); assertEquals("\'", nextLine[1]); assertEquals("c", nextLine[2]); }
@Test public void testASingleQuoteAsDataElementWithEmptyField() throws IOException { StringBuilder sb = new StringBuilder(CSVParser.INITIAL_READ_SIZE); sb.append("a,'',c").append("\n"); CSVReader c = new CSVReader(new StringReader(sb.toString()), ',', '\''); String[] nextLine = c.readNext(); assertEquals(3, nextLine.length); assertEquals("a", nextLine[0]); assertEquals(0, nextLine[1].length()); assertEquals("", nextLine[1]); assertEquals("c", nextLine[2]); }
@Test public void testSpacesAtEndOfString() throws IOException { StringBuilder sb = new StringBuilder(CSVParser.INITIAL_READ_SIZE); sb.append("\"a\",\"b\",\"c\" "); CSVReader c = new CSVReader(new StringReader(sb.toString()), CSVParser.DEFAULT_SEPARATOR, CSVParser.DEFAULT_QUOTE_CHARACTER, true); String[] nextLine = c.readNext(); assertEquals(3, nextLine.length); assertEquals("a", nextLine[0]); assertEquals("b", nextLine[1]); assertEquals("c", nextLine[2]); }
@Test public void testEscapedQuote() throws IOException { StringBuffer sb = new StringBuffer(); sb.append("a,\"123\\\"4567\",c").append("\n"); CSVReader c = new CSVReader(new StringReader(sb.toString())); String[] nextLine = c.readNext(); assertEquals(3, nextLine.length); assertEquals("123\"4567", nextLine[1]); }
@Test public void testEscapedEscape() throws IOException { StringBuffer sb = new StringBuffer(); sb.append("a,\"123\\\\4567\",c").append("\n"); CSVReader c = new CSVReader(new StringReader(sb.toString())); String[] nextLine = c.readNext(); assertEquals(3, nextLine.length); assertEquals("123\\4567", nextLine[1]); }
@Test public void testSingleQuoteWhenDoubleQuoteIsQuoteChar() throws IOException { StringBuilder sb = new StringBuilder(CSVParser.INITIAL_READ_SIZE); sb.append("a,'',c").append("\n"); CSVReader c = new CSVReader(new StringReader(sb.toString())); String[] nextLine = c.readNext(); assertEquals(3, nextLine.length); assertEquals("a", nextLine[0]); assertEquals(2, nextLine[1].length()); assertEquals("''", nextLine[1]); assertEquals("c", nextLine[2]); }
@Test public void testQuotedParsedLine() throws IOException { StringBuilder sb = new StringBuilder(CSVParser.INITIAL_READ_SIZE); sb.append("\"a\",\"1234567\",\"c\"").append("\n"); CSVReader c = new CSVReader(new StringReader(sb.toString()), CSVParser.DEFAULT_SEPARATOR, CSVParser.DEFAULT_QUOTE_CHARACTER, true); String[] nextLine = c.readNext(); assertEquals(3, nextLine.length); assertEquals("a", nextLine[0]); assertEquals(1, nextLine[0].length()); assertEquals("1234567", nextLine[1]); assertEquals("c", nextLine[2]); }
@Test public void testIssue2992134OutOfPlaceQuotes() throws IOException { StringBuilder sb = new StringBuilder(CSVParser.INITIAL_READ_SIZE); sb.append("a,b,c,ddd\\\"eee\nf,g,h,\"iii,jjj\""); CSVReader c = new CSVReader(new StringReader(sb.toString())); String[] nextLine = c.readNext(); assertEquals("a", nextLine[0]); assertEquals("b", nextLine[1]); assertEquals("c", nextLine[2]); assertEquals("ddd\"eee", nextLine[3]); }
@Test public void testParseLine() throws IOException { String[] nextLine = csvr.readNext(); assertEquals("a", nextLine[0]); assertEquals("b", nextLine[1]); assertEquals("c", nextLine[2]); nextLine = csvr.readNext(); assertEquals("a", nextLine[0]); assertEquals("b,b,b", nextLine[1]); assertEquals("c", nextLine[2]); nextLine = csvr.readNext(); assertEquals(3, nextLine.length); nextLine = csvr.readNext(); assertEquals(3, nextLine.length); nextLine = csvr.readNext(); assertEquals("Glen \"The Man\" Smith", nextLine[0]); nextLine = csvr.readNext(); assertEquals("\"\"", nextLine[0]); assertEquals("test", nextLine[1]); nextLine = csvr.readNext(); assertEquals(4, nextLine.length); assertNull(csvr.readNext()); }
@Test public void testParseLineStrictQuote() throws IOException { StringBuilder sb = new StringBuilder(CSVParser.INITIAL_READ_SIZE); sb.append("a,b,c").append("\n"); sb.append("a,\"b,b,b\",c").append("\n"); sb.append(",,").append("\n"); sb.append("a,\"PO Box 123,\nKippax,ACT. 2615.\nAustralia\",d.\n"); sb.append("\"Glen \"\"The Man\"\" Smith\",Athlete,Developer\n"); sb.append("\"\"\"\"\"\",\"test\"\n"); sb.append("\"a\nb\",b,\"\nd\",e\n"); csvr = new CSVReader(new StringReader(sb.toString()), ',', '\"', true); String[] nextLine = csvr.readNext(); assertEquals("", nextLine[0]); assertEquals("", nextLine[1]); assertEquals("", nextLine[2]); nextLine = csvr.readNext(); assertEquals("", nextLine[0]); assertEquals("b,b,b", nextLine[1]); assertEquals("", nextLine[2]); nextLine = csvr.readNext(); assertEquals(3, nextLine.length); nextLine = csvr.readNext(); assertEquals(3, nextLine.length); nextLine = csvr.readNext(); assertEquals("Glen \"The Man\" Smith", nextLine[0]); nextLine = csvr.readNext(); assertTrue(nextLine[0].equals("\"\"")); assertTrue(nextLine[1].equals("test")); nextLine = csvr.readNext(); assertEquals(4, nextLine.length); assertEquals("a\nb", nextLine[0]); assertEquals("", nextLine[1]); assertEquals("\nd", nextLine[2]); assertEquals("", nextLine[3]); assertNull(csvr.readNext()); }
@Test public void testOptionalConstructors() throws IOException { StringBuilder sb = new StringBuilder(CSVParser.INITIAL_READ_SIZE); sb.append("a\tb\tc").append("\n"); sb.append("a\t'b\tb\tb'\tc").append("\n"); CSVReader c = new CSVReader(new StringReader(sb.toString()), '\t', '\''); String[] nextLine = c.readNext(); assertEquals(3, nextLine.length); nextLine = c.readNext(); assertEquals(3, nextLine.length); }
@Test public void parseQuotedStringWithDefinedSeperator() throws IOException { StringBuilder sb = new StringBuilder(CSVParser.INITIAL_READ_SIZE); sb.append("a\tb\tc").append("\n"); CSVReader c = new CSVReader(new StringReader(sb.toString()), '\t'); String[] nextLine = c.readNext(); assertEquals(3, nextLine.length); }
@Test public void testSkippingLines() throws IOException { StringBuilder sb = new StringBuilder(CSVParser.INITIAL_READ_SIZE); sb.append("Skip this line\t with tab").append("\n"); sb.append("And this line too").append("\n"); sb.append("a\t'b\tb\tb'\tc").append("\n"); CSVReader c = new CSVReader(new StringReader(sb.toString()), '\t', '\'', 2); String[] nextLine = c.readNext(); assertEquals(3, nextLine.length); assertEquals("a", nextLine[0]); }
@Test public void testSkippingLinesWithDifferentEscape() throws IOException { StringBuilder sb = new StringBuilder(CSVParser.INITIAL_READ_SIZE); sb.append("Skip this line?t with tab").append("\n"); sb.append("And this line too").append("\n"); sb.append("a\t'b\tb\tb'\t'c'").append("\n"); CSVReader c = new CSVReader(new StringReader(sb.toString()), '\t', '\'', '?', 2); String[] nextLine = c.readNext(); assertEquals(3, nextLine.length); assertEquals("a", nextLine[0]); assertEquals("c", nextLine[2]); }
@Test public void testNormalParsedLine() throws IOException { StringBuilder sb = new StringBuilder(CSVParser.INITIAL_READ_SIZE); sb.append("a,1234567,c").append("\n"); CSVReader c = new CSVReader(new StringReader(sb.toString())); String[] nextLine = c.readNext(); assertEquals(3, nextLine.length); assertEquals("a", nextLine[0]); assertEquals("1234567", nextLine[1]); assertEquals("c", nextLine[2]); }