mirror of
https://gitee.com/dromara/hutool.git
synced 2025-04-05 17:37:59 +08:00
CsvReader修改策略,添加可选是否关闭Reader重载,默认不关闭Reader
This commit is contained in:
parent
a97901f0e3
commit
0ebfb8146d
@ -2,7 +2,7 @@
|
||||
# 🚀Changelog
|
||||
|
||||
-------------------------------------------------------------------------------------------------------------
|
||||
# 5.8.17.M1 (2023-04-05)
|
||||
# 5.8.17.M1 (2023-04-11)
|
||||
|
||||
### 🐣新特性
|
||||
* 【core 】 SerializeUtil.deserialize增加白名单类,避免RCE vulnerability(issue#3021@Github)
|
||||
@ -13,6 +13,7 @@
|
||||
* 【core 】 NamingCase.toCamelCase新增重载,可选是否转换其他字符为小写(issue#3031@ithub)
|
||||
* 【core 】 新增JdkUtil
|
||||
* 【core 】 DateUtil.getZodiac增加越界检查(issue#3036@Github)
|
||||
* 【core 】 CsvReader修改策略,添加可选是否关闭Reader重载,默认不关闭Reader(issue#I6UAX1@Gitee)
|
||||
|
||||
### 🐞Bug修复
|
||||
* 【core 】 CollUtil.split优化切割列表参数判断,避免OOM(pr#3026@Github)
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user