mirror of
https://github.com/openjdk/jdk.git
synced 2025-08-27 14:54:52 +02:00
8273038: ChannelInputStream.transferTo() uses FileChannel.transferTo(FileChannel)
Reviewed-by: alanb
This commit is contained in:
parent
6750c34c92
commit
185557423d
4 changed files with 388 additions and 80 deletions
|
@ -41,6 +41,7 @@ import java.nio.channels.spi.AbstractInterruptibleChannel;
|
|||
import java.util.Objects;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import sun.nio.ch.ChannelInputStream;
|
||||
import sun.nio.ch.ChannelOutputStream;
|
||||
import sun.nio.cs.StreamDecoder;
|
||||
import sun.nio.cs.StreamEncoder;
|
||||
|
||||
|
@ -63,40 +64,6 @@ public final class Channels {
|
|||
|
||||
private Channels() { throw new Error("no instances"); }
|
||||
|
||||
/**
|
||||
* Write all remaining bytes in buffer to the given channel.
|
||||
* If the channel is selectable then it must be configured blocking.
|
||||
*/
|
||||
private static void writeFullyImpl(WritableByteChannel ch, ByteBuffer bb)
|
||||
throws IOException
|
||||
{
|
||||
while (bb.remaining() > 0) {
|
||||
int n = ch.write(bb);
|
||||
if (n <= 0)
|
||||
throw new RuntimeException("no bytes written");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Write all remaining bytes in buffer to the given channel.
|
||||
*
|
||||
* @throws IllegalBlockingModeException
|
||||
* If the channel is selectable and configured non-blocking.
|
||||
*/
|
||||
private static void writeFully(WritableByteChannel ch, ByteBuffer bb)
|
||||
throws IOException
|
||||
{
|
||||
if (ch instanceof SelectableChannel sc) {
|
||||
synchronized (sc.blockingLock()) {
|
||||
if (!sc.isBlocking())
|
||||
throw new IllegalBlockingModeException();
|
||||
writeFullyImpl(ch, bb);
|
||||
}
|
||||
} else {
|
||||
writeFullyImpl(ch, bb);
|
||||
}
|
||||
}
|
||||
|
||||
// -- Byte streams from channels --
|
||||
|
||||
/**
|
||||
|
@ -136,47 +103,7 @@ public final class Channels {
|
|||
*/
|
||||
public static OutputStream newOutputStream(WritableByteChannel ch) {
|
||||
Objects.requireNonNull(ch, "ch");
|
||||
|
||||
return new OutputStream() {
|
||||
|
||||
private ByteBuffer bb;
|
||||
private byte[] bs; // Invoker's previous array
|
||||
private byte[] b1;
|
||||
|
||||
@Override
|
||||
public synchronized void write(int b) throws IOException {
|
||||
if (b1 == null)
|
||||
b1 = new byte[1];
|
||||
b1[0] = (byte) b;
|
||||
this.write(b1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized void write(byte[] bs, int off, int len)
|
||||
throws IOException
|
||||
{
|
||||
if ((off < 0) || (off > bs.length) || (len < 0) ||
|
||||
((off + len) > bs.length) || ((off + len) < 0)) {
|
||||
throw new IndexOutOfBoundsException();
|
||||
} else if (len == 0) {
|
||||
return;
|
||||
}
|
||||
ByteBuffer bb = ((this.bs == bs)
|
||||
? this.bb
|
||||
: ByteBuffer.wrap(bs));
|
||||
bb.limit(Math.min(off + len, bb.capacity()));
|
||||
bb.position(off);
|
||||
this.bb = bb;
|
||||
this.bs = bs;
|
||||
Channels.writeFully(ch, bb);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
ch.close();
|
||||
}
|
||||
|
||||
};
|
||||
return new ChannelOutputStream(ch);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -216,10 +143,8 @@ public final class Channels {
|
|||
public synchronized int read(byte[] bs, int off, int len)
|
||||
throws IOException
|
||||
{
|
||||
if ((off < 0) || (off > bs.length) || (len < 0) ||
|
||||
((off + len) > bs.length) || ((off + len) < 0)) {
|
||||
throw new IndexOutOfBoundsException();
|
||||
} else if (len == 0) {
|
||||
Objects.checkFromIndexSize(off, len, bs.length);
|
||||
if (len == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue