Author: fanningpj
Date: Fri Mar 11 21:45:02 2022
New Revision: 1898862

URL: http://svn.apache.org/viewvc?rev=1898862&view=rev
Log:
fix issue in IOUtils.toByteArrayWithMaxLength

Modified:
    poi/trunk/poi/src/main/java/org/apache/poi/util/IOUtils.java
    poi/trunk/poi/src/test/java/org/apache/poi/util/TestIOUtils.java

Modified: poi/trunk/poi/src/main/java/org/apache/poi/util/IOUtils.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/poi/src/main/java/org/apache/poi/util/IOUtils.java?rev=1898862&r1=1898861&r2=1898862&view=diff
==============================================================================
--- poi/trunk/poi/src/main/java/org/apache/poi/util/IOUtils.java (original)
+++ poi/trunk/poi/src/main/java/org/apache/poi/util/IOUtils.java Fri Mar 11 
21:45:02 2022
@@ -216,12 +216,14 @@ public final class IOUtils {
                 checkByteSizeLimit(totalBytes);
             } while (totalBytes < derivedLen && readBytes > -1);
 
-            if (derivedMaxLength != Integer.MAX_VALUE && totalBytes == 
derivedMaxLength) {
-                throw new IOException("MaxLength (" + derivedMaxLength + ") 
reached - stream seems to be invalid.");
-            }
+            if (checkEOFException) {
+                if (derivedMaxLength != Integer.MAX_VALUE && totalBytes == 
derivedMaxLength) {
+                    throw new IOException("MaxLength (" + derivedMaxLength + 
") reached - stream seems to be invalid.");
+                }
 
-            if (checkEOFException && derivedLen != Integer.MAX_VALUE && 
totalBytes < derivedLen) {
-                throw new EOFException("unexpected EOF - expected len: " + 
derivedLen + " - actual len: " + totalBytes);
+                if (derivedLen != Integer.MAX_VALUE && totalBytes < 
derivedLen) {
+                    throw new EOFException("unexpected EOF - expected len: " + 
derivedLen + " - actual len: " + totalBytes);
+                }
             }
 
             return baos.toByteArray();

Modified: poi/trunk/poi/src/test/java/org/apache/poi/util/TestIOUtils.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/poi/src/test/java/org/apache/poi/util/TestIOUtils.java?rev=1898862&r1=1898861&r2=1898862&view=diff
==============================================================================
--- poi/trunk/poi/src/test/java/org/apache/poi/util/TestIOUtils.java (original)
+++ poi/trunk/poi/src/test/java/org/apache/poi/util/TestIOUtils.java Fri Mar 11 
21:45:02 2022
@@ -144,6 +144,30 @@ final class TestIOUtils {
     }
 
     @Test
+    void testToByteArrayMaxLength() throws IOException {
+        final byte[] array = new byte[]{1, 2, 3, 4, 5, 6, 7};
+        try (ByteArrayInputStream is = new ByteArrayInputStream(array)) {
+            assertArrayEquals(array, IOUtils.toByteArrayWithMaxLength(is, 7));
+        }
+    }
+
+    @Test
+    void testToByteArrayMaxLengthLongerThanArray() throws IOException {
+        final byte[] array = new byte[]{1, 2, 3, 4, 5, 6, 7};
+        try (ByteArrayInputStream is = new ByteArrayInputStream(array)) {
+            assertArrayEquals(array, IOUtils.toByteArrayWithMaxLength(is, 8));
+        }
+    }
+
+    @Test
+    void testToByteArrayMaxLengthShorterThanArray() throws IOException {
+        final byte[] array = new byte[]{1, 2, 3, 4, 5, 6, 7};
+        try (ByteArrayInputStream is = new ByteArrayInputStream(array)) {
+            assertArrayEquals(new byte[]{1, 2, 3}, 
IOUtils.toByteArrayWithMaxLength(is, 3));
+        }
+    }
+
+    @Test
     void testSkipFully() throws IOException {
         try (InputStream is =  new FileInputStream(TMP)) {
             long skipped = IOUtils.skipFully(is, 20000L);



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

Reply via email to