aboutsummaryrefslogtreecommitdiffstats
path: root/poi/src
diff options
context:
space:
mode:
authorPJ Fanning <fanningpj@apache.org>2022-02-19 22:34:44 +0000
committerPJ Fanning <fanningpj@apache.org>2022-02-19 22:34:44 +0000
commit699d71e5245df9e13299081f13e240214e690159 (patch)
tree9574bacb9b1d94765308c02facb3d8226a75ab5b /poi/src
parent7005a44cc72f07564a197769fd3593f4e86b131f (diff)
downloadpoi-699d71e5245df9e13299081f13e240214e690159.tar.gz
poi-699d71e5245df9e13299081f13e240214e690159.zip
add extra max size config
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1898238 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'poi/src')
-rw-r--r--poi/src/main/java/org/apache/poi/util/IOUtils.java39
-rw-r--r--poi/src/test/java9/module-info.classbin4198 -> 4198 bytes
2 files changed, 39 insertions, 0 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 f220d34665..b538276e3b 100644
--- a/poi/src/main/java/org/apache/poi/util/IOUtils.java
+++ b/poi/src/main/java/org/apache/poi/util/IOUtils.java
@@ -202,6 +202,45 @@ public final class IOUtils {
}
}
+ /**
+ * Reads the input stream, and returns the bytes read.
+ *
+ * @param stream The byte stream of data to read.
+ * @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 - 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.
+ * @since POI 5.2.1
+ */
+ public static byte[] toByteArrayWithMaxLength(InputStream stream, final int maxLength) throws IOException {
+ if (maxLength < 0L) {
+ throw new RecordFormatException("Can't allocate an array of length < 0");
+ }
+ final int derivedMaxLength = BYTE_ARRAY_MAX_OVERRIDE <= 0 ? maxLength : Math.max(maxLength, BYTE_ARRAY_MAX_OVERRIDE);
+
+ try (UnsynchronizedByteArrayOutputStream baos = new UnsynchronizedByteArrayOutputStream(derivedMaxLength == Integer.MAX_VALUE ? 4096 : derivedMaxLength)) {
+ byte[] buffer = new byte[4096];
+ int totalBytes = 0, readBytes;
+ do {
+ readBytes = stream.read(buffer, 0, Math.min(buffer.length, derivedMaxLength - totalBytes));
+ totalBytes += Math.max(readBytes, 0);
+ if (readBytes > 0) {
+ baos.write(buffer, 0, readBytes);
+ }
+
+ checkByteSizeLimit(totalBytes);
+ } while (totalBytes < derivedMaxLength && readBytes > -1);
+
+ if (derivedMaxLength != Integer.MAX_VALUE && totalBytes == derivedMaxLength) {
+ throw new IOException("MaxLength (" + derivedMaxLength + ") reached - stream seems to be invalid.");
+ }
+
+ return baos.toByteArray();
+ }
+ }
+
private static void checkLength(long length, int maxLength) {
if (BYTE_ARRAY_MAX_OVERRIDE > 0) {
if (length > BYTE_ARRAY_MAX_OVERRIDE) {
diff --git a/poi/src/test/java9/module-info.class b/poi/src/test/java9/module-info.class
index 5f441458ec..1b7e89aff8 100644
--- a/poi/src/test/java9/module-info.class
+++ b/poi/src/test/java9/module-info.class
Binary files differ