View Javadoc
1   package com.opencsv;
2   
3   import java.io.IOException;
4   import java.io.PrintWriter;
5   import java.io.Writer;
6   import java.sql.ResultSet;
7   import java.sql.SQLException;
8   
9   /**
10   * The AbstractCSVWriter was created to prevent duplication of code between the CSVWriter and the
11   * CSVParserWriter classes.
12   *
13   * @since 4.2
14   */
15  public abstract class AbstractCSVWriter implements ICSVWriter {
16  
17      private final Writer writer;
18      private final String lineEnd;
19      private ResultSetHelper resultService;
20      protected volatile IOException exception;
21  
22      /**
23       * Constructor to initialize the common values.
24       *
25       * @param writer  Writer used for output of csv data.
26       * @param lineEnd String to append at end of data (either "\n" or "\r\n").
27       */
28      protected AbstractCSVWriter(Writer writer, String lineEnd) {
29          this.writer = writer;
30          this.lineEnd = lineEnd;
31      }
32  
33      /**
34       * Provides access to the writer used for outputting CSV data.
35       *
36       * @return The {@link Writer} instance used for writing CSV content.
37       */
38      protected final Writer getWriter() {
39          return writer;
40      }
41  
42      /**
43       * Retrieves the line ending sequence used when writing CSV data.
44       *
45       * @return The line ending sequence, either "\n" or "\r\n".
46       */
47      protected final String getLineEnd() {
48          return lineEnd;
49      }
50  
51      @Override
52      public void writeAll(Iterable<String[]> allLines, boolean applyQuotesToAll) {
53          StringBuilder sb = new StringBuilder(INITIAL_STRING_SIZE);
54          try {
55              for (String[] line : allLines) {
56                  writeNext(line, applyQuotesToAll, sb);
57                  sb.setLength(0);
58              }
59          } catch (IOException e) {
60              exception = e;
61          }
62      }
63  
64      /**
65       * Writes the column names.
66       *
67       * @param rs               ResultSet containing column names.
68       * @param applyQuotesToAll Whether all header names should be quoted.
69       * @throws SQLException Thrown by {@link ResultSetHelper#getColumnNames(java.sql.ResultSet)}
70       */
71      protected void writeColumnNames(ResultSet rs, boolean applyQuotesToAll) throws SQLException {
72          writeNext(resultService().getColumnNames(rs), applyQuotesToAll);
73      }
74  
75      @Override
76      public int writeAll(ResultSet rs, boolean includeColumnNames, boolean trim, boolean applyQuotesToAll) throws SQLException, IOException {
77          int linesWritten = 0;
78  
79          if (includeColumnNames) {
80              writeColumnNames(rs, applyQuotesToAll);
81              linesWritten++;
82          }
83  
84          while (rs.next()) {
85              writeNext(resultService().getColumnValues(rs, trim), applyQuotesToAll);
86              linesWritten++;
87          }
88  
89          return linesWritten;
90      }
91  
92      @Override
93      public void writeNext(String[] nextLine, boolean applyQuotesToAll) {
94          try {
95              writeNext(nextLine, applyQuotesToAll, new StringBuilder(INITIAL_STRING_SIZE));
96          } catch (IOException e) {
97              exception = e;
98          }
99      }
100 
101     /**
102      * Writes the next line to the file.  This method is a fail-fast method that will throw the
103      * IOException of the writer supplied to the CSVWriter (if the Writer does not handle the exceptions itself like
104      * the PrintWriter class).
105      *
106      * @param nextLine         a string array with each comma-separated element as a separate
107      *                         entry.
108      * @param applyQuotesToAll true if all values are to be quoted.  false applies quotes only
109      *                         to values which contain the separator, escape, quote or new line characters.
110      * @param appendable       Appendable used as buffer.
111      * @throws IOException Exceptions thrown by the writer supplied to CSVWriter.
112      */
113     protected abstract void writeNext(String[] nextLine, boolean applyQuotesToAll, Appendable appendable) throws IOException;
114 
115     @Override
116     public void flush() throws IOException {
117         writer.flush();
118     }
119 
120     @Override
121     public void close() throws IOException {
122         flush();
123         writer.close();
124     }
125 
126     @Override
127     public boolean checkError() {
128 
129         if (writer instanceof PrintWriter) {
130             PrintWriter pw = (PrintWriter) writer;
131             return pw.checkError();
132         }
133         if (exception != null) {  // we don't want to lose the original exception
134             flushQuietly();  // checkError in the PrintWriter class flushes the buffer so we shall too.
135         } else {
136             try {
137                 flush();
138             } catch (IOException ioe) {
139                 exception = ioe;
140             }
141         }
142         return exception != null;
143     }
144 
145     @Override
146     public IOException getException() {
147         return exception;
148     }
149 
150     @Override
151     public void resetError() {
152         exception = null;
153     }
154 
155     @Override
156     public void setResultService(ResultSetHelper resultService) {
157         this.resultService = resultService;
158     }
159 
160     /**
161      * Lazy resultSetHelper creation.
162      *
163      * @return Instance of resultSetHelper
164      */
165     protected ResultSetHelper resultService() {
166         if (resultService == null) {
167             resultService = new ResultSetHelperService();
168         }
169         return resultService;
170     }
171 }