Skip to content

Commit

Permalink
8294696: BufferedInputStream.transferTo should drain buffer when mark…
Browse files Browse the repository at this point in the history
… set

Reviewed-by: bpb, alanb
  • Loading branch information
mkarg authored and Brian Burkhalter committed Nov 4, 2022
1 parent d8573b2 commit 581133a
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/java.base/share/classes/java/io/BufferedInputStream.java
Expand Up @@ -25,6 +25,7 @@

package java.io;

import java.util.Arrays;
import java.util.Objects;

import jdk.internal.misc.InternalLock;
Expand Down Expand Up @@ -605,9 +606,15 @@ public long transferTo(OutputStream out) throws IOException {
}

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);
}
Expand Down

1 comment on commit 581133a

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.