Class CsvEncoder
- java.lang.Object
-
- com.fasterxml.jackson.dataformat.csv.impl.CsvEncoder
-
public class CsvEncoder extends java.lang.Object
Helper class that handles actual low-level construction of CSV output, based only on indexes given without worrying about reordering, or binding from logical properties.
-
-
Field Summary
Fields Modifier and Type Field Description protected BufferedValue[]
_buffered
And if output comes in shuffled order we will need to do bit of ordering.protected boolean
_bufferRecyclable
Flag that indicates whether the_outputBuffer
is recyclable (and needs to be returned to recycler once we are done) or not.protected boolean
_cfgAllowsComments
protected boolean
_cfgAlwaysQuoteEmptyStrings
protected boolean
_cfgAlwaysQuoteNumbers
protected boolean
_cfgAlwaysQuoteStrings
protected char
_cfgColumnSeparator
protected char
_cfgControlCharEscapeChar
protected int
_cfgEscapeCharacter
protected boolean
_cfgEscapeControlCharWithEscapeChar
protected boolean
_cfgEscapeQuoteCharWithEscapeChar
protected boolean
_cfgIncludeMissingTail
protected char[]
_cfgLineSeparator
protected int
_cfgLineSeparatorLength
protected int
_cfgMaxQuoteCheckChars
protected int
_cfgMinSafeChar
Lowest-valued character that is safe to output without using quotes around value, NOT including possible escape character.protected char[]
_cfgNullValue
protected boolean
_cfgOptimalQuoting
Marker flag used to determine if to do optimal (aka "strict") quoting checks or not (looser conservative check)protected int
_cfgQuoteCharacter
protected char
_cfgQuoteCharEscapeChar
protected boolean
_cfgUseFastDoubleWriter
protected int
_charsWritten
Let's keep track of how many bytes have been output, may prove useful when debugging.protected int
_columnCount
protected int
_csvFeatures
protected com.fasterxml.jackson.core.io.IOContext
_ioContext
protected int
_lastBuffered
Index of the last buffered valueprotected int
_nextColumnToWrite
Index of column we expect to write nextprotected java.io.Writer
_out
UnderlyingWriter
used for output.protected char[]
_outputBuffer
Intermediate buffer in which contents are buffered before being written using_out
.protected int
_outputEnd
Offset to index after the last valid index in_outputBuffer
.protected int[]
_outputEscapes
Currently active set of output escape code definitions (whether and how to escape or not).protected int
_outputTail
Pointer to the next available char position in_outputBuffer
protected boolean
_trailingLFRemoved
private static char[]
FALSE_CHARS
protected static char[]
HEX_CHARS
protected static int
MAX_QUOTE_CHECK
Also: only do check for optional quotes for short values; longer ones will always be quoted.protected BufferedValue[]
NO_BUFFERED
protected static int
SHORT_WRITE
As an optimization we try coalescing short writes into buffer; but pass longer directly.private static int[]
sOutputEscapes
private static char[]
TRUE_CHARS
-
Constructor Summary
Constructors Constructor Description CsvEncoder(com.fasterxml.jackson.core.io.IOContext ctxt, int csvFeatures, java.io.Writer out, CsvSchema schema)
Deprecated.CsvEncoder(com.fasterxml.jackson.core.io.IOContext ctxt, int csvFeatures, java.io.Writer out, CsvSchema schema, boolean useFastDoubleWriter)
CsvEncoder(CsvEncoder base, CsvSchema newSchema)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void
_append(char[] ch)
private void
_appendCharacterEscape(char ch, int escCode)
Method called to append escape sequence for given character, at the end of standard output buffer; or if not possible, write out directly.protected void
_buffer(int index, BufferedValue v)
private int
_calcSafeChar()
protected void
_flushBuffer()
private char
_getQuoteCharEscapeChar(boolean escapeQuoteCharWithEscapeChar, int quoteCharacter, int escapeCharacter)
protected boolean
_mayNeedQuotes(java.lang.String value, int length, int columnIndex)
Helper method that determines whether given String is likely to require quoting; check tries to optimize for speed.protected boolean
_needsQuotingLoose(java.lang.String value)
protected boolean
_needsQuotingLoose(java.lang.String value, int esc)
protected boolean
_needsQuotingStrict(java.lang.String value)
protected boolean
_needsQuotingStrict(java.lang.String value, int esc)
void
_releaseBuffers()
private void
_removeTrailingLF()
private void
_writeLongQuoted(java.lang.String text, char q)
private void
_writeLongQuotedAndEscaped(java.lang.String text, char esc)
void
_writeQuoted(java.lang.String text)
protected void
_writeQuoted(java.lang.String text, char q, int i)
void
_writeQuotedAndEscaped(java.lang.String text, char esc)
protected void
_writeQuotedAndEscaped(java.lang.String text, char q, char esc, int i)
protected void
appendColumnSeparator()
protected void
appendNull()
protected void
appendNumberValue(java.lang.String numStr)
protected void
appendRawValue(java.lang.String value)
protected void
appendValue(boolean value)
protected void
appendValue(double value)
protected void
appendValue(float value)
protected void
appendValue(int value)
protected void
appendValue(long value)
protected void
appendValue(java.lang.String value)
void
close(boolean autoClose, boolean flushStream)
void
endRow()
void
flush(boolean flushStream)
int
getOutputBuffered()
NOTE: while value does indeed indicate amount that has been written in the buffer, there may be more intermediate data that is buffered as values but not yet in buffer.java.lang.Object
getOutputTarget()
int
nextColumnIndex()
CsvEncoder
overrideFormatFeatures(int feat)
CsvEncoder
setOutputEscapes(int[] esc)
CsvEncoder
withSchema(CsvSchema schema)
void
write(int columnIndex, boolean value)
void
write(int columnIndex, char[] ch, int offset, int len)
void
write(int columnIndex, double value)
void
write(int columnIndex, float value)
void
write(int columnIndex, int value)
void
write(int columnIndex, long value)
void
write(int columnIndex, java.lang.String value)
void
write(int columnIndex, java.math.BigDecimal value, boolean plain)
void
write(int columnIndex, java.math.BigInteger value)
void
writeColumnName(java.lang.String name)
void
writeNonEscaped(int columnIndex, java.lang.String rawValue)
void
writeNull(int columnIndex)
private void
writeNumber(java.lang.String text)
void
writeRaw(char c)
void
writeRaw(char[] text, int offset, int len)
void
writeRaw(java.lang.String text)
void
writeRaw(java.lang.String text, int start, int len)
private void
writeRawLong(java.lang.String text)
-
-
-
Field Detail
-
sOutputEscapes
private static final int[] sOutputEscapes
-
HEX_CHARS
protected static final char[] HEX_CHARS
-
SHORT_WRITE
protected static final int SHORT_WRITE
As an optimization we try coalescing short writes into buffer; but pass longer directly.- See Also:
- Constant Field Values
-
MAX_QUOTE_CHECK
protected static final int MAX_QUOTE_CHECK
Also: only do check for optional quotes for short values; longer ones will always be quoted.- See Also:
- Constant Field Values
-
NO_BUFFERED
protected final BufferedValue[] NO_BUFFERED
-
TRUE_CHARS
private static final char[] TRUE_CHARS
-
FALSE_CHARS
private static final char[] FALSE_CHARS
-
_outputEscapes
protected int[] _outputEscapes
Currently active set of output escape code definitions (whether and how to escape or not).
-
_ioContext
protected final com.fasterxml.jackson.core.io.IOContext _ioContext
-
_out
protected final java.io.Writer _out
UnderlyingWriter
used for output.
-
_cfgColumnSeparator
protected final char _cfgColumnSeparator
-
_cfgQuoteCharacter
protected final int _cfgQuoteCharacter
-
_cfgEscapeCharacter
protected final int _cfgEscapeCharacter
- Since:
- 2.7
-
_cfgLineSeparator
protected final char[] _cfgLineSeparator
-
_cfgNullValue
protected final char[] _cfgNullValue
- Since:
- 2.5
-
_cfgLineSeparatorLength
protected final int _cfgLineSeparatorLength
-
_cfgMaxQuoteCheckChars
protected final int _cfgMaxQuoteCheckChars
-
_cfgMinSafeChar
protected final int _cfgMinSafeChar
Lowest-valued character that is safe to output without using quotes around value, NOT including possible escape character.
-
_csvFeatures
protected int _csvFeatures
-
_cfgOptimalQuoting
protected boolean _cfgOptimalQuoting
Marker flag used to determine if to do optimal (aka "strict") quoting checks or not (looser conservative check)- Since:
- 2.4
-
_cfgAllowsComments
protected final boolean _cfgAllowsComments
-
_cfgIncludeMissingTail
protected boolean _cfgIncludeMissingTail
- Since:
- 2.4
-
_cfgAlwaysQuoteStrings
protected boolean _cfgAlwaysQuoteStrings
- Since:
- 2.5
-
_cfgAlwaysQuoteEmptyStrings
protected boolean _cfgAlwaysQuoteEmptyStrings
-
_cfgAlwaysQuoteNumbers
protected boolean _cfgAlwaysQuoteNumbers
-
_cfgEscapeQuoteCharWithEscapeChar
protected boolean _cfgEscapeQuoteCharWithEscapeChar
-
_cfgEscapeControlCharWithEscapeChar
protected boolean _cfgEscapeControlCharWithEscapeChar
- Since:
- 2.9.9
-
_cfgUseFastDoubleWriter
protected boolean _cfgUseFastDoubleWriter
- Since:
- 2.14
-
_cfgQuoteCharEscapeChar
protected final char _cfgQuoteCharEscapeChar
-
_cfgControlCharEscapeChar
protected final char _cfgControlCharEscapeChar
- Since:
- 2.9.9
-
_columnCount
protected int _columnCount
- Since:
- 2.4
-
_nextColumnToWrite
protected int _nextColumnToWrite
Index of column we expect to write next
-
_buffered
protected BufferedValue[] _buffered
And if output comes in shuffled order we will need to do bit of ordering.
-
_lastBuffered
protected int _lastBuffered
Index of the last buffered value
-
_trailingLFRemoved
protected boolean _trailingLFRemoved
-
_outputBuffer
protected char[] _outputBuffer
Intermediate buffer in which contents are buffered before being written using_out
.
-
_bufferRecyclable
protected boolean _bufferRecyclable
Flag that indicates whether the_outputBuffer
is recyclable (and needs to be returned to recycler once we are done) or not.
-
_outputTail
protected int _outputTail
Pointer to the next available char position in_outputBuffer
-
_outputEnd
protected final int _outputEnd
Offset to index after the last valid index in_outputBuffer
. Typically same as length of the buffer.
-
_charsWritten
protected int _charsWritten
Let's keep track of how many bytes have been output, may prove useful when debugging. This does not include bytes buffered in the output buffer, just bytes that have been written using underlying stream writer.
-
-
Constructor Detail
-
CsvEncoder
@Deprecated public CsvEncoder(com.fasterxml.jackson.core.io.IOContext ctxt, int csvFeatures, java.io.Writer out, CsvSchema schema)
Deprecated.
-
CsvEncoder
public CsvEncoder(com.fasterxml.jackson.core.io.IOContext ctxt, int csvFeatures, java.io.Writer out, CsvSchema schema, boolean useFastDoubleWriter)
-
CsvEncoder
public CsvEncoder(CsvEncoder base, CsvSchema newSchema)
-
-
Method Detail
-
_getQuoteCharEscapeChar
private final char _getQuoteCharEscapeChar(boolean escapeQuoteCharWithEscapeChar, int quoteCharacter, int escapeCharacter)
-
_calcSafeChar
private final int _calcSafeChar()
-
withSchema
public CsvEncoder withSchema(CsvSchema schema)
-
overrideFormatFeatures
public CsvEncoder overrideFormatFeatures(int feat)
-
setOutputEscapes
public CsvEncoder setOutputEscapes(int[] esc)
-
getOutputTarget
public java.lang.Object getOutputTarget()
-
getOutputBuffered
public int getOutputBuffered()
NOTE: while value does indeed indicate amount that has been written in the buffer, there may be more intermediate data that is buffered as values but not yet in buffer.
-
nextColumnIndex
public int nextColumnIndex()
-
write
public final void write(int columnIndex, java.lang.String value) throws java.io.IOException
- Throws:
java.io.IOException
-
write
public final void write(int columnIndex, char[] ch, int offset, int len) throws java.io.IOException
- Throws:
java.io.IOException
-
write
public final void write(int columnIndex, int value) throws java.io.IOException
- Throws:
java.io.IOException
-
write
public final void write(int columnIndex, long value) throws java.io.IOException
- Throws:
java.io.IOException
-
write
public final void write(int columnIndex, java.math.BigInteger value) throws java.io.IOException
- Throws:
java.io.IOException
-
write
public final void write(int columnIndex, float value) throws java.io.IOException
- Throws:
java.io.IOException
-
write
public final void write(int columnIndex, double value) throws java.io.IOException
- Throws:
java.io.IOException
-
write
public final void write(int columnIndex, java.math.BigDecimal value, boolean plain) throws java.io.IOException
- Throws:
java.io.IOException
-
write
public final void write(int columnIndex, boolean value) throws java.io.IOException
- Throws:
java.io.IOException
-
writeNonEscaped
public final void writeNonEscaped(int columnIndex, java.lang.String rawValue) throws java.io.IOException
- Throws:
java.io.IOException
- Since:
- 2.5
-
writeNull
public final void writeNull(int columnIndex) throws java.io.IOException
- Throws:
java.io.IOException
-
writeColumnName
public final void writeColumnName(java.lang.String name) throws java.io.IOException
- Throws:
java.io.IOException
-
endRow
public void endRow() throws java.io.IOException
- Throws:
java.io.IOException
-
appendValue
protected void appendValue(java.lang.String value) throws java.io.IOException
- Throws:
java.io.IOException
-
appendRawValue
protected void appendRawValue(java.lang.String value) throws java.io.IOException
- Throws:
java.io.IOException
-
appendValue
protected void appendValue(int value) throws java.io.IOException
- Throws:
java.io.IOException
-
appendValue
protected void appendValue(long value) throws java.io.IOException
- Throws:
java.io.IOException
-
appendValue
protected void appendValue(float value) throws java.io.IOException
- Throws:
java.io.IOException
-
appendValue
protected void appendValue(double value) throws java.io.IOException
- Throws:
java.io.IOException
-
appendNumberValue
protected void appendNumberValue(java.lang.String numStr) throws java.io.IOException
- Throws:
java.io.IOException
-
appendValue
protected void appendValue(boolean value) throws java.io.IOException
- Throws:
java.io.IOException
-
appendNull
protected void appendNull() throws java.io.IOException
- Throws:
java.io.IOException
-
_append
protected void _append(char[] ch) throws java.io.IOException
- Throws:
java.io.IOException
-
appendColumnSeparator
protected void appendColumnSeparator() throws java.io.IOException
- Throws:
java.io.IOException
-
writeRaw
public void writeRaw(java.lang.String text) throws java.io.IOException
- Throws:
java.io.IOException
-
writeRaw
public void writeRaw(java.lang.String text, int start, int len) throws java.io.IOException
- Throws:
java.io.IOException
-
writeRaw
public void writeRaw(char[] text, int offset, int len) throws java.io.IOException
- Throws:
java.io.IOException
-
writeRaw
public void writeRaw(char c) throws java.io.IOException
- Throws:
java.io.IOException
-
writeRawLong
private void writeRawLong(java.lang.String text) throws java.io.IOException
- Throws:
java.io.IOException
-
writeNumber
private void writeNumber(java.lang.String text) throws java.io.IOException
- Throws:
java.io.IOException
-
_writeQuoted
public void _writeQuoted(java.lang.String text) throws java.io.IOException
- Throws:
java.io.IOException
-
_writeQuoted
protected void _writeQuoted(java.lang.String text, char q, int i) throws java.io.IOException
- Throws:
java.io.IOException
-
_writeLongQuoted
private final void _writeLongQuoted(java.lang.String text, char q) throws java.io.IOException
- Throws:
java.io.IOException
-
_writeQuotedAndEscaped
public void _writeQuotedAndEscaped(java.lang.String text, char esc) throws java.io.IOException
- Throws:
java.io.IOException
-
_writeQuotedAndEscaped
protected void _writeQuotedAndEscaped(java.lang.String text, char q, char esc, int i) throws java.io.IOException
- Throws:
java.io.IOException
-
_writeLongQuotedAndEscaped
private final void _writeLongQuotedAndEscaped(java.lang.String text, char esc) throws java.io.IOException
- Throws:
java.io.IOException
-
flush
public void flush(boolean flushStream) throws java.io.IOException
- Throws:
java.io.IOException
-
close
public void close(boolean autoClose, boolean flushStream) throws java.io.IOException
- Throws:
java.io.IOException
-
_removeTrailingLF
private void _removeTrailingLF() throws java.io.IOException
- Throws:
java.io.IOException
-
_mayNeedQuotes
protected boolean _mayNeedQuotes(java.lang.String value, int length, int columnIndex)
Helper method that determines whether given String is likely to require quoting; check tries to optimize for speed.
-
_needsQuotingLoose
protected final boolean _needsQuotingLoose(java.lang.String value)
- Since:
- 2.4
-
_needsQuotingLoose
protected final boolean _needsQuotingLoose(java.lang.String value, int esc)
-
_needsQuotingStrict
protected boolean _needsQuotingStrict(java.lang.String value)
- Since:
- 2.4
-
_needsQuotingStrict
protected boolean _needsQuotingStrict(java.lang.String value, int esc)
- Since:
- 2.7
-
_buffer
protected void _buffer(int index, BufferedValue v)
-
_flushBuffer
protected void _flushBuffer() throws java.io.IOException
- Throws:
java.io.IOException
-
_releaseBuffers
public void _releaseBuffers()
-
_appendCharacterEscape
private void _appendCharacterEscape(char ch, int escCode) throws java.io.IOException
Method called to append escape sequence for given character, at the end of standard output buffer; or if not possible, write out directly.- Throws:
java.io.IOException
-
-