aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPJ Fanning <fanningpj@apache.org>2022-03-11 22:37:29 +0000
committerPJ Fanning <fanningpj@apache.org>2022-03-11 22:37:29 +0000
commit38e5040ee6a303d9fc992f69e7dd9123ae2f6834 (patch)
tree6b9bcc39e905a2e1a444b65b8c6605df662eb1bf
parent5b5f286a301c7e8f9cc1b5fe5d4abce279388ff3 (diff)
downloadpoi-38e5040ee6a303d9fc992f69e7dd9123ae2f6834.tar.gz
poi-38e5040ee6a303d9fc992f69e7dd9123ae2f6834.zip
fix issue in IOUtils.toByteArrayWithMaxLength
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1898863 13f79535-47bb-0310-9956-ffa450edef68
-rw-r--r--poi/src/main/java/org/apache/poi/util/IOUtils.java27
1 files changed, 26 insertions, 1 deletions
diff --git a/poi/src/main/java/org/apache/poi/util/IOUtils.java b/poi/src/main/java/org/apache/poi/util/IOUtils.java
index e9c83a8f63..f5940d9555 100644
--- a/poi/src/main/java/org/apache/poi/util/IOUtils.java
+++ b/poi/src/main/java/org/apache/poi/util/IOUtils.java
@@ -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 &gt; 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;