am 60195db6: am 9ccfb95e: Merge change 24723 into eclair

Merge commit '60195db609e4f5125e39348651d7a06ff807cc96'

* commit '60195db609e4f5125e39348651d7a06ff807cc96':
  Remove code duplication in InetAddress.getByAddress.
diff --git a/luni/src/main/java/java/net/InetAddress.java b/luni/src/main/java/java/net/InetAddress.java
index 2eaf1cd..a7ea3f6 100644
--- a/luni/src/main/java/java/net/InetAddress.java
+++ b/luni/src/main/java/java/net/InetAddress.java
@@ -1124,7 +1124,7 @@
             throws UnknownHostException {
         // simply call the method by the same name specifying the default scope
         // id of 0
-        return getByAddress(ipAddress, 0);
+        return getByAddressInternal(null, ipAddress, 0);
     }
 
     /**
@@ -1144,51 +1144,35 @@
      */
     static InetAddress getByAddress(byte[] ipAddress, int scope_id)
             throws UnknownHostException {
-        byte[] copy_address;
-        if (ipAddress != null && ipAddress.length == 4) {
-            copy_address = new byte[4];
-            for (int i = 0; i < 4; i++) {
-                copy_address[i] = ipAddress[i];
-            }
-            return new Inet4Address(copy_address);
-        }
-
-        if (ipAddress != null && ipAddress.length == 16) {
-            // First check to see if the address is an IPv6-mapped
-            // IPv4 address. If it is, then we can make it a IPv4
-            // address, otherwise, we'll create an IPv6 address.
-            if (isIPv4MappedAddress(ipAddress)) {
-                copy_address = new byte[4];
-                for (int i = 0; i < 4; i++) {
-                    copy_address[i] = ipAddress[12 + i];
-                }
-                return new Inet4Address(copy_address);
-            }
-            copy_address = ipAddress.clone();
-            return new Inet6Address(copy_address, scope_id);
-        }
-
-        // K0339=Invalid IP Address is neither 4 or 16 bytes
-        throw new UnknownHostException(Msg.getString("K0339")); //$NON-NLS-1$
+        return getByAddressInternal(null, ipAddress, scope_id);
     }
 
     private static boolean isIPv4MappedAddress(byte ipAddress[]) {
         // Check if the address matches ::FFFF:d.d.d.d
         // The first 10 bytes are 0. The next to are -1 (FF).
         // The last 4 bytes are varied.
+        if (ipAddress == null || ipAddress.length != 16) {
+            return false;
+        }
         for (int i = 0; i < 10; i++) {
             if (ipAddress[i] != 0) {
                 return false;
             }
         }
-
         if (ipAddress[10] != -1 || ipAddress[11] != -1) {
             return false;
         }
-
         return true;
     }
 
+    private static byte[] ipv4MappedToIPv4(byte[] mappedAddress) {
+        byte[] ipv4Address = new byte[4];
+        for(int i = 0; i < 4; i++) {
+            ipv4Address[i] = mappedAddress[12 + i];
+        }
+        return ipv4Address;
+    }
+
     /**
      * Returns the {@code InetAddress} corresponding to the array of bytes, and
      * the given hostname. In the case of an IPv4 address there must be exactly
@@ -1238,36 +1222,32 @@
      */
     static InetAddress getByAddressInternal(String hostName, byte[] ipAddress,
             int scope_id) throws UnknownHostException {
-        byte[] copy_address;
-        if (ipAddress != null && ipAddress.length == 4) {
-            copy_address = new byte[4];
-            for (int i = 0; i < 4; i++) {
-                copy_address[i] = ipAddress[i];
-            }
-            return new Inet4Address(ipAddress, hostName);
+        if (ipAddress == null) {
+            throw new NullPointerException();
         }
-
-        if (ipAddress != null && ipAddress.length == 16) {
-            // First check to see if the address is an IPv6-mapped
-            // IPv4 address. If it is, then we can make it a IPv4
-            // address, otherwise, we'll create an IPv6 address.
-            if (isIPv4MappedAddress(ipAddress)) {
-                copy_address = new byte[4];
-                for (int i = 0; i < 4; i++) {
-                    copy_address[i] = ipAddress[12 + i];
+        switch (ipAddress.length) {
+            case 4:
+                return new Inet4Address(ipAddress.clone());
+            case 16:
+                // First check to see if the address is an IPv6-mapped
+                // IPv4 address. If it is, then we can make it a IPv4
+                // address, otherwise, we'll create an IPv6 address.
+                if (isIPv4MappedAddress(ipAddress)) {
+                    return new Inet4Address(ipv4MappedToIPv4(ipAddress));
+                } else {
+                    return new Inet6Address(ipAddress.clone(), scope_id);
                 }
-                return new Inet4Address(ipAddress, hostName);
-            }
-
-            copy_address = new byte[16];
-            for (int i = 0; i < 16; i++) {
-                copy_address[i] = ipAddress[i];
-            }
-
-            return new Inet6Address(ipAddress, hostName, scope_id);
+            default:
+                if (hostName != null) {
+                    // "Invalid IP Address is neither 4 or 16 bytes: <hostName>"
+                    throw new UnknownHostException(
+                            Msg.getString("K0332", hostName)); //$NON-NLS-1$
+                } else {
+                    // "Invalid IP Address is neither 4 or 16 bytes"
+                    throw new UnknownHostException(
+                            Msg.getString("K0339")); //$NON-NLS-1$
+                }
         }
-
-        throw new UnknownHostException(Msg.getString("K0332", hostName)); //$NON-NLS-1$
     }
 
     /**