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();
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)) {