diff options
-rw-r--r-- | poi/src/main/java/org/apache/poi/util/IOUtils.java | 12 | ||||
-rw-r--r-- | poi/src/test/java/org/apache/poi/util/TestIOUtils.java | 24 |
2 files changed, 31 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 f0dbc1aa99..e9c83a8f63 100644 --- a/poi/src/main/java/org/apache/poi/util/IOUtils.java +++ b/poi/src/main/java/org/apache/poi/util/IOUtils.java @@ -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(); diff --git a/poi/src/test/java/org/apache/poi/util/TestIOUtils.java b/poi/src/test/java/org/apache/poi/util/TestIOUtils.java index 01ed21ecbf..b585a24fd3 100644 --- a/poi/src/test/java/org/apache/poi/util/TestIOUtils.java +++ b/poi/src/test/java/org/apache/poi/util/TestIOUtils.java @@ -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); |