From c1d8c8a800371229e5db899378c434dd8ffd7e91 Mon Sep 17 00:00:00 2001 From: alanb Date: Wed, 15 Sep 2010 15:13:50 +0100 Subject: [PATCH] 6984545: (fc) transferFrom does not throw NonReadableChannelException when target is size 0 and non-readable Reviewed-by: forax --- .../classes/sun/nio/ch/FileChannelImpl.java | 2 ++ .../nio/channels/FileChannel/Transfer.java | 23 +++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/src/share/classes/sun/nio/ch/FileChannelImpl.java b/src/share/classes/sun/nio/ch/FileChannelImpl.java index 9a52fa455..208c36789 100644 --- a/src/share/classes/sun/nio/ch/FileChannelImpl.java +++ b/src/share/classes/sun/nio/ch/FileChannelImpl.java @@ -545,6 +545,8 @@ public class FileChannelImpl long position, long count) throws IOException { + if (!src.readable) + throw new NonReadableChannelException(); synchronized (src.positionLock) { long pos = src.position(); long max = Math.min(count, src.size() - pos); diff --git a/test/java/nio/channels/FileChannel/Transfer.java b/test/java/nio/channels/FileChannel/Transfer.java index a6f96b83e..51b774587 100644 --- a/test/java/nio/channels/FileChannel/Transfer.java +++ b/test/java/nio/channels/FileChannel/Transfer.java @@ -23,6 +23,7 @@ /* @test * @bug 4434723 4482726 4559072 4638365 4795550 5081340 5103988 6253145 + * 6984545 * @summary Test FileChannel.transferFrom and transferTo * @library .. */ @@ -55,6 +56,7 @@ public class Transfer { xferTest06(); // for bug 5081340 xferTest07(); // for bug 5103988 xferTest08(); // for bug 6253145 + xferTest09(); // for bug 6984545 } private static void testFileChannel() throws Exception { @@ -505,6 +507,27 @@ public class Transfer { } } + // Test that transferFrom with FileChannel source that is not readable + // throws NonReadableChannelException + static void xferTest09() throws Exception { + File source = File.createTempFile("source", null); + source.deleteOnExit(); + + File target = File.createTempFile("target", null); + target.deleteOnExit(); + + FileChannel fc1 = new FileOutputStream(source).getChannel(); + FileChannel fc2 = new RandomAccessFile(target, "rw").getChannel(); + try { + fc2.transferFrom(fc1, 0L, 0); + throw new RuntimeException("NonReadableChannelException expected"); + } catch (NonReadableChannelException expected) { + } finally { + fc1.close(); + fc2.close(); + } + } + /** * Creates file blah of specified size in bytes. */ -- GitLab