Class CSVSerializer

  • All Implemented Interfaces:
    Serializer<List<?>>

    public class CSVSerializer
    extends java.lang.Object
    implements Serializer<List<?>>
    Implementation of the Serializer interface that reads data from and writes data to a comma-separated value (CSV) file.
    • Constructor Summary

      Constructors 
      Constructor Description
      CSVSerializer()  
      CSVSerializer​(java.lang.reflect.Type itemType)  
      CSVSerializer​(java.nio.charset.Charset charset)  
      CSVSerializer​(java.nio.charset.Charset charset, java.lang.reflect.Type itemType)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.nio.charset.Charset getCharset()
      Returns the character set used to encode/decode the CSV data.
      ListenerList<CSVSerializerListener> getCSVSerializerListeners()  
      java.lang.reflect.Type getItemType()
      Returns the type of the item that will be instantiated by the serializer during a read operation.
      Sequence<java.lang.String> getKeys()
      Returns the keys that will be read or written by this serializer.
      java.lang.String getMIMEType​(List<?> objects)
      Returns the MIME type of the data read and written by this serializer.
      boolean getWriteKeys()
      Returns the serializer's write keys flag.
      boolean isVerbose()
      Returns the serializer's verbosity flag.
      List<?> readObject​(java.io.InputStream inputStream)
      Reads values from a comma-separated value stream.
      List<?> readObject​(java.io.Reader reader)
      Reads values from a comma-separated value stream.
      void setKeys​(java.lang.String... keys)
      Sets the keys that will be read or written by this serializer.
      void setKeys​(Sequence<java.lang.String> keys)
      Sets the keys that will be read or written by this serializer.
      void setVerbose​(boolean verbose)
      Sets the serializer's verbosity flag.
      void setWriteKeys​(boolean writeKeys)
      Sets the serializer's write keys flag.
      void writeObject​(List<?> items, java.io.OutputStream outputStream)
      Writes values to a comma-separated value stream.
      void writeObject​(List<?> items, java.io.Writer writer)
      Writes values to a comma-separated value stream.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • DEFAULT_CHARSET

        public static final java.nio.charset.Charset DEFAULT_CHARSET
      • DEFAULT_ITEM_TYPE

        public static final java.lang.reflect.Type DEFAULT_ITEM_TYPE
    • Constructor Detail

      • CSVSerializer

        public CSVSerializer()
      • CSVSerializer

        public CSVSerializer​(java.nio.charset.Charset charset)
      • CSVSerializer

        public CSVSerializer​(java.lang.reflect.Type itemType)
      • CSVSerializer

        public CSVSerializer​(java.nio.charset.Charset charset,
                             java.lang.reflect.Type itemType)
    • Method Detail

      • getCharset

        public java.nio.charset.Charset getCharset()
        Returns the character set used to encode/decode the CSV data.
        Returns:
        The current character set.
      • getItemType

        public java.lang.reflect.Type getItemType()
        Returns the type of the item that will be instantiated by the serializer during a read operation.
        Returns:
        The type of the item to be returned by the serializer.
      • getKeys

        public Sequence<java.lang.String> getKeys()
        Returns the keys that will be read or written by this serializer.
        Returns:
        The sequence of read/write keys.
      • setKeys

        public void setKeys​(Sequence<java.lang.String> keys)
        Sets the keys that will be read or written by this serializer.
        Parameters:
        keys - The keys to be read/written.
        Throws:
        java.lang.IllegalArgumentException - for null input.
      • setKeys

        public void setKeys​(java.lang.String... keys)
        Sets the keys that will be read or written by this serializer.
        Parameters:
        keys - The list of keys to be read/written.
        Throws:
        java.lang.IllegalArgumentException - for null input.
      • getWriteKeys

        public boolean getWriteKeys()
        Returns the serializer's write keys flag.
        Returns:
        true if keys will be written, false otherwise.
        See Also:
        setWriteKeys(boolean)
      • setWriteKeys

        public void setWriteKeys​(boolean writeKeys)
        Sets the serializer's write keys flag.
        Parameters:
        writeKeys - If true, the first line of the output will contain the keys. Otherwise, the first line will contain the first line of data.
      • isVerbose

        public boolean isVerbose()
        Returns the serializer's verbosity flag.
        Returns:
        true if the serializer is echoing input, false if not.
      • setVerbose

        public void setVerbose​(boolean verbose)
        Sets the serializer's verbosity flag. When verbosity is enabled, all data read or written will be echoed to the console.
        Parameters:
        verbose - Whether or not to echo the input.
      • readObject

        public List<?> readObject​(java.io.InputStream inputStream)
                           throws java.io.IOException,
                                  SerializationException
        Reads values from a comma-separated value stream.
        Specified by:
        readObject in interface Serializer<List<?>>
        Parameters:
        inputStream - The input stream from which data will be read.
        Returns:
        The list of values read from the stream.
        Throws:
        java.io.IOException - for any errors during reading.
        SerializationException - for any formatting errors with the data.
        java.lang.IllegalArgumentException - for null input stream.
        See Also:
        readObject(Reader)
      • readObject

        public List<?> readObject​(java.io.Reader reader)
                           throws java.io.IOException,
                                  SerializationException
        Reads values from a comma-separated value stream.
        Parameters:
        reader - The reader from which data will be read.
        Returns:
        A list containing the data read from the CSV file. The list items are instances of Dictionary<String, Object> populated by mapping columns in the CSV file to keys in the key sequence.

        If no keys have been specified when this method is called, they are assumed to be defined in the first line of the file.

        Throws:
        java.io.IOException - for any errors during reading.
        SerializationException - for any formatting errors with the data.
        java.lang.IllegalArgumentException - for null input reader.
      • writeObject

        public void writeObject​(List<?> items,
                                java.io.OutputStream outputStream)
                         throws java.io.IOException,
                                SerializationException
        Writes values to a comma-separated value stream.
        Specified by:
        writeObject in interface Serializer<List<?>>
        Parameters:
        items - The list of items to write.
        outputStream - The output stream to which data will be written.
        Throws:
        java.io.IOException - for any errors during writing.
        SerializationException - for any formatting errors with the data.
        java.lang.IllegalArgumentException - for null input arguments.
        See Also:
        writeObject(List, Writer)
      • writeObject

        public void writeObject​(List<?> items,
                                java.io.Writer writer)
                         throws java.io.IOException
        Writes values to a comma-separated value stream.
        Parameters:
        items - A list containing the data to write to the CSV file. List items must be instances of Dictionary<String, Object>. The dictionary values will be written out in the order specified by the key sequence.
        writer - The writer to which data will be written.
        Throws:
        java.io.IOException - for any errors during writing.
        java.lang.IllegalArgumentException - for null input arguments.
      • getMIMEType

        public java.lang.String getMIMEType​(List<?> objects)
        Description copied from interface: Serializer
        Returns the MIME type of the data read and written by this serializer.
        Specified by:
        getMIMEType in interface Serializer<List<?>>
        Parameters:
        objects - If provided, allows the serializer to attach parameters to the returned MIME type containing more detailed information about the data. If null, the base MIME type is returned.
        Returns:
        The MIME type of the current data.