8294696: BufferedInputStream.transferTo should drain buffer when mark set

Reviewed-by: bpb, alanb
This commit is contained in:
Markus Karg 2022-11-04 21:07:59 +00:00 committed by Brian Burkhalter
parent d8573b2c5b
commit 581133a0c8

View File

@ -25,6 +25,7 @@
package java.io;
import java.util.Arrays;
import java.util.Objects;
import jdk.internal.misc.InternalLock;
@ -605,9 +606,15 @@ public class BufferedInputStream extends FilterInputStream {
}
private long implTransferTo(OutputStream out) throws IOException {
if (getClass() == BufferedInputStream.class
&& ((count - pos) <= 0) && (markpos == -1)) {
return getInIfOpen().transferTo(out);
if (getClass() == BufferedInputStream.class && markpos == -1) {
int avail = count - pos;
if (avail > 0) {
// Prevent poisoning and leaking of buf
byte[] buffer = Arrays.copyOfRange(getBufIfOpen(), pos, count);
out.write(buffer);
pos = count;
}
return avail + getInIfOpen().transferTo(out);
} else {
return super.transferTo(out);
}