You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@poi.apache.org by fa...@apache.org on 2022/03/11 22:37:29 UTC

svn commit: r1898863 - /poi/trunk/poi/src/main/java/org/apache/poi/util/IOUtils.java

Author: fanningpj
Date: Fri Mar 11 22:37:29 2022
New Revision: 1898863

URL: http://svn.apache.org/viewvc?rev=1898863&view=rev
Log:
fix issue in IOUtils.toByteArrayWithMaxLength

Modified:
    poi/trunk/poi/src/main/java/org/apache/poi/util/IOUtils.java

Modified: poi/trunk/poi/src/main/java/org/apache/poi/util/IOUtils.java
URL: http://svn.apache.org/viewvc/poi/trunk/poi/src/main/java/org/apache/poi/util/IOUtils.java?rev=1898863&r1=1898862&r2=1898863&view=diff
==============================================================================
--- poi/trunk/poi/src/main/java/org/apache/poi/util/IOUtils.java (original)
+++ poi/trunk/poi/src/main/java/org/apache/poi/util/IOUtils.java Fri Mar 11 22:37:29 2022
@@ -54,11 +54,33 @@ public final class IOUtils {
      */
     private static int BYTE_ARRAY_MAX_OVERRIDE = -1;
 
+    /**
+     * The max init size of ByteArrayOutputStream.
+     * -1 means init size of ByteArrayOutputStream could be up to Integer.MAX_VALUE
+     */
+    private static int MAX_BYTE_ARRAY_INIT_SIZE = -1;
+
     private IOUtils() {
         // no instances of this class
     }
 
     /**
+     * @param maxOverride the max init size of ByteArrayOutputStream.
+     * -1 (the default) means init size of ByteArrayOutputStream could be up to Integer.MAX_VALUE
+     */
+    public static void setMaxByteArrayInitSize(final int maxOverride) {
+        MAX_BYTE_ARRAY_INIT_SIZE = maxOverride;
+    }
+
+    /**
+     * @return the max init size of ByteArrayOutputStream.
+     * -1 (the default) means init size of ByteArrayOutputStream could be up to Integer.MAX_VALUE
+     */
+    public static int getMaxByteArrayInitSize() {
+        return MAX_BYTE_ARRAY_INIT_SIZE;
+    }
+
+    /**
      * If this value is set to > 0, {@link #safelyAllocate(long, int)} will ignore the
      * maximum record length parameter.
      *
@@ -202,7 +224,10 @@ public final class IOUtils {
         }
 
         final int derivedLen = Math.min(length, derivedMaxLength);
-        final int bufferLen = isLengthKnown ? derivedLen : Math.min(4096, derivedLen);
+        int bufferLen = isLengthKnown ? derivedLen : Math.min(4096, derivedLen);
+        if (bufferLen > MAX_BYTE_ARRAY_INIT_SIZE && MAX_BYTE_ARRAY_INIT_SIZE > 0) {
+            bufferLen = Math.min(bufferLen, MAX_BYTE_ARRAY_INIT_SIZE);
+        }
         try (UnsynchronizedByteArrayOutputStream baos = new UnsynchronizedByteArrayOutputStream(bufferLen)) {
             byte[] buffer = new byte[4096];
             int totalBytes = 0, readBytes;



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org