Class IndexedCsvReader.IndexedCsvReaderBuilder
- Enclosing class:
IndexedCsvReader<T>
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 Summary
Modifier and TypeMethodDescriptionallowExtraCharsAfterClosingQuote(boolean allowExtraCharsAfterClosingQuote) Deprecated, for removal: This API element is subject to removal in a future version.This option permits non-conforming input (RFC 4180 does not allow any character between a closing quote and the field separator or end of line) and yields unspecified results if the extra characters contain quote characters.allowUnclosedQuote(boolean allowUnclosedQuote) Defines whether input that ends inside a quoted field (EOF before a closing quote) is tolerated.<T> IndexedCsvReader<T> build(CsvCallbackHandler<T> callbackHandler, Path file) Constructs a newIndexedCsvReaderfor the specified callback handler and path using UTF-8 as the character set.<T> IndexedCsvReader<T> build(CsvCallbackHandler<T> callbackHandler, Path file, Charset charset) Constructs a newIndexedCsvReaderfor the specified arguments.commentCharacter(char commentCharacter) Sets thecommentCharacterused to comment lines.commentStrategy(CommentStrategy commentStrategy) Sets the strategy that defines how (and if) commented lines should be handled (default:CommentStrategy.NONEas comments are not defined in RFC 4180).fieldSeparator(char fieldSeparator) Sets thefieldSeparatorused when reading CSV data.Sets a prebuilt index that should be used for accessing the file.maxBufferSize(int maxBufferSize) Defines the maximum buffer size used when parsing data.ofCsvRecord(Path file) Constructs a newIndexedCsvReaderofCsvRecordfor the specified path using UTF-8 as the character set.ofCsvRecord(Path file, Charset charset) Constructs a newIndexedCsvReaderofCsvRecordfor the specified arguments.pageSize(int pageSize) Sets thepageSizefor pages returned byIndexedCsvReader.readPage(int)(default:DEFAULT_PAGE_SIZE).quoteCharacter(char quoteCharacter) Sets thequoteCharacterused when reading CSV data.statusListener(StatusListener statusListener) Sets thestatusListenerto listen for indexer status updates.
-
Method Details
-
fieldSeparator
Sets thefieldSeparatorused 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
Sets thequoteCharacterused 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
Sets the strategy that defines how (and if) commented lines should be handled (default:CommentStrategy.NONEas 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- ifCommentStrategy.SKIPis passed, as this is not supported- See Also:
-
commentCharacter
Sets thecommentCharacterused 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
@Deprecated(forRemoval=true) public IndexedCsvReader.IndexedCsvReaderBuilder allowExtraCharsAfterClosingQuote(boolean allowExtraCharsAfterClosingQuote) Deprecated, for removal: This API element is subject to removal in a future version.This option permits non-conforming input (RFC 4180 does not allow any character between a closing quote and the field separator or end of line) and yields unspecified results if the extra characters contain quote characters. For sequential reading,CsvReader.CsvReaderBuilder.trimWhitespacesAroundQuotes(boolean)provides a saner alternative.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 valueabwill be returned for the first field.If this is set to
false, aCsvParseExceptionwill be thrown.- Parameters:
allowExtraCharsAfterClosingQuote- allow extra characters after closing quotes (default:false).- Returns:
- This updated object, allowing additional method calls to be chained together.
- See Also:
-
allowUnclosedQuote
Defines whether input that ends inside a quoted field (EOF before a closing quote) is tolerated.
Example:
"foo,barIf this is set to
true, the valuefoo,barwill be returned as a single field; otherwise, aCsvParseExceptionwill be thrown.Independent of this flag, a
CsvParseExceptionis thrown if the unclosed region exceedsmaxBufferSize(int).The default will change to
falsein version 5.0.- Parameters:
allowUnclosedQuote- allow input ending inside a quoted field (default:true).- Returns:
- This updated object, allowing additional method calls to be chained together.
-
statusListener
Sets thestatusListenerto 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.
The file and all settings – including
pageSize(int)– have to be the ones the index was built with; otherwise anIllegalArgumentExceptionis thrown.- Parameters:
csvIndex- a prebuilt index- Returns:
- This updated object, allowing additional method calls to be chained together.
-
pageSize
Sets the
pageSizefor pages returned byIndexedCsvReader.readPage(int)(default:DEFAULT_PAGE_SIZE).One index entry (roughly 40 bytes of heap) is kept per
pageSizerecords – a smaller page size grants finer-grained random access at the cost of memory.- Parameters:
pageSize- the maximum size of pages.- Returns:
- This updated object, allowing additional method calls to be chained together.
-
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
Constructs a new
IndexedCsvReaderofCsvRecordfor the specified path using UTF-8 as the character set.Convenience method for
build(CsvCallbackHandler,Path,Charset)withCsvRecordHandleras the callback handler andStandardCharsets.UTF_8as 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 isnull
-
ofCsvRecord
Constructs a new
IndexedCsvReaderofCsvRecordfor the specified arguments.Convenience method for
build(CsvCallbackHandler,Path,Charset)withCsvRecordHandleras 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 isnull
-
build
public <T> IndexedCsvReader<T> build(CsvCallbackHandler<T> callbackHandler, Path file) throws IOException Constructs a new
IndexedCsvReaderfor the specified callback handler and path using UTF-8 as the character set.Convenience method for
build(CsvCallbackHandler,Path,Charset)withStandardCharsets.UTF_8as 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 isnull
-
build
public <T> IndexedCsvReader<T> build(CsvCallbackHandler<T> callbackHandler, Path file, Charset charset) throws IOException Constructs a new
IndexedCsvReaderfor the specified arguments.Only UTF-8 and single-byte charsets are supported – whether passed explicitly or detected via a BOM header. Every configured control character must additionally map to its own single byte in both directions under that charset.
- 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 (UTF-8 or a single-byte charset).- Returns:
- a new IndexedCsvReader - never
null. Remember to close it! - Throws:
IOException- if an I/O error occurs.NullPointerException- if callbackHandler, file or charset isnullIllegalArgumentException- if argument validation fails, the (supplied or BOM-detected) charset is neither UTF-8 nor single-byte, or a configured control character does not map to its own byte in it.
-