Class CSVEncoder<T>

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

    public class CSVEncoder<T>
    extends Object
    implements ItemEncoder<T>
    An ItemEncoder that encodes objects as compliant CSV rows. Supports producing header row/s. Uses user defined function to encode object fields as a string array.
    Author:
    Eyal Schneider
    • Constructor Detail

      • CSVEncoder

        public CSVEncoder​(OutputStream os,
                          Function<T,​String[]> itemTextualizer,
                          Charset charset,
                          char delimiter,
                          String headerBlock,
                          FileWriteOptions writeOptions)
                   throws IOException
        Constructor
        Parameters:
        os - The output stream this encoder is bound to. Not expected to be buffered. Buffering is added internally.
        itemTextualizer - The logic that transforms an item into the corresponding CSV columns. Note that this function is responsible for handling leading/trailing spaces, since they are passed to output unchanged. Also note that an empty string will be written as an empty quoted string, to distinguish it from an empty column which is read as null.
        charset - The character encoding to use when writing to the output stream
        delimiter - The CSV delimiter character to use
        headerBlock - A text to be written to the output, once, at the beginning. Use '\n' to create multiple lines. Use null to add no header block.
        writeOptions - output writing options (buffering/compression)
        Throws:
        IOException - In case of an IO error while preparing to write
      • CSVEncoder

        public CSVEncoder​(OutputStream os,
                          Function<T,​String[]> itemTextualizer)
                   throws IOException
        Constructor Assumes UTF8, comma as a delimiter and default write options (no compression)
        Parameters:
        os - The output stream this encoder is bound to. Not expected to be buffered. Buffering is added internally.
        itemTextualizer - The logic that transforms an item into the corresponding CSV columns. Note that this function is responsible for handling leading/trailing spaces, since they are passed to output unchanged.
        Throws:
        IOException - In case of an IO error while preparing to write
    • Method Detail

      • encode

        public void encode​(T item)
                    throws IOException
        Specified by:
        encode in interface ItemEncoder<T>
        Parameters:
        item - The item to encode into the output stream provided at construction type
        Throws:
        IOException - In case of write error
      • getFactory

        public static <R> EncoderFactory<R> getFactory​(Function<R,​String[]> itemTextualizer,
                                                       Charset charset,
                                                       char delimiter,
                                                       String headerBlock)
        Parameters:
        itemTextualizer - The logic that transforms an item into the corresponding CSV columns. Note that this function is responsible for handling leading/trailing spaces, since they are passed to output unchanged.
        charset - The character encoding to use when writing to the output stream
        delimiter - The CSV delimiter character to use
        headerBlock - A text to be written to the output, once, at the beginning. Use '\n' to create multiple lines. Use null to add no header block.
        Returns:
        An encoder factory producing CSV encoders based on the given settings
      • getFactory

        public static <R> EncoderFactory<R> getFactory​(Function<R,​String[]> itemTextualizer)
        Parameters:
        itemTextualizer - The logic that transforms an item into the corresponding CSV columns. Note that this function is responsible for handling leading/trailing spaces, since they are passed to output unchanged.
        Returns:
        An encoder factory producing CSV encoders based on the given settings. Assumes UTF 8, comma as a CSV delimiter, and no header.