8276779: (ch) InputStream returned by Channels.newInputStream should have fast path for SelectableChannels

Reviewed-by: lancea, alanb
This commit is contained in:
Markus Karg 2021-12-04 09:27:23 +00:00 committed by Alan Bateman
parent 02ee337ae0
commit 9642629d15
3 changed files with 110 additions and 11 deletions

View file

@ -34,6 +34,7 @@ import java.nio.channels.IllegalBlockingModeException;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.SeekableByteChannel;
import java.nio.channels.SelectableChannel;
import java.nio.channels.WritableByteChannel;
import java.util.Arrays;
import java.util.Objects;
import jdk.internal.util.ArraysSupport;
@ -238,15 +239,28 @@ public class ChannelInputStream
Objects.requireNonNull(out, "out");
if (out instanceof ChannelOutputStream cos
&& ch instanceof FileChannel fc
&& cos.channel() instanceof FileChannel dst) {
return transfer(fc, dst);
&& ch instanceof FileChannel fc) {
WritableByteChannel wbc = cos.channel();
if (wbc instanceof FileChannel dst) {
return transfer(fc, dst);
}
if (wbc instanceof SelectableChannel sc) {
synchronized (sc.blockingLock()) {
if (!sc.isBlocking())
throw new IllegalBlockingModeException();
return transfer(fc, wbc);
}
}
return transfer(fc, wbc);
}
return super.transferTo(out);
}
private static long transfer(FileChannel src, FileChannel dst) throws IOException {
private static long transfer(FileChannel src, WritableByteChannel dst) throws IOException {
long initialPos = src.position();
long pos = initialPos;
try {