Skip to content

Commit dbf0cdf

Browse files
author
Gary Gregory
committed
[IO-764] IOUtils.write() throws
OutOfMemoryError/NegativeArraySizeException while writing big strings Reuse solution.
1 parent 7002bf2 commit dbf0cdf

2 files changed

Lines changed: 9 additions & 8 deletions

File tree

src/main/java/org/apache/commons/io/IOUtils.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3193,7 +3193,7 @@ public static void write(final char[] data, final OutputStream output)
31933193
*/
31943194
public static void write(final char[] data, final OutputStream output, final Charset charset) throws IOException {
31953195
if (data != null) {
3196-
output.write(new String(data).getBytes(Charsets.toCharset(charset)));
3196+
write(new String(data), output, charset);
31973197
}
31983198
}
31993199

@@ -3452,9 +3452,9 @@ public static void write(final StringBuffer data, final OutputStream output) //N
34523452
*/
34533453
@Deprecated
34543454
public static void write(final StringBuffer data, final OutputStream output, final String charsetName) //NOSONAR
3455-
throws IOException {
3455+
throws IOException {
34563456
if (data != null) {
3457-
output.write(data.toString().getBytes(Charsets.toCharset(charsetName)));
3457+
write(data.toString(), output, Charsets.toCharset(charsetName));
34583458
}
34593459
}
34603460

@@ -3568,11 +3568,12 @@ public static void writeLines(final Collection<?> lines, String lineEnding, fina
35683568
lineEnding = System.lineSeparator();
35693569
}
35703570
final Charset cs = Charsets.toCharset(charset);
3571+
final byte[] eolBytes = lineEnding.getBytes(cs);
35713572
for (final Object line : lines) {
35723573
if (line != null) {
3573-
output.write(line.toString().getBytes(cs));
3574+
write(line.toString(), output, cs);
35743575
}
3575-
output.write(lineEnding.getBytes(cs));
3576+
output.write(eolBytes);
35763577
}
35773578
}
35783579

src/test/java/org/apache/commons/io/IOUtilsTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,7 @@ public void testContentEqualsIgnoreEOL() throws Exception {
601601
testSingleEOL("1235", "1234", false);
602602
}
603603

604-
public void testSingleEOL(String s1, String s2, boolean ifEquals) throws IOException {
604+
public void testSingleEOL(final String s1, final String s2, final boolean ifEquals) throws IOException {
605605
assertEquals(ifEquals, IOUtils.contentEqualsIgnoreEOL(
606606
new CharArrayReader(s1.toCharArray()),
607607
new CharArrayReader(s2.toCharArray())
@@ -1662,7 +1662,7 @@ public void testWriteBigString() throws IOException {
16621662
final String data;
16631663
try {
16641664
data = StringUtils.repeat("\uD83D", repeat);
1665-
} catch (OutOfMemoryError e) {
1665+
} catch (final OutOfMemoryError e) {
16661666
System.err.printf("Don't fail the test if we cannot build the fixture, just log, fixture size = %,d%n.", repeat);
16671667
e.printStackTrace();
16681668
return;
@@ -1676,7 +1676,7 @@ public void testWriteBigString() throws IOException {
16761676
@Test
16771677
public void testWriteLittleString() throws IOException {
16781678
final String data = "\uD83D";
1679-
// White-box test to check that not closing the internal channel is not a problem.
1679+
// White-box test to check that not closing the internal channel is not a problem.
16801680
for (int i = 0; i < 1_000_000; i++) {
16811681
try (CountingOutputStream os = new CountingOutputStream(NullOutputStream.INSTANCE)) {
16821682
IOUtils.write(data, os, StandardCharsets.UTF_8);

0 commit comments

Comments
 (0)