checkLength(length, derivedMaxLength);
}
- final int derivedLen = Math.min(length, derivedMaxLength);
+ final int derivedLen = isLengthKnown ? Math.min(length, derivedMaxLength) : derivedMaxLength;
final int byteArrayInitLen = calculateByteArrayInitLength(isLengthKnown, length, derivedMaxLength);
final int internalBufferLen = DEFAULT_BUFFER_SIZE;
try (UnsynchronizedByteArrayOutputStream baos = new UnsynchronizedByteArrayOutputStream(byteArrayInitLen)) {
if (readBytes > 0) {
baos.write(buffer, 0, readBytes);
}
-
checkByteSizeLimit(totalBytes);
} while (totalBytes < derivedLen && readBytes > -1);
- if (checkEOFException) {
- if (derivedMaxLength != Integer.MAX_VALUE && totalBytes == derivedMaxLength) {
- throw new IOException("MaxLength (" + derivedMaxLength + ") reached - stream seems to be invalid.");
- }
+ if (BYTE_ARRAY_MAX_OVERRIDE < 0 && readBytes > -1 && !isLengthKnown && stream.read() >= 0) {
+ throwRecordTruncationException(derivedMaxLength);
+ }
- if (derivedLen != Integer.MAX_VALUE && totalBytes < derivedLen) {
- throw new EOFException("unexpected EOF - expected len: " + derivedLen + " - actual len: " + totalBytes);
- }
+ if (checkEOFException && derivedLen != Integer.MAX_VALUE && totalBytes < derivedLen) {
+ throw new EOFException("unexpected EOF - expected len: " + derivedLen + " - actual len: " + totalBytes);
}
return baos.toByteArray();
* Simple utility function to check that you haven't hit EOF
* when reading a byte.
*
- * @param is inputstream to read
+ * @param is input stream to read
* @return byte read, unless
* @throws IOException on IOException or EOF if -1 is read
*/
length, maxLength));
}
+
+ private static void throwRecordTruncationException(final int maxLength) {
+ throw new RecordFormatException(String.format(Locale.ROOT, "Tried to read data but the maximum length " +
+ "for this record type is %,d.\n" +
+ "If the file is not corrupt or large, please open an issue on bugzilla to request \n" +
+ "increasing the maximum allowable size for this record type.\n"+
+ "As a temporary workaround, consider setting a higher override value with " +
+ "IOUtils.setByteArrayMaxOverride()", maxLength));
+
+ }
}
}
}
+ @Test
+ void testToByteArrayMaxLengthWithByteArrayInitLenShort() throws IOException {
+ final byte[] array = new byte[]{1, 2, 3, 4, 5, 6, 7};
+ IOUtils.setMaxByteArrayInitSize(2);
+ try (ByteArrayInputStream is = new ByteArrayInputStream(array)) {
+ assertEquals(2, IOUtils.getMaxByteArrayInitSize());
+ assertArrayEquals(array, IOUtils.toByteArrayWithMaxLength(is, 7));
+ } finally {
+ IOUtils.setMaxByteArrayInitSize(-1);
+ }
+ }
+
+ @Test
+ void testToByteArrayMaxLengthWithByteArrayInitLenLong() throws IOException {
+ final byte[] array = new byte[]{1, 2, 3, 4, 5, 6, 7};
+ IOUtils.setMaxByteArrayInitSize(8192);
+ try (ByteArrayInputStream is = new ByteArrayInputStream(array)) {
+ assertEquals(8192, IOUtils.getMaxByteArrayInitSize());
+ assertArrayEquals(array, IOUtils.toByteArrayWithMaxLength(is, 7));
+ } finally {
+ IOUtils.setMaxByteArrayInitSize(-1);
+ }
+ }
+
@Test
void testToByteArrayMaxLengthLongerThanArray() throws IOException {
final byte[] array = new byte[]{1, 2, 3, 4, 5, 6, 7};
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));
+ assertThrows(RecordFormatException.class, () -> IOUtils.toByteArrayWithMaxLength(is, 3));
+ }
+ }
+
+ @Test
+ void testToByteArrayMaxLengthShorterThanArrayWithByteArrayOverride() throws IOException {
+ final byte[] array = new byte[]{1, 2, 3, 4, 5, 6, 7};
+ IOUtils.setByteArrayMaxOverride(30 * 1024 * 1024);
+ try (ByteArrayInputStream is = new ByteArrayInputStream(array)) {
+ assertArrayEquals(array, IOUtils.toByteArrayWithMaxLength(is, 3));
+ } finally {
+ IOUtils.setByteArrayMaxOverride(-1);
}
}