This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-io.git


The following commit(s) were added to refs/heads/master by this push:
     new dbf0cdf0 [IO-764] IOUtils.write() throws 
OutOfMemoryError/NegativeArraySizeException while writing big strings
dbf0cdf0 is described below

commit dbf0cdf07a9e1db846c74133a1b7d1663cd8cf40
Author: Gary Gregory <[email protected]>
AuthorDate: Tue Apr 5 15:57:38 2022 -0400

    [IO-764] IOUtils.write() throws
    OutOfMemoryError/NegativeArraySizeException while writing big strings
    
    Reuse solution.
---
 src/main/java/org/apache/commons/io/IOUtils.java     | 11 ++++++-----
 src/test/java/org/apache/commons/io/IOUtilsTest.java |  6 +++---
 2 files changed, 9 insertions(+), 8 deletions(-)

diff --git a/src/main/java/org/apache/commons/io/IOUtils.java 
b/src/main/java/org/apache/commons/io/IOUtils.java
index 518ba579..23611852 100644
--- a/src/main/java/org/apache/commons/io/IOUtils.java
+++ b/src/main/java/org/apache/commons/io/IOUtils.java
@@ -3193,7 +3193,7 @@ public class IOUtils {
      */
     public static void write(final char[] data, final OutputStream output, 
final Charset charset) throws IOException {
         if (data != null) {
-            output.write(new 
String(data).getBytes(Charsets.toCharset(charset)));
+            write(new String(data), output, charset);
         }
     }
 
@@ -3452,9 +3452,9 @@ public class IOUtils {
      */
     @Deprecated
     public static void write(final StringBuffer data, final OutputStream 
output, final String charsetName) //NOSONAR
-            throws IOException {
+        throws IOException {
         if (data != null) {
-            
output.write(data.toString().getBytes(Charsets.toCharset(charsetName)));
+            write(data.toString(), output, Charsets.toCharset(charsetName));
         }
     }
 
@@ -3568,11 +3568,12 @@ public class IOUtils {
             lineEnding = System.lineSeparator();
         }
         final Charset cs = Charsets.toCharset(charset);
+        final byte[] eolBytes = lineEnding.getBytes(cs);
         for (final Object line : lines) {
             if (line != null) {
-                output.write(line.toString().getBytes(cs));
+                write(line.toString(), output, cs);
             }
-            output.write(lineEnding.getBytes(cs));
+            output.write(eolBytes);
         }
     }
 
diff --git a/src/test/java/org/apache/commons/io/IOUtilsTest.java 
b/src/test/java/org/apache/commons/io/IOUtilsTest.java
index 71a406ca..114b5be8 100644
--- a/src/test/java/org/apache/commons/io/IOUtilsTest.java
+++ b/src/test/java/org/apache/commons/io/IOUtilsTest.java
@@ -601,7 +601,7 @@ public class IOUtilsTest {
         testSingleEOL("1235", "1234", false);
     }
 
-    public void testSingleEOL(String s1, String s2, boolean ifEquals) throws 
IOException {
+    public void testSingleEOL(final String s1, final String s2, final boolean 
ifEquals) throws IOException {
         assertEquals(ifEquals, IOUtils.contentEqualsIgnoreEOL(
                 new CharArrayReader(s1.toCharArray()),
                 new CharArrayReader(s2.toCharArray())
@@ -1662,7 +1662,7 @@ public class IOUtilsTest {
         final String data;
         try {
             data = StringUtils.repeat("\uD83D", repeat);
-        } catch (OutOfMemoryError e) {
+        } catch (final OutOfMemoryError e) {
             System.err.printf("Don't fail the test if we cannot build the 
fixture, just log, fixture size = %,d%n.", repeat);
             e.printStackTrace();
             return;
@@ -1676,7 +1676,7 @@ public class IOUtilsTest {
     @Test
     public void testWriteLittleString() throws IOException {
         final String data = "\uD83D";
-        // White-box test to check that not closing the internal channel is 
not a problem. 
+        // White-box test to check that not closing the internal channel is 
not a problem.
         for (int i = 0; i < 1_000_000; i++) {
             try (CountingOutputStream os = new 
CountingOutputStream(NullOutputStream.INSTANCE)) {
                 IOUtils.write(data, os, StandardCharsets.UTF_8);

Reply via email to