Class CsvRecordsReader<T>

  • Type Parameters:
    T - The type managed by the Records.
    All Implemented Interfaces:
    java.lang.AutoCloseable, java.lang.Iterable<Record<T>>, java.util.Iterator<Record<T>>, org.refcodes.mixin.DelimiterAccessor, org.refcodes.mixin.DelimiterAccessor.DelimiterBuilder<org.refcodes.textual.CsvMixin>, org.refcodes.mixin.DelimiterAccessor.DelimiterMutator, org.refcodes.mixin.DelimiterAccessor.DelimiterProperty, org.refcodes.mixin.TrimAccessor, org.refcodes.mixin.TrimAccessor.TrimBuilder<org.refcodes.textual.CsvMixin>, org.refcodes.mixin.TrimAccessor.TrimMutator, org.refcodes.mixin.TrimAccessor.TrimProperty, HeaderAccessor<T>, Records<T>, RecordsReader<T>, org.refcodes.textual.CsvEscapeModeAccessor, org.refcodes.textual.CsvEscapeModeAccessor.CsvEscapeModeBuilder<org.refcodes.textual.CsvMixin>, org.refcodes.textual.CsvEscapeModeAccessor.CsvEscapeModeMutator, org.refcodes.textual.CsvEscapeModeAccessor.CsvEscapeModeProperty, org.refcodes.textual.CsvMixin
    Direct Known Subclasses:
    CsvStringRecordsReader

    public class CsvRecordsReader<T>
    extends java.lang.Object
    implements org.refcodes.textual.CsvMixin, RecordsReader<T>
    The CsvRecordsReader is an implementation of the Records interface and provides functionality to parse CSV input streams.
    • Constructor Detail

      • CsvRecordsReader

        public CsvRecordsReader​(Header<T> aHeader,
                                java.io.File aCsvFile)
                         throws java.io.FileNotFoundException,
                                java.io.IOException,
                                java.util.zip.ZipException
        Constructs a CsvRecordsReader with the given parameters. Internally Column instances are generated according to the keys found in the CSV top line. The Column instances are required to convert the CSV line values. If a Header is provided, then the Header is used for generating the Column instances instead of the top line of the CSV file.
        Parameters:
        aHeader - The Header to use when parsing the lines retrieved from the InputStream.
        aCsvFile - The CSV File which to parse.
        Throws:
        java.io.FileNotFoundException - the file not found exception
        java.io.IOException - in case there were problems working with the given File.
        java.util.zip.ZipException - the zip exception
      • CsvRecordsReader

        public CsvRecordsReader​(Header<T> aHeader,
                                java.io.File aCsvFile,
                                char aCsvSeparator)
                         throws java.io.FileNotFoundException,
                                java.io.IOException,
                                java.util.zip.ZipException
        Constructs a CsvRecordsReader with the given parameters. Internally Column instances are generated according to the keys found in the CSV top line. The Column instances are required to convert the CSV line values. If a Header is provided, then the Header is used for generating the Column instances instead of the top line of the CSV file.
        Parameters:
        aHeader - The Header to use when parsing the lines retrieved from the InputStream.
        aCsvFile - The CSV File which to parse.
        aCsvSeparator - The delimiter being expected for the CSV input stream.
        Throws:
        java.io.FileNotFoundException - the file not found exception
        java.io.IOException - in case there were problems working with the given File.
        java.util.zip.ZipException - the zip exception
      • CsvRecordsReader

        public CsvRecordsReader​(Header<T> aHeader,
                                java.io.File aCsvFile,
                                boolean isStrict)
                         throws java.io.FileNotFoundException,
                                java.io.IOException,
                                java.util.zip.ZipException
        Constructs a CsvRecordsReader with the given parameters. Internally Column instances are generated according to the keys found in the CSV top line. The Column instances are required to convert the CSV line values. If a Header is provided, then the Header is used for generating the Column instances instead of the top line of the CSV file.
        Parameters:
        aHeader - The Header to use when parsing the lines retrieved from the InputStream.
        aCsvFile - The CSV File which to parse.
        isStrict - When true, then parsing will abort with an exception in case of parsing problems, else parsing is gracefully continued and erroneous records are skipped. The error count getErroneousRecordCount() is incremented by each erroneous Record.
        Throws:
        java.io.FileNotFoundException - the file not found exception
        java.io.IOException - in case there were problems working with the given File.
        java.util.zip.ZipException - the zip exception
      • CsvRecordsReader

        public CsvRecordsReader​(Header<T> aHeader,
                                java.io.File aCsvFile,
                                char aCsvSeparator,
                                boolean isStrict)
                         throws java.io.FileNotFoundException,
                                java.io.IOException,
                                java.util.zip.ZipException
        Constructs a CsvRecordsReader with the given parameters. Internally Column instances are generated according to the keys found in the CSV top line. The Column instances are required to convert the CSV line values. If a Header is provided, then the Header is used for generating the Column instances instead of the top line of the CSV file.
        Parameters:
        aHeader - The Header to use when parsing the lines retrieved from the InputStream.
        aCsvFile - The CSV File which to parse.
        aCsvSeparator - The delimiter being expected for the CSV input stream.
        isStrict - When true, then parsing will abort with an exception in case of parsing problems, else parsing is gracefully continued and erroneous records are skipped. The error count getErroneousRecordCount() is incremented by each erroneous Record.
        Throws:
        java.io.FileNotFoundException - the file not found exception
        java.io.IOException - in case there were problems working with the given File.
        java.util.zip.ZipException - the zip exception
      • CsvRecordsReader

        public CsvRecordsReader​(ColumnFactory<T> aColumnFactory,
                                java.io.File aCsvFile)
                         throws java.io.FileNotFoundException,
                                java.io.IOException,
                                java.util.zip.ZipException
        Constructs a CsvRecordsReader with the given parameters. This constructor supports a ColumnFactory for creating Column instance according to the keys found in the CSV top line. The Column instances are required to convert the CSV line values from the storage format to the actual required type.
        Parameters:
        aColumnFactory - A ColumnFactory to be used to generate Column instances from the top line of the CSF file, required for parsing the CSV lines and converting them to Record instances.
        aCsvFile - The CSV File which to parse.
        Throws:
        java.io.FileNotFoundException - the file not found exception
        java.io.IOException - in case there were problems working with the given File.
        java.util.zip.ZipException - the zip exception
      • CsvRecordsReader

        public CsvRecordsReader​(ColumnFactory<T> aColumnFactory,
                                java.io.File aCsvFile,
                                char aCsvSeparator)
                         throws java.io.FileNotFoundException,
                                java.io.IOException,
                                java.util.zip.ZipException
        Constructs a CsvRecordsReader with the given parameters. This constructor supports a ColumnFactory for creating Column instance according to the keys found in the CSV top line. The Column instances are required to convert the CSV line values from the storage format to the actual required type.
        Parameters:
        aColumnFactory - A ColumnFactory to be used to generate Column instances from the top line of the CSF file, required for parsing the CSV lines and converting them to Record instances.
        aCsvFile - The CSV File which to parse.
        aCsvSeparator - The delimiter being expected for the CSV input stream.
        Throws:
        java.io.FileNotFoundException - the file not found exception
        java.io.IOException - in case there were problems working with the given File.
        java.util.zip.ZipException - the zip exception
      • CsvRecordsReader

        public CsvRecordsReader​(ColumnFactory<T> aColumnFactory,
                                java.io.File aCsvFile,
                                boolean isStrict)
                         throws java.io.FileNotFoundException,
                                java.io.IOException,
                                java.util.zip.ZipException
        Constructs a CsvRecordsReader with the given parameters. This constructor supports a ColumnFactory for creating Column instance according to the keys found in the CSV top line. The Column instances are required to convert the CSV line values from the storage format to the actual required type.
        Parameters:
        aColumnFactory - A ColumnFactory to be used to generate Column instances from the top line of the CSF file, required for parsing the CSV lines and converting them to Record instances.
        aCsvFile - The CSV File which to parse.
        isStrict - When true, then parsing will abort with an exception in case of parsing problems, else parsing is gracefully continued and erroneous records are skipped. The error count getErroneousRecordCount() is incremented by each erroneous Record.
        Throws:
        java.io.FileNotFoundException - the file not found exception
        java.io.IOException - in case there were problems working with the given File.
        java.util.zip.ZipException - the zip exception
      • CsvRecordsReader

        public CsvRecordsReader​(ColumnFactory<T> aColumnFactory,
                                java.io.File aCsvFile,
                                char aCsvSeparator,
                                boolean isStrict)
                         throws java.io.FileNotFoundException,
                                java.io.IOException,
                                java.util.zip.ZipException
        Constructs a CsvRecordsReader with the given parameters. This constructor supports a ColumnFactory for creating Column instance according to the keys found in the CSV top line. The Column instances are required to convert the CSV line values from the storage format to the actual required type.
        Parameters:
        aColumnFactory - A ColumnFactory to be used to generate Column instances from the top line of the CSF file, required for parsing the CSV lines and converting them to Record instances.
        aCsvFile - The CSV File which to parse.
        aCsvSeparator - The delimiter being expected for the CSV input stream.
        isStrict - When true, then parsing will abort with an exception in case of parsing problems, else parsing is gracefully continued and erroneous records are skipped. The error count getErroneousRecordCount() is incremented by each erroneous Record.
        Throws:
        java.io.FileNotFoundException - the file not found exception
        java.io.IOException - in case there were problems working with the given File.
        java.util.zip.ZipException - the zip exception
      • CsvRecordsReader

        public CsvRecordsReader​(Header<T> aHeader,
                                java.io.InputStream aCsvInputStream)
                         throws java.io.IOException
        Constructs a CsvRecordsReader with the given parameters. Internally Column instances are generated according to the keys found in the CSV top line. The Column instances are required to convert the CSV line values. If a Header is provided, then the Header is used for generating the Column instances instead of the top line of the CSV file.
        Parameters:
        aHeader - The Header to use when parsing the lines retrieved from the InputStream.
        aCsvInputStream - The CSV InputStream which to parse.
        Throws:
        java.io.IOException - in case there were problems working with the given InputStream.
      • CsvRecordsReader

        public CsvRecordsReader​(Header<T> aHeader,
                                java.io.InputStream aCsvInputStream,
                                boolean isStrict)
                         throws java.io.IOException
        Constructs a CsvRecordsReader with the given parameters. Internally Column instances are generated according to the keys found in the CSV top line. The Column instances are required to convert the CSV line values. If a Header is provided, then the Header is used for generating the Column instances instead of the top line of the CSV file.
        Parameters:
        aHeader - The Header to use when parsing the lines retrieved from the InputStream.
        aCsvInputStream - The CSV InputStream which to parse.
        isStrict - When true, then parsing will abort with an exception in case of parsing problems, else parsing is gracefully continued and erroneous records are skipped. The error count getErroneousRecordCount() is incremented by each erroneous Record.
        Throws:
        java.io.IOException - in case there were problems working with the given InputStream.
      • CsvRecordsReader

        public CsvRecordsReader​(Header<T> aHeader,
                                java.io.InputStream aCsvInputStream,
                                char aCsvDelimiter)
                         throws java.io.IOException
        Constructs a CsvRecordsReader with the given parameters. Internally Column instances are generated according to the keys found in the CSV top line. The Column instances are required to convert the CSV line values. If a Header is provided, then the Header is used for generating the Column instances instead of the top line of the CSV file.
        Parameters:
        aHeader - The Header to use when parsing the lines retrieved from the InputStream.
        aCsvInputStream - The CSV InputStream which to parse.
        aCsvDelimiter - The delimiter being expected for the CSV input stream.
        Throws:
        java.io.IOException - in case there were problems working with the given InputStream.
      • CsvRecordsReader

        public CsvRecordsReader​(Header<T> aHeader,
                                java.io.InputStream aCsvInputStream,
                                char aCsvDelimiter,
                                boolean isStrict)
                         throws java.io.IOException
        Constructs a CsvRecordsReader with the given parameters. Internally Column instances are generated according to the keys found in the CSV top line. The Column instances are required to convert the CSV line values. If a Header is provided, then the Header is used for generating the Column instances instead of the top line of the CSV file.
        Parameters:
        aHeader - The Header to use when parsing the lines retrieved from the InputStream.
        aCsvInputStream - The CSV InputStream which to parse.
        aCsvDelimiter - The delimiter being expected for the CSV input stream.
        isStrict - When true, then parsing will abort with an exception in case of parsing problems, else parsing is gracefully continued and erroneous records are skipped. The error count getErroneousRecordCount() is incremented by each erroneous Record.
        Throws:
        java.io.IOException - in case there were problems working with the given InputStream.
      • CsvRecordsReader

        public CsvRecordsReader​(ColumnFactory<T> aColumnFactory,
                                java.io.InputStream aCsvInputStream)
                         throws java.io.IOException
        Constructs a CsvRecordsReader with the given parameters. This constructor supports a ColumnFactory for creating Column instance according to the keys found in the CSV top line. The Column instances are required to convert the CSV line values from the storage format to the actual required type.
        Parameters:
        aColumnFactory - A ColumnFactory to be used to generate Column instances from the top line of the CSF file, required for parsing the CSV lines and converting them to Record instances.
        aCsvInputStream - The CSV InputStream which to parse.
        Throws:
        java.io.IOException - in case there were problems working with the given InputStream.
      • CsvRecordsReader

        public CsvRecordsReader​(ColumnFactory<T> aColumnFactory,
                                java.io.InputStream aCsvInputStream,
                                boolean isStrict)
                         throws java.io.IOException
        Constructs a CsvRecordsReader with the given parameters. This constructor supports a ColumnFactory for creating Column instance according to the keys found in the CSV top line. The Column instances are required to convert the CSV line values from the storage format to the actual required type.
        Parameters:
        aColumnFactory - A ColumnFactory to be used to generate Column instances from the top line of the CSF file, required for parsing the CSV lines and converting them to Record instances.
        aCsvInputStream - The CSV InputStream which to parse.
        isStrict - When true, then parsing will abort with an exception in case of parsing problems, else parsing is gracefully continued and erroneous records are skipped. The error count getErroneousRecordCount() is incremented by each erroneous Record.
        Throws:
        java.io.IOException - in case there were problems working with the given InputStream.
      • CsvRecordsReader

        public CsvRecordsReader​(ColumnFactory<T> aColumnFactory,
                                java.io.InputStream aCsvInputStream,
                                char aCsvDelimiter)
                         throws java.io.IOException
        Constructs a CsvRecordsReader with the given parameters. This constructor supports a ColumnFactory for creating Column instance according to the keys found in the CSV top line. The Column instances are required to convert the CSV line values from the storage format to the actual required type.
        Parameters:
        aColumnFactory - A ColumnFactory to be used to generate Column instances from the top line of the CSF file, required for parsing the CSV lines and converting them to Record instances.
        aCsvInputStream - The CSV InputStream which to parse.
        aCsvDelimiter - The delimiter being expected for the CSV input stream.
        Throws:
        java.io.IOException - in case there were problems working with the given InputStream.
      • CsvRecordsReader

        public CsvRecordsReader​(ColumnFactory<T> aColumnFactory,
                                java.io.InputStream aCsvInputStream,
                                char aCsvDelimiter,
                                boolean isStrict)
                         throws java.io.IOException
        Constructs a CsvRecordsReader with the given parameters. This constructor supports a ColumnFactory for creating Column instance according to the keys found in the CSV top line. The Column instances are required to convert the CSV line values from the storage format to the actual required type.
        Parameters:
        aColumnFactory - A ColumnFactory to be used to generate Column instances from the top line of the CSF file, required for parsing the CSV lines and converting them to Record instances.
        aCsvInputStream - The CSV InputStream which to parse.
        aCsvDelimiter - The delimiter being expected for the CSV input stream.
        isStrict - When true, then parsing will abort with an exception in case of parsing problems, else parsing is gracefully continued and erroneous records are skipped. The error count getErroneousRecordCount() is incremented by each erroneous Record.
        Throws:
        java.io.IOException - in case there were problems working with the given InputStream.
      • CsvRecordsReader

        protected CsvRecordsReader​(Header<T> aHeader,
                                   ColumnFactory<T> aColumnFactory,
                                   java.io.InputStream aCsvInputStream,
                                   char aCsvDelimiter,
                                   boolean isStrict)
                            throws java.io.IOException
        Constructs a CsvRecordsReader with the given parameters. This constructor supports a ColumnFactory for creating Column instance according to the keys found in the CSV top line. The Column instances are required to convert the CSV line values from the storage format to the actual required type. If a Header is provided, then the Header is used for generating the Column instances instead of the top line of the CSV file.
        Parameters:
        aHeader - The Header to use when parsing the lines retrieved from the InputStream.
        aColumnFactory - A ColumnFactory to be used to generate Column instances from the top line of the CSF file, required for parsing the CSV lines and converting them to Record instances.
        aCsvInputStream - The CSV InputStream which to parse.
        aCsvDelimiter - The delimiter being expected for the CSV input stream.
        isStrict - When true, then parsing will abort with an exception in case of parsing problems, else parsing is gracefully continued and erroneous records are skipped. The error count getErroneousRecordCount() is incremented by each erroneous Record.
        Throws:
        java.io.IOException - in case there were problems working with the given InputStream.
    • Method Detail

      • readHeader

        public Header<T> readHeader()
                             throws java.io.IOException
        Creates a Header from the next line to be read .
        Specified by:
        readHeader in interface RecordsReader<T>
        Returns:
        Returns the Header being read.
        Throws:
        java.io.IOException - thrown in case reading from IO failed.
      • skipHeader

        public java.lang.String skipHeader()
                                    throws java.io.IOException
        Skips the next line to be read. This line will not be processed by the Iterator.next() method. This methods is useful when skipping the first line (header).
        Specified by:
        skipHeader in interface RecordsReader<T>
        Returns:
        The line being skipped.
        Throws:
        java.io.IOException - thrown in case if IO problems.
      • hasNext

        public boolean hasNext()
        Specified by:
        hasNext in interface java.util.Iterator<T>
      • readNext

        public java.lang.String[] readNext()
        Reads the next line from the (standard) input (stream or file) and returns an array of its String representation.
        Specified by:
        readNext in interface RecordsReader<T>
        Returns:
        The String array representation of the next Record instance read.
      • next

        public Record<T> next()
        Specified by:
        next in interface java.util.Iterator<T>
      • remove

        public void remove()
        Specified by:
        remove in interface java.util.Iterator<T>
      • setCommentPrefixes

        public void setCommentPrefixes​(java.lang.String... aCommentPrefixes)
        Specified by:
        setCommentPrefixes in interface org.refcodes.textual.CsvMixin
      • getCommentPrefixes

        public java.lang.String[] getCommentPrefixes()
        Specified by:
        getCommentPrefixes in interface org.refcodes.textual.CsvMixin
      • clearCommentPrefixes

        public void clearCommentPrefixes()
        Specified by:
        clearCommentPrefixes in interface org.refcodes.textual.CsvMixin
      • withCommentPrefixes

        public org.refcodes.textual.CsvMixin withCommentPrefixes​(java.lang.String... aCommentPrefixes)
        Specified by:
        withCommentPrefixes in interface org.refcodes.textual.CsvMixin
      • close

        public void close()
                   throws java.io.IOException
        Specified by:
        close in interface java.lang.AutoCloseable
        Throws:
        java.io.IOException
      • getCsvEscapeMode

        public org.refcodes.textual.CsvEscapeMode getCsvEscapeMode()
        Specified by:
        getCsvEscapeMode in interface org.refcodes.textual.CsvEscapeModeAccessor
      • isTrim

        public boolean isTrim()
        Specified by:
        isTrim in interface org.refcodes.mixin.TrimAccessor
      • getDelimiter

        public char getDelimiter()
        Specified by:
        getDelimiter in interface org.refcodes.mixin.DelimiterAccessor
      • setTrim

        public void setTrim​(boolean isTrim)
        Specified by:
        setTrim in interface org.refcodes.mixin.TrimAccessor.TrimMutator
      • setDelimiter

        public void setDelimiter​(char aCsvDelimiter)
        Specified by:
        setDelimiter in interface org.refcodes.mixin.DelimiterAccessor.DelimiterMutator
      • withTrim

        public CsvRecordsReader<T> withTrim​(boolean isTrimRecords)
        Specified by:
        withTrim in interface org.refcodes.mixin.TrimAccessor.TrimBuilder<T>
      • withCsvEscapeMode

        public CsvRecordsReader<T> withCsvEscapeMode​(org.refcodes.textual.CsvEscapeMode aCsvEscapeMode)
        Specified by:
        withCsvEscapeMode in interface org.refcodes.textual.CsvEscapeModeAccessor.CsvEscapeModeBuilder<T>
      • withDelimiter

        public CsvRecordsReader<T> withDelimiter​(char aCsvDelimiter)
        Specified by:
        withDelimiter in interface org.refcodes.mixin.DelimiterAccessor.DelimiterBuilder<T>
      • setCsvEscapeMode

        public void setCsvEscapeMode​(org.refcodes.textual.CsvEscapeMode aCsvEscapeMode)
        Specified by:
        setCsvEscapeMode in interface org.refcodes.textual.CsvEscapeModeAccessor.CsvEscapeModeMutator
      • getErroneousRecordCount

        public long getErroneousRecordCount()
        Gets the erroneous record count.
        Specified by:
        getErroneousRecordCount in interface RecordsReader<T>
        Returns:
        the erroneous record count
      • toInputStream

        protected static java.io.InputStream toInputStream​(java.io.File aFile)
                                                    throws java.util.zip.ZipException,
                                                           java.io.IOException,
                                                           java.io.FileNotFoundException
        Returns an InputStream from the provided File. In case the file points to a ZIP compressed file, then the uncompressed data is provided by the InputStream.
        Parameters:
        aFile - The File for which to get the InputStream.
        Returns:
        An InputStream, in case of a ZIP compressed File, an uncompressed InputStream is returned.
        Throws:
        java.util.zip.ZipException - in case there were problems when accessing the ZIP compressed File.
        java.io.IOException - in case there were problems working with the File.
        java.io.FileNotFoundException - in case there was none such File found.
      • toFileNameFromZip

        protected static java.lang.String toFileNameFromZip​(java.lang.String aZipFileName)
        Truncates the ".zip" suffix from the filename and returns the result. For example a file with name "log-2023-07-12.txt.zip" results in "log-2023-07-12.txt".
        Parameters:
        aZipFileName - The file name of the ZIP file for which to get the "inner" file name.
        Returns:
        The "inner" file name if the file suffix was ".zip", else null.