Class CSVDecoder<T>
- java.lang.Object
-
- org.pipecraft.pipes.serialization.CSVDecoder<T>
-
- All Implemented Interfaces:
Closeable,AutoCloseable,ItemDecoder<T>
public class CSVDecoder<T> extends Object implements ItemDecoder<T>
AnItemDecoderthat 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 Summary
Constructors Constructor Description CSVDecoder(InputStream is, FailableFunction<String[],T,ValidationPipeException> itemDetextualizer)Constructor Assumes UTF8, comma as a delimiter, no header rows and default read options (no compression)CSVDecoder(InputStream is, FailableFunction<String[],T,ValidationPipeException> itemDetextualizer, int rowsToSkip)Constructor Assumes UTF8, comma as a delimiter and default read options (no compression)CSVDecoder(InputStream is, FailableFunction<String[],T,ValidationPipeException> itemDetextualizer, Charset charset, char delimiter, int rowsToSkip, FileReadOptions readOptions)Constructor
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()Tdecode()Reads and decodes the next object from the input stream.static <R> DecoderFactory<R>getFactory(FailableFunction<String[],R,ValidationPipeException> itemDetextualizer)static <R> DecoderFactory<R>getFactory(FailableFunction<String[],R,ValidationPipeException> itemDetextualizer, Charset charset, char delimiter, int rowsToSkip)
-
-
-
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 streamdelimiter- The CSV delimiter character to userowsToSkip- 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
-
close
public void close() throws IOException- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Specified by:
closein interfaceItemDecoder<T>- Throws:
IOException
-
decode
public T decode() throws IOException, ValidationPipeException
Description copied from interface:ItemDecoderReads and decodes the next object from the input stream.- Specified by:
decodein interfaceItemDecoder<T>- Returns:
- The decoded item from the input stream provided on construction, or null if end of stream was reached
- Throws:
IOException- In case of a read errorValidationPipeException- In case that the data for decoding is illegal
-
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 streamdelimiter- The CSV delimiter character to userowsToSkip- 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.
-
-