shade.com.alibaba.fastjson2.support.csv.CSVReaderUTF8 Maven / Gradle / Ivy
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.InputStream;
import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.*;
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 CSVReaderUTF8
extends CSVReader {
static final Map> valueConsumerCreators
= new ConcurrentHashMap<>();
byte[] buf;
InputStream input;
Charset charset = StandardCharsets.UTF_8;
ByteArrayValueConsumer valueConsumer;
CSVReaderUTF8(Feature... features) {
for (Feature feature : features) {
this.features |= feature.mask;
}
}
CSVReaderUTF8(byte[] bytes, int off, int len, Charset charset, Class objectClass) {
super(objectClass);
this.buf = bytes;
this.off = off;
this.end = off + len;
this.charset = charset;
}
CSVReaderUTF8(byte[] bytes, int off, int len, Charset charset, ByteArrayValueConsumer valueConsumer) {
this.valueConsumer = valueConsumer;
this.buf = bytes;
this.off = off;
this.end = off + len;
this.charset = charset;
}
CSVReaderUTF8(byte[] bytes, int off, int len, Type[] types) {
super(types);
this.buf = bytes;
this.off = off;
this.end = off + len;
this.types = types;
}
CSVReaderUTF8(byte[] bytes, int off, int len, Class objectClass) {
super(objectClass);
this.buf = bytes;
this.off = off;
this.end = off + len;
}
CSVReaderUTF8(InputStream input, Charset charset, Type[] types) {
super(types);
this.charset = charset;
this.input = input;
}
CSVReaderUTF8(InputStream input, Charset charset, Class objectClass) {
super(objectClass);
this.charset = charset;
this.input = input;
}
CSVReaderUTF8(InputStream input, Charset charset, ByteArrayValueConsumer valueConsumer) {
this.charset = charset;
this.input = input;
this.valueConsumer = valueConsumer;
}
protected boolean seekLine() throws IOException {
byte[] buf = this.buf;
int off = this.off;
if (buf == null) {
if (input != null) {
buf = this.buf = new byte[SIZE_512K];
int cnt = input.read(buf);
if (cnt == -1) {
inputEnd = true;
return false;
}
this.end = cnt;
if (end > 3) {
// UTF8-BOM EF BB BF
if (buf[0] == -17 && buf[1] == -69 && buf[2] == -65) {
off = 3;
lineNextStart = off;
}
}
}
}
for (int k = 0; k < 3; ++k) {
lineTerminated = false;
for (int i = off; i < end; i++) {
byte 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(byte[] bytes, int off, int len, Type type) {
if (len == 0) {
return null;
}
if (type == Integer.class) {
return TypeUtils.parseInt(bytes, off, len);
}
if (type == Long.class) {
return TypeUtils.parseLong(bytes, off, len);
}
if (type == BigDecimal.class) {
return TypeUtils.parseBigDecimal(bytes, off, len);
}
if (type == Float.class) {
return TypeUtils.parseFloat(bytes, off, len);
}
if (type == Double.class) {
return TypeUtils.parseDouble(bytes, off, len);
}
if (type == Date.class) {
long millis = DateUtils.parseMillis(bytes, off, len, charset, DEFAULT_ZONE_ID);
return new Date(millis);
}
if (type == Boolean.class) {
return TypeUtils.parseBoolean(bytes, off, len);
}
String str = new String(bytes, off, len, charset);
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
© 2015 - 2025 Weber Informatics LLC | Privacy Policy