8272297: FileInputStream should override transferTo() for better performance

Reviewed-by: alanb
This commit is contained in:
Brian Burkhalter 2021-08-16 15:53:33 +00:00
parent 3677734584
commit 82688258f6
2 changed files with 148 additions and 0 deletions

View file

@ -354,6 +354,24 @@ public class FileInputStream extends InputStream
return (capacity == nread) ? buf : Arrays.copyOf(buf, nread);
}
/**
* {@inheritDoc}
*/
public long transferTo(OutputStream out) throws IOException {
long transferred = 0L;
if (out instanceof FileOutputStream fos) {
FileChannel fc = getChannel();
long pos = fc.position();
transferred = fc.transferTo(pos, Long.MAX_VALUE, fos.getChannel());
long newPos = pos + transferred;
fc.position(newPos);
if (newPos >= fc.size()) {
return transferred;
}
}
return transferred + super.transferTo(out);
}
private long length() throws IOException {
return length0();
}