- java.lang.Object
-
- de.siegmar.fastcsv.reader.CsvCallbackHandler<T>
-
- de.siegmar.fastcsv.reader.AbstractCsvCallbackHandler<T>
-
- Type Parameters:
T
- the type of the resulting records
- Direct Known Subclasses:
CsvRecordHandler
,NamedCsvRecordHandler
,StringArrayHandler
abstract class AbstractCsvCallbackHandler<T> extends CsvCallbackHandler<T>
Abstract base class forCsvCallbackHandler
implementations.This implementation is stateful and must not be reused.
-
-
Field Summary
Fields Modifier and Type Field Description protected boolean
comment
Whether the current record is a comment.protected boolean
emptyLine
Whether the line is empty.protected int
fieldIdx
The current index in the internal fields array.protected FieldModifier
fieldModifier
The field modifier.protected java.lang.String[]
fields
The internal fields array.private static int
INITIAL_FIELDS_SIZE
protected int
recordSize
The total size (sum of all characters) of the current record.protected long
startingLineNumber
The starting line number of the current record.
-
Constructor Summary
Constructors Modifier Constructor Description protected
AbstractCsvCallbackHandler()
Constructs a new instance with an initial fields array of size 32.protected
AbstractCsvCallbackHandler(FieldModifier fieldModifier)
Constructs a new instance with the given field modifier and initial fields array of size 32.(package private)
AbstractCsvCallbackHandler(FieldModifier fieldModifier, int len)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void
addField(char[] buf, int offset, int len, boolean quoted)
Called for each field in the record.protected void
addField(java.lang.String value, boolean quoted)
Adds the given value to the internal fields array.protected void
beginRecord(long startingLineNumber)
Called at the beginning of each record.protected RecordWrapper<T>
buildWrapper(T rec)
Builds a record wrapper for the given record.protected java.lang.String[]
compactFields()
Builds a compact fields array (a copy of the internal fields array with the length of the current record).private void
extendCapacity()
protected java.lang.String
materializeComment(char[] buf, int offset, int len)
Materializes comment from the given buffer.protected java.lang.String
materializeField(char[] buf, int offset, int len)
Materializes field from the given buffer.private static java.lang.String
maxRecordSizeExceededMessage(long line)
protected java.lang.String
modifyComment(java.lang.String field)
Modifies comment value.protected java.lang.String
modifyField(java.lang.String value, boolean quoted)
Modifies field value.protected void
setComment(char[] buf, int offset, int len)
Called for each comment line.protected void
setComment(java.lang.String value)
Sets the given value as the only field in the internal fields array.-
Methods inherited from class de.siegmar.fastcsv.reader.CsvCallbackHandler
buildRecord, terminate
-
-
-
-
Field Detail
-
INITIAL_FIELDS_SIZE
private static final int INITIAL_FIELDS_SIZE
- See Also:
- Constant Field Values
-
fieldModifier
protected final FieldModifier fieldModifier
The field modifier.
-
startingLineNumber
protected long startingLineNumber
The starting line number of the current record.See
CsvCallbackHandler.beginRecord(long)
andCsvRecord.getStartingLineNumber()
.
-
fields
protected java.lang.String[] fields
The internal fields array.
-
recordSize
protected int recordSize
The total size (sum of all characters) of the current record.
-
fieldIdx
protected int fieldIdx
The current index in the internal fields array.
-
comment
protected boolean comment
Whether the current record is a comment.
-
emptyLine
protected boolean emptyLine
Whether the line is empty.
-
-
Constructor Detail
-
AbstractCsvCallbackHandler
protected AbstractCsvCallbackHandler()
Constructs a new instance with an initial fields array of size 32.
-
AbstractCsvCallbackHandler
protected AbstractCsvCallbackHandler(FieldModifier fieldModifier)
Constructs a new instance with the given field modifier and initial fields array of size 32.- Parameters:
fieldModifier
- the field modifier, must not benull
- Throws:
java.lang.NullPointerException
- ifnull
is passed
-
AbstractCsvCallbackHandler
AbstractCsvCallbackHandler(FieldModifier fieldModifier, int len)
-
-
Method Detail
-
beginRecord
protected void beginRecord(long startingLineNumber)
Called at the beginning of each record.The
startingLineNumber
is the line number where the record starts (starting with 1). SeeCsvRecord.getStartingLineNumber()
. Resets the internal state of this handler.- Specified by:
beginRecord
in 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
quoted
parameter indicates whether the field was quoted. It is for informational purposes only. Any potential escape characters are already removed and theoffset
points to the first character after the opening quote and thelen
does 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 betweennull
and empty fields (foo,,bar
vs.foo,"",bar
).The
buf
parameter 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:
addField
in 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
-true
if the field was quoted- Throws:
CsvParseException
- if the addition exceeds the limit of record size or maximum fields count.
-
addField
protected void addField(java.lang.String value, boolean quoted)
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
-true
if the field was quoted- Throws:
CsvParseException
- if the addition exceeds the maximum fields count.
-
modifyField
protected java.lang.String modifyField(java.lang.String value, boolean quoted)
Modifies field value.- Parameters:
value
- the field valuequoted
-true
if the field was quoted- Returns:
- the modified field value
-
materializeField
protected java.lang.String materializeField(char[] buf, int offset, int len)
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
-
maxRecordSizeExceededMessage
private static java.lang.String maxRecordSizeExceededMessage(long line)
-
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
buf
parameter 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:
setComment
in 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
protected void setComment(java.lang.String value)
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
protected java.lang.String modifyComment(java.lang.String field)
Modifies comment value.- Parameters:
field
- the comment value- Returns:
- the modified comment value
-
materializeComment
protected java.lang.String materializeComment(char[] buf, int offset, int len)
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
-
extendCapacity
private void extendCapacity()
-
compactFields
protected java.lang.String[] 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
protected RecordWrapper<T> buildWrapper(T rec)
Builds a record wrapper for the given record.- Parameters:
rec
- the record, must not benull
- Returns:
- the record wrapper
- Throws:
java.lang.NullPointerException
- ifrec
isnull
-
-