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
11
12
13
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
24
25
26
27
28 protected AbstractCSVWriter(Writer writer, String lineEnd) {
29 this.writer = writer;
30 this.lineEnd = lineEnd;
31 }
32
33
34
35
36
37
38 protected final Writer getWriter() {
39 return writer;
40 }
41
42
43
44
45
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
66
67
68
69
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
103
104
105
106
107
108
109
110
111
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) {
134 flushQuietly();
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
162
163
164
165 protected ResultSetHelper resultService() {
166 if (resultService == null) {
167 resultService = new ResultSetHelperService();
168 }
169 return resultService;
170 }
171 }