1 package au.com.bytecode.opencsv;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 import java.io.BufferedReader;
20 import java.io.Closeable;
21 import java.io.IOException;
22 import java.io.Reader;
23 import java.util.ArrayList;
24 import java.util.List;
25
26
27
28
29
30
31
32 public class CSVReader implements Closeable {
33
34 private BufferedReader br;
35
36 private boolean hasNext = true;
37
38 private CSVParser parser;
39
40 private int skipLines;
41
42 private boolean linesSkiped;
43
44
45
46
47 public static final int DEFAULT_SKIP_LINES = 0;
48
49
50
51
52
53
54
55 public CSVReader(Reader reader) {
56 this(reader, CSVParser.DEFAULT_SEPARATOR, CSVParser.DEFAULT_QUOTE_CHARACTER, CSVParser.DEFAULT_ESCAPE_CHARACTER);
57 }
58
59
60
61
62
63
64
65
66
67 public CSVReader(Reader reader, char separator) {
68 this(reader, separator, CSVParser.DEFAULT_QUOTE_CHARACTER, CSVParser.DEFAULT_ESCAPE_CHARACTER);
69 }
70
71
72
73
74
75
76
77
78
79
80
81 public CSVReader(Reader reader, char separator, char quotechar) {
82 this(reader, separator, quotechar, CSVParser.DEFAULT_ESCAPE_CHARACTER, DEFAULT_SKIP_LINES, CSVParser.DEFAULT_STRICT_QUOTES);
83 }
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98 public CSVReader(Reader reader, char separator, char quotechar, boolean strictQuotes) {
99 this(reader, separator, quotechar, CSVParser.DEFAULT_ESCAPE_CHARACTER, DEFAULT_SKIP_LINES, strictQuotes);
100 }
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115 public CSVReader(Reader reader, char separator,
116 char quotechar, char escape) {
117 this(reader, separator, quotechar, escape, DEFAULT_SKIP_LINES, CSVParser.DEFAULT_STRICT_QUOTES);
118 }
119
120
121
122
123
124
125
126
127
128
129
130
131
132 public CSVReader(Reader reader, char separator, char quotechar, int line) {
133 this(reader, separator, quotechar, CSVParser.DEFAULT_ESCAPE_CHARACTER, line, CSVParser.DEFAULT_STRICT_QUOTES);
134 }
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150 public CSVReader(Reader reader, char separator, char quotechar, char escape, int line) {
151 this(reader, separator, quotechar, escape, line, CSVParser.DEFAULT_STRICT_QUOTES);
152 }
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170 public CSVReader(Reader reader, char separator, char quotechar, char escape, int line, boolean strictQuotes) {
171 this.br = new BufferedReader(reader);
172 this.parser = new CSVParser(separator, quotechar, escape, strictQuotes);
173 this.skipLines = line;
174 }
175
176
177
178
179
180
181
182
183
184
185
186
187 public List<String[]> readAll() throws IOException {
188
189 List<String[]> allElements = new ArrayList<String[]>();
190 while (hasNext) {
191 String[] nextLineAsTokens = readNext();
192 if (nextLineAsTokens != null)
193 allElements.add(nextLineAsTokens);
194 }
195 return allElements;
196
197 }
198
199
200
201
202
203
204
205
206
207
208 public String[] readNext() throws IOException {
209
210 String[] result = null;
211 do {
212 String nextLine = getNextLine();
213 if (!hasNext) {
214 return result;
215 }
216 String[] r = parser.parseLineMulti(nextLine);
217 if (r.length > 0) {
218 if (result == null) {
219 result = r;
220 } else {
221 String[] t = new String[result.length+r.length];
222 System.arraycopy(result, 0, t, 0, result.length);
223 System.arraycopy(r, 0, t, result.length, r.length);
224 result = t;
225 }
226 }
227 } while (parser.isPending());
228 return result;
229 }
230
231
232
233
234
235
236
237
238 private String getNextLine() throws IOException {
239 if (!this.linesSkiped) {
240 for (int i = 0; i < skipLines; i++) {
241 br.readLine();
242 }
243 this.linesSkiped = true;
244 }
245 String nextLine = br.readLine();
246 if (nextLine == null) {
247 hasNext = false;
248 }
249 return hasNext ? nextLine : null;
250 }
251
252
253
254
255
256
257 public void close() throws IOException{
258 br.close();
259 }
260
261 }