- java.lang.Object
-
- de.siegmar.fastcsv.reader.IndexedCsvReader<T>
-
- Type Parameters:
T- the type of the CSV record.
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable
public final class IndexedCsvReader<T> extends java.lang.Object implements java.io.CloseableCSV 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 viaIndexedCsvReader.IndexedCsvReaderBuilder.statusListener(StatusListener).This class is thread-safe.
Example use:
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classIndexedCsvReader.IndexedCsvReaderBuilderThis builder is used to create configured instances ofIndexedCsvReader.private classIndexedCsvReader.ScannerListener
-
Field Summary
Fields Modifier and Type Field Description private booleanacceptCharsAfterQuotesprivate java.nio.charset.Charsetcharsetprivate charcommentCharacterprivate CommentStrategycommentStrategyprivate CsvIndexcsvIndexprivate CsvParsercsvParserprivate CsvCallbackHandler<T>csvRecordHandlerprivate charfieldSeparatorprivate java.nio.file.Pathfileprivate intpageSizeprivate charquoteCharacterprivate java.io.RandomAccessFileraf
-
Constructor Summary
Constructors Constructor Description IndexedCsvReader(java.nio.file.Path file, java.nio.charset.Charset defaultCharset, char fieldSeparator, char quoteCharacter, CommentStrategy commentStrategy, char commentCharacter, boolean acceptCharsAfterQuotes, int pageSize, CsvCallbackHandler<T> csvRecordHandler, CsvIndex csvIndex, StatusListener statusListener)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static IndexedCsvReader.IndexedCsvReaderBuilderbuilder()Constructs aIndexedCsvReader.IndexedCsvReaderBuilderto configure and build instances of this class.private CsvIndexbuildIndex(int bomHeaderLength, StatusListener statusListener)voidclose()private static java.util.Optional<BomHeader>detectBom(java.nio.file.Path file, StatusListener statusListener)CsvIndexgetIndex()Get the index used for accessing the CSV file.java.util.List<T>readPage(int page)Reads a page of records.private java.util.List<T>readPage(CsvIndex.CsvPage page)java.lang.StringtoString()private static CsvIndexvalidatePrebuiltIndex(java.nio.file.Path file, int bomHeaderLength, byte fieldSeparator, byte quoteCharacter, CommentStrategy commentStrategy, byte commentCharacter, CsvIndex csvIndex)
-
-
-
Field Detail
-
file
private final java.nio.file.Path file
-
charset
private final java.nio.charset.Charset charset
-
fieldSeparator
private final char fieldSeparator
-
quoteCharacter
private final char quoteCharacter
-
commentStrategy
private final CommentStrategy commentStrategy
-
commentCharacter
private final char commentCharacter
-
acceptCharsAfterQuotes
private final boolean acceptCharsAfterQuotes
-
pageSize
private final int pageSize
-
raf
private final java.io.RandomAccessFile raf
-
csvRecordHandler
private final CsvCallbackHandler<T> csvRecordHandler
-
csvParser
private final CsvParser csvParser
-
csvIndex
private final CsvIndex csvIndex
-
-
Constructor Detail
-
IndexedCsvReader
IndexedCsvReader(java.nio.file.Path file, java.nio.charset.Charset defaultCharset, char fieldSeparator, char quoteCharacter, CommentStrategy commentStrategy, char commentCharacter, boolean acceptCharsAfterQuotes, int pageSize, CsvCallbackHandler<T> csvRecordHandler, CsvIndex csvIndex, StatusListener statusListener) throws java.io.IOException- Throws:
java.io.IOException
-
-
Method Detail
-
detectBom
private static java.util.Optional<BomHeader> detectBom(java.nio.file.Path file, StatusListener statusListener) throws java.io.IOException
- Throws:
java.io.IOException
-
validatePrebuiltIndex
private static CsvIndex validatePrebuiltIndex(java.nio.file.Path file, int bomHeaderLength, byte fieldSeparator, byte quoteCharacter, CommentStrategy commentStrategy, byte commentCharacter, CsvIndex csvIndex) throws java.io.IOException
- Throws:
java.io.IOException
-
buildIndex
private CsvIndex buildIndex(int bomHeaderLength, StatusListener statusListener) throws java.io.IOException
- Throws:
java.io.IOException
-
builder
public static IndexedCsvReader.IndexedCsvReaderBuilder builder()
Constructs aIndexedCsvReader.IndexedCsvReaderBuilderto configure and build instances of this class.- Returns:
- a new
IndexedCsvReader.IndexedCsvReaderBuilderinstance.
-
getIndex
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 viaIndexedCsvReader.IndexedCsvReaderBuilder.index(CsvIndex).- Returns:
- the index that is used for accessing the CSV file.
-
readPage
public java.util.List<T> readPage(int page) throws java.io.IOException
Reads a page of records.- Parameters:
page- the page to read (0-based).- Returns:
- a page of records, never
null. - Throws:
java.io.IOException- if an I/O error occurs.java.lang.IllegalArgumentException- ifpageis < 0java.lang.IndexOutOfBoundsException- if the file does not contain the specified page
-
readPage
private java.util.List<T> readPage(CsvIndex.CsvPage page) throws java.io.IOException
- Throws:
java.io.IOException
-
close
public void close() throws java.io.IOException- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Throws:
java.io.IOException
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
-