aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPJ Fanning <fanningpj@apache.org>2022-03-11 17:03:36 +0000
committerPJ Fanning <fanningpj@apache.org>2022-03-11 17:03:36 +0000
commite7f9cd277d400e1a726d4b5acfe5c9ddf21a4eee (patch)
tree0783b040564d55c23d20423fa4a8a3d7430817d7
parent85356f1d54346fe3465d6249d0c95e2876640c5f (diff)
downloadpoi-e7f9cd277d400e1a726d4b5acfe5c9ddf21a4eee.tar.gz
poi-e7f9cd277d400e1a726d4b5acfe5c9ddf21a4eee.zip
fix issue in IOUtils.toByteArrayWithMaxLength
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1898856 13f79535-47bb-0310-9956-ffa450edef68
-rw-r--r--poi/src/main/java/org/apache/poi/util/IOUtils.java10
1 files changed, 5 insertions, 5 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 c47efc7044..37ad46846f 100644
--- a/poi/src/main/java/org/apache/poi/util/IOUtils.java
+++ b/poi/src/main/java/org/apache/poi/util/IOUtils.java
@@ -171,7 +171,7 @@ public final class IOUtils {
* @throws RecordFormatException If the requested length is invalid.
*/
public static byte[] toByteArray(InputStream stream, final int length, final int maxLength) throws IOException {
- return toByteArray(stream, length, maxLength, true);
+ return toByteArray(stream, length, maxLength, true, length != Integer.MAX_VALUE);
}
/**
@@ -188,11 +188,11 @@ public final class IOUtils {
* @since POI 5.2.1
*/
public static byte[] toByteArrayWithMaxLength(InputStream stream, final int maxLength) throws IOException {
- return toByteArray(stream, maxLength, maxLength, false);
+ return toByteArray(stream, maxLength, maxLength, false, false);
}
private static byte[] toByteArray(InputStream stream, final int length, final int maxLength,
- final boolean checkEOFException) throws IOException {
+ final boolean checkEOFException, final boolean isLengthKnown) throws IOException {
if (length < 0 || maxLength < 0) {
throw new RecordFormatException("Can't allocate an array of length < 0");
}
@@ -202,8 +202,8 @@ public final class IOUtils {
}
final int derivedLen = Math.min(length, derivedMaxLength);
- try (UnsynchronizedByteArrayOutputStream baos =
- new UnsynchronizedByteArrayOutputStream(derivedLen == Integer.MAX_VALUE ? 4096 : derivedLen)) {
+ final int bufferLen = isLengthKnown ? derivedLen : 4096;
+ try (UnsynchronizedByteArrayOutputStream baos = new UnsynchronizedByteArrayOutputStream(bufferLen)) {
byte[] buffer = new byte[4096];
int totalBytes = 0, readBytes;
do {