Browse Source

fix issue in IOUtils.toByteArrayWithMaxLength

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1898862 13f79535-47bb-0310-9956-ffa450edef68
tags/REL_5_2_2
PJ Fanning 2 years ago
parent
commit
5b5f286a30

+ 7
- 5
poi/src/main/java/org/apache/poi/util/IOUtils.java View 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();

+ 24
- 0
poi/src/test/java/org/apache/poi/util/TestIOUtils.java View 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)) {

Loading…
Cancel
Save