alibaba / alibaba/fastjson2

[BUG] 序列化对象时出现:java.io.IOException: Stream closed

Open
#1,394 5 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
Java
Stars
4.4k
Forks
613
Avg merge
1d 22h
Merged PRs (30d)
6

Description

### 问题描述

项目中使用 [Apache的CSV工具库](https://github.com/apache/commons-csv) 读取了一个 `CSV` 文件后得到一个集合,需要使用 `fastjson` 打印这个集合里的对象,伪代码如下:

```java
List csvRecordList= readCsvFile("/csvFilePath");
CSVRecord csvRecord = csvRecordList.get(0);
System.out.println(JSON.toJSONString(csvRecord));
```

此时需要序列化 [CSVRecord](https://github.com/apache/commons-csv/blob/rel/commons-csv-1.7/src/main/java/org/apache/commons/csv/CSVRecord.java) 对象

原来项目中使用的 `fastjson` 版本是:`1.2.11`,上述代码运行正常。当 `1.2.83` 发布时说修复了安全漏洞需要尽快升级,于是我们升级到了 `1.2.83` ,结果上述代码出现了异常:

```java
Exception in thread "main" com.alibaba.fastjson.JSONException: Stream closed
at com.alibaba.fastjson.serializer.JSONSerializer.write(JSONSerializer.java:314)
at com.alibaba.fastjson.JSON.toJSONString(JSON.java:793)
at com.alibaba.fastjson.JSON.toJSONString(JSON.java:731)
at com.alibaba.fastjson.JSON.toJSONString(JSON.java:688)
at xxx.util.CsvUtil.main(CsvUtil.java:123)
Caused by: java.io.IOException: Stream closed
at sun.nio.cs.StreamDecoder.ensureOpen(StreamDecoder.java:46)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:148)
at java.io.InputStreamReader.read(InputStreamReader.java:184)
at java.io.BufferedReader.fill(BufferedReader.java:161)
at java.io.BufferedReader.read(BufferedReader.java:182)
at org.apache.commons.csv.ExtendedBufferedReader.read(ExtendedBufferedReader.java:58)
at org.apache.commons.csv.Lexer.nextToken(Lexer.java:95)
at org.apache.commons.csv.CSVParser.nextRecord(CSVParser.java:664)
at org.apache.commons.csv.CSVParser.getRecords(CSVParser.java:618)
at com.alibaba.fastjson.serializer.ASMSerializer_2_CSVParser.write(Unknown Source)
at com.alibaba.fastjson.serializer.ASMSerializer_1_CSVRecord.write(Unknown Source)
at com.alibaba.fastjson.serializer.JSONSerializer.write(JSONSerializer.java:312)
... 4 more
```

于是将 `fastjson` 版本升级到 `2.x` ,即最新版:`2.0.28`,依然出现该异常,使用任意 `CSV` 文件均可复现

Contributor guide

Open the contributing guide

Research direction

Reproduce the CSVRecord serialization example with fastjson 1.2.83 or fastjson2 2.0.28 and a CSV file. Start at JSONSerializer.write and JSON.toJSONString, then inspect the CSVRecord and CSVParser stack frames named in the report. Done means the reproducible Stream closed failure is resolved or clearly documented with a verified result.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.