If no prebuilt index passed in (via IndexedCsvReaderBuilder#index(CsvIndex)) the constructor will initiate
indexing the file.
This process is optimized on performance and low memory usage – no CSV data is stored in memory.
The current status can be monitored via IndexedCsvReaderBuilder#statusListener(StatusListener).
This class is thread-safe.
Example use:
try (IndexedCsvReader<CsvRecord> csv = IndexedCsvReader.builder().ofCsvRecord(file)){
CsvIndex index = csv.getIndex();
int lastPage = index.getPageCount() - 1;
List<CsvRecord> csvRecords = csv.readPage(lastPage);
}
| Modifier and Type | Class and Description |
|---|---|
| public static class | IndexedCsvReader.
This builder is used to create configured instances of |
| private class |
| Modifier and Type | Field and Description |
|---|---|
| private final boolean | |
| private final Charset | |
| private final char | |
| private final CommentStrategy | |
| private final CsvIndex | |
| private final CsvParser | |
| private final CsvCallbackHandler | |
| private final char | |
| private final Path | |
| private final Lock | |
| private final int | |
| private final char | |
| private final RandomAccessFile |
| Access | Constructor and Description |
|---|---|
| pack-priv | IndexedCsvReader(final Path file, final Charset defaultCharset, final char fieldSeparator, final char quoteCharacter, final CommentStrategy commentStrategy, final char commentCharacter, final boolean allowExtraCharsAfterClosingQuote, final int maxBufferSize, final int pageSize, final CsvCallbackHandler<T> csvRecordHandler, final CsvIndex csvIndex, final StatusListener statusListener)
|
| Modifier and Type | Method and Description |
|---|---|
| private static void | assertFields(final char fieldSeparator, final char quoteCharacter, final char commentCharacter, final CommentStrategy commentStrategy)
|
| public static IndexedCsvReader. | Returns: a newIndexedCsvReaderBuilder instance.Constructs a |
| private String | |
| private CsvIndex | |
| public void | close()
Implements java. Closes this stream and releases any system resources associated with it. |
| private static Optional | |
| public CsvIndex | Returns: the index that is used for accessing the CSV file.Get the index used for accessing the CSV file. |
| public List | Returns: a page of records, nevernull.the page to read (0-based). page)Reads a page of records. |
| private List | |
| public String | |
| private static CsvIndex | validatePrebuiltIndex(final Path file, final int bomHeaderLength, final byte fieldSeparator, final byte quoteCharacter, final CommentStrategy commentStrategy, final byte commentCharacter, final CsvIndex csvIndex)
|
| allowExtraCharsAfterClosingQuote | back to summary |
|---|---|
| private final boolean allowExtraCharsAfterClosingQuote | |
| charset | back to summary |
|---|---|
| private final Charset charset | |
| commentCharacter | back to summary |
|---|---|
| private final char commentCharacter | |
| commentStrategy | back to summary |
|---|---|
| private final CommentStrategy commentStrategy | |
| csvIndex | back to summary |
|---|---|
| private final CsvIndex csvIndex | |
| csvParser | back to summary |
|---|---|
| private final CsvParser csvParser | |
| csvRecordHandler | back to summary |
|---|---|
| private final CsvCallbackHandler<T> csvRecordHandler | |
| fieldSeparator | back to summary |
|---|---|
| private final char fieldSeparator | |
| file | back to summary |
|---|---|
| private final Path file | |
| fileLock | back to summary |
|---|---|
| private final Lock fileLock | |
| pageSize | back to summary |
|---|---|
| private final int pageSize | |
| quoteCharacter | back to summary |
|---|---|
| private final char quoteCharacter | |
| raf | back to summary |
|---|---|
| private final RandomAccessFile raf | |
| IndexedCsvReader | back to summary |
|---|---|
pack-priv IndexedCsvReader(final Path file, final Charset defaultCharset, final char fieldSeparator, final char quoteCharacter, final CommentStrategy commentStrategy, final char commentCharacter, final boolean allowExtraCharsAfterClosingQuote, final int maxBufferSize, final int pageSize, final CsvCallbackHandler<T> csvRecordHandler, final CsvIndex csvIndex, final StatusListener statusListener) throws IOException
| |
| assertFields | back to summary |
|---|---|
| private static void assertFields(final char fieldSeparator, final char quoteCharacter, final char commentCharacter, final CommentStrategy commentStrategy) | |
| builder | back to summary |
|---|---|
| public static IndexedCsvReader. Constructs a
| |
| buildExceptionMessage | back to summary |
|---|---|
| private String buildExceptionMessage() | |
| buildIndex | back to summary |
|---|---|
private CsvIndex buildIndex(final int bomHeaderLength, final StatusListener statusListener) throws IOException
| |
| close | back to summary |
|---|---|
| public void close() throws IOException Implements java. Doc from java. Closes this stream and releases any system resources associated with it. If the stream is already closed then invoking this method has no effect. As noted in
| |
| detectBom | back to summary |
|---|---|
| private static Optional | |
| getIndex | back to summary |
|---|---|
| public CsvIndex getIndex() Get the index used for accessing the CSV file.
That index is either a freshly built index or the index that has been
passed via
| |
| readPage | back to summary |
|---|---|
| public List Reads a page of records.
| |
| readPage | back to summary |
|---|---|
private List
| |
| toString | back to summary |
|---|---|
| public String toString() Overrides java. Doc from java. Returns a string representation of the object.
Satisfying this method's contract implies a non-
| |
| validatePrebuiltIndex | back to summary |
|---|---|
| private static CsvIndex validatePrebuiltIndex(final Path file, final int bomHeaderLength, final byte fieldSeparator, final byte quoteCharacter, final CommentStrategy commentStrategy, final byte commentCharacter, final CsvIndex csvIndex) throws IOException | |
IndexedCsvReader. The default
configuration of this class adheres with RFC 4180:
, (comma)
" (double quotes)
CommentStrategy#NONE (as RFC doesn't handle comments)
# (hash) (in case comment strategy is enabled)
false
The line delimiter (line-feed, carriage-return or the combination of both) is detected automatically and thus not configurable.
| Modifier and Type | Field and Description |
|---|---|
| private boolean | |
| private char | |
| private CommentStrategy | |
| private CsvIndex | |
| private static final int | |
| private static final int | |
| private char | |
| private static final int | |
| private int | |
| private static final int | |
| private int | |
| private char | |
| private StatusListener |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public IndexedCsvReader. | Returns: This updated object, allowing additional method calls to be chained together.allow extra characters after closing quotes (default: allowExtraCharsAfterClosingQuote)false).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. |
| public < the type of the CSV record. T> IndexedCsvReader | Returns: a new IndexedCsvReader - nevernull. Remember to close it!the callback handler to use. callbackHandler, final Path the file to read data from. file)Constructs a new |
| public < the type of the CSV record. T> IndexedCsvReader | Returns: a new IndexedCsvReader - nevernull. Remember to close it!the callback handler to use. callbackHandler, final Path the file to read data from. file, final Charset the character set to use. charset)Constructs a new |
| private static void | |
| public IndexedCsvReader. | Returns: This updated object, allowing additional method calls to be chained together.the character used to comment lines (default: commentCharacter)# - hash)Sets the |
| public IndexedCsvReader. | Returns: This updated object, allowing additional method calls to be chained together.the strategy for handling comments. commentStrategy)Sets the strategy that defines how (and if) commented lines should be handled
(default: |
| public IndexedCsvReader. | Returns: This updated object, allowing additional method calls to be chained together.the field separator character (default: fieldSeparator), - comma).Sets the |
| public IndexedCsvReader. | |
| public IndexedCsvReader. | Returns: This updated object, allowing additional method calls to be chained together.the maximum buffer size in characters (default: 16777216) maxBufferSize)Defines the maximum buffer size used when parsing data. |
| public IndexedCsvReader | Returns: a new IndexedCsvReader - nevernull. Remember to close it!the file to read data from. file)Constructs a new |
| public IndexedCsvReader | Returns: a new IndexedCsvReader - nevernull. Remember to close it!the file to read data from. file, final Charset the character set to use. charset)Constructs a new |
| public IndexedCsvReader. | Returns: This updated object, allowing additional method calls to be chained together.the maximum size of pages. pageSize)Sets the |
| public IndexedCsvReader. | Returns: This updated object, allowing additional method calls to be chained together.the character used to enclose fields
(default: quoteCharacter)" - double quotes).Sets the |
| public IndexedCsvReader. | Returns: This updated object, allowing additional method calls to be chained together.the status listener. statusListener)Sets the |
| allowExtraCharsAfterClosingQuote | back to summary |
|---|---|
| private boolean allowExtraCharsAfterClosingQuote | |
| commentCharacter | back to summary |
|---|---|
| private char commentCharacter | |
| commentStrategy | back to summary |
|---|---|
| private CommentStrategy commentStrategy | |
| csvIndex | back to summary |
|---|---|
| private CsvIndex csvIndex
| |
| DEFAULT_MAX_BUFFER_SIZE | back to summary |
|---|---|
| private static final int DEFAULT_MAX_BUFFER_SIZE | |
| DEFAULT_PAGE_SIZE | back to summary |
|---|---|
| private static final int DEFAULT_PAGE_SIZE | |
| fieldSeparator | back to summary |
|---|---|
| private char fieldSeparator | |
| MAX_BASE_ASCII | back to summary |
|---|---|
| private static final int MAX_BASE_ASCII | |
| maxBufferSize | back to summary |
|---|---|
| private int maxBufferSize | |
| MIN_PAGE_SIZE | back to summary |
|---|---|
| private static final int MIN_PAGE_SIZE | |
| pageSize | back to summary |
|---|---|
| private int pageSize | |
| quoteCharacter | back to summary |
|---|---|
| private char quoteCharacter | |
| statusListener | back to summary |
|---|---|
| private StatusListener statusListener
| |
| IndexedCsvReaderBuilder | back to summary |
|---|---|
| private IndexedCsvReaderBuilder() | |
| allowExtraCharsAfterClosingQuote | back to summary |
|---|---|
| public IndexedCsvReader. 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: If this is set to If this is set to
| |
| build | back to summary |
|---|---|
| public <T> IndexedCsvReader Constructs a new Convenience method for
| |
| build | back to summary |
|---|---|
| public <T> IndexedCsvReader Constructs a new
| |
| checkControlCharacter | back to summary |
|---|---|
| private static void checkControlCharacter(final char controlChar) | |
| commentCharacter | back to summary |
|---|---|
| public IndexedCsvReader. Sets the
| |
| commentStrategy | back to summary |
|---|---|
| public IndexedCsvReader. Sets the strategy that defines how (and if) commented lines should be handled
(default:
| |
| fieldSeparator | back to summary |
|---|---|
| public IndexedCsvReader. Sets the
| |
| index | back to summary |
|---|---|
| public IndexedCsvReader. Sets a prebuilt index that should be used for accessing the file.
| |
| maxBufferSize | back to summary |
|---|---|
| public IndexedCsvReader. 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:
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.
| |
| ofCsvRecord | back to summary |
|---|---|
| public IndexedCsvReader Constructs a new Convenience method for
| |
| ofCsvRecord | back to summary |
|---|---|
| public IndexedCsvReader Constructs a new Convenience method for
| |
| pageSize | back to summary |
|---|---|
| public IndexedCsvReader. Sets the
| |
| quoteCharacter | back to summary |
|---|---|
| public IndexedCsvReader. Sets the
| |
| statusListener | back to summary |
|---|---|
| public IndexedCsvReader. Sets the
| |
| Modifier and Type | Field and Description |
|---|---|
| private final List | |
| private final AtomicLong | |
| private final AtomicLong | |
| private final StatusListener |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public void | |
| public void | onReadBytes(final int readCnt)
Implements de.
|
| public void | |
| public void | startOffset(final long offset)
Implements de.
|
| pageOffsets | back to summary |
|---|---|
| private final List<CsvIndex. | |
| recordCounter | back to summary |
|---|---|
| private final AtomicLong recordCounter | |
| startingLineNumber | back to summary |
|---|---|
| private final AtomicLong startingLineNumber | |
| statusListener | back to summary |
|---|---|
| private final StatusListener statusListener | |
| ScannerListener | back to summary |
|---|---|
| private ScannerListener(final StatusListener statusListener) | |
| additionalLine | back to summary |
|---|---|
| public void additionalLine() Implements de.
| |
| onReadBytes | back to summary |
|---|---|
| public void onReadBytes(final int readCnt) Implements de.
| |
| onReadRecord | back to summary |
|---|---|
| public void onReadRecord() Implements de.
| |
| startOffset | back to summary |
|---|---|
| public void startOffset(final long offset) Implements de.
| |