harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ghar...@apache.org
Subject svn commit: r419217 - in /incubator/harmony/enhanced/classlib/trunk/modules/nio/src: main/java/org/apache/harmony/nio/internal/DatagramChannelImpl.java test/java/org/apache/harmony/tests/java/nio/channels/DatagramChannelTest.java
Date Wed, 05 Jul 2006 11:33:06 GMT
Author: gharley
Date: Wed Jul  5 04:33:05 2006
New Revision: 419217

URL: http://svn.apache.org/viewvc?rev=419217&view=rev
Log:
HARMONY 754 : [nio] DatagramChannel.read(ByteBuffer[]) and read(ByteBuffer[],int,int) fail
to return even some data are available

Modified:
    incubator/harmony/enhanced/classlib/trunk/modules/nio/src/main/java/org/apache/harmony/nio/internal/DatagramChannelImpl.java
    incubator/harmony/enhanced/classlib/trunk/modules/nio/src/test/java/org/apache/harmony/tests/java/nio/channels/DatagramChannelTest.java

Modified: incubator/harmony/enhanced/classlib/trunk/modules/nio/src/main/java/org/apache/harmony/nio/internal/DatagramChannelImpl.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/nio/src/main/java/org/apache/harmony/nio/internal/DatagramChannelImpl.java?rev=419217&r1=419216&r2=419217&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/nio/src/main/java/org/apache/harmony/nio/internal/DatagramChannelImpl.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/nio/src/main/java/org/apache/harmony/nio/internal/DatagramChannelImpl.java
Wed Jul  5 04:33:05 2006
@@ -55,7 +55,7 @@
             .getNetworkSystem();
 
     // default timeout used to nonblocking mode.
-    private static final int DEFAULT_TIMEOUT = 100;
+    private static final int DEFAULT_TIMEOUT = 1;
 
     // error messages, for native dependent.
     private static final String ERRMSG_TIMEOUT = "The operation timed out";
@@ -372,14 +372,26 @@
      * @see java.nio.channels.DatagramChannel#read(java.nio.ByteBuffer)
      */
     public int read(ByteBuffer target) throws IOException {
+        if (null == target){
+            throw new NullPointerException();
+        }
         // status must be open and connected
         checkOpenConnected();
         // target buffer must be not null and not readonly
         checkNotNullNotReadOnly(target);
 
+        if (!target.hasRemaining()){
+            return 0;
+        }        
+        byte[] readArray = new byte[target.remaining()];
+        int readCount;
         synchronized (readLock) {
-            return readImpl(target);
+            readCount = readImpl(readArray);
+        }
+        if (0 != readCount) {
+            target.put(readArray, 0, readCount);
         }
+        return readCount;
     }
 
     /*
@@ -389,26 +401,43 @@
      */
     public long read(ByteBuffer[] targets, int offset, int length)
             throws IOException {
-        if (length >= 0 && offset >= 0 && length + offset <= targets.length)
{
-            // status must be open and connected
-            checkOpenConnected();
-            synchronized (readLock) {
-                long readCount = 0;
-                for (int val = offset; val < length; val++) {
-                    // target buffer must be not null and not readonly
-                    checkNotNullNotReadOnly(targets[val]);
-                    readCount = readCount + readImpl(targets[val]);
-                }
-                return readCount;
+        if (!(length >= 0 && offset >= 0 && length + offset <= targets.length))
{
+            throw new ArrayIndexOutOfBoundsException();
+        }        
+        // status must be open and connected
+        checkOpenConnected();
+        
+        int totalCount = 0;
+        for (int val = offset; val < length; val++) {
+            // target buffer must be not null and not readonly
+            checkNotNullNotReadOnly(targets[val]);
+            totalCount += targets[val].remaining();
+        }
+        // read data to readBuffer, and then transfer data from readBuffer to
+        // targets.
+        byte[] readBuffer = new byte[totalCount];
+        int readCount ;
+        synchronized (readLock) {
+            readCount = readImpl(readBuffer);
+        }
+        if (readCount > 0) {
+            int left = readCount;
+            int index = offset;
+            // transfer data from readBuffer to targets
+            while (left > 0) {
+                int putLength = Math.min(targets[index].remaining(), left);             
  
+                targets[index].put(readBuffer, readCount - left, putLength);
+                index++;
+                left -= putLength;
             }
         }
-        throw new ArrayIndexOutOfBoundsException();
+        return readCount;
     }
 
     /*
      * read from channel, and store the result in the target.
      */
-    private int readImpl(ByteBuffer target) throws IOException {
+    private int readImpl(byte[] target) throws IOException {
         // the return value
         int readCount = 0;
 
@@ -418,32 +447,14 @@
             // DEFAULT_TIMEOUT is used in non-block mode.
             int timeout = isBlocking() ? 0 : DEFAULT_TIMEOUT;
             DatagramPacket pack;
-            if (target.hasRemaining()) {
-                pack = new DatagramPacket(new byte[target.remaining()], target
-                        .remaining());
+            pack = new DatagramPacket(target, target.length);
+            if (isConnected()) {
+                readCount = networkSystem.recvConnectedDatagram(fd, pack, pack
+                        .getData(), 0, pack.getLength(), timeout, false);
             } else {
-                return 0;
+                readCount = networkSystem.receiveDatagram(fd, pack, pack
+                        .getData(), 0, pack.getLength(), timeout, false);
             }
-            boolean loop = isBlocking();
-            do {
-                if (!isOpen()) {
-                    // AsynchronizeCloseException will be thrown by end(boolean)
-                    // in finally block.
-                    break;
-                }
-                if (isConnected()) {
-                    readCount = networkSystem
-                            .recvConnectedDatagram(fd, pack, pack.getData(), 0,
-                                    pack.getLength(), timeout, false);
-                } else {
-                    readCount = networkSystem.receiveDatagram(fd, pack, pack
-                            .getData(), 0, pack.getLength(), timeout, false);
-                }
-                if (0 < readCount) {
-                    target.put(pack.getData());
-                    return readCount;
-                }
-            } while (loop);
             return readCount;
         } catch (InterruptedIOException e) {
             // FIXME improve native code.
@@ -464,6 +475,10 @@
         checkNotNull(source);
         // status must be open and connected
         checkOpenConnected();
+        // return immediately if source is full
+        if (!source.hasRemaining()){
+            return 0;
+        }
 
         synchronized (writeLock) {
             return writeImpl(source);

Modified: incubator/harmony/enhanced/classlib/trunk/modules/nio/src/test/java/org/apache/harmony/tests/java/nio/channels/DatagramChannelTest.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/nio/src/test/java/org/apache/harmony/tests/java/nio/channels/DatagramChannelTest.java?rev=419217&r1=419216&r2=419217&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/nio/src/test/java/org/apache/harmony/tests/java/nio/channels/DatagramChannelTest.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/nio/src/test/java/org/apache/harmony/tests/java/nio/channels/DatagramChannelTest.java
Wed Jul  5 04:33:05 2006
@@ -2434,7 +2434,99 @@
         // assert the position of ByteBuffer has been set
         assertEquals(CAPACITY_NORMAL, sourceBuf.position());
     }
+    
+    /**
+     * @tests DatagramChannel#read(ByteBuffer[])
+     */
+    public void test_read_$LByteBuffer() throws Exception {
+        // regression test for Harmony-754
+        channel2.socket().bind(localAddr1);
+        channel1.socket().bind(localAddr2);
+        channel1.connect(localAddr1);         
+        channel2.connect(localAddr2);
+        channel2.write(ByteBuffer.allocate(CAPACITY_NORMAL));
+        
+        ByteBuffer[] readBuf = new ByteBuffer[2];
+        readBuf[0] = ByteBuffer.allocateDirect(CAPACITY_NORMAL);
+        readBuf[1] = ByteBuffer.allocateDirect(CAPACITY_NORMAL);        
+        
+        channel1.configureBlocking(true);
+        assertEquals(CAPACITY_NORMAL, channel1.read(readBuf));
+    }
 
+    /**
+     * @tests DatagramChannel#read(ByteBuffer[],int,int)
+     */
+    public void test_read_$LByteBufferII() throws Exception {
+        // regression test for Harmony-754
+        channel2.socket().bind(localAddr1);
+        channel1.socket().bind(localAddr2);
+        channel1.connect(localAddr1);         
+        channel2.connect(localAddr2);
+        channel2.write(ByteBuffer.allocate(CAPACITY_NORMAL));
+        
+        ByteBuffer[] readBuf = new ByteBuffer[2];
+        readBuf[0] = ByteBuffer.allocateDirect(CAPACITY_NORMAL);
+        readBuf[1] = ByteBuffer.allocateDirect(CAPACITY_NORMAL);        
+        
+        channel1.configureBlocking(true);
+        assertEquals(CAPACITY_NORMAL, channel1.read(readBuf,0,2));
+    }
+    
+    /**
+     * @tests DatagramChannel#read(ByteBuffer)
+     */
+    public void test_read_LByteBuffer_closed_nullBuf() throws Exception {
+        // regression test for Harmony-754
+        ByteBuffer c = null;
+        DatagramChannel channel = DatagramChannel.open();
+        channel.close();
+        try{
+            channel.read(c);
+            fail("Should throw NullPointerException");
+        } catch (NullPointerException e){
+            // expected
+        }
+    }
+    
+    /**
+     * @tests DatagramChannel#read(ByteBuffer)
+     */
+    public void test_read_LByteBuffer_NotConnected_nullBuf() throws Exception {
+        // regression test for Harmony-754
+        ByteBuffer c = null;
+        DatagramChannel channel = DatagramChannel.open();
+        try{
+            channel.read(c);
+            fail("Should throw NullPointerException");
+        } catch (NullPointerException e){
+            // expected
+        }
+    }
+    
+    /**
+     * @tests DatagramChannel#read(ByteBuffer)
+     */
+    public void test_read_LByteBuffer_readOnlyBuf() throws Exception {
+        // regression test for Harmony-754
+        ByteBuffer c = ByteBuffer.allocate(1);
+        DatagramChannel channel = DatagramChannel.open();
+        try{
+            channel.read(c.asReadOnlyBuffer());
+            fail("Should throw NotYetConnectedException");
+        } catch (NotYetConnectedException e){
+            // expected
+        }
+        channel.connect(localAddr1);
+        try{
+            channel.read(c.asReadOnlyBuffer());
+            fail("Should throw IllegalArgumentException");
+        } catch (IllegalArgumentException e){
+            // expected
+        }
+    }
+
+    
     // -------------------------------------------------------------------
     // Mock class for security test.
     // -------------------------------------------------------------------



Mime
View raw message