6984545: (fc) transferFrom does not throw NonReadableChannelException when target is size 0 and non-readable
authoralanb
Wed, 15 Sep 2010 15:13:50 +0100
changeset 6545 9d2efd6ddd0c
parent 6544 ab54bd98662c
child 6546 0ab9777645b5
6984545: (fc) transferFrom does not throw NonReadableChannelException when target is size 0 and non-readable Reviewed-by: forax
jdk/src/share/classes/sun/nio/ch/FileChannelImpl.java
jdk/test/java/nio/channels/FileChannel/Transfer.java
--- a/jdk/src/share/classes/sun/nio/ch/FileChannelImpl.java	Tue Sep 14 10:18:16 2010 +0800
+++ b/jdk/src/share/classes/sun/nio/ch/FileChannelImpl.java	Wed Sep 15 15:13:50 2010 +0100
@@ -545,6 +545,8 @@
                                          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);
--- a/jdk/test/java/nio/channels/FileChannel/Transfer.java	Tue Sep 14 10:18:16 2010 +0800
+++ b/jdk/test/java/nio/channels/FileChannel/Transfer.java	Wed Sep 15 15:13:50 2010 +0100
@@ -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 @@
         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 @@
         }
     }
 
+    // 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.
      */