Class AbstractInternalCsvCallbackHandler<T>
- Type Parameters:
T- the type of the resulting records
- Direct Known Subclasses:
CsvRecordHandler, NamedCsvRecordHandler, StringArrayHandler
CsvCallbackHandler implementations.-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classAbstractInternalCsvCallbackHandler.AbstractInternalCsvCallbackHandlerBuilder<T extends AbstractInternalCsvCallbackHandler.AbstractInternalCsvCallbackHandlerBuilder<?>>Abstract builder forAbstractInternalCsvCallbackHandlersubclasses. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected intThe current index in the internal fields array.protected final FieldModifierThe field modifier.protected String[]The internal fields array.protected final intThe maximum number of fields a single record may have.protected final intThe maximum number of characters a single field may have.protected final intThe maximum number of characters a single record may have.protected intThe total size (sum of all characters) of the current record.protected RecordTypeThe type of the current record.protected longThe starting line number of the current record. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedAbstractInternalCsvCallbackHandler(int maxFields, int maxFieldSize, int maxRecordSize, FieldModifier fieldModifier) Constructs a new instance with the given configuration. -
Method Summary
Modifier and TypeMethodDescriptionprotected voidaddField(char[] buf, int offset, int len, boolean quoted) Called for each field in the record.protected voidbeginRecord(long startingLineNumber) Called at the beginning of each record.protected String[]Builds a compact fields array (a copy of the internal fields array with the length of the current record).protected intReturns the number of fields in the record.Returns the type of the record that is built from the CSV data.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 voidsetEmpty()Called for each empty line.Methods inherited from class CsvCallbackHandler
buildRecord, terminate
-
Field Details
-
maxFields
protected final int maxFieldsThe maximum number of fields a single record may have. -
maxFieldSize
protected final int maxFieldSizeThe maximum number of characters a single field may have. -
maxRecordSize
protected final int maxRecordSizeThe maximum number of characters a single record may have. -
fieldModifier
The field modifier. -
startingLineNumber
protected long startingLineNumberThe starting line number of the current record.
See
beginRecord(long). -
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. -
recordType
The type of the current record.
-
-
Constructor Details
-
AbstractInternalCsvCallbackHandler
protected AbstractInternalCsvCallbackHandler(int maxFields, int maxFieldSize, int maxRecordSize, FieldModifier fieldModifier) Constructs a new instance with the given configuration.- Parameters:
maxFields- the maximum number of fields; must be > 0maxFieldSize- the maximum field size; must be > 0maxRecordSize- the maximum record size; must be > 0 and >=maxFieldSizefieldModifier- the field modifier; must not benull- Throws:
IllegalArgumentException- if the arguments are invalidNullPointerException- ifnullis passed
-
-
Method Details
-
getRecordType
Description copied from class:CsvCallbackHandlerReturns 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.
- Specified by:
getRecordTypein classCsvCallbackHandler<T>- Returns:
- the type of the record that is built from the CSV data
- records of type
-
getFieldCount
protected int getFieldCount()Description copied from class:CsvCallbackHandlerReturns 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.- Specified by:
getFieldCountin classCsvCallbackHandler<T>- Returns:
- the number of fields in the record
-
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).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.Materializes the field value, apply field modifier, checks constraints and adds the field to the record.
- 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.
-
modifyField
-
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.Materializes the comment value, apply field modifier, checks constraints and adds the field to the record.
- 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.
-
modifyComment
-
setEmpty
protected void setEmpty()Description copied from class:CsvCallbackHandlerCalled for each empty line.- Specified by:
setEmptyin classCsvCallbackHandler<T>
-
compactFields
-