Skip to content

Commit

Permalink
fix csv write ArrayIndexOutOfBoundsException error, for issue #2848
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Aug 13, 2024
1 parent cd66e18 commit c758be6
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,12 @@ public void writeDecimal(BigDecimal value) {

String str = value.toString();
int strlen = str.length();

int minCapacity = off + 24;
if (minCapacity - this.chars.length > 0) {
flush();
}

str.getChars(0, strlen, chars, off);
off += strlen;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,12 @@ public void writeDecimal(BigDecimal value) {

String str = value.toString();
int strlen = str.length();

int minCapacity = off + 24;
if (minCapacity - this.bytes.length > 0) {
flush();
}

str.getBytes(0, strlen, bytes, off);
off += strlen;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.alibaba.fastjson2.issues_2800;

import com.alibaba.fastjson2.support.csv.CSVWriter;
import org.junit.jupiter.api.Test;

import java.io.ByteArrayOutputStream;
import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;

public class Issue2848 {
@Test
public void test() throws Exception {
StringBuilder buf = new StringBuilder();
for (int i = 0; i < 65531; i++) {
buf.append('1');
}

String str = buf.toString();

try (CSVWriter writer = CSVWriter.of()) {
writer.writeValue(str);
writer.writeComma();
writer.writeValue(new BigDecimal("1.00"));
writer.writeComma();
}

try (CSVWriter writer = CSVWriter.of(new ByteArrayOutputStream(), StandardCharsets.UTF_16)) {
writer.writeValue(str);
writer.writeComma();
writer.writeValue(new BigDecimal("1.00"));
writer.writeComma();
}
}
}

0 comments on commit c758be6

Please sign in to comment.