Class CSVOutput

java.lang.Object
com.invirgance.convirgance.output.CSVOutput
All Implemented Interfaces:
Output

@Wiring public class CSVOutput extends Object implements Output
Writes data in RFC 4180 compliant CSV format. String data will be quoted with double quotes, quotes in data will be escaped by two double quotes, and newlines are allowed in quoted data. Lines are always terminated with CRLF as recommended by the specification. This class provides functionality to:
  • Write data with optional column headers
  • Handles string data with proper quote escaping
  • Supports configurable character encoding
Author:
tadghh
See Also:
  • Constructor Details

    • CSVOutput

      public CSVOutput()
      Creates a new CSVOutput instance without predefined headers. Headers will be automatically generated from the first record's keys if not set explicitly.
    • CSVOutput

      public CSVOutput(String... headers)
      Creates a new CSVOutput with the provided headers.
      Parameters:
      headers - The columns to use when writing out CSV data
    • CSVOutput

      public CSVOutput(String[] headers, String encoding)
      Creates a new CSVOutput with the provided headers.
      Parameters:
      headers - The columns to use when writing out CSV data
      encoding - The character encoding to use for the output
  • Method Details

    • setEncoding

      public void setEncoding(String encoding)
      Sets the character encoding for the output stream.
      Parameters:
      encoding - The character encoding to use (e.g., "UTF-8", "ISO-8859-1"). Must be a valid character encoding supported by the JVM.
    • getEncoding

      public String getEncoding()
      Returns the current character encoding used for the output stream.
      Returns:
      The current character encoding (defaults to "UTF-8")
    • setHeaders

      public void setHeaders(String... columns)
      Set the headers to use when writing out CSV values
      Parameters:
      columns - The column headers.
    • getHeaders

      public String[] getHeaders()
      Returns the current headers that will be used when writing CSV data. If headers were not explicitly set, they will be generated from the keys found in the first record when writing begins.
      Returns:
      The headers used for CSV output.
    • getContentType

      public String getContentType()
      Returns the text/csv MIME type
      Specified by:
      getContentType in interface Output
      Returns:
      the MIME type
    • write

      public OutputCursor write(Target target)
      Creates a new writer to output CSV data to the specified target.
      Specified by:
      write in interface Output
      Parameters:
      target - The target writeable output stream
      Returns:
      An OutputCursor instance for writing CSV records