8273038: ChannelInputStream.transferTo() uses FileChannel.transferTo(FileChannel)

Reviewed-by: alanb
This commit is contained in:
Markus Karg 2021-09-08 11:22:47 +00:00 committed by Alan Bateman
parent 6750c34c92
commit 185557423d
4 changed files with 388 additions and 80 deletions

View File

@ -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;
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2001, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2001, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -142,4 +142,29 @@ public class ChannelInputStream
ch.close();
}
@Override
public long transferTo(OutputStream out) throws IOException {
Objects.requireNonNull(out, "out");
if (out instanceof ChannelOutputStream cos
&& ch instanceof FileChannel fc
&& cos.channel() instanceof FileChannel dst) {
return transfer(fc, dst);
}
return super.transferTo(out);
}
private static long transfer(FileChannel src, FileChannel dst) throws IOException {
long initialPos = src.position();
long pos = initialPos;
try {
while (pos < src.size()) {
pos += src.transferTo(pos, Long.MAX_VALUE, dst);
}
} finally {
src.position(pos);
}
return pos - initialPos;
}
}

View File

@ -0,0 +1,130 @@
/*
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package sun.nio.ch;
import java.io.*;
import java.nio.*;
import java.nio.channels.*;
import java.nio.channels.spi.*;
import java.util.Objects;
/**
* This class is defined here rather than in java.nio.channels.Channels
* so that it will be accessible from java.nio.channels.Channels and
* sun.nio.ch.ChannelInputStream.
*
*
* @author Mark Reinhold
* @author Mike McCloskey
* @author JSR-51 Expert Group
* @since 18
*/
public class ChannelOutputStream extends OutputStream {
private final WritableByteChannel ch;
private ByteBuffer bb;
private byte[] bs; // Invoker's previous array
private byte[] b1;
/**
* 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);
}
}
/**
* @param ch The channel wrapped by this stream.
*/
public ChannelOutputStream(WritableByteChannel ch) {
this.ch = ch;
}
/**
* @return The channel wrapped by this stream.
*/
WritableByteChannel channel() {
return ch;
}
@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 {
Objects.checkFromIndexSize(off, len, bs.length);
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;
writeFully(ch, bb);
}
@Override
public void close() throws IOException {
ch.close();
}
}

View File

@ -0,0 +1,228 @@
/*
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
import static java.lang.String.format;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.Arrays;
import java.util.Random;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Consumer;
import java.util.function.Supplier;
import jdk.test.lib.RandomFactory;
/*
* @test
* @library /test/lib
* @build jdk.test.lib.RandomFactory
* @run main TransferTo
* @bug 8265891
* @summary tests whether sun.nio.ChannelInputStream.transferTo conforms to the
* InputStream.transferTo contract defined in the javadoc
* @key randomness
*/
public class TransferTo {
private static final int MIN_SIZE = 10_000;
private static final int MAX_SIZE_INCR = 100_000_000 - MIN_SIZE;
private static final int ITERATIONS = 10;
private static final Random RND = RandomFactory.getRandom();
public static void main(String[] args) throws Exception {
test(fileChannelInput(), fileChannelOutput());
test(readableByteChannelInput(), defaultOutput());
}
private static void test(InputStreamProvider inputStreamProvider, OutputStreamProvider outputStreamProvider)
throws Exception {
testNullPointerException(inputStreamProvider);
testStreamContents(inputStreamProvider, outputStreamProvider);
}
private static void testNullPointerException(InputStreamProvider inputStreamProvider) throws Exception {
try (InputStream in = inputStreamProvider.input()) {
assertThrowsNPE(() -> in.transferTo(null), "out");
}
try (InputStream in = inputStreamProvider.input((byte) 1)) {
assertThrowsNPE(() -> in.transferTo(null), "out");
}
try (InputStream in = inputStreamProvider.input((byte) 1, (byte) 2)) {
assertThrowsNPE(() -> in.transferTo(null), "out");
}
}
private static void testStreamContents(InputStreamProvider inputStreamProvider,
OutputStreamProvider outputStreamProvider) throws Exception {
checkTransferredContents(inputStreamProvider, outputStreamProvider, new byte[0]);
checkTransferredContents(inputStreamProvider, outputStreamProvider, createRandomBytes(1024, 4096));
// to span through several batches
checkTransferredContents(inputStreamProvider, outputStreamProvider, createRandomBytes(16384, 16384));
// randomly chosen starting positions within source and target
for (int i = 0; i < ITERATIONS; i++) {
byte[] inBytes = createRandomBytes(MIN_SIZE, MAX_SIZE_INCR);
int posIn = RND.nextInt(inBytes.length);
int posOut = RND.nextInt(MIN_SIZE);
checkTransferredContents(inputStreamProvider, outputStreamProvider, inBytes, posIn, posOut);
}
// beyond source EOF
checkTransferredContents(inputStreamProvider, outputStreamProvider, createRandomBytes(4096, 0), 4096, 0);
// beyond target EOF
checkTransferredContents(inputStreamProvider, outputStreamProvider, createRandomBytes(4096, 0), 0, 4096);
}
private static void checkTransferredContents(InputStreamProvider inputStreamProvider,
OutputStreamProvider outputStreamProvider, byte[] inBytes) throws Exception {
checkTransferredContents(inputStreamProvider, outputStreamProvider, inBytes, 0, 0);
}
private static void checkTransferredContents(InputStreamProvider inputStreamProvider,
OutputStreamProvider outputStreamProvider, byte[] inBytes, int posIn, int posOut) throws Exception {
AtomicReference<Supplier<byte[]>> recorder = new AtomicReference<>();
try (InputStream in = inputStreamProvider.input(inBytes);
OutputStream out = outputStreamProvider.output(recorder::set)) {
// skip bytes till starting position
in.skipNBytes(posIn);
out.write(new byte[posOut]);
long reported = in.transferTo(out);
int count = inBytes.length - posIn;
if (reported != count)
throw new AssertionError(
format("reported %d bytes but should report %d", reported, count));
byte[] outBytes = recorder.get().get();
if (!Arrays.equals(inBytes, posIn, posIn + count, outBytes, posOut, posOut + count))
throw new AssertionError(
format("inBytes.length=%d, outBytes.length=%d", count, outBytes.length));
}
}
private static byte[] createRandomBytes(int min, int maxRandomAdditive) {
byte[] bytes = new byte[min + (maxRandomAdditive == 0 ? 0 : RND.nextInt(maxRandomAdditive))];
RND.nextBytes(bytes);
return bytes;
}
private interface InputStreamProvider {
InputStream input(byte... bytes) throws Exception;
}
private interface OutputStreamProvider {
OutputStream output(Consumer<Supplier<byte[]>> spy) throws Exception;
}
private static OutputStreamProvider defaultOutput() {
return new OutputStreamProvider() {
@Override
public OutputStream output(Consumer<Supplier<byte[]>> spy) {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
spy.accept(outputStream::toByteArray);
return outputStream;
}
};
}
private static InputStreamProvider fileChannelInput() {
return new InputStreamProvider() {
@Override
public InputStream input(byte... bytes) throws Exception {
Path path = Files.createTempFile(null, null);
Files.write(path, bytes);
FileChannel fileChannel = FileChannel.open(path);
return Channels.newInputStream(fileChannel);
}
};
}
private static InputStreamProvider readableByteChannelInput() {
return new InputStreamProvider() {
@Override
public InputStream input(byte... bytes) throws Exception {
return Channels.newInputStream(Channels.newChannel(new ByteArrayInputStream(bytes)));
}
};
}
private static OutputStreamProvider fileChannelOutput() {
return new OutputStreamProvider() {
public OutputStream output(Consumer<Supplier<byte[]>> spy) throws Exception {
Path path = Files.createTempFile(null, null);
FileChannel fileChannel = FileChannel.open(path, StandardOpenOption.WRITE);
spy.accept(() -> {
try {
return Files.readAllBytes(path);
} catch (IOException e) {
throw new AssertionError("Failed to verify output file", e);
}
});
return Channels.newOutputStream(fileChannel);
}
};
}
public interface Thrower {
public void run() throws Throwable;
}
public static void assertThrowsNPE(Thrower thrower, String message) {
assertThrows(thrower, NullPointerException.class, message);
}
public static <T extends Throwable> void assertThrows(Thrower thrower, Class<T> throwable, String message) {
Throwable thrown;
try {
thrower.run();
thrown = null;
} catch (Throwable caught) {
thrown = caught;
}
if (!throwable.isInstance(thrown)) {
String caught = thrown == null ? "nothing" : thrown.getClass().getCanonicalName();
throw new AssertionError(format("Expected to catch %s, but caught %s", throwable, caught), thrown);
}
if (thrown != null && !message.equals(thrown.getMessage())) {
throw new AssertionError(
format("Expected exception message to be '%s', but it's '%s'", message, thrown.getMessage()));
}
}
}