- 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.Closeable
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 viaIndexedCsvReader.IndexedCsvReaderBuilder.statusListener(StatusListener)
.This class is thread-safe.
Example use:
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
IndexedCsvReader.IndexedCsvReaderBuilder
This builder is used to create configured instances ofIndexedCsvReader
.private class
IndexedCsvReader.ScannerListener
-
Field Summary
Fields Modifier and Type Field Description private boolean
acceptCharsAfterQuotes
private java.nio.charset.Charset
charset
private char
commentCharacter
private CommentStrategy
commentStrategy
private CsvIndex
csvIndex
private CsvParser
csvParser
private CsvCallbackHandler<T>
csvRecordHandler
private char
fieldSeparator
private java.nio.file.Path
file
private int
pageSize
private char
quoteCharacter
private java.io.RandomAccessFile
raf
-
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.IndexedCsvReaderBuilder
builder()
Constructs aIndexedCsvReader.IndexedCsvReaderBuilder
to configure and build instances of this class.private CsvIndex
buildIndex(int bomHeaderLength, StatusListener statusListener)
void
close()
private static java.util.Optional<BomHeader>
detectBom(java.nio.file.Path file, StatusListener statusListener)
CsvIndex
getIndex()
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.String
toString()
private static CsvIndex
validatePrebuiltIndex(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.IndexedCsvReaderBuilder
to configure and build instances of this class.- Returns:
- a new
IndexedCsvReader.IndexedCsvReaderBuilder
instance.
-
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
- ifpage
is < 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:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.io.Closeable
- Throws:
java.io.IOException
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
-