Skip to content

Commit f71a2ac

Browse files
committed
Fix #191
1 parent 8962b95 commit f71a2ac

File tree

4 files changed

+59
-28
lines changed

4 files changed

+59
-28
lines changed

csv/src/main/java/com/fasterxml/jackson/dataformat/csv/impl/CsvDecoder.java

+49-24
Original file line numberDiff line numberDiff line change
@@ -505,44 +505,68 @@ public boolean startNewLine() throws IOException
505505
* @since 2.10.1
506506
*/
507507
public boolean skipLinesWhenNeeded() throws IOException {
508-
if (!(_allowComments || _skipBlankLines)) {
508+
if (_allowComments) {
509+
return _skipCommentLines();
510+
}
511+
if (!_skipBlankLines) {
509512
return hasMoreInput();
510513
}
511-
int firstCharacterPtr = _inputPtr;
514+
515+
// only need to skip fully empty lines
512516
while (hasMoreInput()) {
513-
char ch = _inputBuffer[_inputPtr++];
517+
char ch = _inputBuffer[_inputPtr];
514518
if (ch == '\r' || ch == '\n') {
519+
++_inputPtr;
515520
_pendingLF = ch;
516521
_handleLF();
517-
// track the start of the new line
518-
firstCharacterPtr = _inputPtr;
519522
continue;
520523
}
521-
if (ch == ' ') {
524+
if (ch != ' ') {
525+
return true; // processing can go on
526+
}
527+
++_inputPtr;
528+
}
529+
return false; // end of input
530+
}
531+
532+
public boolean _skipCommentLines() throws IOException
533+
{
534+
while ((_inputPtr < _inputEnd) || loadMore()) {
535+
char ch = _inputBuffer[_inputPtr];
536+
switch (ch) {
537+
case '#':
538+
++_inputPtr;
539+
_skipCommentContents();
540+
continue;
541+
case '\r':
542+
case '\n':
543+
++_inputPtr;
544+
_pendingLF = ch;
545+
_handleLF();
546+
continue;
547+
case ' ':
522548
// skip all blanks (in both comments/blanks skip mode)
549+
++_inputPtr;
523550
continue;
551+
default:
552+
return true;
524553
}
525-
if (_allowComments) {
526-
if (_inputBuffer[firstCharacterPtr] == '#') {
527-
// on a commented line, skip everything
528-
continue;
529-
}
530-
if (ch == '#') {
531-
// we reach this point when whitespaces precedes the hash character
532-
// move the firstCharacterPtr to the '#' location in order to skip the line completely
533-
firstCharacterPtr = _inputPtr-1;
534-
continue;
535-
}
536-
}
537-
// we reached a non skippable character, this line needs to be parsed
538-
// rollback the input pointer to the beginning of the line
539-
_inputPtr = firstCharacterPtr;
540-
return true; // processing can go on
541554
}
542555
return false; // end of input
543556
}
544557

545-
// 12-Apr-2020, tatu: Not used any more (probably replaced by above?)
558+
private void _skipCommentContents() throws IOException
559+
{
560+
while ((_inputPtr < _inputEnd) || loadMore()) {
561+
char ch = _inputBuffer[_inputPtr++];
562+
if (ch == '\r' || ch == '\n') {
563+
_pendingLF = ch;
564+
_handleLF();
565+
break;
566+
}
567+
}
568+
}
569+
546570
/*
547571
private final static int INT_HASH = '#';
548572
@@ -559,7 +583,8 @@ protected int _skipCommentLines() throws IOException
559583
// Ok, skipped the end of the line. Check next one...
560584
int i = _nextChar();
561585
if (i != INT_HASH) {
562-
return i;
586+
--_inputPtr;
587+
return true;
563588
}
564589
}
565590
return -1; // end of input

csv/src/test/java/com/fasterxml/jackson/dataformat/csv/deser/SkipBlankLines15Test.java renamed to csv/src/test/java/com/fasterxml/jackson/dataformat/csv/deser/SkipEmptyLines15Test.java

+6-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import static org.junit.Assert.assertArrayEquals;
1010

1111
// for [dataformats-text#15]: Allow skipping of empty lines
12-
public class SkipBlankLines15Test extends ModuleTestBase {
12+
public class SkipEmptyLines15Test extends ModuleTestBase {
1313

1414
private static final String CSV_WITH_EMPTY_LINE = "1,\"xyz\"\n\ntrue,\n";
1515
private static final String CSV_WITH_BLANK_LINE = "1,\"xyz\"\n \ntrue,\n";
@@ -129,14 +129,18 @@ public void testCsvWithBlankLineAndCommentSkipBlankLinesFeatureDisabled() throws
129129
), rows);
130130
}
131131

132+
// 14-Apr-2020, tatu: Due to [dataformats-text#191], can not retain leading spaces
133+
// when trimming empty lines and/or comments, so test changed for 2.11
132134
public void testCsvWithBlankLineAndCommentSkipBlankLinesFeatureEnabled() throws Exception {
133135
String[][] rows = mapperForCsvAsArray()
134136
.with(CsvParser.Feature.SKIP_EMPTY_LINES)
135137
.readValue(CSV_WITH_BLANK_LINE_AND_COMMENT);
136138
// blank/empty lines are skipped
137139
assertArrayEquals(expected(
138140
row("1", "xyz"),
139-
row(" #comment"),
141+
// As per: [dataformats-text#191]
142+
// row(" #comment"),
143+
row("#comment"),
140144
row("true", "")
141145
), rows);
142146
}

csv/src/test/java/com/fasterxml/jackson/dataformat/csv/failing/ParserSkipEmpty191Test.java renamed to csv/src/test/java/com/fasterxml/jackson/dataformat/csv/deser/SkipEmptyLines191Test.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.fasterxml.jackson.dataformat.csv.failing;
1+
package com.fasterxml.jackson.dataformat.csv.deser;
22

33
import java.io.Reader;
44
import java.io.StringReader;
@@ -10,7 +10,7 @@
1010
import com.fasterxml.jackson.dataformat.csv.ModuleTestBase;
1111

1212
// [dataformats-text#191]
13-
public class ParserSkipEmpty191Test extends ModuleTestBase {
13+
public class SkipEmptyLines191Test extends ModuleTestBase {
1414

1515
private static String COL_1 = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
1616
private static String COL_2 = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb";

release-notes/VERSION-2.x

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ Modules:
1818
#180: (yaml) YAMLGenerator serializes string with special chars unquoted when
1919
using `MINIMIZE_QUOTES` mode
2020
(reported, fix contributed by Timo R)
21+
#191: (csv) `ArrayIndexOutOfBoundsException` when skipping empty lines, comments
22+
(reported by f-julian@github)
2123

2224
2.10.4 (not yet released)
2325

0 commit comments

Comments
 (0)