4040import java .util .Objects ;
4141import java .util .Set ;
4242
43- import org .apache .commons .codec .binary .Base64OutputStream ;
44- import org .apache .commons .io .IOUtils ;
45- import org .apache .commons .io .function .Uncheck ;
46- import org .apache .commons .io .output .AppendableOutputStream ;
4743
4844/**
4945 * Specifies the format of a CSV file for parsing and writing.
@@ -1642,19 +1638,7 @@ private void escape(final char c, final Appendable appendable) throws IOExceptio
16421638 * @param values the values to format
16431639 * @return the formatted values
16441640 */
1645- public String format (final Object ... values ) {
1646- return Uncheck .get (() -> format_ (values ));
1647- }
16481641
1649- private String format_ (final Object ... values ) throws IOException {
1650- final StringWriter out = new StringWriter ();
1651- try (CSVPrinter csvPrinter = new CSVPrinter (out , this )) {
1652- csvPrinter .printRecord (values );
1653- final String res = out .toString ();
1654- final int len = recordSeparator != null ? res .length () - recordSeparator .length () : res .length ();
1655- return res .substring (0 , len );
1656- }
1657- }
16581642
16591643 /**
16601644 * Gets whether duplicate names are allowed in the headers.
@@ -2050,9 +2034,6 @@ public CSVParser parse(final Reader reader) throws IOException {
20502034 * @return a printer to an output.
20512035 * @throws IOException thrown if the optional header cannot be printed.
20522036 */
2053- public CSVPrinter print (final Appendable out ) throws IOException {
2054- return new CSVPrinter (out , this );
2055- }
20562037
20572038 /**
20582039 * Prints to the specified {@code File} with given {@code Charset}.
@@ -2067,31 +2048,7 @@ public CSVPrinter print(final Appendable out) throws IOException {
20672048 * @throws IOException thrown if the optional header cannot be printed.
20682049 * @since 1.5
20692050 */
2070- @ SuppressWarnings ("resource" )
2071- public CSVPrinter print (final File out , final Charset charset ) throws IOException {
2072- // The writer will be closed when close() is called.
2073- return new CSVPrinter (new OutputStreamWriter (new FileOutputStream (out ), charset ), this );
2074- }
20752051
2076- private void print (final InputStream inputStream , final Appendable out , final boolean newRecord ) throws IOException {
2077- // InputStream is never null here
2078- // There is nothing to escape when quoting is used which is the default.
2079- if (!newRecord ) {
2080- append (getDelimiterString (), out );
2081- }
2082- final boolean quoteCharacterSet = isQuoteCharacterSet ();
2083- if (quoteCharacterSet ) {
2084- append (getQuoteCharacter ().charValue (), out );
2085- }
2086- // Stream the input to the output without reading or holding the whole value in memory.
2087- // AppendableOutputStream cannot "close" an Appendable.
2088- try (OutputStream outputStream = new Base64OutputStream (new AppendableOutputStream <>(out ))) {
2089- IOUtils .copy (inputStream , outputStream );
2090- }
2091- if (quoteCharacterSet ) {
2092- append (getQuoteCharacter ().charValue (), out );
2093- }
2094- }
20952052
20962053 /**
20972054 * Prints the {@code value} as the next value on the line to {@code out}. The value will be escaped or encapsulated as needed. Useful when one wants to
@@ -2103,51 +2060,7 @@ private void print(final InputStream inputStream, final Appendable out, final bo
21032060 * @throws IOException If an I/O error occurs.
21042061 * @since 1.4
21052062 */
2106- public synchronized void print (final Object value , final Appendable out , final boolean newRecord ) throws IOException {
2107- // null values are considered empty
2108- // Only call CharSequence.toString() if you have to, helps GC-free use cases.
2109- CharSequence charSequence ;
2110- if (value == null ) {
2111- // https://issues.apache.org/jira/browse/CSV-203
2112- if (null == nullString ) {
2113- charSequence = Constants .EMPTY ;
2114- } else if (QuoteMode .ALL == quoteMode ) {
2115- charSequence = quotedNullString ;
2116- } else {
2117- charSequence = nullString ;
2118- }
2119- } else if (value instanceof CharSequence ) {
2120- charSequence = (CharSequence ) value ;
2121- } else if (value instanceof Reader ) {
2122- print ((Reader ) value , out , newRecord );
2123- return ;
2124- } else if (value instanceof InputStream ) {
2125- print ((InputStream ) value , out , newRecord );
2126- return ;
2127- } else {
2128- charSequence = value .toString ();
2129- }
2130- charSequence = getTrim () ? trim (charSequence ) : charSequence ;
2131- print (value , charSequence , out , newRecord );
2132- }
21332063
2134- private synchronized void print (final Object object , final CharSequence value , final Appendable out , final boolean newRecord ) throws IOException {
2135- final int offset = 0 ;
2136- final int len = value .length ();
2137- if (!newRecord ) {
2138- out .append (getDelimiterString ());
2139- }
2140- if (object == null ) {
2141- out .append (value );
2142- } else if (isQuoteCharacterSet ()) {
2143- // The original object is needed so can check for Number
2144- printWithQuotes (object , value , out , newRecord );
2145- } else if (isEscapeCharacterSet ()) {
2146- printWithEscapes (value , out );
2147- } else {
2148- out .append (value , offset , len );
2149- }
2150- }
21512064
21522065 /**
21532066 * Prints to the specified {@code Path} with given {@code Charset},
@@ -2163,26 +2076,7 @@ private synchronized void print(final Object object, final CharSequence value, f
21632076 * @throws IOException thrown if the optional header cannot be printed.
21642077 * @since 1.5
21652078 */
2166- @ SuppressWarnings ("resource" )
2167- public CSVPrinter print (final Path out , final Charset charset ) throws IOException {
2168- return print (Files .newBufferedWriter (out , charset ));
2169- }
21702079
2171- private void print (final Reader reader , final Appendable out , final boolean newRecord ) throws IOException {
2172- // Reader is never null here
2173- if (!newRecord ) {
2174- append (getDelimiterString (), out );
2175- }
2176- if (isQuoteCharacterSet ()) {
2177- printWithQuotes (reader , out );
2178- } else if (isEscapeCharacterSet ()) {
2179- printWithEscapes (reader , out );
2180- } else if (out instanceof Writer ) {
2181- IOUtils .copyLarge (reader , (Writer ) out );
2182- } else {
2183- IOUtils .copy (reader , out );
2184- }
2185- }
21862080
21872081 /**
21882082 * Prints to the {@link System#out}.
@@ -2195,9 +2089,6 @@ private void print(final Reader reader, final Appendable out, final boolean newR
21952089 * @throws IOException thrown if the optional header cannot be printed.
21962090 * @since 1.5
21972091 */
2198- public CSVPrinter printer () throws IOException {
2199- return new CSVPrinter (System .out , this );
2200- }
22012092
22022093 /**
22032094 * Outputs the trailing delimiter (if set) followed by the record separator (if set).
@@ -2228,12 +2119,6 @@ public synchronized void println(final Appendable appendable) throws IOException
22282119 * @throws IOException If an I/O error occurs.
22292120 * @since 1.4
22302121 */
2231- public synchronized void printRecord (final Appendable appendable , final Object ... values ) throws IOException {
2232- for (int i = 0 ; i < values .length ; i ++) {
2233- print (values [i ], appendable , i == 0 );
2234- }
2235- println (appendable );
2236- }
22372122
22382123 /*
22392124 * Note: Must only be called if escaping is enabled, otherwise can throw exceptions.
0 commit comments