aboutsummaryrefslogtreecommitdiffstats
path: root/poi
diff options
context:
space:
mode:
authorPJ Fanning <fanningpj@apache.org>2022-02-19 19:35:57 +0000
committerPJ Fanning <fanningpj@apache.org>2022-02-19 19:35:57 +0000
commit8e9496a475b43c94e8c9ad37396a89fecd00902c (patch)
treecde06e3114feb32da91df694ef31fdde1d0e6294 /poi
parentddd3bc2f9f5af05190161aa8058da66e0db81b54 (diff)
downloadpoi-8e9496a475b43c94e8c9ad37396a89fecd00902c.tar.gz
poi-8e9496a475b43c94e8c9ad37396a89fecd00902c.zip
[bug-65639] take BYTE_ARRAY_MAX_OVERRIDE into account in IOUtils#toByteArray
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1898229 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'poi')
-rw-r--r--poi/src/main/java/org/apache/poi/util/IOUtils.java19
1 files changed, 9 insertions, 10 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 824ffc4450..d508833464 100644
--- a/poi/src/main/java/org/apache/poi/util/IOUtils.java
+++ b/poi/src/main/java/org/apache/poi/util/IOUtils.java
@@ -161,8 +161,9 @@ public final class IOUtils {
* @param length The maximum length to read, use {@link Integer#MAX_VALUE} to read the stream
* until EOF
* @param maxLength if the input is equal to/longer than {@code maxLength} bytes,
- * then throw an {@link IOException} complaining about the length.
-* use {@link Integer#MAX_VALUE} to disable the check
+ * then throw an {@link IOException} complaining about the length.
+ * use {@link Integer#MAX_VALUE} to disable the check - if {@link #setByteArrayMaxOverride(int)} is
+ * set then that max of that value and this maxLength is used
* @return A byte array with the read bytes.
* @throws IOException If reading data fails or EOF is encountered too early for the given length.
*/
@@ -170,14 +171,12 @@ public final class IOUtils {
if (length < 0L || maxLength < 0L) {
throw new RecordFormatException("Can't allocate an array of length < 0");
}
- // if (length > (long)Integer.MAX_VALUE) {
- // throw new RecordFormatException("Can't allocate an array > "+Integer.MAX_VALUE);
- // }
- if ((length != Integer.MAX_VALUE) || (maxLength != Integer.MAX_VALUE)) {
- checkLength(length, maxLength);
+ final int derivedMaxLength = BYTE_ARRAY_MAX_OVERRIDE <= 0 ? maxLength : Math.max(maxLength, BYTE_ARRAY_MAX_OVERRIDE);
+ if ((length != Integer.MAX_VALUE) || (derivedMaxLength != Integer.MAX_VALUE)) {
+ checkLength(length, derivedMaxLength);
}
- final int len = Math.min(length, maxLength);
+ final int len = Math.min(length, derivedMaxLength);
try (UnsynchronizedByteArrayOutputStream baos = new UnsynchronizedByteArrayOutputStream(len == Integer.MAX_VALUE ? 4096 : len)) {
byte[] buffer = new byte[4096];
int totalBytes = 0, readBytes;
@@ -191,8 +190,8 @@ public final class IOUtils {
checkByteSizeLimit(totalBytes);
} while (totalBytes < len && readBytes > -1);
- if (maxLength != Integer.MAX_VALUE && totalBytes == maxLength) {
- throw new IOException("MaxLength (" + maxLength + ") reached - stream seems to be invalid.");
+ if (derivedMaxLength != Integer.MAX_VALUE && totalBytes == derivedMaxLength) {
+ throw new IOException("MaxLength (" + derivedMaxLength + ") reached - stream seems to be invalid.");
}
if (len != Integer.MAX_VALUE && totalBytes < len) {