Class CSVEncoder<T>
- java.lang.Object
-
- org.pipecraft.pipes.serialization.CSVEncoder<T>
-
- All Implemented Interfaces:
Closeable,AutoCloseable,ItemEncoder<T>
public class CSVEncoder<T> extends Object implements ItemEncoder<T>
AnItemEncoderthat 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 Summary
Constructors Constructor Description CSVEncoder(OutputStream os, Function<T,String[]> itemTextualizer)Constructor Assumes UTF8, comma as a delimiter and default write options (no compression)CSVEncoder(OutputStream os, Function<T,String[]> itemTextualizer, Charset charset, char delimiter, String headerBlock, FileWriteOptions writeOptions)Constructor
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()voidencode(T item)static <R> EncoderFactory<R>getFactory(Function<R,String[]> itemTextualizer)static <R> EncoderFactory<R>getFactory(Function<R,String[]> itemTextualizer, Charset charset, char delimiter, String headerBlock)
-
-
-
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 streamdelimiter- The CSV delimiter character to useheaderBlock- 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
-
close
public void close() throws IOException- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Throws:
IOException
-
encode
public void encode(T item) throws IOException
- Specified by:
encodein interfaceItemEncoder<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 streamdelimiter- The CSV delimiter character to useheaderBlock- 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.
-
-