You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by gh...@apache.org on 2006/07/13 12:22:48 UTC

svn commit: r421577 - in /incubator/harmony/enhanced/classlib/trunk/modules: luni/src/main/native/luni/windows/ nio/src/main/java/org/apache/harmony/nio/internal/ nio/src/test/java/common/org/apache/harmony/tests/java/nio/channels/

Author: gharley
Date: Thu Jul 13 03:22:47 2006
New Revision: 421577

URL: http://svn.apache.org/viewvc?rev=421577&view=rev
Log:
HARMONY 849 : [nio] FileChannel.read(ByteBuffer[]) returns incorrect value and doesn't set the position of ByteBuffer[] in some cases

Modified:
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/native/luni/windows/OSFileSystemWin32.c
    incubator/harmony/enhanced/classlib/trunk/modules/nio/src/main/java/org/apache/harmony/nio/internal/FileChannelImpl.java
    incubator/harmony/enhanced/classlib/trunk/modules/nio/src/test/java/common/org/apache/harmony/tests/java/nio/channels/FileChannelTest.java

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/native/luni/windows/OSFileSystemWin32.c
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/native/luni/windows/OSFileSystemWin32.c?rev=421577&r1=421576&r2=421577&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/native/luni/windows/OSFileSystemWin32.c (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/native/luni/windows/OSFileSystemWin32.c Thu Jul 13 03:22:47 2006
@@ -144,7 +144,9 @@
   while(i<size){
     long bytesRead = hyfile_read ((IDATA) fd, (void *) (*(bufs+i)+*(offsets+i)), (IDATA) *(lengths+i));
     if(bytesRead == -1){
-        totalRead = -1;
+        if (totalRead == 0){
+                totalRead = -1;
+        }
         break;
     }
     totalRead += bytesRead;

Modified: incubator/harmony/enhanced/classlib/trunk/modules/nio/src/main/java/org/apache/harmony/nio/internal/FileChannelImpl.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/nio/src/main/java/org/apache/harmony/nio/internal/FileChannelImpl.java?rev=421577&r1=421576&r2=421577&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/nio/src/main/java/org/apache/harmony/nio/internal/FileChannelImpl.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/nio/src/main/java/org/apache/harmony/nio/internal/FileChannelImpl.java Thu Jul 13 03:22:47 2006
@@ -371,8 +371,17 @@
                     break;
                 }
             } else {
-                bytesRemaining -= directBuffers[i - offset].remaining();
-                buffers[i].put(directBuffers[i - offset]);
+                ByteBuffer buf = directBuffers[i - offset];
+                if (bytesRemaining < buf.remaining()){
+                    // this is the last step.                  
+                    int pos = buf.position();
+                    buffers[i].put(buf);
+                    buffers[i].position(pos + (int)bytesRemaining);
+                    bytesRemaining = 0;
+                } else {
+                    bytesRemaining -= buf.remaining();
+                    buffers[i].put(buf);
+                }                
             }
         }
         return bytesRead;

Modified: incubator/harmony/enhanced/classlib/trunk/modules/nio/src/test/java/common/org/apache/harmony/tests/java/nio/channels/FileChannelTest.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/nio/src/test/java/common/org/apache/harmony/tests/java/nio/channels/FileChannelTest.java?rev=421577&r1=421576&r2=421577&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/nio/src/test/java/common/org/apache/harmony/tests/java/nio/channels/FileChannelTest.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/nio/src/test/java/common/org/apache/harmony/tests/java/nio/channels/FileChannelTest.java Thu Jul 13 03:22:47 2006
@@ -1251,6 +1251,38 @@
             assertEquals(CONTENT_AS_BYTES[i], readBuffer.get());
         }
     }
+    
+    /**
+     * @tests java.nio.channels.FileChannel#read(ByteBuffer[])
+     */
+    public void test_read$LByteBuffer() throws Exception {
+        // regression test for Harmony-849
+        writeDataToFile(fileOfReadOnlyFileChannel);
+        ByteBuffer[] readBuffers = new ByteBuffer[2];
+        readBuffers[0] = ByteBuffer.allocate(CAPACITY);
+        readBuffers[1] = ByteBuffer.allocate(CAPACITY);
+        
+        long readCount = readOnlyFileChannel.read(readBuffers);
+        assertEquals(CONTENT_AS_BYTES_LENGTH, readCount);
+        assertEquals(CONTENT_AS_BYTES_LENGTH, readBuffers[0].position());
+        assertEquals(0, readBuffers[1].position());
+        readBuffers[0].flip();
+        for (int i = 0; i < CONTENT_AS_BYTES_LENGTH; i++) {
+            assertEquals(CONTENT_AS_BYTES[i], readBuffers[0].get());
+        }
+    }
+    
+    /**
+     * @tests java.nio.channels.FileChannel#read(ByteBuffer[])
+     */
+    public void test_read$LByteBuffer_mock() throws Exception {
+        FileChannel mockChannel = new MockFileChannel();
+        ByteBuffer[] buffers = new ByteBuffer[2];
+        mockChannel.read(buffers);
+        // Verify that calling read(ByteBuffer[] dsts) leads to the method
+        // read(dsts, 0, dsts.length)
+        assertTrue(((MockFileChannel)mockChannel).isReadCalled);
+    }
 
     /**
      * @tests java.nio.channels.FileChannel#read(ByteBuffer[], int, int)
@@ -1477,6 +1509,27 @@
             assertEquals(CONTENT_AS_BYTES[i], readBuffers[1].get());
         }
     }
+    
+    /**
+     * @tests java.nio.channels.FileChannel#read(ByteBuffer[], int, int)
+     */
+    public void test_read$LByteBufferII() throws Exception {
+        writeDataToFile(fileOfReadOnlyFileChannel);
+        ByteBuffer[] readBuffers = new ByteBuffer[2];
+        readBuffers[0] = ByteBuffer.allocate(CAPACITY);
+        readBuffers[1] = ByteBuffer.allocate(CAPACITY);
+
+        // writes to the second buffer
+        assertEquals(CONTENT_AS_BYTES_LENGTH, readOnlyFileChannel.read(
+                readBuffers, 1, 1));
+        assertEquals(0, readBuffers[0].position());
+        assertEquals(CONTENT_AS_BYTES_LENGTH, readBuffers[1].position());
+
+        readBuffers[1].flip();
+        for (int i = 0; i < CONTENT_AS_BYTES_LENGTH; i++) {
+            assertEquals(CONTENT_AS_BYTES[i], readBuffers[1].get());
+        }
+    }
 
     /**
      * @tests java.nio.channels.FileChannel#isOpen()
@@ -1517,6 +1570,8 @@
         private boolean isLockCalled = false;
         
         private boolean isTryLockCalled = false;
+        
+        private boolean isReadCalled = false;
 
         public void force(boolean arg0) throws IOException {
             // do nothing
@@ -1555,6 +1610,9 @@
 
         public long read(ByteBuffer[] srcs, int offset, int length)
                 throws IOException {
+            if (!isReadCalled){
+                isReadCalled = true;
+            }
             return 0;
         }