Class CsvCallbackHandler<T>
- Type Parameters:
T- the type of the record that is built from the CSV data
- Direct Known Subclasses:
AbstractBaseCsvCallbackHandler, AbstractInternalCsvCallbackHandler
This class defines the methods that are called during the CSV reading process.
Implementations highly affect the behavior of the CsvReader. With great power comes great responsibility.
Don't mess up the CSV reading process!
Even if you need custom handling, you typically don't need to extend this class directly.
Check out AbstractBaseCsvCallbackHandler first.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected abstract voidaddField(char[] buf, int offset, int len, boolean quoted) Called for each field in the record.protected abstract voidbeginRecord(long startingLineNumber) Called at the beginning of each record.protected abstract TCalled at the end of each CSV record to build the actual record representation.protected abstract intReturns the number of fields in the record.protected abstract RecordTypeReturns the type of the record that is built from the CSV data.protected abstract voidsetComment(char[] buf, int offset, int len) Called for each comment line.protected abstract voidsetEmpty()Called for each empty line.protected voidCalled at the end of the CSV reading process.
-
Constructor Details
-
CsvCallbackHandler
protected CsvCallbackHandler()Default constructor.
-
-
Method Details
-
getRecordType
Returns the type of the record that is built from the CSV data.
The
CsvReaderwill skip- records of type
RecordType.COMMENTifCsvReader.CsvReaderBuilder.commentStrategy(CommentStrategy)is set toCommentStrategy.SKIP - records of type
RecordType.EMPTYifCsvReader.CsvReaderBuilder.skipEmptyLines(boolean)is set totrue.
- Returns:
- the type of the record that is built from the CSV data
- records of type
-
getFieldCount
protected abstract int getFieldCount()Returns the number of fields in the record.
The
CsvReaderwill verify that the number of fields in each record matches the number of fields in the first record unlessCsvReader.CsvReaderBuilder.allowExtraFields(boolean)orCsvReader.CsvReaderBuilder.allowMissingFields(boolean)are set totrue.- Returns:
- the number of fields in the record
-
beginRecord
protected abstract void beginRecord(long startingLineNumber) Called at the beginning of each record.
The
startingLineNumberis the line number where the record starts (starting with 1).- Parameters:
startingLineNumber- the line number where the record starts (starting with 1)
-
addField
protected abstract void addField(char[] buf, int offset, int len, boolean quoted) Called for each field in the record.
A record can either be a comment or a regular record. If this method is called, the record is a regular record and cannot be a comment.
The
quotedparameter indicates whether the field was quoted. It is for informational purposes only. Any potential escape characters are already removed and theoffsetpoints to the first character after the opening quote and thelendoes not include the closing quote. Hence, a quoted field can be processed in the same way as an unquoted field. Some implementations need the information whether a field was quoted, e.g., for differentiating betweennulland empty fields (foo,,barvs.foo,"",bar).The
bufparameter is the internal buffer that contains the field value (among other data). Do not attempt to modify the buffer or store a reference to it. The buffer is reused for performance reasons.- Parameters:
buf- the internal buffer that contains the field value (among other data)offset- the offset of the field value in the bufferlen- the length of the field valuequoted-trueif the field was quoted
-
setComment
protected abstract void setComment(char[] buf, int offset, int len) Called for each comment line.
Note that the comment character is not included in the value.
This method is not called if
CsvReader.CsvReaderBuilder.commentStrategy(CommentStrategy)is set toCommentStrategy.NONE.There can only be one invocation of this method per record. A record can either be a comment or a regular record. If this method is called, the record is a comment and cannot be a regular record.
The
bufparameter is the internal buffer that contains the field value (among other data). Do not attempt to modify the buffer or store a reference to it. The buffer is reused for performance reasons.- Parameters:
buf- the internal buffer that contains the field value (among other data)offset- the offset of the field value in the bufferlen- the length of the field value
-
setEmpty
protected abstract void setEmpty()Called for each empty line. -
buildRecord
Called at the end of each CSV record to build the actual record representation.- Returns:
- the record or
nullif the record should be ignored/skipped as it is consumed by the callback handler.
-
terminate
protected void terminate()Called at the end of the CSV reading process.
-