This is an automated email from the ASF dual-hosted git repository.

pjfanning pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/poi.git


The following commit(s) were added to refs/heads/trunk by this push:
     new f1f9f45e12 new IOUtils methods
f1f9f45e12 is described below

commit f1f9f45e12baeca9a0780d1deaea2e94e9e412c8
Author: PJ Fanning <[email protected]>
AuthorDate: Fri Jun 5 17:06:03 2026 +0100

    new IOUtils methods
---
 .../poi/openxml4j/util/ZipArchiveFakeEntry.java    |  2 +-
 .../apache/poi/hslf/record/CurrentUserAtom.java    |  3 +-
 .../apache/poi/poifs/filesystem/Ole10Native.java   |  2 +-
 poi/src/main/java/org/apache/poi/util/IOUtils.java | 67 +++++++++++++++++++---
 4 files changed, 64 insertions(+), 10 deletions(-)

diff --git 
a/poi-ooxml/src/main/java/org/apache/poi/openxml4j/util/ZipArchiveFakeEntry.java
 
b/poi-ooxml/src/main/java/org/apache/poi/openxml4j/util/ZipArchiveFakeEntry.java
index fc33f072fa..cb2cd2996a 100644
--- 
a/poi-ooxml/src/main/java/org/apache/poi/openxml4j/util/ZipArchiveFakeEntry.java
+++ 
b/poi-ooxml/src/main/java/org/apache/poi/openxml4j/util/ZipArchiveFakeEntry.java
@@ -90,7 +90,7 @@ public final class ZipArchiveFakeEntry extends 
ZipArchiveEntry implements Closea
 
             // Grab the de-compressed contents for later
             data = (entrySize == -1) ? IOUtils.toByteArrayWithMaxLength(inp, 
getMaxEntrySize()) :
-                    IOUtils.toByteArray(inp, entrySize, getMaxEntrySize());
+                    IOUtils.toByteArray(inp, entrySize, getMaxEntrySize(), 
"ZipArchiveFakeEntry.setMaxEntrySize()");
         }
     }
 
diff --git 
a/poi-scratchpad/src/main/java/org/apache/poi/hslf/record/CurrentUserAtom.java 
b/poi-scratchpad/src/main/java/org/apache/poi/hslf/record/CurrentUserAtom.java
index 4644366a6b..abf3ac4f1f 100644
--- 
a/poi-scratchpad/src/main/java/org/apache/poi/hslf/record/CurrentUserAtom.java
+++ 
b/poi-scratchpad/src/main/java/org/apache/poi/hslf/record/CurrentUserAtom.java
@@ -134,7 +134,8 @@ public class CurrentUserAtom {
 
         // Grab the contents
         try (InputStream in = dir.createDocumentInputStream("Current User")) {
-            _contents = IOUtils.toByteArray(in, docProps.getSize(), 
RecordAtom.getMaxRecordLength());
+            _contents = IOUtils.toByteArray(in, docProps.getSize(), 
RecordAtom.getMaxRecordLength(),
+                    "RecordAtom.setMaxRecordLength()");
         }
 
         // See how long it is. If it's under 28 bytes long, we can't
diff --git a/poi/src/main/java/org/apache/poi/poifs/filesystem/Ole10Native.java 
b/poi/src/main/java/org/apache/poi/poifs/filesystem/Ole10Native.java
index d59b129128..b933d7c2fc 100644
--- a/poi/src/main/java/org/apache/poi/poifs/filesystem/Ole10Native.java
+++ b/poi/src/main/java/org/apache/poi/poifs/filesystem/Ole10Native.java
@@ -291,7 +291,7 @@ public class Ole10Native {
     private static String readUtf16(LittleEndianByteArrayInputStream leis) 
throws IOException {
         final int size = leis.readInt();
         final long arraySize = 2L * size;
-        final byte[] buf = IOUtils.toByteArray(leis, arraySize, 
MAX_STRING_LENGTH);
+        final byte[] buf = IOUtils.toByteArray(leis, arraySize, 
MAX_STRING_LENGTH, "Ole10Native.setMaxStringLength()");
         return StringUtil.getFromUnicodeLE(buf, 0, size);
     }
 
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 733bcb1c56..97782e7ffe 100644
--- a/poi/src/main/java/org/apache/poi/util/IOUtils.java
+++ b/poi/src/main/java/org/apache/poi/util/IOUtils.java
@@ -197,7 +197,6 @@ public final class IOUtils {
         return toByteArray(stream, length, Integer.MAX_VALUE);
     }
 
-
     /**
      * Reads up to {@code length} bytes from the input stream, and returns the 
bytes read.
      *
@@ -213,7 +212,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, length != 
Integer.MAX_VALUE);
+        return toByteArray(stream, length, maxLength, true, length != 
Integer.MAX_VALUE, null);
     }
 
     /**
@@ -232,9 +231,54 @@ public final class IOUtils {
      * @since 5.4.1
      */
     public static byte[] toByteArray(InputStream stream, final long length, 
final int maxLength) throws IOException {
-        globalLengthChecks(length);
+        checkLengthIsAnInt(length);
         return toByteArray(stream, Math.toIntExact(length),
-                maxLength, true, length != Integer.MAX_VALUE);
+                maxLength, true, length != Integer.MAX_VALUE, null);
+    }
+
+    /**
+     * Reads up to {@code length} bytes from the input stream, and returns the 
bytes read.
+     *
+     * @param stream The byte stream of data to read.
+     * @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 - 
if {@link #setByteArrayMaxOverride(int)} is
+     *                  set then that max of that value and this maxLength is 
used
+     * @param limitMethod name of method that can be used to change the max 
length
+     * @return A byte array with the read bytes.
+     * @throws IOException If reading data fails or EOF is encountered too 
early for the given length.
+     * @throws RecordFormatException If the requested length is invalid.
+     * @since 6.0.0
+     */
+    public static byte[] toByteArray(final InputStream stream, final int 
length, final int maxLength,
+                                     final String limitMethod) throws 
IOException {
+        return toByteArray(stream, length,
+                maxLength, true, length != Integer.MAX_VALUE, limitMethod);
+    }
+
+    /**
+     * Reads up to {@code length} bytes from the input stream, and returns the 
bytes read.
+     *
+     * @param stream The byte stream of data to read.
+     * @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 - 
if {@link #setByteArrayMaxOverride(int)} is
+     *                  set then that max of that value and this maxLength is 
used
+     * @param limitMethod name of method that can be used to change the max 
length
+     * @return A byte array with the read bytes.
+     * @throws IOException If reading data fails or EOF is encountered too 
early for the given length.
+     * @throws RecordFormatException If the requested length is invalid.
+     * @since 6.0.0
+     */
+    public static byte[] toByteArray(final InputStream stream, final long 
length, final int maxLength,
+                                     final String limitMethod) throws 
IOException {
+        checkLengthIsAnInt(length);
+        return toByteArray(stream, Math.toIntExact(length),
+                maxLength, true, length != Integer.MAX_VALUE, limitMethod);
     }
 
     /**
@@ -251,14 +295,19 @@ public final class IOUtils {
      * @since 5.2.1
      */
     public static byte[] toByteArrayWithMaxLength(InputStream stream, final 
int maxLength) throws IOException {
-        return toByteArray(stream, maxLength, maxLength, false, false);
+        return toByteArray(stream, maxLength, maxLength, false, false, null);
     }
 
     private static byte[] toByteArray(InputStream stream, final int length, 
final int maxLength,
-                                      final boolean checkEOFException, final 
boolean isLengthKnown) throws IOException {
+                                      final boolean checkEOFException, final 
boolean isLengthKnown,
+                                      final String limitMethod) throws 
IOException {
         final int derivedMaxLength = Math.max(maxLength, 
BYTE_ARRAY_MAX_OVERRIDE);
         if ((length != Integer.MAX_VALUE) || (derivedMaxLength != 
Integer.MAX_VALUE)) {
-            checkLength(length, derivedMaxLength);
+            if (limitMethod != null) {
+                checkLength(length, derivedMaxLength, limitMethod);
+            } else {
+                checkLength(length, derivedMaxLength);
+            }
         }
 
         final int derivedLen = isLengthKnown && length >= 0 ? Math.min(length, 
derivedMaxLength) : derivedMaxLength;
@@ -732,6 +781,10 @@ public final class IOUtils {
                     "Tried to allocate an array with negative length; %d was 
requested.",
                     length));
         }
+        checkLengthIsAnInt(length);
+    }
+
+    static void checkLengthIsAnInt(long length) {
         if (length > (long)Integer.MAX_VALUE) {
             throw new RecordFormatException(String.format(Locale.ROOT,
                     "Tried to allocate an array with length greater than max 
allowed int (%d); %d was requested.",


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to