Class AbstractInternalCsvCallbackHandler<T>

java.lang.Object
de.siegmar.fastcsv.reader.CsvCallbackHandler<T>
de.siegmar.fastcsv.reader.AbstractInternalCsvCallbackHandler<T>
Type Parameters:
T - the type of the resulting records
Direct Known Subclasses:
CsvRecordHandler, NamedCsvRecordHandler, StringArrayHandler

public abstract sealed class AbstractInternalCsvCallbackHandler<T> extends CsvCallbackHandler<T> permits CsvRecordHandler, NamedCsvRecordHandler, StringArrayHandler
Abstract base class for CsvCallbackHandler implementations.
  • Field Details

    • maxFields

      protected final int maxFields
      The maximum number of fields a single record may have.
    • maxFieldSize

      protected final int maxFieldSize
      The maximum number of characters a single field may have.
    • maxRecordSize

      protected final int maxRecordSize
      The maximum number of characters a single record may have.
    • fieldModifier

      protected final FieldModifier fieldModifier
      The field modifier.
    • startingLineNumber

      protected long startingLineNumber

      The starting line number of the current record.

      See beginRecord(long).

    • fields

      protected 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.
    • recordType

      protected RecordType 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 > 0
      maxFieldSize - the maximum field size; must be > 0
      maxRecordSize - the maximum record size; must be > 0 and >= maxFieldSize
      fieldModifier - the field modifier; must not be null
      Throws:
      IllegalArgumentException - if the arguments are invalid
      NullPointerException - if null is passed
  • Method Details

    • getRecordType

      public RecordType getRecordType()
      Description copied from class: CsvCallbackHandler

      Returns the type of the record that is built from the CSV data.

      The CsvReader will skip

      Specified by:
      getRecordType in class CsvCallbackHandler<T>
      Returns:
      the type of the record that is built from the CSV data
    • getFieldCount

      protected int getFieldCount()
      Description copied from class: CsvCallbackHandler

      Returns the number of fields in the record.

      The CsvReader will verify that the number of fields in each record matches the number of fields in the first record unless CsvReader.CsvReaderBuilder.allowExtraFields(boolean) or CsvReader.CsvReaderBuilder.allowMissingFields(boolean) are set to true.

      Specified by:
      getFieldCount in class CsvCallbackHandler<T>
      Returns:
      the number of fields in the record
    • 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).

      Resets the internal state of this handler.

      Specified by:
      beginRecord in class CsvCallbackHandler<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 the offset points to the first character after the opening quote and the len 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 between null 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.

      Materializes the field value, apply field modifier, checks constraints and adds the field to the record.

      Specified by:
      addField in class CsvCallbackHandler<T>
      Parameters:
      buf - the internal buffer that contains the field value (among other data)
      offset - the offset of the field value in the buffer
      len - the length of the field value
      quoted - true if the field was quoted
      Throws:
      CsvParseException - if the addition exceeds the limit of record size or maximum fields count.
    • modifyField

      protected String modifyField(String value, boolean quoted)
      Modifies field value.
      Parameters:
      value - the field value
      quoted - true if the field was quoted
      Returns:
      the modified 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 to CommentStrategy.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.

      Materializes the comment value, apply field modifier, checks constraints and adds the field to the record.

      Specified by:
      setComment in class CsvCallbackHandler<T>
      Parameters:
      buf - the internal buffer that contains the field value (among other data)
      offset - the offset of the field value in the buffer
      len - the length of the field value
      Throws:
      CsvParseException - if the addition exceeds the limit of record size.
    • modifyComment

      protected String modifyComment(String field)
      Modifies comment value.
      Parameters:
      field - the comment value
      Returns:
      the modified comment value
    • setEmpty

      protected void setEmpty()
      Description copied from class: CsvCallbackHandler
      Called for each empty line.
      Specified by:
      setEmpty in class CsvCallbackHandler<T>
    • compactFields

      protected 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