@@ -68,15 +68,10 @@ public CSVPrinter(final Appendable out, final CSVFormat format) {
6868 // printing implementation
6969 // ======================================================
7070
71- /**
72- * Outputs the record separator.
73- *
74- * @throws IOException
75- * If an I/O error occurs
76- */
77- public void println () throws IOException {
78- out .append (format .getRecordSeparator ());
79- newRecord = true ;
71+ public void close () throws IOException {
72+ if (out instanceof Closeable ) {
73+ ((Closeable ) out ).close ();
74+ }
8075 }
8176
8277 /**
@@ -92,77 +87,23 @@ public void flush() throws IOException {
9287 }
9388
9489 /**
95- * Prints a single line of delimiter separated values. The values will be quoted if needed. Quotes and newLine
96- * characters will be escaped.
97- *
98- * @param values
99- * values to output.
100- * @throws IOException
101- * If an I/O error occurs
102- */
103- public void printRecord (final Object ... values ) throws IOException {
104- for (final Object value : values ) {
105- print (value );
106- }
107- println ();
108- }
109-
110- /**
111- * Prints a single line of delimiter separated values. The values will be quoted if needed. Quotes and newLine
112- * characters will be escaped.
113- *
114- * @param values
115- * values to output.
116- * @throws IOException
117- * If an I/O error occurs
118- */
119- public void printRecord (final Iterable <?> values ) throws IOException {
120- for (final Object value : values ) {
121- print (value );
122- }
123- println ();
124- }
125-
126- /**
127- * Prints a comment on a new line among the delimiter separated values. Comments will always begin on a new line
128- * and occupy a least one full line. The character specified to start comments and a space will be inserted at the
129- * beginning of each new line in the comment.
130- * <p/>
131- * If comments are disabled in the current CSV format this method does nothing.
90+ * Prints the string as the next value on the line. The value will be escaped or encapsulated as needed.
13291 *
133- * @param comment
134- * the comment to output
92+ * @param value
93+ * value to be output.
13594 * @throws IOException
13695 * If an I/O error occurs
13796 */
138- public void printComment (final String comment ) throws IOException {
139- if (!format .isCommentingEnabled ()) {
140- return ;
141- }
142- if (!newRecord ) {
143- println ();
144- }
145- out .append (format .getCommentStart ().charValue ());
146- out .append (SP );
147- for (int i = 0 ; i < comment .length (); i ++) {
148- final char c = comment .charAt (i );
149- switch (c ) {
150- case CR :
151- if (i + 1 < comment .length () && comment .charAt (i + 1 ) == LF ) {
152- i ++;
153- }
154- //$FALL-THROUGH$ break intentionally excluded.
155- case LF :
156- println ();
157- out .append (format .getCommentStart ().charValue ());
158- out .append (SP );
159- break ;
160- default :
161- out .append (c );
162- break ;
163- }
97+ public void print (final Object value ) throws IOException {
98+ // null values are considered empty
99+ String strValue ;
100+ if (value == null ) {
101+ final String nullString = format .getNullString ();
102+ strValue = nullString == null ? Constants .EMPTY : nullString ;
103+ } else {
104+ strValue = value .toString ();
164105 }
165- println ( );
106+ this . print ( value , strValue , 0 , strValue . length () );
166107 }
167108
168109 private void print (final Object object , final CharSequence value ,
@@ -332,34 +273,99 @@ private void printAndQuote(final Object object, final CharSequence value,
332273 }
333274
334275 /**
335- * Prints the string as the next value on the line. The value will be escaped or encapsulated as needed.
276+ * Prints a comment on a new line among the delimiter separated values. Comments will always begin on a new line
277+ * and occupy a least one full line. The character specified to start comments and a space will be inserted at the
278+ * beginning of each new line in the comment.
279+ * <p/>
280+ * If comments are disabled in the current CSV format this method does nothing.
336281 *
337- * @param value
338- * value to be output.
282+ * @param comment
283+ * the comment to output
339284 * @throws IOException
340285 * If an I/O error occurs
341286 */
342- public void print (final Object value ) throws IOException {
343- // null values are considered empty
344- String strValue ;
345- if (value == null ) {
346- final String nullString = format .getNullString ();
347- strValue = nullString == null ? Constants .EMPTY : nullString ;
348- } else {
349- strValue = value .toString ();
287+ public void printComment (final String comment ) throws IOException {
288+ if (!format .isCommentingEnabled ()) {
289+ return ;
350290 }
351- this .print (value , strValue , 0 , strValue .length ());
291+ if (!newRecord ) {
292+ println ();
293+ }
294+ out .append (format .getCommentStart ().charValue ());
295+ out .append (SP );
296+ for (int i = 0 ; i < comment .length (); i ++) {
297+ final char c = comment .charAt (i );
298+ switch (c ) {
299+ case CR :
300+ if (i + 1 < comment .length () && comment .charAt (i + 1 ) == LF ) {
301+ i ++;
302+ }
303+ //$FALL-THROUGH$ break intentionally excluded.
304+ case LF :
305+ println ();
306+ out .append (format .getCommentStart ().charValue ());
307+ out .append (SP );
308+ break ;
309+ default :
310+ out .append (c );
311+ break ;
312+ }
313+ }
314+ println ();
352315 }
353316
354317 /**
355- * Prints all the objects in the given array.
318+ * Outputs the record separator.
319+ *
320+ * @throws IOException
321+ * If an I/O error occurs
322+ */
323+ public void println () throws IOException {
324+ out .append (format .getRecordSeparator ());
325+ newRecord = true ;
326+ }
327+
328+ /**
329+ * Prints a single line of delimiter separated values. The values will be quoted if needed. Quotes and newLine
330+ * characters will be escaped.
331+ *
332+ * @param values
333+ * values to output.
334+ * @throws IOException
335+ * If an I/O error occurs
336+ */
337+ public void printRecord (final Iterable <?> values ) throws IOException {
338+ for (final Object value : values ) {
339+ print (value );
340+ }
341+ println ();
342+ }
343+
344+ /**
345+ * Prints a single line of delimiter separated values. The values will be quoted if needed. Quotes and newLine
346+ * characters will be escaped.
347+ *
348+ * @param values
349+ * values to output.
350+ * @throws IOException
351+ * If an I/O error occurs
352+ */
353+ public void printRecord (final Object ... values ) throws IOException {
354+ for (final Object value : values ) {
355+ print (value );
356+ }
357+ println ();
358+ }
359+
360+ /**
361+ * Prints all the objects in the given collection.
356362 *
357363 * @param values
358364 * the values to print.
359365 * @throws IOException
360366 * If an I/O error occurs
361367 */
362- public void printRecords (final Object [] values ) throws IOException {
368+ public void printRecords (final Iterable <?> values ) throws IOException {
363369 for (final Object value : values ) {
364370 if (value instanceof Object []) {
365371 this .printRecord ((Object []) value );
@@ -372,14 +378,14 @@ public void printRecords(final Object[] values) throws IOException {
372378 }
373379
374380 /**
375- * Prints all the objects in the given collection .
381+ * Prints all the objects in the given array .
376382 *
377383 * @param values
378384 * the values to print.
379385 * @throws IOException
380386 * If an I/O error occurs
381387 */
382- public void printRecords (final Iterable <?> values ) throws IOException {
388+ public void printRecords (final Object [] values ) throws IOException {
383389 for (final Object value : values ) {
384390 if (value instanceof Object []) {
385391 this .printRecord ((Object []) value );
@@ -410,9 +416,12 @@ public void printRecords(final ResultSet resultSet) throws SQLException, IOExcep
410416 }
411417 }
412418
413- public void close () throws IOException {
414- if (out instanceof Closeable ) {
415- ((Closeable ) out ).close ();
416- }
419+ /**
420+ * Gets the target Appendable.
421+ *
422+ * @return the target Appendable.
423+ */
424+ public Appendable getOut () {
425+ return this .out ;
417426 }
418427}
0 commit comments