]> source.dussan.org Git - poi.git/commitdiff
fix issue in IOUtils.toByteArrayWithMaxLength
authorPJ Fanning <fanningpj@apache.org>
Fri, 11 Mar 2022 21:45:02 +0000 (21:45 +0000)
committerPJ Fanning <fanningpj@apache.org>
Fri, 11 Mar 2022 21:45:02 +0000 (21:45 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1898862 13f79535-47bb-0310-9956-ffa450edef68

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

index f0dbc1aa99ebf595ae2bc60f32fd848d062abe94..e9c83a8f631ae372466b5b2620b17999623da341 100644 (file)
@@ -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();
index 01ed21ecbf2f5fcad3ac907cf98f6c86d09092cf..b585a24fd38a8e38633e2de1d13c7f05ca70df12 100644 (file)
@@ -143,6 +143,30 @@ final class TestIOUtils {
                 IOUtils.toByteArray(ByteBuffer.wrap(new byte[]{1, 2, 3, 4, 5, 6, 7}), 3));
     }
 
+    @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)) {