Skip to content

Commit

Permalink
CSV-196-master: More changes (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
DarrenJAN authored Dec 27, 2024
1 parent 8387f79 commit bdd152f
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 30 deletions.
5 changes: 3 additions & 2 deletions src/main/java/org/apache/commons/csv/CSVParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -539,13 +539,14 @@ public CSVParser(final Reader reader, final CSVFormat format, final long charact
* @param recordNumber
* The next record number to assign.
* @param charset
* The character encoding to be used for the reader.
* The character encoding to be used for the reader when enableByteTracking is true.
* @param enableByteTracking
* {@code true} to enable byte tracking for the parser; {@code false} to disable it.
* @throws IllegalArgumentException
* If the parameters of the format are inconsistent or if either the reader or format is null.
* @throws IOException
* If there is a problem reading the header or skipping the first record.
* @throws CSVException Thrown on invalid input.
* @since 1.13.0.
*/
private CSVParser(final Reader reader, final CSVFormat format, final long characterOffset, final long recordNumber,
final Charset charset, final boolean enableByteTracking)
Expand Down
20 changes: 5 additions & 15 deletions src/main/java/org/apache/commons/csv/CSVRecord.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public final class CSVRecord implements Serializable, Iterable<String> {
/**
* The start byte of this record as a character byte in the source stream.
*/
private final long characterByte;
private final long bytePosition;

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

CSVRecord(final CSVParser parser, final String[] values, final String comment, final long recordNumber,
final long characterPosition) {
this.recordNumber = recordNumber;
this.values = values != null ? values : Constants.EMPTY_STRING_ARRAY;
this.parser = parser;
this.comment = comment;
this.characterPosition = characterPosition;
this.characterByte = 0L;
}

CSVRecord(final CSVParser parser, final String[] values, final String comment, final long recordNumber,
final long characterPosition, final long characterByte) {
final long characterPosition, final long bytePosition) {
this.recordNumber = recordNumber;
this.values = values != null ? values : Constants.EMPTY_STRING_ARRAY;
this.parser = parser;
this.comment = comment;
this.characterPosition = characterPosition;
this.characterByte = characterByte;
this.bytePosition = bytePosition;
}
/**
* Returns a value by {@link Enum}.
Expand Down Expand Up @@ -164,8 +154,8 @@ public long getCharacterPosition() {
*
* @return the start byte of this record as a character byte in the source stream.
*/
public long getCharacterByte() {
return characterByte;
public long getBytePosition() {
return bytePosition;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public int read() throws IOException {
lineNumber++;
}
if (encoder != null) {
this.bytesRead += getCharBytes(current);
this.bytesRead += getEncodedCharLength(current);
}
lastChar = current;
position++;
Expand Down Expand Up @@ -180,7 +180,7 @@ public int read() throws IOException {
* @return the byte length of the character.
* @throws CharacterCodingException if the character cannot be encoded.
*/
private long getCharBytes(int current) throws CharacterCodingException {
private int getEncodedCharLength(int current) throws CharacterCodingException {
final char cChar = (char) current;
final char lChar = (char) lastChar;
if (!Character.isSurrogate(cChar)) {
Expand Down
16 changes: 8 additions & 8 deletions src/test/java/org/apache/commons/csv/CSVParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -718,22 +718,22 @@ public void testGetRecordThreeBytesRead() throws Exception {
assertNotNull(record = parser.nextRecord());
assertEquals(1, record.getRecordNumber());
assertEquals(code.indexOf('i'), record.getCharacterPosition());
assertEquals(record.getCharacterByte(), record.getCharacterPosition());
assertEquals(record.getBytePosition(), record.getCharacterPosition());

assertNotNull(record = parser.nextRecord());
assertEquals(2, record.getRecordNumber());
assertEquals(code.indexOf('1'), record.getCharacterPosition());
assertEquals(record.getCharacterByte(), record.getCharacterPosition());
assertEquals(record.getBytePosition(), record.getCharacterPosition());

assertNotNull(record = parser.nextRecord());
assertEquals(3, record.getRecordNumber());
assertEquals(code.indexOf('2'), record.getCharacterPosition());
assertEquals(record.getCharacterByte(), 95);
assertEquals(record.getBytePosition(), 95);

assertNotNull(record = parser.nextRecord());
assertEquals(4, record.getRecordNumber());
assertEquals(code.indexOf('3'), record.getCharacterPosition());
assertEquals(record.getCharacterByte(), 154);
assertEquals(record.getBytePosition(), 154);
};

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

assertNotNull(record = parser.nextRecord());
assertEquals(2, record.getRecordNumber());
assertEquals(code.indexOf('1'), record.getCharacterPosition());
assertEquals(record.getCharacterByte(), record.getCharacterPosition());
assertEquals(record.getBytePosition(), record.getCharacterPosition());
assertNotNull(record = parser.nextRecord());
assertEquals(3, record.getRecordNumber());
assertEquals(code.indexOf('2'), record.getCharacterPosition());
assertEquals(record.getCharacterByte(), 26);
assertEquals(record.getBytePosition(), 26);
assertNotNull(record = parser.nextRecord());
assertEquals(4, record.getRecordNumber());
assertEquals(code.indexOf('3'), record.getCharacterPosition());
assertEquals(record.getCharacterByte(), 43);
assertEquals(record.getBytePosition(), 43);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/test/java/org/apache/commons/csv/CSVRecordTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ record = parser.iterator().next();
@Test
public void testCSVRecordNULLValues() throws IOException {
try (CSVParser parser = CSVParser.parse("A,B\r\nONE,TWO", CSVFormat.DEFAULT.withHeader())) {
final CSVRecord csvRecord = new CSVRecord(parser, null, null, 0L, 0L);
final CSVRecord csvRecord = new CSVRecord(parser, null, null, 0L, 0L, 0L);
assertEquals(0, csvRecord.size());
assertThrows(IllegalArgumentException.class, () -> csvRecord.get("B"));
}
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/org/apache/commons/csv/JiraCsv196Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public void parseThreeBytes() throws IOException {
long[] charByteKey = {0, 89, 242, 395};
int idx = 0;
for (CSVRecord record : parser) {
assertEquals(charByteKey[idx++], record.getCharacterByte());
assertEquals(charByteKey[idx++], record.getBytePosition());
}
parser.close();
}
Expand All @@ -63,7 +63,7 @@ public void parseFourBytes() throws IOException {
long[] charByteKey = {0, 84, 701, 1318, 1935};
int idx = 0;
for (CSVRecord record : parser) {
assertEquals(charByteKey[idx++], record.getCharacterByte());
assertEquals(charByteKey[idx++], record.getBytePosition());
}
parser.close();
}
Expand Down

0 comments on commit bdd152f

Please sign in to comment.