aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPJ Fanning <fanningpj@apache.org>2022-03-11 21:45:02 +0000
committerPJ Fanning <fanningpj@apache.org>2022-03-11 21:45:02 +0000
commit5b5f286a301c7e8f9cc1b5fe5d4abce279388ff3 (patch)
tree9f6f517d333daa756df16138f3191dab72d37f98
parentf4bfcaeec9c08e1a6ef7bf3ffcb8c3085dd92b7b (diff)
downloadpoi-5b5f286a301c7e8f9cc1b5fe5d4abce279388ff3.tar.gz
poi-5b5f286a301c7e8f9cc1b5fe5d4abce279388ff3.zip
fix issue in IOUtils.toByteArrayWithMaxLength
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1898862 13f79535-47bb-0310-9956-ffa450edef68
-rw-r--r--poi/src/main/java/org/apache/poi/util/IOUtils.java12
-rw-r--r--poi/src/test/java/org/apache/poi/util/TestIOUtils.java24
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);