@@ -248,15 +248,12 @@ public static byte[] toByteArrayWithMaxLength(InputStream stream, final int maxL
248
248
249
249
private static byte [] toByteArray (InputStream stream , final int length , final int maxLength ,
250
250
final boolean checkEOFException , final boolean isLengthKnown ) throws IOException {
251
- if (length < 0 || maxLength < 0 ) {
252
- throw new RecordFormatException ("Can't allocate an array of length < 0" );
253
- }
254
251
final int derivedMaxLength = Math .max (maxLength , BYTE_ARRAY_MAX_OVERRIDE );
255
252
if ((length != Integer .MAX_VALUE ) || (derivedMaxLength != Integer .MAX_VALUE )) {
256
253
checkLength (length , derivedMaxLength );
257
254
}
258
255
259
- final int derivedLen = isLengthKnown ? Math .min (length , derivedMaxLength ) : derivedMaxLength ;
256
+ final int derivedLen = isLengthKnown && length >= 0 ? Math .min (length , derivedMaxLength ) : derivedMaxLength ;
260
257
final int byteArrayInitLen = calculateByteArrayInitLength (isLengthKnown , length , derivedMaxLength );
261
258
final int internalBufferLen = DEFAULT_BUFFER_SIZE ;
262
259
try (UnsynchronizedByteArrayOutputStream baos = UnsynchronizedByteArrayOutputStream .builder ().setBufferSize (byteArrayInitLen ).get ()) {
@@ -275,7 +272,7 @@ private static byte[] toByteArray(InputStream stream, final int length, final in
275
272
throwRecordTruncationException (derivedMaxLength );
276
273
}
277
274
278
- if (checkEOFException && derivedLen != Integer .MAX_VALUE && totalBytes < derivedLen ) {
275
+ if (checkEOFException && length >= 0 && derivedLen != Integer .MAX_VALUE && totalBytes < derivedLen ) {
279
276
throw new EOFException ("unexpected EOF - expected len: " + derivedLen + " - actual len: " + totalBytes );
280
277
}
281
278
0 commit comments