15 Star 129 Fork 23

wenshao / fastjson2

 / 详情

core/src/main/java/com/alibaba/fastjson2/support/csv/CSVReader.java的rowCount方法未能正确关闭资源,可能导致应用程序崩溃

待办的
创建于  
2023-12-21 09:42

修改方法

public static int rowCount(String str, Feature... features) throws IOException {
try (CSVReader state = new CSVReaderUTF8(features)) {
state.rowCount(str, str.length());
return state.rowCount();
} catch (Exception e) {
// 处理异常
e.printStackTrace();
throw e;
}
// try-with-resources会自动关闭资源
}

public static int rowCount(byte[] bytes, Feature... features) {
    try (CSVReaderUTF8 state = new CSVReaderUTF8(features)) {
        state.rowCount(bytes, bytes.length);
        return state.rowCount();
    } catch (Exception e) {
        // 处理异常
        e.printStackTrace();
        throw e;
    }
    // try-with-resources会自动关闭资源
}


public static int rowCount(char[] chars, Feature... features) {
    try (CSVReaderUTF16 state = new CSVReaderUTF16(features)) {
        state.rowCount(chars, chars.length);
        return state.rowCount();
    } catch (Exception e) {
        // 处理异常
        e.printStackTrace();
        throw e;
    }
    // try-with-resources会自动关闭资源
}

public static int rowCount(InputStream in) throws IOException {
if (in == null) {
throw new IllegalArgumentException("InputStream cannot be null");
}

    try (CSVReaderUTF8 state = new CSVReaderUTF8()) {
        byte[] bytes = new byte[SIZE_512K];
        while (true) {
            int cnt = in.read(bytes);
            if (cnt == -1) {
                break;
            }
            state.rowCount(bytes, cnt);
        }
        return state.rowCount();
    } catch (Exception e) {
        // 处理异常
        e.printStackTrace();
        throw e;
    }
}

输入图片说明
输入图片说明

评论 (0)

神马都是浮云 创建了任务
神马都是浮云 修改了描述
展开全部操作日志

登录 后才可以发表评论

状态
负责人
里程碑
Pull Requests
关联的 Pull Requests 被合并后可能会关闭此 issue
分支
开始日期   -   截止日期
-
置顶选项
优先级
参与者(1)
Java
1
https://gitee.com/wenshao/fastjson2.git
git@gitee.com:wenshao/fastjson2.git
wenshao
fastjson2
fastjson2

搜索帮助