-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGigaCSV.java
More file actions
1098 lines (1031 loc) · 35.9 KB
/
GigaCSV.java
File metadata and controls
1098 lines (1031 loc) · 35.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// Copyright (c) 2024 - Ardika Rommy Sanjaya
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
package gigacsv;
/**
* GigaCSV is a tools for reading/writing large CSV file efficiently.
*
* @see <a href="https://github.com/ardikars/GigaCSV">GigaCSV</a>
*/
public final class GigaCSV {
private static final boolean PRINT_STACK_TRACE =
Boolean.parseBoolean(System.getProperty("gigacsv.stack_trace", "true"));
private static final byte LINE_BREAK_CR = '\r';
private static final byte LINE_BREAK_LF = '\n';
private final Option option;
/**
* Create GigaCSV instance.
*
* <p>
*/
public GigaCSV() {
this(new Option());
}
/**
* Create GigaCSV instance.
*
* @param option config.
*/
public GigaCSV(final Option option) {
this.option = option;
}
/**
* Read CSV file.
*
* <p>
*
* <pre>
* final GigaCSV gigaCSV = new GigaCSV();
* try (final GigaCSV.Reader reader = gigaCSV.reader("file.csv")) {
* for (final GigaCSV.Record record : reader) {
* //
* }
* } catch (Exception e) {
* //
* }
* </pre>
*
* @param path file path.
* @return returns {@link Reader}.
* @throws java.io.FileNotFoundException file not found.
* @throws java.lang.Exception unable to close file.
*/
public final Reader reader(final String path) throws java.io.FileNotFoundException {
return new Reader(path, option);
}
/**
* Write to CSV file.
*
* <p>
*
* <pre>
* try (final GigaCSV.Writer writer = gigaCSV.writer("file.csv")) {
* writer.write(...);
* } catch(Exception e) {
* //
* }
* </pre>
*
* @param path file path.
* @return returns {@link Writer}.
* @throws java.io.FileNotFoundException file not found.
* @throws java.lang.Exception unable to close file.
*/
public final Writer writer(final String path) throws java.io.FileNotFoundException {
return new Writer(path, "rw", false, option);
}
/**
* Write to CSV file.
*
* <p>
*
* <pre>
* try (final GigaCSV.Writer writer = gigaCSV.writerWithMode("file.csv", "rw")) {
* writer.write(...);
* } catch(Exception e) {
* //
* }
* </pre>
*
* @param path file path.
* @param mode writing mode.
* @return returns {@link Writer}.
* @throws java.io.FileNotFoundException file not found.
* @throws java.lang.Exception unable to close file.
*/
public final Writer writerWithMode(final String path, final String mode)
throws java.io.FileNotFoundException {
return new Writer(path, mode, false, option);
}
/**
* Write to CSV file, all field is quoted.
*
* <p>
*
* <pre>
* try (final GigaCSV.Writer writer = gigaCSV.writerWithModeAndAlwaysQuote("file.csv", "rw")) {
* writer.write(...);
* } catch(Exception e) {
* //
* }
* </pre>
*
* @param path file path.
* @param mode writing mode.
* @return returns {@link Writer}.
* @throws java.io.FileNotFoundException file not found.
* @throws java.lang.Exception unable to close file.
*/
public final Writer writerWithModeAndAlwaysQuote(final String path, final String mode)
throws java.io.FileNotFoundException {
return new Writer(path, mode, true, option);
}
/**
* CSV writer.
*
* <p>
*/
public static final class Writer implements AutoCloseable {
private final java.io.RandomAccessFile sequentialAccessFile;
private final byte[] buffer;
private final byte quote;
private final byte separator;
private final boolean alwaysQuote;
private Writer(
final String path, final String mode, final boolean alwaysQuote, final Option option)
throws java.io.FileNotFoundException {
this.sequentialAccessFile = new java.io.RandomAccessFile(path, mode);
this.buffer = new byte[option.bufferSize];
this.alwaysQuote = alwaysQuote;
this.quote = option.quote;
this.separator = option.separator;
}
public final int write(final Iterable<Record> records) throws java.io.IOException {
int written = 0;
for (final Record record : records) {
written += write(record);
}
return written;
}
public final int write(final Record... records) throws java.io.IOException {
int written = 0;
int idx = 0;
int checkpoint = 0;
for (int i = 0; i < records.length; i++) {
f:
for (int j = 0; j < records[i].fields.length; j++) {
final byte[] bytes =
records[i].fields[j].getBytes(java.nio.charset.StandardCharsets.UTF_8);
int startIdx = idx;
boolean addQuote = false;
int c = 0;
while (c < bytes.length) {
final byte b = bytes[c];
if (((b & 0x80) == 0)) {
if (b == quote) {
if (idx + 2 == buffer.length) {
written += flush(checkpoint);
i -= 1;
idx = 0;
break f;
} else {
buffer[idx++] = quote;
buffer[idx++] = b;
c += 1;
if (!addQuote) {
addQuote = true;
}
}
} else {
if (idx + 1 == buffer.length) {
written += flush(checkpoint);
i -= 1;
idx = 0;
break f;
} else {
buffer[idx++] = b;
c += 1;
if (!addQuote && (b == separator || b == LINE_BREAK_CR || b == LINE_BREAK_LF)) {
addQuote = true;
}
}
}
} else if ((b & 0xf0) == 0xf0) { // four bytes unicode
if (idx + 4 == buffer.length) {
written += flush(checkpoint);
i -= 1;
idx = 0;
break f;
} else {
buffer[idx++] = bytes[c++];
buffer[idx++] = bytes[c++];
buffer[idx++] = bytes[c++];
buffer[idx++] = bytes[c++];
}
} else if ((b & 0xe0) == 0xe0) { // three bytes unicode
if (idx + 3 == buffer.length) {
written += flush(checkpoint);
i -= 1;
idx = 0;
break f;
} else {
buffer[idx++] = bytes[c++];
buffer[idx++] = bytes[c++];
buffer[idx++] = bytes[c++];
}
} else if ((b & 0xc0) == 0xc0) { // two bytes unicode
if (idx + 2 == buffer.length) {
written += flush(checkpoint);
i -= 1;
idx = 0;
break f;
} else {
buffer[idx++] = bytes[c++];
buffer[idx++] = bytes[c++];
}
} else {
throw new java.io.UTFDataFormatException("Invalid csv fields");
}
}
if (addQuote || alwaysQuote) {
if (idx + 2 == buffer.length) {
written += flush(checkpoint);
i -= 1;
idx = 0;
break f;
} else {
System.arraycopy(buffer, startIdx, buffer, startIdx + 1, idx - startIdx);
buffer[startIdx] = quote;
idx += 1;
buffer[idx++] = quote;
}
}
if (j < records[i].fields.length - 1) {
if (idx + 1 == buffer.length) {
written += flush(checkpoint);
i -= 1;
idx = 0;
break f;
} else {
buffer[idx++] = separator;
}
}
}
if (idx > 0) {
if (records[i].lineBreak == LineBreak.CRLF) {
if (idx + 2 == buffer.length) {
written += flush(checkpoint);
i -= 1;
idx = 0;
} else {
buffer[idx++] = LINE_BREAK_CR;
buffer[idx++] = LINE_BREAK_LF;
}
} else if (records[i].lineBreak == LineBreak.LF) {
if (idx + 1 == buffer.length) {
written += flush(checkpoint);
i -= 1;
idx = 0;
} else {
buffer[idx++] = LINE_BREAK_LF;
}
} else if (records[i].lineBreak == LineBreak.CR) {
if (idx + 1 == buffer.length) {
written += flush(checkpoint);
i -= 1;
idx = 0;
} else {
buffer[idx++] = LINE_BREAK_CR;
}
}
checkpoint = idx;
}
}
written += flush(checkpoint);
return written;
}
private final int flush(int length) throws java.io.IOException {
sequentialAccessFile.write(buffer, 0, length);
return length;
}
@Override
public final void close() throws Exception {
sequentialAccessFile.close();
}
}
/**
* Iterable CSV reader.
*
* <p>
*/
public static final class Reader implements Iterable<Record>, AutoCloseable {
private final java.io.RandomAccessFile sequentialAccessFile;
private final Option option;
private Reader(final String path, final Option option) throws java.io.FileNotFoundException {
this.sequentialAccessFile = new java.io.RandomAccessFile(path, "r");
this.option = option;
}
/**
* Get {@link Record} iterator.
*
* @return returns {@link Record} iterator.
*/
public final java.util.Iterator<Record> iterator() {
return new ReaderIterator(sequentialAccessFile, option);
}
/**
* Close the CSV file.
*
* @throws Exception error while closing CSV file.
*/
@Override
public final void close() throws Exception {
sequentialAccessFile.close();
}
}
private static final class ReaderIterator implements java.util.Iterator<Record> {
private final java.io.RandomAccessFile file;
private long number;
private int readLength;
private int offset;
private int fieldSize;
private final byte[] buffer;
private final int bufferSize;
private final int headerSize; // default header size
private final byte quote;
private final byte separator;
//
private Record next;
private ReaderIterator(
final java.io.RandomAccessFile sequentialAccessFile, final Option option) {
this.file = sequentialAccessFile;
this.number = 0;
this.readLength = 0;
this.offset = 0;
this.fieldSize = 0;
this.buffer = new byte[option.bufferSize];
this.bufferSize = option.bufferSize;
this.headerSize = option.headerSize;
;
this.quote = option.quote;
this.separator = option.separator;
try {
this.file.seek(0L);
next();
} catch (final java.io.IOException e) {
if (PRINT_STACK_TRACE) {
e.printStackTrace();
}
this.next = null;
}
}
@Override
public final boolean hasNext() {
return next != null;
}
@Override
public final Record next() {
final Record previous = next;
try {
if (offset < readLength) {
NextRecord record = nextRecord(offset, readLength);
if (record.status == 0) {
offset += record.offset + record.lineBreak.length;
this.next = toRecord(record);
this.number += 1;
} else {
if (record.status == NextRecord.BUFFER_UNDERFLOW.status) {
final int remainingLength = readLength - offset;
System.arraycopy(buffer, offset, buffer, 0, remainingLength);
final int read = file.read(buffer, remainingLength, bufferSize - remainingLength);
if (read == -1) { // end of file
readLength = remainingLength;
} else {
readLength = remainingLength + read;
}
if (readLength < 1) {
this.next = null;
return previous;
}
offset = 0;
record = nextRecord(offset, readLength);
if (record.status == 0) {
offset += record.offset + record.lineBreak.length;
this.next = toRecord(record);
this.number += 1;
} else {
throwException(record.status);
}
} else {
throwException(record.status);
}
}
} else {
readLength = file.read(buffer, 0, bufferSize);
if (readLength < 1) {
this.next = null;
return previous;
}
offset = 0;
final NextRecord record = nextRecord(offset, readLength);
if (record.status == 0) {
offset += record.offset + record.lineBreak.length;
this.next = toRecord(record);
this.number += 1;
} else {
throwException(record.status);
}
}
} catch (final java.lang.Throwable e) {
if (PRINT_STACK_TRACE) {
e.printStackTrace();
}
this.next = null;
return previous;
}
return previous;
}
@Override
public final void remove() {
throw new UnsupportedOperationException();
}
private static void throwException(final int status) throws Exception {
if (status == NextRecord.INVALID_CSV_FORMAT.status) {
throw new java.io.UTFDataFormatException("Invalid CSV format");
} else if (status == NextRecord.UNSUPPORTED_ENCODING.status) {
throw new java.io.UTFDataFormatException("Invalid CSV file");
} else if (status == NextRecord.BUFFER_OVERFLOW.status) {
throw new ArrayIndexOutOfBoundsException("Buffer overflow");
} else if (status == NextRecord.BUFFER_UNDERFLOW.status) {
throw new ArrayIndexOutOfBoundsException("Buffer too small");
} else {
throw new Exception("Unknown exception");
}
}
private final Record toRecord(final NextRecord record) {
final FieldBuf[] fieldBufs = record.fields;
final String[] fields = new String[fieldBufs.length];
for (int i = 0; i < fieldBufs.length; i++) {
final FieldBuf fieldBuf = fieldBufs[i];
final int len = fieldBuf.len;
if (len > 0) {
if (buffer[fieldBuf.start] == quote) {
// assert len > 1 && buffer[fieldBuf.end - 1] == quote;
final int newStart = fieldBuf.start + 1;
final int newEnd = fieldBuf.end - 1;
final byte[] newStr = new byte[len - 2];
int index = 0;
boolean skip = true;
for (int j = newStart; j < newEnd; j++) {
if (buffer[j] == quote) {
if (j > newStart && buffer[j - 1] == quote && skip) {
skip = false;
continue;
} else {
skip = true;
newStr[index] = buffer[j];
}
} else {
newStr[index] = buffer[j];
}
index += 1;
}
fields[i] = new String(newStr, 0, index, java.nio.charset.StandardCharsets.UTF_8);
} else {
fields[i] =
new String(
buffer, fieldBuf.start, fieldBuf.len, java.nio.charset.StandardCharsets.UTF_8);
}
} else {
fields[i] =
new String(
buffer, fieldBuf.start, fieldBuf.len, java.nio.charset.StandardCharsets.UTF_8);
}
}
return new Record(number, fields, record.lineBreak);
}
private final NextRecord nextRecord(final int offset, final int length) {
FieldBuf[] fieldBufs;
if (fieldSize == 0) {
fieldBufs = new FieldBuf[headerSize];
} else {
fieldBufs = new FieldBuf[fieldSize];
}
int fieldBufIdx = 0;
int i = offset;
int startField = offset;
State state = State.START;
while (i < length) {
final byte b = buffer[i];
if ((b & 0x80) == 0) { // single byte ASCII
switch (state) {
case START:
if (b == quote) {
startField = i;
state = State.START_QUOTE;
} else {
if (b == separator) {
if (fieldBufIdx == fieldBufs.length) {
final FieldBuf[] newCopy = new FieldBuf[fieldBufIdx << 1];
System.arraycopy(fieldBufs, 0, newCopy, 0, fieldBufs.length);
newCopy[fieldBufIdx] = new FieldBuf(startField, i);
fieldBufs = newCopy;
fieldBufIdx += 1;
} else {
fieldBufs[fieldBufIdx] = new FieldBuf(startField, i);
fieldBufIdx += 1;
}
startField = i + 1;
// state = State.START;
} else {
// state = State.START;
}
}
break;
case START_QUOTE:
if (b == quote) {
state = State.MUST_QUOTE;
} /* else if (b == separator) {
state = State.START_QUOTE;
} else {
state = State.START_QUOTE;
} */
break;
case MUST_QUOTE:
if (b == quote) {
state = State.START_QUOTE;
} else if (b == separator) {
if (fieldBufIdx == fieldBufs.length) {
final FieldBuf[] newCopy = new FieldBuf[fieldBufIdx << 1];
System.arraycopy(fieldBufs, 0, newCopy, 0, fieldBufs.length);
newCopy[fieldBufIdx] = new FieldBuf(startField, i);
fieldBufs = newCopy;
fieldBufIdx += 1;
} else {
fieldBufs[fieldBufIdx] = new FieldBuf(startField, i);
fieldBufIdx += 1;
}
startField = i + 1;
state = State.START;
} else if (b == LINE_BREAK_CR || b == LINE_BREAK_LF) {
state = State.START;
} else {
return NextRecord.INVALID_CSV_FORMAT;
}
break;
}
if (b == LINE_BREAK_CR) {
if (state == State.START_QUOTE) {
i += 1;
} else {
if (i + 1 < length) {
if (buffer[i + 1] == LINE_BREAK_LF) {
if (fieldBufIdx + 1 == fieldBufs.length) {
fieldBufs[fieldBufIdx] = new FieldBuf(startField, i);
fieldSize = fieldBufs.length;
return new NextRecord(i - offset, fieldBufs, LineBreak.CRLF);
} else {
if (fieldBufIdx == fieldBufs.length) {
final FieldBuf[] copy = new FieldBuf[fieldBufIdx + 1];
System.arraycopy(fieldBufs, 0, copy, 0, fieldBufIdx);
copy[fieldBufIdx] = new FieldBuf(startField, i);
fieldSize = copy.length;
return new NextRecord(i - offset, copy, LineBreak.CRLF);
} else {
final FieldBuf[] copy = new FieldBuf[fieldBufIdx + 1];
System.arraycopy(fieldBufs, 0, copy, 0, fieldBufIdx);
copy[fieldBufIdx] = new FieldBuf(startField, i);
fieldSize = copy.length;
return new NextRecord(i - offset, copy, LineBreak.CRLF);
}
}
} else {
if (fieldBufIdx + 1 == fieldBufs.length) {
fieldBufs[fieldBufIdx] = new FieldBuf(startField, i);
fieldSize = fieldBufs.length;
return new NextRecord(i - offset, fieldBufs, LineBreak.CR);
} else {
if (fieldBufIdx == fieldBufs.length) {
final FieldBuf[] copy = new FieldBuf[fieldBufIdx + 1];
System.arraycopy(fieldBufs, 0, copy, 0, fieldBufIdx);
copy[fieldBufIdx] = new FieldBuf(startField, i);
fieldSize = copy.length;
return new NextRecord(i - offset, copy, LineBreak.CR);
} else {
final FieldBuf[] copy = new FieldBuf[fieldBufIdx + 1];
System.arraycopy(fieldBufs, 0, copy, 0, fieldBufIdx);
copy[fieldBufIdx] = new FieldBuf(startField, i);
fieldSize = copy.length;
return new NextRecord(i - offset, copy, LineBreak.CR);
}
}
}
} else if (i + 1 == length) {
if (fieldBufIdx + 1 == fieldBufs.length) {
fieldBufs[fieldBufIdx] = new FieldBuf(startField, i);
fieldSize = fieldBufs.length;
return new NextRecord(i - offset, fieldBufs, LineBreak.CR);
} else {
if (fieldBufIdx == fieldBufs.length) {
final FieldBuf[] copy = new FieldBuf[fieldBufIdx + 1];
System.arraycopy(fieldBufs, 0, copy, 0, fieldBufIdx);
copy[fieldBufIdx] = new FieldBuf(startField, i);
fieldSize = copy.length;
return new NextRecord(i - offset, copy, LineBreak.CR);
} else {
final FieldBuf[] copy = new FieldBuf[fieldBufIdx + 1];
System.arraycopy(fieldBufs, 0, copy, 0, fieldBufIdx);
copy[fieldBufIdx] = new FieldBuf(startField, i);
fieldSize = copy.length;
return new NextRecord(i - offset, copy, LineBreak.CR);
}
}
} else {
return NextRecord.BUFFER_UNDERFLOW;
}
}
} else if (b == LINE_BREAK_LF) {
if (state == State.START_QUOTE) {
i += 1;
} else {
if (fieldBufIdx + 1 == fieldBufs.length) {
fieldBufs[fieldBufIdx] = new FieldBuf(startField, i);
fieldSize = fieldBufs.length;
return new NextRecord(i - offset, fieldBufs, LineBreak.LF);
} else {
if (fieldBufIdx == fieldBufs.length) {
final FieldBuf[] copy = new FieldBuf[fieldBufIdx + 1];
System.arraycopy(fieldBufs, 0, copy, 0, fieldBufIdx);
copy[fieldBufIdx] = new FieldBuf(startField, i);
fieldSize = copy.length;
return new NextRecord(i - offset, copy, LineBreak.LF);
} else {
final FieldBuf[] copy = new FieldBuf[fieldBufIdx + 1];
System.arraycopy(fieldBufs, 0, copy, 0, fieldBufIdx);
copy[fieldBufIdx] = new FieldBuf(startField, i);
fieldSize = copy.length;
return new NextRecord(i - offset, copy, LineBreak.LF);
}
}
}
} else {
i += 1;
}
} else if ((b & 0xf0) == 0xf0) { // four bytes unicode
i += 4;
if (i < length) {
if (((buffer[i - 1] & 0xff) >>> 6) != 2) {
return NextRecord.UNSUPPORTED_ENCODING;
}
if (((buffer[i - 2] & 0xff) >>> 6) != 2) {
return NextRecord.UNSUPPORTED_ENCODING;
}
if (((buffer[i - 3] & 0xff) >>> 6) != 2) {
return NextRecord.UNSUPPORTED_ENCODING;
}
} else if (i == length) {
if (length < bufferSize) {
if (fieldBufIdx + 1 == fieldBufs.length) {
fieldBufs[fieldBufIdx] = new FieldBuf(startField, i);
fieldSize = fieldBufs.length;
return new NextRecord(i - offset, fieldBufs, LineBreak.NONE);
} else {
if (fieldBufIdx == fieldBufs.length) {
final FieldBuf[] copy = new FieldBuf[fieldBufIdx + 1];
System.arraycopy(fieldBufs, 0, copy, 0, fieldBufIdx);
copy[fieldBufIdx] = new FieldBuf(startField, i);
fieldSize = copy.length;
return new NextRecord(i - offset, copy, LineBreak.NONE);
} else {
final FieldBuf[] copy = new FieldBuf[fieldBufIdx + 1];
System.arraycopy(fieldBufs, 0, copy, 0, fieldBufIdx);
copy[fieldBufIdx] = new FieldBuf(startField, i);
fieldSize = copy.length;
return new NextRecord(i - offset, copy, LineBreak.NONE);
}
}
} else {
return NextRecord.BUFFER_UNDERFLOW;
}
} else {
return NextRecord.BUFFER_UNDERFLOW;
}
} else if ((b & 0xe0) == 0xe0) { // three bytes unicode
i += 3;
if (i < length) {
if (((buffer[i - 1] & 0xff) >>> 6) != 2) {
return NextRecord.UNSUPPORTED_ENCODING;
}
if (((buffer[i - 2] & 0xff) >>> 6) != 2) {
return NextRecord.UNSUPPORTED_ENCODING;
}
} else if (i == length) {
if (length < bufferSize) {
if (fieldBufIdx + 1 == fieldBufs.length) {
fieldBufs[fieldBufIdx] = new FieldBuf(startField, i);
fieldSize = fieldBufs.length;
return new NextRecord(i - offset, fieldBufs, LineBreak.NONE);
} else {
if (fieldBufIdx == fieldBufs.length) {
final FieldBuf[] copy = new FieldBuf[fieldBufIdx + 1];
System.arraycopy(fieldBufs, 0, copy, 0, fieldBufIdx);
copy[fieldBufIdx] = new FieldBuf(startField, i);
fieldSize = copy.length;
return new NextRecord(i - offset, copy, LineBreak.NONE);
} else {
final FieldBuf[] copy = new FieldBuf[fieldBufIdx + 1];
System.arraycopy(fieldBufs, 0, copy, 0, fieldBufIdx);
copy[fieldBufIdx] = new FieldBuf(startField, i);
fieldSize = copy.length;
return new NextRecord(i - offset, copy, LineBreak.NONE);
}
}
} else {
return NextRecord.BUFFER_UNDERFLOW;
}
} else {
return NextRecord.BUFFER_UNDERFLOW;
}
} else if ((b & 0xc0) == 0xc0) { // two bytes unicode
i += 2;
if (i < length) {
final byte c = buffer[i - 1];
if (((c & 0xff) >>> 6) != 2) {
return NextRecord.UNSUPPORTED_ENCODING;
}
} else if (i == length) {
if (length < bufferSize) {
if (fieldBufIdx + 1 == fieldBufs.length) {
fieldBufs[fieldBufIdx] = new FieldBuf(startField, i);
fieldSize = fieldBufs.length;
return new NextRecord(i - offset, fieldBufs, LineBreak.NONE);
} else {
if (fieldBufIdx == fieldBufs.length) {
final FieldBuf[] copy = new FieldBuf[fieldBufIdx + 1];
System.arraycopy(fieldBufs, 0, copy, 0, fieldBufIdx);
copy[fieldBufIdx] = new FieldBuf(startField, i);
fieldSize = copy.length;
return new NextRecord(i - offset, copy, LineBreak.NONE);
} else {
final FieldBuf[] copy = new FieldBuf[fieldBufIdx + 1];
System.arraycopy(fieldBufs, 0, copy, 0, fieldBufIdx);
copy[fieldBufIdx] = new FieldBuf(startField, i);
fieldSize = copy.length;
return new NextRecord(i - offset, copy, LineBreak.NONE);
}
}
} else {
return NextRecord.BUFFER_UNDERFLOW;
}
} else {
return NextRecord.BUFFER_UNDERFLOW;
}
} else {
return NextRecord.UNSUPPORTED_ENCODING;
}
}
if (i == length) {
if (length < bufferSize) {
if (fieldBufIdx + 1 == fieldBufs.length) {
fieldBufs[fieldBufIdx] = new FieldBuf(startField, i);
fieldSize = fieldBufs.length;
return new NextRecord(i - offset, fieldBufs, LineBreak.NONE);
} else {
if (fieldBufIdx == fieldBufs.length) {
final FieldBuf[] copy = new FieldBuf[fieldBufIdx + 1];
System.arraycopy(fieldBufs, 0, copy, 0, fieldBufIdx);
copy[fieldBufIdx] = new FieldBuf(startField, i);
fieldSize = copy.length;
return new NextRecord(i - offset, copy, LineBreak.NONE);
} else {
final FieldBuf[] copy = new FieldBuf[fieldBufIdx + 1];
System.arraycopy(fieldBufs, 0, copy, 0, fieldBufIdx);
copy[fieldBufIdx] = new FieldBuf(startField, i);
fieldSize = copy.length;
return new NextRecord(i - offset, copy, LineBreak.NONE);
}
}
} else {
return NextRecord.BUFFER_UNDERFLOW;
}
} else {
return NextRecord.BUFFER_OVERFLOW;
}
}
}
private static final class NextRecord {
private static final NextRecord BUFFER_UNDERFLOW = new NextRecord(-1);
private static final NextRecord BUFFER_OVERFLOW = new NextRecord(-2);
private static final NextRecord INVALID_CSV_FORMAT = new NextRecord(-3);
private static final NextRecord UNSUPPORTED_ENCODING = new NextRecord(-4);
private final int offset;
private final FieldBuf[] fields;
private final LineBreak lineBreak;
private final int status;
private NextRecord(final int offset, final FieldBuf[] fields, final LineBreak lineBreak) {
this.offset = offset;
this.fields = fields;
this.lineBreak = lineBreak;
this.status = 0;
}
private NextRecord(final int error) {
this.offset = 0;
this.fields = new FieldBuf[0];
this.lineBreak = LineBreak.NONE;
this.status = error;
}
}
private enum State {
START,
START_QUOTE,
MUST_QUOTE
}
private static final class FieldBuf {
private final int start;
private final int end;
private final int len;
private FieldBuf(final int start, final int end) {
this.start = start;
this.end = end;
this.len = end - start;
}
}
/**
* CSV Record
*
* <p>
*/
public static final class Record {
private final long number;
private final String[] fields;
private final LineBreak lineBreak;
private Record(final long number, final String[] fields, final LineBreak lineBreak) {
this.number = number;
this.fields = fields;
this.lineBreak = lineBreak;
}
/**
* Create CSV record with {@code CRLF} line endings.
*
* @param fields CSV fields.
* @return returns new {@link Record}.
*/
public static Record create(final String... fields) {
return new Record(-1, fields, LineBreak.CRLF);
}
/**
* Create CSV record with specified line endings.
*
* @param lineBreak line endings.
* @param fields CSV fields.
* @return returns CSV {@link Record} with specified line endings.
*/
public static Record create(final LineBreak lineBreak, final String... fields) {
return new Record(-1, fields, lineBreak);
}
/**
* Create CSV record with specified line ending and number.
*
* @param lineBreak line ending.
* @param number line number.
* @param fields CSV fields.
* @return returns CSV {@link Record} with specified line endings.
*/
public static Record create(final LineBreak lineBreak, final long number, String[] fields) {
return new Record(number, fields, lineBreak);
}
public final long number() {
return number;
}
public final String[] fields() {
return fields;
}
public final LineBreak lineBreak() {
return lineBreak;
}
@Override
public final boolean equals(final Object o) {
if (o instanceof Record) {
final Record record = (Record) o;
if (fields.length != record.fields.length) {
return false;
}
for (int i = 0; i < fields.length; i++) {
if (!fields[i].equals(record.fields[i])) {
return false;
}
}
return number == record.number && lineBreak == record.lineBreak;
} else {
return false;
}
}
@Override
public final int hashCode() {
int result = (int) (number ^ (number >>> 32));
for (final String element : fields) {
result = 31 * result + (element == null ? 0 : element.hashCode());
}
result = 31 * result + lineBreak.length;
return result;
}
@Override
public final String toString() {
final StringBuilder sb = new StringBuilder();
sb.append('[');
sb.append(fields[0]);
for (int i = 1; i < fields.length; i++) {
sb.append(',');
sb.append(fields[i]);
}
sb.append(']');
return String.format(
"[number=%d, fields=%s, lineBreak=%s]", number, sb.toString(), lineBreak);
}
}
/**
* GigaCSV configuration options.