8322141: SequenceInputStream.transferTo should not return as soon as Long.MAX_VALUE bytes have been transferred

Reviewed-by: vsitnikov, bpb, jpai
This commit is contained in:
Markus KARG 2023-12-20 17:00:44 +00:00 committed by Brian Burkhalter
parent e0bad5153b
commit 2d609557ff
2 changed files with 50 additions and 2 deletions

View file

@ -242,11 +242,14 @@ public class SequenceInputStream extends InputStream {
if (getClass() == SequenceInputStream.class) {
long transferred = 0;
while (in != null) {
long numTransferred = in.transferTo(out);
// increment the total transferred byte count
// only if we haven't already reached the Long.MAX_VALUE
if (transferred < Long.MAX_VALUE) {
try {
transferred = Math.addExact(transferred, in.transferTo(out));
transferred = Math.addExact(transferred, numTransferred);
} catch (ArithmeticException ignore) {
return Long.MAX_VALUE;
transferred = Long.MAX_VALUE;
}
}
nextStream();