From 44550df73a4aff18af123c65c5fbc69c02cbb1bd Mon Sep 17 00:00:00 2001
From: Elliott Hughes <enh@google.com>
Date: Tue, 8 Sep 2009 19:44:54 -0700
Subject: Use Get*ArrayRegion/Set*ArrayRegion instead of Get*ArrayElements.

This fixes all instances in the networking code, but doesn't address similar
patterns, nor non-networking code. This seemed like a reasonably-sized
meaningful chunk. Tested on sapphire-eng.

Bug: 1639287
---
 luni/src/main/native/java_net_InetAddress.cpp      | 64 +++++++++-------------
 luni/src/main/native/java_net_NetworkInterface.c   | 14 ++---
 ...pache_harmony_luni_platform_OSNetworkSystem.cpp | 56 ++++++++-----------
 3 files changed, 51 insertions(+), 83 deletions(-)

(limited to 'luni/src')

diff --git a/luni/src/main/native/java_net_InetAddress.cpp b/luni/src/main/native/java_net_InetAddress.cpp
index 90a88ee..14a8b82 100644
--- a/luni/src/main/native/java_net_InetAddress.cpp
+++ b/luni/src/main/native/java_net_InetAddress.cpp
@@ -24,7 +24,6 @@
 
 #include <stdio.h>
 #include <string.h>
-#include <assert.h>
 #include <netdb.h>
 #include <errno.h>
 
@@ -254,53 +253,40 @@ static jstring InetAddress_gethostbyaddr(JNIEnv* env, jobject obj,
         return NULL;
     }
 
-    size_t addrlen = env->GetArrayLength(javaAddress);
-    jbyte* rawAddress = env->GetByteArrayElements(javaAddress, NULL);
-    if (rawAddress == NULL) {
-        throwNullPointerException(env);
-        return NULL;
-    }
-
     // Convert the raw address bytes into a socket address structure.
-    int ret = 0;
     struct sockaddr_storage ss;
-    struct sockaddr_in *sin = (struct sockaddr_in *) &ss;
-    struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) &ss;
-    size_t socklen;
     memset(&ss, 0, sizeof(ss));
-    switch (addrlen) {
-        case 4:
-            socklen = sizeof(struct sockaddr_in);
-            sin->sin_family = AF_INET;
-            memcpy(&sin->sin_addr.s_addr, rawAddress, addrlen);
-            env->ReleaseByteArrayElements(javaAddress, rawAddress, JNI_ABORT);
-            break;
-        case 16:
-            socklen = sizeof(struct sockaddr_in6);
-            sin6->sin6_family = AF_INET6;
-            memcpy(&sin6->sin6_addr.s6_addr, rawAddress, addrlen);
-            env->ReleaseByteArrayElements(javaAddress, rawAddress, JNI_ABORT);
-            break;
-        default:
-            // The caller already throws an exception in this case. Don't worry
-            // about it here.
-            env->ReleaseByteArrayElements(javaAddress, rawAddress, JNI_ABORT);
-            return NULL;
+
+    size_t socklen;
+    const size_t addressLength = env->GetArrayLength(javaAddress);
+    if (addressLength == 4) {
+        struct sockaddr_in* sin = reinterpret_cast<sockaddr_in*>(&ss);
+        sin->sin_family = AF_INET;
+        socklen = sizeof(struct sockaddr_in);
+        jbyte* dst = reinterpret_cast<jbyte*>(&sin->sin_addr.s_addr);
+        env->GetByteArrayRegion(javaAddress, 0, 4, dst);
+    } else if (addressLength == 16) {
+        struct sockaddr_in6 *sin6 = reinterpret_cast<sockaddr_in6*>(&ss);
+        sin6->sin6_family = AF_INET6;
+        socklen = sizeof(struct sockaddr_in6);
+        jbyte* dst = reinterpret_cast<jbyte*>(&sin6->sin6_addr.s6_addr);
+        env->GetByteArrayRegion(javaAddress, 0, 16, dst);
+    } else {
+        // The caller already throws an exception in this case. Don't worry
+        // about it here.
+        return NULL;
     }
 
     // Look up the host name from the IP address.
     char name[NI_MAXHOST];
-    if (ret == 0) {
-        ret = getnameinfo((struct sockaddr *) &ss, socklen, name, sizeof(name),
-                          NULL, 0, NI_NAMEREQD);
-    }
-
-    if (ret == 0) {
-        return env->NewStringUTF(name);
+    int ret = getnameinfo(reinterpret_cast<sockaddr*>(&ss), socklen,
+                          name, sizeof(name), NULL, 0, NI_NAMEREQD);
+    if (ret != 0) {
+        jniThrowException(env, "java/net/UnknownHostException", gai_strerror(ret));
+        return NULL;
     }
 
-    jniThrowException(env, "java/net/UnknownHostException", gai_strerror(ret));
-    return NULL;
+    return env->NewStringUTF(name);
 }
 
 /*
diff --git a/luni/src/main/native/java_net_NetworkInterface.c b/luni/src/main/native/java_net_NetworkInterface.c
index 379ccdf..c659ae1 100644
--- a/luni/src/main/native/java_net_NetworkInterface.c
+++ b/luni/src/main/native/java_net_NetworkInterface.c
@@ -241,24 +241,18 @@ static char * netLookupErrorString(int anErrorNum) {
 static int structInToJavaAddress(
         JNIEnv *env, struct in_addr *address, jbyteArray java_address) {
 
-    if(java_address == NULL) {
+    if (java_address == NULL) {
         throwNullPointerException(env);
         return -1;
     }
 
-    if((*env)->GetArrayLength(env, java_address) != sizeof(address->s_addr)) {
+    if ((*env)->GetArrayLength(env, java_address) != sizeof(address->s_addr)) {
         jniThrowIOException(env, errno);
         return -1;
     }
 
-    jbyte *java_address_bytes;
-
-    java_address_bytes = (*env)->GetByteArrayElements(env, java_address, NULL);
-
-    memcpy(java_address_bytes, &(address->s_addr), sizeof(address->s_addr));
-
-    (*env)->ReleaseByteArrayElements(env, java_address, java_address_bytes, 0);
-
+    jbyte* src = (jbyte*)(&(address->s_addr));
+    (*env)->SetByteArrayRegion(env, java_address, 0, sizeof(address->s_addr), src);
     return 0;
 }
 
diff --git a/luni/src/main/native/org_apache_harmony_luni_platform_OSNetworkSystem.cpp b/luni/src/main/native/org_apache_harmony_luni_platform_OSNetworkSystem.cpp
index 78ec2d0..02e18e9 100644
--- a/luni/src/main/native/org_apache_harmony_luni_platform_OSNetworkSystem.cpp
+++ b/luni/src/main/native/org_apache_harmony_luni_platform_OSNetworkSystem.cpp
@@ -222,9 +222,6 @@ static void throwNullPointerException(JNIEnv *env) {
  */
 static int javaAddressToStructIn(
         JNIEnv *env, jbyteArray java_address, struct in_addr *address) {
-
-    memset(address, 0, sizeof(address));
-
     if (java_address == NULL) {
         return -1;
     }
@@ -233,15 +230,10 @@ static int javaAddressToStructIn(
         return -1;
     }
 
-    jbyte * java_address_bytes
-        =  env->GetByteArrayElements(java_address, NULL);
-
-    memcpy(&(address->s_addr),
-        java_address_bytes,
-        sizeof(address->s_addr));
-
-    env->ReleaseByteArrayElements(java_address, java_address_bytes, JNI_ABORT);
+    memset(address, 0, sizeof(address));
 
+    jbyte* dst = reinterpret_cast<jbyte*>(&(address->s_addr));
+    env->GetByteArrayRegion(java_address, 0, sizeof(address->s_addr), dst);
     return 0;
 }
 
@@ -336,49 +328,45 @@ static jobject socketAddressToInetAddress(JNIEnv *env,
  *
  * @exception SocketError if the address family is unknown
  */
-static int inetAddressToSocketAddress(JNIEnv *env,
-        jobject inetaddress, int port, struct sockaddr_storage *sockaddress) {
-
+static int inetAddressToSocketAddress(JNIEnv *env, jobject inetaddress,
+        int port, struct sockaddr_storage *sockaddress) {
     // Get the byte array that stores the IP address bytes in the InetAddress.
-    jbyteArray addressByteArray;
-    addressByteArray = (jbyteArray)env->GetObjectField(inetaddress,
-            gCachedFields.iaddr_ipaddress);
-    if (addressByteArray == NULL) {
-      throwNullPointerException(env);
-      return -1;
+    if (inetaddress == NULL) {
+        throwNullPointerException(env);
+        return -1;
     }
-
-    // Get the raw IP address bytes.
-    jbyte *addressBytes = env->GetByteArrayElements(addressByteArray, NULL);
+    jbyteArray addressBytes =
+        reinterpret_cast<jbyteArray>(env->GetObjectField(inetaddress,
+            gCachedFields.iaddr_ipaddress));
     if (addressBytes == NULL) {
-      throwNullPointerException(env);
-      return -1;
+        throwNullPointerException(env);
+        return -1;
     }
 
     // Convert the IP address bytes to the proper IP address type.
-    size_t addressLength = env->GetArrayLength(addressByteArray);
-    int result = 0;
+    size_t addressLength = env->GetArrayLength(addressBytes);
     if (addressLength == 4) {
         // IPv4 address.
-        struct sockaddr_in *sin = (struct sockaddr_in *) sockaddress;
+        struct sockaddr_in *sin = reinterpret_cast<sockaddr_in*>(sockaddress);
         memset(sin, 0, sizeof(struct sockaddr_in));
         sin->sin_family = AF_INET;
         sin->sin_port = htons(port);
-        memcpy(&sin->sin_addr.s_addr, addressBytes, 4);
+        jbyte* dst = reinterpret_cast<jbyte*>(&sin->sin_addr.s_addr);
+        env->GetByteArrayRegion(addressBytes, 0, 4, dst);
     } else if (addressLength == 16) {
         // IPv6 address.
-        struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) sockaddress;
+        struct sockaddr_in6 *sin6 = reinterpret_cast<sockaddr_in6*>(sockaddress);
         memset(sin6, 0, sizeof(struct sockaddr_in6));
         sin6->sin6_family = AF_INET6;
         sin6->sin6_port = htons(port);
-        memcpy(&sin6->sin6_addr.s6_addr, addressBytes, 16);
+        jbyte* dst = reinterpret_cast<jbyte*>(&sin6->sin6_addr.s6_addr);
+        env->GetByteArrayRegion(addressBytes, 0, 16, dst);
     } else {
         // Unknown address family.
         throwSocketException(env, SOCKERR_BADAF);
-        result = -1;
+        return -1;
     }
-    env->ReleaseByteArrayElements(addressByteArray, addressBytes, 0);
-    return result;
+    return 0;
 }
 
 /**
-- 
cgit v1.1