com.alibaba.fastjson2.support.csv.CSVReaderUTF16 Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of fastjson2 Show documentation
Show all versions of fastjson2 Show documentation
Fastjson is a JSON processor (JSON parser + JSON generator) written in Java
package com.alibaba.fastjson2.support.csv;
import com.alibaba.fastjson2.JSONException;
import com.alibaba.fastjson2.JSONFactory;
import com.alibaba.fastjson2.reader.*;
import com.alibaba.fastjson2.stream.StreamReader;
import com.alibaba.fastjson2.util.DateUtils;
import com.alibaba.fastjson2.util.Fnv;
import com.alibaba.fastjson2.util.IOUtils;
import com.alibaba.fastjson2.util.TypeUtils;
import java.io.IOException;
import java.io.Reader;
import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Consumer;
import java.util.function.Function;
import static com.alibaba.fastjson2.util.DateUtils.DEFAULT_ZONE_ID;
final class CSVReaderUTF16
extends CSVReader {
static final Map> valueConsumerCreators
= new ConcurrentHashMap<>();
CharArrayValueConsumer valueConsumer;
char[] buf;
Reader input;
CSVReaderUTF16(Feature... features) {
for (Feature feature : features) {
this.features |= feature.mask;
}
}
CSVReaderUTF16(Reader input, Class objectClass) {
super(objectClass);
this.input = input;
}
CSVReaderUTF16(Reader input, CharArrayValueConsumer valueConsumer) {
this.valueConsumer = valueConsumer;
this.input = input;
}
CSVReaderUTF16(Reader input, Type[] types) {
super(types);
this.input = input;
}
CSVReaderUTF16(char[] bytes, int off, int len, Class objectClass) {
super(objectClass);
this.buf = bytes;
this.off = off;
this.end = off + len;
}
CSVReaderUTF16(char[] bytes, int off, int len, CharArrayValueConsumer valueConsumer) {
this.valueConsumer = valueConsumer;
this.buf = bytes;
this.off = off;
this.end = off + len;
}
CSVReaderUTF16(
char[] bytes,
int off,
int len,
Type[] types
) {
super(types);
this.buf = bytes;
this.off = off;
this.end = off + len;
}
protected boolean seekLine() throws IOException {
char[] buf = this.buf;
int off = this.off;
if (buf == null) {
if (input != null) {
buf = this.buf = new char[SIZE_512K];
int cnt = input.read(buf);
if (cnt == -1) {
inputEnd = true;
return false;
}
this.end = cnt;
}
}
for (int k = 0; k < 3; ++k) {
lineTerminated = false;
for (int i = off; i < end; i++) {
if (i + 4 < end) {
char b0 = buf[i];
char b1 = buf[i + 1];
char b2 = buf[i + 2];
char b3 = buf[i + 3];
if (b0 > '"' && b1 > '"' && b2 > '"' && b3 > '"') {
lineSize += 4;
i += 3;
continue;
}
}
char ch = buf[i];
if (ch == '"') {
lineSize++;
if (!quote) {
quote = true;
} else {
int n = i + 1;
if (n >= end) {
break;
}
if (buf[n] == '"') {
lineSize++;
i++;
} else {
quote = false;
}
}
continue;
}
if (quote) {
lineSize++;
continue;
}
if (ch == '\n') {
if (lineSize > 0 || (features & Feature.IgnoreEmptyLine.mask) == 0) {
rowCount++;
}
lineTerminated = true;
lineSize = 0;
lineEnd = i;
lineStart = lineNextStart;
lineNextStart = off = i + 1;
break;
} else if (ch == '\r') {
if (lineSize > 0 || (features & Feature.IgnoreEmptyLine.mask) == 0) {
rowCount++;
}
lineTerminated = true;
lineSize = 0;
lineEnd = i;
int n = i + 1;
if (n >= end) {
break;
}
if (buf[n] == '\n') {
i++;
}
lineStart = lineNextStart;
lineNextStart = off = i + 1;
break;
} else {
lineSize++;
}
}
if (!lineTerminated) {
if (input != null && !inputEnd) {
int len = end - off;
if (off > 0) {
if (len > 0) {
System.arraycopy(buf, off, buf, 0, len);
}
lineStart = lineNextStart = 0;
off = 0;
end = len;
quote = false;
}
int cnt = input.read(buf, end, buf.length - end);
if (cnt == -1) {
inputEnd = true;
if (off == end) {
this.off = off;
return false;
}
} else {
end += cnt;
continue;
}
}
lineStart = lineNextStart;
lineEnd = end;
rowCount++;
lineSize = 0;
off = end;
}
lineTerminated = off == end;
break;
}
this.off = off;
return true;
}
Object readValue(char[] chars, int off, int len, Type type) {
if (len == 0) {
return null;
}
if (type == Integer.class) {
return TypeUtils.parseInt(chars, off, len);
}
if (type == Long.class) {
return TypeUtils.parseLong(chars, off, len);
}
if (type == BigDecimal.class) {
return TypeUtils.parseBigDecimal(chars, off, len);
}
if (type == Float.class) {
return TypeUtils.parseFloat(chars, off, len);
}
if (type == Double.class) {
return TypeUtils.parseDouble(chars, off, len);
}
if (type == Date.class) {
long millis = DateUtils.parseMillis(chars, off, len, DEFAULT_ZONE_ID);
return new Date(millis);
}
if (type == Boolean.class) {
return TypeUtils.parseBoolean(chars, off, len);
}
String str = new String(chars, off, len);
return TypeUtils.cast(str, type);
}
public boolean isEnd() {
return inputEnd;
}
public Object[] readLineValues(boolean strings) {
try {
if (inputEnd) {
return null;
}
if (input == null) {
if (off >= end) {
return null;
}
}
boolean result = seekLine();
if (!result) {
return null;
}
} catch (IOException e) {
throw new JSONException("seekLine error", e);
}
Object[] values = null;
List