diff options
-rw-r--r-- | poi/src/main/java/org/apache/poi/util/IOUtils.java | 27 |
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 > 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; |