1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.orangesignal.csv;
18
19 import java.io.Closeable;
20 import java.io.File;
21 import java.io.FileInputStream;
22 import java.io.FileOutputStream;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.io.InputStreamReader;
26 import java.io.OutputStream;
27 import java.io.OutputStreamWriter;
28 import java.io.Reader;
29 import java.io.Writer;
30 import java.util.ArrayList;
31 import java.util.Enumeration;
32 import java.util.List;
33 import java.util.zip.ZipEntry;
34 import java.util.zip.ZipFile;
35 import java.util.zip.ZipInputStream;
36 import java.util.zip.ZipOutputStream;
37
38 import com.orangesignal.jlha.LhaFile;
39 import com.orangesignal.jlha.LhaHeader;
40 import com.orangesignal.jlha.LhaInputStream;
41 import com.orangesignal.jlha.LhaOutputStream;
42
43
44
45
46
47
48 public abstract class Csv {
49
50
51
52
53 protected Csv() {
54 }
55
56
57
58
59
60
61
62
63
64
65
66
67
68 public static <T> T load(final CsvReader reader, final CsvHandler<T> handler) throws IOException {
69 return handler.load(reader);
70 }
71
72
73
74
75
76
77
78
79
80
81
82 public static <T> T load(final Reader reader, final CsvConfig cfg, final CsvHandler<T> handler) throws IOException {
83 return load(new CsvReader(reader, cfg), handler);
84 }
85
86
87
88
89
90
91
92
93
94
95
96
97 public static <T> T load(final InputStream in, final String encoding, final CsvConfig cfg, final CsvHandler<T> handler) throws IOException {
98 return load(new InputStreamReader(in, encoding), cfg, handler);
99 }
100
101
102
103
104
105
106
107
108
109
110
111 public static <T> T load(final InputStream in, final CsvConfig cfg, final CsvHandler<T> handler) throws IOException {
112 return load(new InputStreamReader(in), cfg, handler);
113 }
114
115
116
117
118
119
120
121
122
123
124
125
126 public static <T> T load(final File file, final String encoding, final CsvConfig cfg, final CsvHandler<T> handler) throws IOException {
127 final InputStream in = new FileInputStream(file);
128 try {
129 return load(in, encoding, cfg, handler);
130 } finally {
131 closeQuietly(in);
132 }
133 }
134
135
136
137
138
139
140
141
142
143
144
145 public static <T> T load(final File file, final CsvConfig cfg, final CsvHandler<T> handler) throws IOException {
146 final InputStream in = new FileInputStream(file);
147 try {
148 return load(in, cfg, handler);
149 } finally {
150 closeQuietly(in);
151 }
152 }
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170 public static <T> List<T> load(final LhaInputStream in, final String encoding, final CsvConfig cfg, final CsvListHandler<T> handler, final LhaEntryFilter filter) throws IOException {
171 final List<T> list = new ArrayList<T>();
172 LhaHeader entry;
173 while ((entry = in.getNextEntry()) != null) {
174 try {
175 if (filter != null && !filter.accept(entry)) {
176 continue;
177 }
178 list.addAll(handler.load(new CsvReader(new InputStreamReader(in, encoding), cfg), true));
179 } finally {
180 in.closeEntry();
181 }
182 }
183 return handler.processScalar(list);
184 }
185
186
187
188
189
190
191
192
193
194
195
196
197
198 public static <T> List<T> load(final LhaInputStream in, final String encoding, final CsvConfig cfg, final CsvListHandler<T> handler) throws IOException {
199 return load(in, encoding, cfg, handler, null);
200 }
201
202
203
204
205
206
207
208
209
210
211
212
213 public static <T> List<T> load(final LhaInputStream in, final CsvConfig cfg, final CsvListHandler<T> handler, final LhaEntryFilter filter) throws IOException {
214 final List<T> list = new ArrayList<T>();
215 LhaHeader entry;
216 while ((entry = in.getNextEntry()) != null) {
217 try {
218 if (filter != null && !filter.accept(entry)) {
219 continue;
220 }
221 list.addAll(handler.load(new CsvReader(new InputStreamReader(in), cfg), true));
222 } finally {
223 in.closeEntry();
224 }
225 }
226 return handler.processScalar(list);
227 }
228
229
230
231
232
233
234
235
236
237
238
239 public static <T> List<T> load(final LhaInputStream in, final CsvConfig cfg, final CsvListHandler<T> handler) throws IOException {
240 return load(in, cfg, handler, null);
241 }
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256 public static <T> List<T> load(final LhaFile lhaFile, final String encoding, final CsvConfig cfg, final CsvListHandler<T> handler, final LhaEntryFilter filter) throws IOException {
257 final List<T> list = new ArrayList<T>();
258 final LhaHeader[] entries = lhaFile.getEntries();
259 for (final LhaHeader entry : entries) {
260 if (filter != null && !filter.accept(entry)) {
261 continue;
262 }
263 list.addAll(handler.load(new CsvReader(new InputStreamReader(lhaFile.getInputStream(entry), encoding), cfg), true));
264 }
265 return handler.processScalar(list);
266 }
267
268
269
270
271
272
273
274
275
276
277
278
279
280 public static <T> List<T> load(final LhaFile lhaFile, final String encoding, final CsvConfig cfg, final CsvListHandler<T> handler) throws IOException {
281 return load(lhaFile, encoding, cfg, handler, null);
282 }
283
284
285
286
287
288
289
290
291
292
293
294
295 public static <T> List<T> load(final LhaFile lhaFile, final CsvConfig cfg, final CsvListHandler<T> handler, final LhaEntryFilter filter) throws IOException {
296 final List<T> list = new ArrayList<T>();
297 final LhaHeader[] entries = lhaFile.getEntries();
298 for (final LhaHeader entry : entries) {
299 if (filter != null && !filter.accept(entry)) {
300 continue;
301 }
302 list.addAll(handler.load(new CsvReader(new InputStreamReader(lhaFile.getInputStream(entry)), cfg), true));
303 }
304 return handler.processScalar(list);
305 }
306
307
308
309
310
311
312
313
314
315
316
317 public static <T> List<T> load(final LhaFile lhaFile, final CsvConfig cfg, final CsvListHandler<T> handler) throws IOException {
318 return load(lhaFile, cfg, handler, null);
319 }
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334 public static <T> List<T> load(final ZipInputStream in, final String encoding, final CsvConfig cfg, final CsvListHandler<T> handler, final ZipEntryFilter filter) throws IOException {
335 final List<T> list = new ArrayList<T>();
336 ZipEntry entry;
337 while ((entry = in.getNextEntry()) != null) {
338 try {
339 if (filter != null && !filter.accept(entry)) {
340 continue;
341 }
342 list.addAll(handler.load(new CsvReader(new InputStreamReader(in, encoding), cfg), true));
343 } finally {
344 in.closeEntry();
345 }
346 }
347 return handler.processScalar(list);
348 }
349
350
351
352
353
354
355
356
357
358
359
360
361
362 public static <T> List<T> load(final ZipInputStream in, final String encoding, final CsvConfig cfg, final CsvListHandler<T> handler) throws IOException {
363 return load(in, encoding, cfg, handler, null);
364 }
365
366
367
368
369
370
371
372
373
374
375
376
377 public static <T> List<T> load(final ZipInputStream in, final CsvConfig cfg, final CsvListHandler<T> handler, final ZipEntryFilter filter) throws IOException {
378 final List<T> list = new ArrayList<T>();
379 ZipEntry entry;
380 while ((entry = in.getNextEntry()) != null) {
381 try {
382 if (filter != null && !filter.accept(entry)) {
383 continue;
384 }
385 list.addAll(handler.load(new CsvReader(new InputStreamReader(in), cfg), true));
386 } finally {
387 in.closeEntry();
388 }
389 }
390 return handler.processScalar(list);
391 }
392
393
394
395
396
397
398
399
400
401
402
403 public static <T> List<T> load(final ZipInputStream in, final CsvConfig cfg, final CsvListHandler<T> handler) throws IOException {
404 return load(in, cfg, handler, null);
405 }
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420 public static <T> List<T> load(final ZipFile zipFile, final String encoding, final CsvConfig cfg, final CsvListHandler<T> handler, final ZipEntryFilter filter) throws IOException {
421 final List<T> list = new ArrayList<T>();
422 final Enumeration<? extends ZipEntry> entries = zipFile.entries();
423 while (entries.hasMoreElements()) {
424 final ZipEntry entry = entries.nextElement();
425 if (filter != null && !filter.accept(entry)) {
426 continue;
427 }
428 final InputStream in = zipFile.getInputStream(entry);
429 try {
430 list.addAll(handler.load(new CsvReader(new InputStreamReader(in, encoding), cfg), true));
431 } finally {
432 closeQuietly(in);
433 }
434 }
435 return handler.processScalar(list);
436 }
437
438
439
440
441
442
443
444
445
446
447
448
449
450 public static <T> List<T> load(final ZipFile zipFile, final String encoding, final CsvConfig cfg, final CsvListHandler<T> handler) throws IOException {
451 return load(zipFile, encoding, cfg, handler, null);
452 }
453
454
455
456
457
458
459
460
461
462
463
464
465 public static <T> List<T> load(final ZipFile zipFile, final CsvConfig cfg, final CsvListHandler<T> handler, final ZipEntryFilter filter) throws IOException {
466 final List<T> list = new ArrayList<T>();
467 final Enumeration<? extends ZipEntry> entries = zipFile.entries();
468 while (entries.hasMoreElements()) {
469 final ZipEntry entry = entries.nextElement();
470 if (filter != null && !filter.accept(entry)) {
471 continue;
472 }
473 final InputStream in = zipFile.getInputStream(entry);
474 try {
475 list.addAll(handler.load(new CsvReader(new InputStreamReader(in), cfg), true));
476 } finally {
477 closeQuietly(in);
478 }
479 }
480 return handler.processScalar(list);
481 }
482
483
484
485
486
487
488
489
490
491
492
493 public static <T> List<T> load(final ZipFile zipFile, final CsvConfig cfg, final CsvListHandler<T> handler) throws IOException {
494 return load(zipFile, cfg, handler, null);
495 }
496
497
498
499
500
501
502
503
504
505
506
507
508
509 public static <T> void save(final T obj, final CsvWriter writer, final CsvHandler<T> handler) throws IOException {
510 handler.save(obj, writer);
511 writer.flush();
512 }
513
514
515
516
517
518
519
520
521
522
523
524 public static <T> void save(final T obj, final Writer writer, final CsvConfig cfg, final CsvHandler<T> handler) throws IOException {
525 save(obj, new CsvWriter(writer, cfg), handler);
526 }
527
528
529
530
531
532
533
534
535
536
537
538
539 public static <T> void save(final T obj, final OutputStream out, final String encoding, final CsvConfig cfg, final CsvHandler<T> handler) throws IOException {
540 save(obj, new OutputStreamWriter(out, encoding), cfg, handler);
541 }
542
543
544
545
546
547
548
549
550
551
552
553 public static <T> void save(final T obj, final OutputStream out, final CsvConfig cfg, final CsvHandler<T> handler) throws IOException {
554 save(obj, new OutputStreamWriter(out), cfg, handler);
555 }
556
557
558
559
560
561
562
563
564
565
566
567
568 public static <T> void save(final T obj, final File file, final String encoding, final CsvConfig cfg, final CsvHandler<T> handler) throws IOException {
569 final OutputStream out = new FileOutputStream(file);
570 try {
571 save(obj, out, encoding, cfg, handler);
572 } finally {
573 closeQuietly(out);
574 }
575 }
576
577
578
579
580
581
582
583
584
585
586
587 public static <T> void save(final T obj, final File file, final CsvConfig cfg, final CsvHandler<T> handler) throws IOException {
588 final OutputStream out = new FileOutputStream(file);
589 try {
590 save(obj, out, cfg, handler);
591 } finally {
592 closeQuietly(out);
593 }
594 }
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612 public static <T> void save(final List<T> obj, final LhaOutputStream out, final String encoding, final CsvConfig cfg, final CsvListHandler<T> handler, final String entryName) throws IOException {
613 out.putNextEntry(new LhaHeader(entryName));
614 try {
615 save(obj, out, encoding, cfg, handler);
616 } finally {
617 out.closeEntry();
618 }
619 }
620
621
622
623
624
625
626
627
628
629
630
631
632 public static <T> void save(final List<T> obj, final LhaOutputStream out, final CsvConfig cfg, final CsvListHandler<T> handler, final String entryName) throws IOException {
633 out.putNextEntry(new LhaHeader(entryName));
634 try {
635 save(obj, out, cfg, handler);
636 } finally {
637 out.closeEntry();
638 }
639 }
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654 public static <T> void save(final List<T> obj, final ZipOutputStream out, final String encoding, final CsvConfig cfg, final CsvListHandler<T> handler, final String entryName) throws IOException {
655 out.putNextEntry(new ZipEntry(entryName));
656 try {
657 save(obj, out, encoding, cfg, handler);
658 } finally {
659 out.closeEntry();
660 }
661 }
662
663
664
665
666
667
668
669
670
671
672
673
674 public static <T> void save(final List<T> obj, final ZipOutputStream out, final CsvConfig cfg, final CsvListHandler<T> handler, final String entryName) throws IOException {
675 out.putNextEntry(new ZipEntry(entryName));
676 try {
677 save(obj, out, cfg, handler);
678 } finally {
679 out.closeEntry();
680 }
681 }
682
683
684
685
686
687
688
689
690
691 protected static void closeQuietly(final Closeable closeable) {
692 try {
693 if (closeable != null) {
694 closeable.close();
695 }
696 } catch (final IOException e) {
697
698 }
699 }
700
701 }