mirror of
https://github.com/openjdk/jdk.git
synced 2025-08-28 07:14:30 +02:00
8276779: (ch) InputStream returned by Channels.newInputStream should have fast path for SelectableChannels
Reviewed-by: lancea, alanb
This commit is contained in:
parent
02ee337ae0
commit
9642629d15
3 changed files with 110 additions and 11 deletions
|
@ -69,9 +69,12 @@ public final class Channels {
|
|||
/**
|
||||
* Constructs a stream that reads bytes from the given channel.
|
||||
*
|
||||
* <p> The {@code read} methods of the resulting stream will throw an
|
||||
* {@link IllegalBlockingModeException} if invoked while the underlying
|
||||
* channel is in non-blocking mode. The stream will not be buffered, and
|
||||
* <p> The {@code read} and {@code transferTo} methods of the resulting stream
|
||||
* will throw an {@link IllegalBlockingModeException} if invoked while the
|
||||
* underlying channel is in non-blocking mode. The {@code transferTo} method
|
||||
* will also throw an {@code IllegalBlockingModeException} if invoked to
|
||||
* transfer bytes to an output stream that writes to an underlying channel in
|
||||
* non-blocking mode. The stream will not be buffered, and
|
||||
* it will not support the {@link InputStream#mark mark} or {@link
|
||||
* InputStream#reset reset} methods. The stream will be safe for access by
|
||||
* multiple concurrent threads. Closing the stream will in turn cause the
|
||||
|
|
|
@ -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 {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue