Class 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 via IndexedCsvReader.IndexedCsvReaderBuilder.statusListener(StatusListener).

    This class is thread-safe.

    Example use:

    • 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
      • commentCharacter

        private final char commentCharacter
      • acceptCharsAfterQuotes

        private final boolean acceptCharsAfterQuotes
      • pageSize

        private final int pageSize
      • raf

        private final java.io.RandomAccessFile raf
      • 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
      • 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 - if page is < 0
        java.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 interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Throws:
        java.io.IOException
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object