Skip to content
This repository was archived by the owner on Jan 22, 2019. It is now read-only.

Commit 5190aaf

Browse files
committed
minor refactoring wrt #40
1 parent 6b77212 commit 5190aaf

File tree

2 files changed

+25
-13
lines changed

2 files changed

+25
-13
lines changed

src/main/java/com/fasterxml/jackson/dataformat/csv/CsvGenerator.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,18 @@ private Feature(boolean defaultState) {
110110
public CsvGenerator(IOContext ctxt, int jsonFeatures, int csvFeatures,
111111
ObjectCodec codec, Writer out,
112112
char columnSeparator, char quoteChar, char[] linefeed)
113+
{
114+
this(ctxt, jsonFeatures, csvFeatures, codec,
115+
new CsvWriter(ctxt, out, columnSeparator, quoteChar, linefeed));
116+
}
117+
118+
public CsvGenerator(IOContext ctxt, int jsonFeatures, int csvFeatures,
119+
ObjectCodec codec, CsvWriter csvWriter)
113120
{
114121
super(jsonFeatures, codec);
115122
_ioContext = ctxt;
116123
_csvFeatures = csvFeatures;
117-
_writer = new CsvWriter(ctxt, out, columnSeparator, quoteChar, linefeed);
124+
_writer = csvWriter;
118125
}
119126

120127
/*

src/main/java/com/fasterxml/jackson/dataformat/csv/impl/CsvWriter.java

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* Low-level helper class that handles actual output of CSV, purely
1313
* based on indexes given without worrying about reordering etc.
1414
*/
15-
public final class CsvWriter
15+
public class CsvWriter
1616
{
1717
/* As an optimization we try coalescing short writes into
1818
* buffer; but pass longer directly.
@@ -50,6 +50,8 @@ public final class CsvWriter
5050

5151
final protected int _cfgLineSeparatorLength;
5252

53+
protected int _cfgMaxQuoteCheckChars;
54+
5355
/**
5456
* Lowest-valued character that is safe to output without using
5557
* quotes around value
@@ -136,6 +138,8 @@ public CsvWriter(IOContext ctxt, Writer out,
136138
_cfgLineSeparatorLength = linefeed.length;
137139

138140
_cfgMinSafeChar = _calcSafeChar();
141+
142+
_cfgMaxQuoteCheckChars = MAX_QUOTE_CHECK;
139143
}
140144

141145
public CsvWriter(CsvWriter base, CsvSchema newSchema)
@@ -145,6 +149,7 @@ public CsvWriter(CsvWriter base, CsvSchema newSchema)
145149
_bufferRecyclable = base._bufferRecyclable;
146150
_outputEnd = base._outputEnd;
147151
_out = base._out;
152+
_cfgMaxQuoteCheckChars = base._cfgMaxQuoteCheckChars;
148153

149154
_cfgColumnSeparator = newSchema.getColumnSeparator();
150155
_cfgQuoteCharacter = newSchema.getQuoteChar();
@@ -187,7 +192,7 @@ public int nextColumnIndex() {
187192
/**********************************************************
188193
*/
189194

190-
public void write(int columnIndex, String value) throws IOException
195+
public final void write(int columnIndex, String value) throws IOException
191196
{
192197
// easy case: all in order
193198
if (columnIndex == _nextColumnToWrite) {
@@ -198,13 +203,13 @@ public void write(int columnIndex, String value) throws IOException
198203
_buffer(columnIndex, BufferedValue.buffered(value));
199204
}
200205

201-
public void write(int columnIndex, char[] ch, int offset, int len) throws IOException
206+
public final void write(int columnIndex, char[] ch, int offset, int len) throws IOException
202207
{
203208
// !!! TODO: optimize
204209
write(columnIndex, new String(ch, offset, len));
205210
}
206211

207-
public void write(int columnIndex, int value) throws IOException
212+
public final void write(int columnIndex, int value) throws IOException
208213
{
209214
// easy case: all in order
210215
if (columnIndex == _nextColumnToWrite) {
@@ -215,7 +220,7 @@ public void write(int columnIndex, int value) throws IOException
215220
_buffer(columnIndex, BufferedValue.buffered(value));
216221
}
217222

218-
public void write(int columnIndex, long value) throws IOException
223+
public final void write(int columnIndex, long value) throws IOException
219224
{
220225
// easy case: all in order
221226
if (columnIndex == _nextColumnToWrite) {
@@ -226,7 +231,7 @@ public void write(int columnIndex, long value) throws IOException
226231
_buffer(columnIndex, BufferedValue.buffered(value));
227232
}
228233

229-
public void write(int columnIndex, float value) throws IOException
234+
public final void write(int columnIndex, float value) throws IOException
230235
{
231236
// easy case: all in order
232237
if (columnIndex == _nextColumnToWrite) {
@@ -237,7 +242,7 @@ public void write(int columnIndex, float value) throws IOException
237242
_buffer(columnIndex, BufferedValue.buffered(value));
238243
}
239244

240-
public void write(int columnIndex, double value) throws IOException
245+
public final void write(int columnIndex, double value) throws IOException
241246
{
242247
// easy case: all in order
243248
if (columnIndex == _nextColumnToWrite) {
@@ -249,7 +254,7 @@ public void write(int columnIndex, double value) throws IOException
249254
}
250255

251256

252-
public void write(int columnIndex, boolean value) throws IOException
257+
public final void write(int columnIndex, boolean value) throws IOException
253258
{
254259
// easy case: all in order
255260
if (columnIndex == _nextColumnToWrite) {
@@ -260,7 +265,7 @@ public void write(int columnIndex, boolean value) throws IOException
260265
_buffer(columnIndex, BufferedValue.buffered(value));
261266
}
262267

263-
public void writeColumnName(String name) throws IOException
268+
public final void writeColumnName(String name) throws IOException
264269
{
265270
appendValue(name);
266271
++_nextColumnToWrite;
@@ -572,14 +577,14 @@ public void close(boolean autoClose) throws IOException
572577
* Helper method that determines whether given String is likely
573578
* to require quoting; check tries to optimize for speed.
574579
*/
575-
protected final boolean _mayNeedQuotes(String value, int length)
580+
protected boolean _mayNeedQuotes(String value, int length)
576581
{
577582
// 21-Mar-2014, tatu: If quoting disabled, don't quote
578583
if (_cfgQuoteCharacter < 0) {
579584
return false;
580585
}
581586
// let's not bother checking long Strings, just quote already:
582-
if (length > MAX_QUOTE_CHECK) {
587+
if (length > _cfgMaxQuoteCheckChars) {
583588
return true;
584589
}
585590
for (int i = 0; i < length; ++i) {
@@ -599,7 +604,7 @@ protected void _buffer(int index, BufferedValue v)
599604
_buffered[index] = v;
600605
}
601606

602-
protected final void _flushBuffer() throws IOException
607+
protected void _flushBuffer() throws IOException
603608
{
604609
if (_outputTail > 0) {
605610
_charsWritten += _outputTail;

0 commit comments

Comments
 (0)