harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From telli...@apache.org
Subject svn commit: r418784 - in /incubator/harmony/enhanced/classlib/trunk/modules/nio/src: main/java/org/apache/harmony/nio/internal/SocketChannelImpl.java test/java/org/apache/harmony/tests/java/nio/channels/SocketChannelTest.java
Date Mon, 03 Jul 2006 15:00:18 GMT
Author: tellison
Date: Mon Jul  3 08:00:17 2006
New Revision: 418784

URL: http://svn.apache.org/viewvc?rev=418784&view=rev
Log:
Apply patch HARMONY-728 ([classlib][nio] java.nio.channels.SocketChannel.read(ByteBuffer[],int,int)
fails to return even there are some data available)

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

Modified: incubator/harmony/enhanced/classlib/trunk/modules/nio/src/main/java/org/apache/harmony/nio/internal/SocketChannelImpl.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/nio/src/main/java/org/apache/harmony/nio/internal/SocketChannelImpl.java?rev=418784&r1=418783&r2=418784&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/nio/src/main/java/org/apache/harmony/nio/internal/SocketChannelImpl.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/nio/src/main/java/org/apache/harmony/nio/internal/SocketChannelImpl.java
Mon Jul  3 08:00:17 2006
@@ -29,7 +29,6 @@
 import java.net.UnknownHostException;
 import java.nio.ByteBuffer;
 import java.nio.channels.AlreadyConnectedException;
-import java.nio.channels.AsynchronousCloseException;
 import java.nio.channels.ClosedChannelException;
 import java.nio.channels.ConnectionPendingException;
 import java.nio.channels.IllegalBlockingModeException;
@@ -58,8 +57,6 @@
     // Class variables
     // -------------------------------------------------------------------
 
-    private static final String ERRORMSG_SOCKET_INVALID = "The socket argument is not a socket";
-
     private static final int MAX_PORT_NUMBER = 65535;
 
     private static final int EOF = -1;
@@ -102,9 +99,6 @@
     // error msg
     private static final String ERRCODE_PORT_ERROR = "K0032"; //$NON-NLS-1$
 
-    // error messages, for native dependent.
-    private static final String ERRORMSG_ASYNCHRONOUSCLOSE = "The call was cancelled";
-
     // a address of localhost
     private static final byte[] localAddrArray = { 127, 0, 0, 1 };
 
@@ -375,10 +369,18 @@
             throw new NullPointerException();
         }
         checkOpenConnected();
-
+        if (!target.hasRemaining()) {
+            return 0;
+        }
+        byte[] readArray = new byte[target.remaining()];
+        int readCount;
         synchronized (readLock) {
-            return readImpl(target);
+            readCount = readImpl(readArray);
         }
+        if (EOF != readCount) {
+            target.put(readArray, 0, readCount);
+        }
+        return readCount;
     }
 
     /*
@@ -387,29 +389,34 @@
      */
     public long read(ByteBuffer[] targets, int offset, int length)
             throws IOException {
-        if (isIndexValid(targets, offset, length)) {
-            checkOpenConnected();
-            if (0 == calculateByteBufferArray(targets, offset, length)){
-                return 0;
-            }
-            synchronized (readLock) {
-                long totalCount = 0;
-                for (int val = offset; val < offset + length; val++) {
-                    int readCount = readImpl(targets[val]);
-                    // only -1 or a integer >=0 may return
-                    if (EOF != readCount) {
-                        totalCount = totalCount + readCount;
-                    } else {
-                        if (0 == totalCount){
-                            totalCount = -1;
-                        }
-                        break;
-                    }
-                }
-                return totalCount;
+        if (!isIndexValid(targets, offset, length)) {
+            throw new ArrayIndexOutOfBoundsException();
+        }
+        checkOpenConnected();
+
+        int totalCount = calculateByteBufferArray(targets, offset, length);
+        if (0 == totalCount) {
+            return 0;
+        }
+        byte[] readBuffer = new byte[totalCount];
+        int readCount;
+        // read data to readBuffer, and then transfer data from readBuffer to
+        // targets.
+        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;
     }
 
     private boolean isIndexValid(ByteBuffer[] targets, int offset, int length) {
@@ -419,31 +426,18 @@
 
     /*
      * read from channel, and store the result in the target.
-     * 
+     *
+     * @param target    output parameter
      */
-    private int readImpl(ByteBuffer target) throws IOException {
-        if (!target.hasRemaining()) {
-            return 0;
-        }
+    private int readImpl(byte[] target) throws IOException {
         int readCount = 0;
         try {
-            byte[] readArray = new byte[target.remaining()];
             if (isBlocking()){
                 begin();
             }
-            readCount = networkSystem.read(fd, readArray, 0, readArray.length,
+            readCount = networkSystem.read(fd, target, 0, target.length,
                     (isBlocking() ? TIMEOUT_BLOCK : TIMEOUT_NONBLOCK));
-            if (EOF != readCount) {
-                target.put(readArray, 0, readCount);
-            }
             return readCount;
-        } catch (SocketException e) {
-            // FIXME improve native code
-            if (ERRORMSG_ASYNCHRONOUSCLOSE.equals(e.getMessage())
-                    || ERRORMSG_SOCKET_INVALID.equals(e.getMessage())) {
-                throw new AsynchronousCloseException();
-            }
-            throw e;
         } finally {
             if (isBlocking()){
                 end(readCount > 0);
@@ -522,9 +516,6 @@
             }
             source.position(pos + writeCount);
         } catch (SocketException e) {
-            if (ERRORMSG_ASYNCHRONOUSCLOSE.equals(e.getMessage())) {
-                throw new AsynchronousCloseException();
-            }
             if (!ERRMSG_SOCKET_NONBLOCKING_WOULD_BLOCK.equals(e.getMessage())) {
                 throw e;
             }            
@@ -653,8 +644,6 @@
         // ----------------------------------------------------
         // Class Variables
         // ----------------------------------------------------
-
-        private static final String ERRCODE_CHANNEL_NOT_CONNECTED = "K0320"; //$NON-NLS-1$
 
         private static final String ERRCODE_CHANNEL_CLOSED = "K003d"; //$NON-NLS-1$
 

Modified: incubator/harmony/enhanced/classlib/trunk/modules/nio/src/test/java/org/apache/harmony/tests/java/nio/channels/SocketChannelTest.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/nio/src/test/java/org/apache/harmony/tests/java/nio/channels/SocketChannelTest.java?rev=418784&r1=418783&r2=418784&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/nio/src/test/java/org/apache/harmony/tests/java/nio/channels/SocketChannelTest.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/nio/src/test/java/org/apache/harmony/tests/java/nio/channels/SocketChannelTest.java
Mon Jul  3 08:00:17 2006
@@ -2677,4 +2677,31 @@
             assertEquals(read[i], write[i + 2]);
         }
     }
+    
+    /**
+     * @tests SocketChannelImpl#read(ByteBuffer[])
+     */
+    public void test_read_$ByteBuffer_Blocking() throws IOException {
+        // regression test for Harmony-728
+        byte[] data = new byte[CAPACITY_NORMAL];
+        for (int i = 0; i < CAPACITY_NORMAL; i++) {
+            data[i] = (byte) i;
+        }
+        ByteBuffer[] buf = new ByteBuffer[2];
+        buf[0] = ByteBuffer.allocate(CAPACITY_NORMAL);
+        buf[1] = ByteBuffer.allocate(CAPACITY_NORMAL);
+        channel1.connect(localAddr1);
+        Socket socket = null;
+        try {
+            socket = server1.accept();
+            OutputStream out = socket.getOutputStream();
+            out.write(data);
+            // should not block here
+            channel1.read(buf);
+        } finally {
+            if (null != socket) {
+                socket.close();
+            }
+        }
+    }
 }



Mime
View raw message