java.lang.Object
de.siegmar.fastcsv.reader.IndexedCsvReader<T>
- Type Parameters:
T
- the type of the CSV record.
- All Implemented Interfaces:
Closeable
,AutoCloseable
CSV reader implementation for indexed based access.
If no prebuilt index passed in (via IndexedCsvReader.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 IndexedCsvReader.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);
}
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final class
This builder is used to create configured instances ofIndexedCsvReader
.private final class
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final boolean
private final Charset
private final char
private final CommentStrategy
private final CsvIndex
private final CsvParser
private final CsvCallbackHandler
<T> private final char
private final Path
private final int
private final char
private final RandomAccessFile
-
Constructor Summary
ConstructorsConstructorDescriptionIndexedCsvReader
(Path file, Charset defaultCharset, char fieldSeparator, char quoteCharacter, CommentStrategy commentStrategy, char commentCharacter, boolean acceptCharsAfterQuotes, int pageSize, CsvCallbackHandler<T> csvRecordHandler, CsvIndex csvIndex, StatusListener statusListener) -
Method Summary
Modifier and TypeMethodDescriptionbuilder()
Constructs aIndexedCsvReader.IndexedCsvReaderBuilder
to configure and build instances of this class.private CsvIndex
buildIndex
(int bomHeaderLength, StatusListener statusListener) void
close()
detectBom
(Path file, StatusListener statusListener) getIndex()
Get the index used for accessing the CSV file.readPage
(int page) Reads a page of records.readPage
(CsvIndex.CsvPage page) toString()
private static CsvIndex
validatePrebuiltIndex
(Path file, int bomHeaderLength, byte fieldSeparator, byte quoteCharacter, CommentStrategy commentStrategy, byte commentCharacter, CsvIndex csvIndex)
-
Field Details
-
file
-
charset
-
fieldSeparator
private final char fieldSeparator -
quoteCharacter
private final char quoteCharacter -
commentStrategy
-
commentCharacter
private final char commentCharacter -
acceptCharsAfterQuotes
private final boolean acceptCharsAfterQuotes -
pageSize
private final int pageSize -
raf
-
csvRecordHandler
-
csvParser
-
csvIndex
-
-
Constructor Details
-
IndexedCsvReader
IndexedCsvReader(Path file, Charset defaultCharset, char fieldSeparator, char quoteCharacter, CommentStrategy commentStrategy, char commentCharacter, boolean acceptCharsAfterQuotes, int pageSize, CsvCallbackHandler<T> csvRecordHandler, CsvIndex csvIndex, StatusListener statusListener) throws IOException - Throws:
IOException
-
-
Method Details
-
detectBom
private static Optional<BomHeader> detectBom(Path file, StatusListener statusListener) throws IOException - Throws:
IOException
-
validatePrebuiltIndex
private static CsvIndex validatePrebuiltIndex(Path file, int bomHeaderLength, byte fieldSeparator, byte quoteCharacter, CommentStrategy commentStrategy, byte commentCharacter, CsvIndex csvIndex) throws IOException - Throws:
IOException
-
buildIndex
- Throws:
IOException
-
builder
Constructs aIndexedCsvReader.IndexedCsvReaderBuilder
to configure and build instances of this class.- Returns:
- a new
IndexedCsvReader.IndexedCsvReaderBuilder
instance.
-
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 viaIndexedCsvReader.IndexedCsvReaderBuilder.index(CsvIndex)
.- Returns:
- the index that is used for accessing the CSV file.
-
readPage
Reads a page of records.- Parameters:
page
- the page to read (0-based).- Returns:
- a page of records, never
null
. - Throws:
IOException
- if an I/O error occurs.IllegalArgumentException
- ifpage
is < 0IndexOutOfBoundsException
- if the file does not contain the specified page
-
readPage
- Throws:
IOException
-
close
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Throws:
IOException
-
toString
-