Skip to content

Commit bdd152f

Browse files
authored
CSV-196-master: More changes (#16)
1 parent 8387f79 commit bdd152f

File tree

6 files changed

+21
-30
lines changed

6 files changed

+21
-30
lines changed

src/main/java/org/apache/commons/csv/CSVParser.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -539,13 +539,14 @@ public CSVParser(final Reader reader, final CSVFormat format, final long charact
539539
* @param recordNumber
540540
* The next record number to assign.
541541
* @param charset
542-
* The character encoding to be used for the reader.
542+
* The character encoding to be used for the reader when enableByteTracking is true.
543+
* @param enableByteTracking
544+
* {@code true} to enable byte tracking for the parser; {@code false} to disable it.
543545
* @throws IllegalArgumentException
544546
* If the parameters of the format are inconsistent or if either the reader or format is null.
545547
* @throws IOException
546548
* If there is a problem reading the header or skipping the first record.
547549
* @throws CSVException Thrown on invalid input.
548-
* @since 1.13.0.
549550
*/
550551
private CSVParser(final Reader reader, final CSVFormat format, final long characterOffset, final long recordNumber,
551552
final Charset charset, final boolean enableByteTracking)

src/main/java/org/apache/commons/csv/CSVRecord.java

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public final class CSVRecord implements Serializable, Iterable<String> {
5151
/**
5252
* The start byte of this record as a character byte in the source stream.
5353
*/
54-
private final long characterByte;
54+
private final long bytePosition;
5555

5656
/** The accumulated comments (if any) */
5757
private final String comment;
@@ -65,24 +65,14 @@ public final class CSVRecord implements Serializable, Iterable<String> {
6565
/** The parser that originates this record. This is not serialized. */
6666
private final transient CSVParser parser;
6767

68-
CSVRecord(final CSVParser parser, final String[] values, final String comment, final long recordNumber,
69-
final long characterPosition) {
70-
this.recordNumber = recordNumber;
71-
this.values = values != null ? values : Constants.EMPTY_STRING_ARRAY;
72-
this.parser = parser;
73-
this.comment = comment;
74-
this.characterPosition = characterPosition;
75-
this.characterByte = 0L;
76-
}
77-
7868
CSVRecord(final CSVParser parser, final String[] values, final String comment, final long recordNumber,
79-
final long characterPosition, final long characterByte) {
69+
final long characterPosition, final long bytePosition) {
8070
this.recordNumber = recordNumber;
8171
this.values = values != null ? values : Constants.EMPTY_STRING_ARRAY;
8272
this.parser = parser;
8373
this.comment = comment;
8474
this.characterPosition = characterPosition;
85-
this.characterByte = characterByte;
75+
this.bytePosition = bytePosition;
8676
}
8777
/**
8878
* Returns a value by {@link Enum}.
@@ -164,8 +154,8 @@ public long getCharacterPosition() {
164154
*
165155
* @return the start byte of this record as a character byte in the source stream.
166156
*/
167-
public long getCharacterByte() {
168-
return characterByte;
157+
public long getBytePosition() {
158+
return bytePosition;
169159
}
170160

171161
/**

src/main/java/org/apache/commons/csv/ExtendedBufferedReader.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ public int read() throws IOException {
147147
lineNumber++;
148148
}
149149
if (encoder != null) {
150-
this.bytesRead += getCharBytes(current);
150+
this.bytesRead += getEncodedCharLength(current);
151151
}
152152
lastChar = current;
153153
position++;
@@ -180,7 +180,7 @@ public int read() throws IOException {
180180
* @return the byte length of the character.
181181
* @throws CharacterCodingException if the character cannot be encoded.
182182
*/
183-
private long getCharBytes(int current) throws CharacterCodingException {
183+
private int getEncodedCharLength(int current) throws CharacterCodingException {
184184
final char cChar = (char) current;
185185
final char lChar = (char) lastChar;
186186
if (!Character.isSurrogate(cChar)) {

src/test/java/org/apache/commons/csv/CSVParserTest.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -718,22 +718,22 @@ public void testGetRecordThreeBytesRead() throws Exception {
718718
assertNotNull(record = parser.nextRecord());
719719
assertEquals(1, record.getRecordNumber());
720720
assertEquals(code.indexOf('i'), record.getCharacterPosition());
721-
assertEquals(record.getCharacterByte(), record.getCharacterPosition());
721+
assertEquals(record.getBytePosition(), record.getCharacterPosition());
722722

723723
assertNotNull(record = parser.nextRecord());
724724
assertEquals(2, record.getRecordNumber());
725725
assertEquals(code.indexOf('1'), record.getCharacterPosition());
726-
assertEquals(record.getCharacterByte(), record.getCharacterPosition());
726+
assertEquals(record.getBytePosition(), record.getCharacterPosition());
727727

728728
assertNotNull(record = parser.nextRecord());
729729
assertEquals(3, record.getRecordNumber());
730730
assertEquals(code.indexOf('2'), record.getCharacterPosition());
731-
assertEquals(record.getCharacterByte(), 95);
731+
assertEquals(record.getBytePosition(), 95);
732732

733733
assertNotNull(record = parser.nextRecord());
734734
assertEquals(4, record.getRecordNumber());
735735
assertEquals(code.indexOf('3'), record.getCharacterPosition());
736-
assertEquals(record.getCharacterByte(), 154);
736+
assertEquals(record.getBytePosition(), 154);
737737
};
738738

739739
}
@@ -755,20 +755,20 @@ public void testGetRecordFourBytesRead() throws Exception {
755755
assertNotNull(record = parser.nextRecord());
756756
assertEquals(1, record.getRecordNumber());
757757
assertEquals(code.indexOf('i'), record.getCharacterPosition());
758-
assertEquals(record.getCharacterByte(), record.getCharacterPosition());
758+
assertEquals(record.getBytePosition(), record.getCharacterPosition());
759759

760760
assertNotNull(record = parser.nextRecord());
761761
assertEquals(2, record.getRecordNumber());
762762
assertEquals(code.indexOf('1'), record.getCharacterPosition());
763-
assertEquals(record.getCharacterByte(), record.getCharacterPosition());
763+
assertEquals(record.getBytePosition(), record.getCharacterPosition());
764764
assertNotNull(record = parser.nextRecord());
765765
assertEquals(3, record.getRecordNumber());
766766
assertEquals(code.indexOf('2'), record.getCharacterPosition());
767-
assertEquals(record.getCharacterByte(), 26);
767+
assertEquals(record.getBytePosition(), 26);
768768
assertNotNull(record = parser.nextRecord());
769769
assertEquals(4, record.getRecordNumber());
770770
assertEquals(code.indexOf('3'), record.getCharacterPosition());
771-
assertEquals(record.getCharacterByte(), 43);
771+
assertEquals(record.getBytePosition(), 43);
772772
}
773773
}
774774

src/test/java/org/apache/commons/csv/CSVRecordTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ record = parser.iterator().next();
8585
@Test
8686
public void testCSVRecordNULLValues() throws IOException {
8787
try (CSVParser parser = CSVParser.parse("A,B\r\nONE,TWO", CSVFormat.DEFAULT.withHeader())) {
88-
final CSVRecord csvRecord = new CSVRecord(parser, null, null, 0L, 0L);
88+
final CSVRecord csvRecord = new CSVRecord(parser, null, null, 0L, 0L, 0L);
8989
assertEquals(0, csvRecord.size());
9090
assertThrows(IllegalArgumentException.class, () -> csvRecord.get("B"));
9191
}

src/test/java/org/apache/commons/csv/JiraCsv196Test.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public void parseThreeBytes() throws IOException {
4242
long[] charByteKey = {0, 89, 242, 395};
4343
int idx = 0;
4444
for (CSVRecord record : parser) {
45-
assertEquals(charByteKey[idx++], record.getCharacterByte());
45+
assertEquals(charByteKey[idx++], record.getBytePosition());
4646
}
4747
parser.close();
4848
}
@@ -63,7 +63,7 @@ public void parseFourBytes() throws IOException {
6363
long[] charByteKey = {0, 84, 701, 1318, 1935};
6464
int idx = 0;
6565
for (CSVRecord record : parser) {
66-
assertEquals(charByteKey[idx++], record.getCharacterByte());
66+
assertEquals(charByteKey[idx++], record.getBytePosition());
6767
}
6868
parser.close();
6969
}

0 commit comments

Comments
 (0)