Class IndexedCsvReader<T>
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 classThis builder is used to create configured instances ofIndexedCsvReader.private final class -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final booleanprivate final Charsetprivate final charprivate final CommentStrategyprivate final CsvIndexprivate final CsvParserprivate final CsvCallbackHandler<T> private final charprivate final Pathprivate final intprivate final charprivate 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.IndexedCsvReaderBuilderto configure and build instances of this class.private CsvIndexbuildIndex(int bomHeaderLength, StatusListener statusListener) voidclose()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 CsvIndexvalidatePrebuiltIndex(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.IndexedCsvReaderBuilderto configure and build instances of this class.- Returns:
- a new
IndexedCsvReader.IndexedCsvReaderBuilderinstance.
-
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- ifpageis < 0IndexOutOfBoundsException- if the file does not contain the specified page
-
readPage
- Throws:
IOException
-
close
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Throws:
IOException
-
toString
-