CsvReader修改策略,添加可选是否关闭Reader重载,默认不关闭Reader

This commit is contained in:
Looly 2023-04-11 22:44:06 +08:00
parent a97901f0e3
commit 0ebfb8146d
3 changed files with 37 additions and 9 deletions

View File

@ -2,7 +2,7 @@
# 🚀Changelog
-------------------------------------------------------------------------------------------------------------
# 5.8.17.M1 (2023-04-05)
# 5.8.17.M1 (2023-04-11)
### 🐣新特性
* 【core 】 SerializeUtil.deserialize增加白名单类避免RCE vulnerabilityissue#3021@Github
@ -13,6 +13,7 @@
* 【core 】 NamingCase.toCamelCase新增重载可选是否转换其他字符为小写issue#3031@ithub
* 【core 】 新增JdkUtil
* 【core 】 DateUtil.getZodiac增加越界检查issue#3036@Github
* 【core 】 CsvReader修改策略添加可选是否关闭Reader重载默认不关闭Readerissue#I6UAX1@Gitee
### 🐞Bug修复
* 【core 】 CollUtil.split优化切割列表参数判断避免OOMpr#3026@Github

View File

@ -126,7 +126,7 @@ public class CsvBaseReader implements Serializable {
* @param rowHandler 行处理器用于一行一行的处理数据
*/
public void readFromStr(String csvStr, CsvRowHandler rowHandler) {
read(parse(new StringReader(csvStr)), rowHandler);
read(parse(new StringReader(csvStr)), true, rowHandler);
}
@ -174,9 +174,21 @@ public class CsvBaseReader implements Serializable {
* @throws IORuntimeException IO异常
*/
public CsvData read(Reader reader) throws IORuntimeException {
return read(reader, true);
}
/**
* 从Reader中读取CSV数据
*
* @param reader Reader
* @param close 读取结束是否关闭Reader
* @return {@link CsvData}包含数据列表和行信息
* @throws IORuntimeException IO异常
*/
public CsvData read(Reader reader, boolean close) throws IORuntimeException {
final CsvParser csvParser = parse(reader);
final List<CsvRow> rows = new ArrayList<>();
read(csvParser, rows::add);
read(csvParser, close, rows::add);
final List<String> header = config.headerLineNo > -1 ? csvParser.getHeader() : null;
return new CsvData(header, rows);
@ -243,7 +255,19 @@ public class CsvBaseReader implements Serializable {
* @throws IORuntimeException IO异常
*/
public void read(Reader reader, CsvRowHandler rowHandler) throws IORuntimeException {
read(parse(reader), rowHandler);
read(reader, true, rowHandler);
}
/**
* 从Reader中读取CSV数据读取后关闭Reader
*
* @param reader Reader
* @param close 读取结束是否关闭Reader
* @param rowHandler 行处理器用于一行一行的处理数据
* @throws IORuntimeException IO异常
*/
public void read(Reader reader, boolean close, CsvRowHandler rowHandler) throws IORuntimeException {
read(parse(reader), close, rowHandler);
}
//--------------------------------------------------------------------------------------------- Private method start
@ -252,17 +276,20 @@ public class CsvBaseReader implements Serializable {
* 读取CSV数据读取后关闭Parser
*
* @param csvParser CSV解析器
* @param close 读取结束是否关闭{@link CsvParser}
* @param rowHandler 行处理器用于一行一行的处理数据
* @throws IORuntimeException IO异常
* @since 5.0.4
*/
private void read(CsvParser csvParser, CsvRowHandler rowHandler) throws IORuntimeException {
private void read(CsvParser csvParser, boolean close, CsvRowHandler rowHandler) throws IORuntimeException {
try {
while (csvParser.hasNext()){
while (csvParser.hasNext()) {
rowHandler.handle(csvParser.next());
}
} finally {
IoUtil.close(csvParser);
if(close){
IoUtil.close(csvParser);
}
}
}

View File

@ -109,7 +109,7 @@ public class CsvReader extends CsvBaseReader implements Iterable<CsvRow>, Closea
* @throws IORuntimeException IO异常
*/
public CsvData read() throws IORuntimeException {
return read(this.reader);
return read(this.reader, false);
}
/**
@ -121,7 +121,7 @@ public class CsvReader extends CsvBaseReader implements Iterable<CsvRow>, Closea
* @since 5.0.4
*/
public void read(CsvRowHandler rowHandler) throws IORuntimeException {
read(this.reader, rowHandler);
read(this.reader, false, rowHandler);
}
/**