Class CSVDecoder<T>

  • All Implemented Interfaces:
    Closeable, AutoCloseable, ItemDecoder<T>

    public class CSVDecoder<T>
    extends Object
    implements ItemDecoder<T>
    An ItemDecoder that decodes CSV rows. Supports skipping header rows. Uses user defined function to create the java object from the string array parsed from an input line.
    Author:
    Eyal Schneider
    • Constructor Detail

      • CSVDecoder

        public CSVDecoder​(InputStream is,
                          FailableFunction<String[],​T,​ValidationPipeException> itemDetextualizer,
                          Charset charset,
                          char delimiter,
                          int rowsToSkip,
                          FileReadOptions readOptions)
                   throws IOException
        Constructor
        Parameters:
        is - The input stream this decoder is bound to. Not expected to be buffered. Buffering is added internally.
        itemDetextualizer - The logic that transforms a parsed line (String[]) into the corresponding java object. A column which is empty in the input is passed as null, unless it's an empty quoted string, which is passed as an empty string. Leading/trailing spaces from the input are kept (even if not quoted), so it's up to this function to decide what to do with them.
        charset - The character encoding to assume when reading from the input stream
        delimiter - The CSV delimiter character to use
        rowsToSkip - The number of header rows to skip before parsing data lines. Non-negative.
        readOptions - Input reading options (buffering/compression)
        Throws:
        IOException - In case of an IO error while preparing to read
      • CSVDecoder

        public CSVDecoder​(InputStream is,
                          FailableFunction<String[],​T,​ValidationPipeException> itemDetextualizer,
                          int rowsToSkip)
                   throws IOException
        Constructor Assumes UTF8, comma as a delimiter and default read options (no compression)
        Parameters:
        is - The input stream this decoder is bound to. Not expected to be buffered. Buffering is added internally.
        itemDetextualizer - The logic that transforms a parsed line (String[]) into the corresponding java object. Columns which are empty in the input are passes as null. Leading/trailing spaces from the input are kept, so it's up to this function to decide what to do with them.
        rowsToSkip - The number of header rows to skip before parsing data lines. Non-negative.
        Throws:
        IOException - In case of an IO error while preparing to read
      • CSVDecoder

        public CSVDecoder​(InputStream is,
                          FailableFunction<String[],​T,​ValidationPipeException> itemDetextualizer)
                   throws IOException
        Constructor Assumes UTF8, comma as a delimiter, no header rows and default read options (no compression)
        Parameters:
        is - The input stream this decoder is bound to. Not expected to be buffered. Buffering is added internally.
        itemDetextualizer - The logic that transforms a parsed line (String[]) into the corresponding java object. Columns which are empty in the input are passes as null. Leading/trailing spaces from the input are kept, so it's up to this function to decide what to do with them.
        Throws:
        IOException - In case of an IO error while preparing to read
    • Method Detail

      • getFactory

        public static <R> DecoderFactory<R> getFactory​(FailableFunction<String[],​R,​ValidationPipeException> itemDetextualizer,
                                                       Charset charset,
                                                       char delimiter,
                                                       int rowsToSkip)
        Parameters:
        itemDetextualizer - The logic that transforms a parsed line (String[]) into the corresponding java object. Columns which are empty in the input are passes as null. Leading/trailing spaces from the input are kept, so it's up to this function to decide what to do with them.
        charset - The character encoding to assume when reading from the input stream
        delimiter - The CSV delimiter character to use
        rowsToSkip - The number of header rows to skip before parsing data lines. Non-negative.
        Returns:
        A decoder factory producing CSV decoders based on the given settings
      • getFactory

        public static <R> DecoderFactory<R> getFactory​(FailableFunction<String[],​R,​ValidationPipeException> itemDetextualizer)
        Parameters:
        itemDetextualizer - The logic that transforms a parsed line (String[]) into the corresponding java object. Columns which are empty in the input are passes as null. Leading/trailing spaces from the input are kept, so it's up to this function to decide what to do with them.
        Returns:
        A decoder factory producing CSV decoders based on the given settings. Assumes UTF8, comma as delimiter and no header rows.