Class IndexedCsvReader.IndexedCsvReaderBuilder

java.lang.Object
de.siegmar.fastcsv.reader.IndexedCsvReader.IndexedCsvReaderBuilder
Enclosing class:
IndexedCsvReader<T>

public static final class IndexedCsvReader.IndexedCsvReaderBuilder extends Object

This builder is used to create configured instances of IndexedCsvReader. The default configuration of this class adheres with RFC 4180:

  • Field separator: , (comma)
  • Quote character: " (double quotes)
  • Comment strategy: CommentStrategy.NONE (as RFC doesn't handle comments)
  • Comment character: # (hash) (in case comment strategy is enabled)
  • Allow extra characters after closing quotes: false
  • Max buffer size: 16,777,216 characters

The line delimiter (line-feed, carriage-return or the combination of both) is detected automatically and thus not configurable.

  • Method Details

    • fieldSeparator

      public IndexedCsvReader.IndexedCsvReaderBuilder fieldSeparator(char fieldSeparator)
      Sets the fieldSeparator used when reading CSV data.
      Parameters:
      fieldSeparator - the field separator character (default: , - comma).
      Returns:
      This updated object, allowing additional method calls to be chained together.
    • quoteCharacter

      public IndexedCsvReader.IndexedCsvReaderBuilder quoteCharacter(char quoteCharacter)
      Sets the quoteCharacter used when reading CSV data.
      Parameters:
      quoteCharacter - the character used to enclose fields (default: " - double quotes).
      Returns:
      This updated object, allowing additional method calls to be chained together.
    • commentStrategy

      public IndexedCsvReader.IndexedCsvReaderBuilder commentStrategy(CommentStrategy commentStrategy)
      Sets the strategy that defines how (and if) commented lines should be handled (default: CommentStrategy.NONE as comments are not defined in RFC 4180).
      Parameters:
      commentStrategy - the strategy for handling comments.
      Returns:
      This updated object, allowing additional method calls to be chained together.
      Throws:
      IllegalArgumentException - if CommentStrategy.SKIP is passed, as this is not supported
      See Also:
    • commentCharacter

      public IndexedCsvReader.IndexedCsvReaderBuilder commentCharacter(char commentCharacter)
      Sets the commentCharacter used to comment lines.
      Parameters:
      commentCharacter - the character used to comment lines (default: # - hash)
      Returns:
      This updated object, allowing additional method calls to be chained together.
      See Also:
    • allowExtraCharsAfterClosingQuote

      public IndexedCsvReader.IndexedCsvReaderBuilder allowExtraCharsAfterClosingQuote(boolean allowExtraCharsAfterClosingQuote)

      Specifies whether the presence of characters between a closing quote and a field separator or the end of a line should be treated as an error or not.

      Example: "a"b,"c"

      If this is set to true, the value ab will be returned for the first field.

      If this is set to false, a CsvParseException will be thrown.

      Parameters:
      allowExtraCharsAfterClosingQuote - allow extra characters after closing quotes (default: false).
      Returns:
      This updated object, allowing additional method calls to be chained together.
    • statusListener

      public IndexedCsvReader.IndexedCsvReaderBuilder statusListener(StatusListener statusListener)
      Sets the statusListener to listen for indexer status updates.
      Parameters:
      statusListener - the status listener.
      Returns:
      This updated object, allowing additional method calls to be chained together.
    • index

      Sets a prebuilt index that should be used for accessing the file.
      Parameters:
      csvIndex - a prebuilt index
      Returns:
      This updated object, allowing additional method calls to be chained together.
    • pageSize

      public IndexedCsvReader.IndexedCsvReaderBuilder pageSize(int pageSize)
      Sets the pageSize for pages returned by IndexedCsvReader.readPage(int) (default: DEFAULT_PAGE_SIZE).
      Parameters:
      pageSize - the maximum size of pages.
      Returns:
      This updated object, allowing additional method calls to be chained together.
    • maxBufferSize

      public IndexedCsvReader.IndexedCsvReaderBuilder maxBufferSize(int maxBufferSize)

      Defines the maximum buffer size used when parsing data.

      The size of the internal buffer is automatically adjusted to the needs of the parser. To protect against out-of-memory errors, its maximum size is limited.

      The buffer is used for two purposes:

      • Reading data from the underlying stream of data in chunks
      • Storing the data of a single field before it is passed to the callback handler

      Set a larger value only if you expect to read fields larger than the default limit. In that case you probably also need to adjust the maximum field size of the callback handler.

      Set a smaller value if your runtime environment has not enough memory available for the default value. Setting values smaller than 16,384 characters will most likely lead to performance degradation.

      Parameters:
      maxBufferSize - the maximum buffer size in characters (default: 16,777,216)
      Returns:
      This updated object, allowing additional method calls to be chained together.
      Throws:
      IllegalArgumentException - if maxBufferSize is not positive
    • ofCsvRecord

      public IndexedCsvReader<CsvRecord> ofCsvRecord(Path file) throws IOException

      Constructs a new IndexedCsvReader of CsvRecord for the specified path using UTF-8 as the character set.

      Convenience method for build(CsvCallbackHandler,Path,Charset) with CsvRecordHandler as the callback handler and StandardCharsets.UTF_8 as the charset.

      Parameters:
      file - the file to read data from.
      Returns:
      a new IndexedCsvReader - never null. Remember to close it!
      Throws:
      IOException - if an I/O error occurs.
      NullPointerException - if file or charset is null
    • ofCsvRecord

      public IndexedCsvReader<CsvRecord> ofCsvRecord(Path file, Charset charset) throws IOException

      Constructs a new IndexedCsvReader of CsvRecord for the specified arguments.

      Convenience method for build(CsvCallbackHandler,Path,Charset) with CsvRecordHandler as the callback handler.

      Parameters:
      file - the file to read data from.
      charset - the character set to use.
      Returns:
      a new IndexedCsvReader - never null. Remember to close it!
      Throws:
      IOException - if an I/O error occurs.
      NullPointerException - if file or charset is null
    • build

      public <T> IndexedCsvReader<T> build(CsvCallbackHandler<T> callbackHandler, Path file) throws IOException

      Constructs a new IndexedCsvReader for the specified callback handler and path using UTF-8 as the character set.

      Convenience method for build(CsvCallbackHandler,Path,Charset) with StandardCharsets.UTF_8 as charset.

      Type Parameters:
      T - the type of the CSV record.
      Parameters:
      callbackHandler - the callback handler to use.
      file - the file to read data from.
      Returns:
      a new IndexedCsvReader - never null. Remember to close it!
      Throws:
      IOException - if an I/O error occurs.
      NullPointerException - if callbackHandler, file or charset is null
    • build

      public <T> IndexedCsvReader<T> build(CsvCallbackHandler<T> callbackHandler, Path file, Charset charset) throws IOException
      Constructs a new IndexedCsvReader for the specified arguments.
      Type Parameters:
      T - the type of the CSV record.
      Parameters:
      callbackHandler - the callback handler to use.
      file - the file to read data from.
      charset - the character set to use.
      Returns:
      a new IndexedCsvReader - never null. Remember to close it!
      Throws:
      IOException - if an I/O error occurs.
      NullPointerException - if callbackHandler, file or charset is null
      IllegalArgumentException - if argument validation fails.