changeset 2899:a248eb631aa2

6984545: (fc) transferFrom does not throw NonReadableChannelException when target is size 0 and non-readable Reviewed-by: forax
author alanb
date Wed, 15 Sep 2010 15:13:50 +0100
parents 5c3bad1d7f8a
children 441e86ab3233
files src/share/classes/sun/nio/ch/FileChannelImpl.java test/java/nio/channels/FileChannel/Transfer.java
diffstat 2 files changed, 25 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/sun/nio/ch/FileChannelImpl.java	Tue Sep 14 10:18:16 2010 +0800
+++ b/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/test/java/nio/channels/FileChannel/Transfer.java	Tue Sep 14 10:18:16 2010 +0800
+++ b/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.
      */