NamedCsvRecord for each record.
This implementation is stateful and must not be reused.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected booleanWhether the current record is a comment.protected booleanWhether the line is empty.protected intThe current index in the internal fields array.protected final FieldModifierThe field modifier.protected String[]The internal fields array.protected intThe total size (sum of all characters) of the current record.protected longThe starting line number of the current record. -
Constructor Summary
ConstructorsConstructorDescriptionConstructs a newNamedCsvRecordHandlerwith an empty header.NamedCsvRecordHandler(FieldModifier fieldModifier) Constructs a newNamedCsvRecordHandlerwith the given field modifier.NamedCsvRecordHandler(FieldModifier fieldModifier, String... header) Constructs a newNamedCsvRecordHandlerwith the given header and field modifier.NamedCsvRecordHandler(FieldModifier fieldModifier, List<String> header) Constructs a newNamedCsvRecordHandlerwith the given header and field modifier.NamedCsvRecordHandler(String... header) Constructs a newNamedCsvRecordHandlerwith the given header.NamedCsvRecordHandler(List<String> header) Constructs a newNamedCsvRecordHandlerwith the given header. -
Method Summary
Modifier and TypeMethodDescriptionprotected voidaddField(char[] buf, int offset, int len, boolean quoted) Called for each field in the record.protected voidAdds the given value to the internal fields array.protected voidbeginRecord(long startingLineNumber) Called at the beginning of each record.protected RecordWrapper<NamedCsvRecord> Called at the end of each CSV record to build an object representation of the record.protected RecordWrapper<NamedCsvRecord> Builds a record wrapper for the given record.protected String[]Builds a compact fields array (a copy of the internal fields array with the length of the current record).protected StringmaterializeComment(char[] buf, int offset, int len) Materializes comment from the given buffer.protected StringmaterializeField(char[] buf, int offset, int len) Materializes field from the given buffer.protected StringmodifyComment(String field) Modifies comment value.protected StringmodifyField(String value, boolean quoted) Modifies field value.protected voidsetComment(char[] buf, int offset, int len) Called for each comment line.protected voidsetComment(String value) Sets the given value as the only field in the internal fields array.Methods inherited from class de.siegmar.fastcsv.reader.CsvCallbackHandler
terminate
-
Field Details
-
fieldModifier
The field modifier. -
startingLineNumber
protected long startingLineNumberThe starting line number of the current record.See
CsvCallbackHandler.beginRecord(long)andCsvRecord.getStartingLineNumber(). -
fields
The internal fields array. -
recordSize
protected int recordSizeThe total size (sum of all characters) of the current record. -
fieldIdx
protected int fieldIdxThe current index in the internal fields array. -
comment
protected boolean commentWhether the current record is a comment. -
emptyLine
protected boolean emptyLineWhether the line is empty.
-
-
Constructor Details
-
NamedCsvRecordHandler
public NamedCsvRecordHandler()Constructs a newNamedCsvRecordHandlerwith an empty header. -
NamedCsvRecordHandler
Constructs a newNamedCsvRecordHandlerwith the given header.- Parameters:
header- the header, must not benullor containnullelements- Throws:
NullPointerException- ifnullis passed
-
NamedCsvRecordHandler
Constructs a newNamedCsvRecordHandlerwith the given header.- Parameters:
header- the header, must not benullor containnullelements- Throws:
NullPointerException- ifnullis passed
-
NamedCsvRecordHandler
Constructs a newNamedCsvRecordHandlerwith the given field modifier.- Parameters:
fieldModifier- the field modifier, must not benull- Throws:
NullPointerException- ifnullis passed
-
NamedCsvRecordHandler
Constructs a newNamedCsvRecordHandlerwith the given header and field modifier.- Parameters:
fieldModifier- the field modifier, must not benullheader- the header, must not benullor containnullelements- Throws:
NullPointerException- ifnullis passed
-
NamedCsvRecordHandler
Constructs a newNamedCsvRecordHandlerwith the given header and field modifier.- Parameters:
fieldModifier- the field modifier, must not benullheader- the header, must not benullor containnullelements- Throws:
NullPointerException- ifnullis passed
-
-
Method Details
-
buildRecord
Description copied from class:CsvCallbackHandlerCalled at the end of each CSV record to build an object representation of the record.The returned wrapper is used by the
CsvReaderin order to determine how to process the record.- Specified by:
buildRecordin classCsvCallbackHandler<NamedCsvRecord>- Returns:
- the record wrapper or
nullif the record should be ignored/skipped
-
beginRecord
protected void beginRecord(long startingLineNumber) Called at the beginning of each record.The
startingLineNumberis the line number where the record starts (starting with 1). SeeCsvRecord.getStartingLineNumber(). Resets the internal state of this handler.- Specified by:
beginRecordin classCsvCallbackHandler<T>- Parameters:
startingLineNumber- the line number where the record starts (starting with 1)
-
addField
protected 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. Passes the materialized (materializeField(char[], int, int)) and modified (modifyField(String, boolean)) value toaddField(String, boolean).- Specified by:
addFieldin classCsvCallbackHandler<T>- 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- Throws:
CsvParseException- if the addition exceeds the limit of record size or maximum fields count.
-
addField
Adds the given value to the internal fields array.Extends the array if necessary and keeps track of the total record size and fields count.
- Parameters:
value- the field valuequoted-trueif the field was quoted- Throws:
CsvParseException- if the addition exceeds the maximum fields count.
-
modifyField
Modifies field value.- Parameters:
value- the field valuequoted-trueif the field was quoted- Returns:
- the modified field value
-
materializeField
Materializes field from the given buffer.- 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- Returns:
- the materialized field value
-
setComment
protected 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. Passes the materialized (materializeComment(char[], int, int)) and modified (modifyComment(String)) value tosetComment(String).- Specified by:
setCommentin classCsvCallbackHandler<T>- 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- Throws:
CsvParseException- if the addition exceeds the limit of record size.
-
setComment
Sets the given value as the only field in the internal fields array.Keeps track of the total record size.
- Parameters:
value- the comment value- Throws:
CsvParseException- if the addition exceeds the limit of record size.
-
modifyComment
Modifies comment value.- Parameters:
field- the comment value- Returns:
- the modified comment value
-
materializeComment
Materializes comment from the given buffer.- Parameters:
buf- the internal buffer that contains the comment value (among other data)offset- the offset of the field value in the bufferlen- the length of the field value- Returns:
- the materialized field value
-
compactFields
Builds a compact fields array (a copy of the internal fields array with the length of the current record).In contrast to the class property
fields, the returned array does only contain the fields of the current record.- Returns:
- the compact fields array
-
buildWrapper
Builds a record wrapper for the given record.- Parameters:
rec- the record, must not benull- Returns:
- the record wrapper
- Throws:
NullPointerException- ifrecisnull
-