harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From telli...@apache.org
Subject svn commit: r601668 - /harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/platform/OSNetworkSystem.java
Date Thu, 06 Dec 2007 09:34:22 GMT
Author: tellison
Date: Thu Dec  6 01:34:21 2007
New Revision: 601668

URL: http://svn.apache.org/viewvc?rev=601668&view=rev
Log:
Add Java-side assertions for the validity of file descriptors being passed into the select
call.

Modified:
    harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/platform/OSNetworkSystem.java

Modified: harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/platform/OSNetworkSystem.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/platform/OSNetworkSystem.java?rev=601668&r1=601667&r2=601668&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/platform/OSNetworkSystem.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/platform/OSNetworkSystem.java
Thu Dec  6 01:34:21 2007
@@ -275,42 +275,74 @@
                 timeout);
     }
 
-	/*
-	 * 
-	 * @param 
-     *      readChannels all channels interested in read and accept 
-     * @param
-	 *      writeChannels all channels interested in write and connect 
+	/**
+     * Select the given file descriptors for read and write operations.
+     * 
+     * The file descriptors passed in as readFDs will be selected for read-ready
+     * operations, and those in the writeFDs will be selected for write-ready
+     * operations. A file descriptor can appear in either or both array, and
+     * must not be <code>null</code>. If the file descriptor is closed during
+     * the select the behavior depends upon the underlying OS.
+     * 
+     * Upon return the result is a single array of length
+     * <code>readFDs.length</code> + <code>writeFDs.length</code>
laid out
+     * as the result of the select operation on the corresponding file
+     * descriptors.
+     * 
+     * @param readChannels
+     *            all channels interested in read and accept
+     * @param writeChannels
+     *            all channels interested in write and connect
      * @param timeout
-	 *      timeout in millis @return a set of channels that are ready for operation
-	 * @throws 
-     *      SocketException @return int array, each int approve one of the	 * channel if
OK
-	 */
-
-	public int[] select(FileDescriptor[] readFDs,
-            FileDescriptor[] writeFDs, long timeout)
-			throws SocketException {
-		int countRead = readFDs.length;
-		int countWrite = writeFDs.length;
-		int result = 0;
+     *            timeout in millis
+     * @returns int array, each element describes the corresponding state of the
+     *          descriptor in the read and write arrays.
+     * @throws SocketException
+     */
+    public int[] select(FileDescriptor[] readFDs, FileDescriptor[] writeFDs,
+            long timeout) throws SocketException {
+        int countRead = readFDs.length;
+        int countWrite = writeFDs.length;
+        int result = 0;
         if (0 == countRead + countWrite) {
             return (new int[0]);
         }
-		int[] flags = new int[countRead + countWrite];
+        int[] flags = new int[countRead + countWrite];
+
+        assert validateFDs(readFDs, writeFDs) : "Invalid file descriptor arrays";
 
         // handle timeout in native
-		result = selectImpl(readFDs, writeFDs, countRead, countWrite, flags,
-				timeout);
+        result = selectImpl(readFDs, writeFDs, countRead, countWrite, flags,
+                timeout);
 
-		if (0 <= result) {
-			return flags;
-		}
-		if (ERRORCODE_SOCKET_TIMEOUT == result) {
-			return new int[0];
-		}
-		throw new SocketException();
+        if (0 <= result) {
+            return flags;
+        }
+        if (ERRORCODE_SOCKET_TIMEOUT == result) {
+            return new int[0];
+        }
+        throw new SocketException();
+    }
 
-	}
+    /*
+     * Used to check if the file descriptor arrays are valid before passing them
+     * into the select native call.
+     */
+    private boolean validateFDs(FileDescriptor[] readFDs,
+            FileDescriptor[] writeFDs) {
+        for (FileDescriptor fd : readFDs) {
+            // Also checks fd not null
+            if (!fd.valid()) {
+                return false;
+            }
+        }
+        for (FileDescriptor fd : writeFDs) {
+            if (!fd.valid()) {
+                return false;
+            }
+        }
+        return true;
+    }
 
 	public InetAddress getSocketLocalAddress(FileDescriptor aFD,
 			boolean preferIPv6Addresses) {



Mime
View raw message