8316156: ByteArrayInputStream.transferTo causes MaxDirectMemorySize overflow

Reviewed-by: alanb
This commit is contained in:
Brian Burkhalter 2023-09-20 20:42:31 +00:00
parent 3461c7b165
commit 5cacf212f0
2 changed files with 83 additions and 2 deletions

View file

@ -44,6 +44,7 @@ import java.util.Objects;
* @since 1.0
*/
public class ByteArrayInputStream extends InputStream {
private static final int MAX_TRANSFER_SIZE = 128*1024;
/**
* An array of bytes that was provided
@ -205,8 +206,16 @@ public class ByteArrayInputStream extends InputStream {
@Override
public synchronized long transferTo(OutputStream out) throws IOException {
int len = count - pos;
out.write(buf, pos, len);
pos = count;
if (len > 0) {
int nwritten = 0;
while (nwritten < len) {
int nbyte = Integer.min(len - nwritten, MAX_TRANSFER_SIZE);
out.write(buf, pos, nbyte);
pos += nbyte;
nwritten += nbyte;
}
assert pos == count;
}
return len;
}