harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From telli...@apache.org
Subject svn commit: r720202 - in /harmony/enhanced/classlib/trunk/modules/luni/src/main: java/org/apache/harmony/luni/platform/OSNetworkSystem.java native/luni/shared/OSNetworkSystem.c
Date Mon, 24 Nov 2008 14:22:04 GMT
Author: tellison
Date: Mon Nov 24 06:22:04 2008
New Revision: 720202

URL: http://svn.apache.org/viewvc?rev=720202&view=rev
Log:
Improve write functions:
 - Avoid unnecessarily allocating and copying data locally when writing from a bytearray.
 - Don't loop in a direct write, just send back partial write results.
 - Add javadoc comments on java side.

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

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=720202&r1=720201&r2=720202&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
Mon Nov 24 06:22:04 2008
@@ -540,9 +540,41 @@
         return true;
     }
 
+    /**
+     * Write bytes from a byte array to a socket.
+     * 
+     * @param fd
+     *            the socket on which to write the bytes
+     * @param data
+     *            the array containing the bytes to be written
+     * @param offset
+     *            the offset in the byte array from which to take the bytes
+     * @param count
+     *            the maximum number of bytes to be written. Callers are trusted
+     *            not to send values of length+count that are larger than
+     *            data.length
+     * @return the actual number of bytes written, which will be between zero
+     *         and count
+     * @throws IOException
+     *             if there is an underlying socket problem
+     */
     public native int write(FileDescriptor fd, byte[] data, int offset,
             int count) throws IOException;
 
+    /**
+     * Write bytes from the given address to a socket.
+     * 
+     * @param fd
+     *            the socket on which to write the bytes
+     * @param address
+     *            the start address of the bytes to be written
+     * @param count
+     *            the maximum number of bytes to be written
+     * @return the actual number of bytes written, which will be between zero
+     *         and count
+     * @throws IOException
+     *             if there is an underlying socket problem
+     */
     public native int writeDirect(FileDescriptor fd, long address, int count)
             throws IOException;
 }

Modified: harmony/enhanced/classlib/trunk/modules/luni/src/main/native/luni/shared/OSNetworkSystem.c
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/main/native/luni/shared/OSNetworkSystem.c?rev=720202&r1=720201&r2=720202&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/luni/src/main/native/luni/shared/OSNetworkSystem.c
(original)
+++ harmony/enhanced/classlib/trunk/modules/luni/src/main/native/luni/shared/OSNetworkSystem.c
Mon Nov 24 06:22:04 2008
@@ -402,41 +402,27 @@
  */
 JNIEXPORT jint JNICALL
 Java_org_apache_harmony_luni_platform_OSNetworkSystem_write
-  (JNIEnv * env, jobject thiz, jobject fileDescriptor, jbyteArray data,
+  (JNIEnv * env, jobject thiz, jobject fd, jbyteArray data,
    jint offset, jint count)
 {
-  PORT_ACCESS_FROM_ENV(env);
   jbyte *message;
-  jint result = 0;
-
-/* TODO: ARRAY PINNING */
-#define INTERNAL_SEND_BUFFER_MAX 512
-  U_8 internalBuffer[INTERNAL_SEND_BUFFER_MAX];
-
-  if (count > INTERNAL_SEND_BUFFER_MAX) {
-    message = hymem_allocate_memory(count);
-    if (message == NULL) {
-      throwNewOutOfMemoryError(env, "");
-      return 0;
-    }
-  } else {
-    message = (jbyte *) internalBuffer;
-  }
+  jboolean isCopy = JNI_FALSE;
+  jint result;
 
-  (*env)->GetByteArrayRegion(env, data, offset, count, message);
-  if ((*env)->ExceptionCheck(env)) {
-    goto out;
-  }
+  /* Get a pointer to the start of the bytearray */
+  message = (*env)->GetByteArrayElements (env, data, &isCopy);
 
+  /* Write directly from the byte array */
   result =
     Java_org_apache_harmony_luni_platform_OSNetworkSystem_writeDirect
-    (env, thiz, fileDescriptor, (jlong)(IDATA) message, count);
+    (env, thiz, fd, (jlong)((IDATA) message + offset), count);
 
-out:
-  if ((U_8 *) message != internalBuffer) {
-    hymem_free_memory(message);
+
+  /* If the pointer was to a copy it needs to be released */
+  if (isCopy == JNI_TRUE) {
+    (*env)->ReleaseByteArrayElements (env, data, message, JNI_ABORT);
   }
-#undef INTERNAL_SEND_BUFFER_MAX
+
   return result;
 }
 
@@ -447,47 +433,24 @@
  */
 JNIEXPORT jint JNICALL
 Java_org_apache_harmony_luni_platform_OSNetworkSystem_writeDirect
-  (JNIEnv * env, jobject thiz, jobject fileDescriptor, jlong address,
-   jint count)
+  (JNIEnv * env, jobject thiz, jobject fd, jlong address, jint count)
 {
   PORT_ACCESS_FROM_ENV(env);
   jbyte *message = (jbyte *) (IDATA)address;
-  I_32 result = 0, sent = 0;
-
-  if (sent < count) {
-    hysocket_t socketP =
-        getJavaIoFileDescriptorContentsAsAPointer(env, fileDescriptor);
-    if (!hysock_socketIsValid(socketP)) {
-      throwJavaNetSocketException(env,
-                                  sent ==0 ?
-                                    HYPORT_ERROR_SOCKET_BADSOCKET :
-                                    HYPORT_ERROR_SOCKET_INTERRUPTED);
-      return (jint) 0;
-    }
+  I_32 result;
 
-    while (sent < count) {
-      result =
-        hysock_write(socketP, (U_8 *) message + sent, (I_32) count - sent,
-                     HYSOCK_NOFLAGS);
-      if (result < 0) {
-        break;
-      }
-      sent += result;
-    }
-  }
+  hysocket_t socketP = getJavaIoFileDescriptorContentsAsAPointer(env, fd);
 
-  /**
-   * We should always throw an exception if all the data cannot be sent because Java methods
-   * assume all the data will be sent or an error occurs.
-   */
-  if (result < 0) {
+  result = hysock_write(socketP, (U_8 *) message, (I_32) count, HYSOCK_NOFLAGS);
+  if (0 > result) {
     throwJavaNetSocketException(env, result);
-    return (jint) 0;
-  } else {
-    return (jint) sent;
+    return (jint) 0;  // Ignored, exception takes precedence
   }
+
+  return (jint) result;
 }
 
+
 /*
  * Class:     org_apache_harmony_luni_platform_OSNetworkSystem
  * Method:    setNonBlocking



Mime
View raw message