harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From telli...@apache.org
Subject svn commit: r492101 - in /harmony/enhanced/classlib/trunk/modules/luni/src: main/java/java/net/InetAddress.java main/java/org/apache/harmony/luni/util/Inet6Util.java test/java/org/apache/harmony/luni/tests/java/net/InetAddressTest.java
Date Wed, 03 Jan 2007 10:56:10 GMT
Author: tellison
Date: Wed Jan  3 02:56:10 2007
New Revision: 492101

URL: http://svn.apache.org/viewvc?view=rev&rev=492101
Log:
Apply patch HARMONY-2936 ([classlib][luni]InetAddress.getByName does not support some textual
representations)

Modified:
    harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/InetAddress.java
    harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/util/Inet6Util.java
    harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/luni/tests/java/net/InetAddressTest.java

Modified: harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/InetAddress.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/InetAddress.java?view=diff&rev=492101&r1=492100&r2=492101
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/InetAddress.java (original)
+++ harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/InetAddress.java Wed
Jan  3 02:56:10 2007
@@ -1137,20 +1137,31 @@
 		InetAddress address = null;
 
 		if (Inet6Util.isValidIPV4Address(ipAddressString)) {
-
-			StringTokenizer tokenizer = new StringTokenizer(ipAddressString,
-					".");
-			String token = "";
-			int tempInt = 0;
-			byte[] byteAddress = new byte[4];
-			for (int i = 0; i < 4; i++) {
-				token = tokenizer.nextToken();
-				tempInt = Integer.parseInt(token);
-				byteAddress[i] = (byte) tempInt;
-			}
-
-			address = new Inet4Address(byteAddress);
-
+            byte[] byteAddress = new byte[4];
+            String[] parts = ipAddressString.split("\\.");
+            int length = parts.length;
+            if (length == 1) {
+                Long value = Long.parseLong(parts[0]);
+                for (int i = 0; i < 4; i++) {
+                    byteAddress[i] = (byte) (value>>((3-i)*8));
+                }
+            } else {
+                for (int i = 0; i < length; i++) {
+                    byteAddress[i]=(byte)Integer.parseInt(parts[i]);
+                }
+            }
+            
+            // adjust for 2/3 parts address
+            if(length == 2){
+                byteAddress[3]=byteAddress[1];
+                byteAddress[1]=0;
+            }
+            if(length == 3){
+                byteAddress[3]=byteAddress[2];
+                byteAddress[2]=0;
+            }
+            
+            address = new Inet4Address(byteAddress);
 		} else { // otherwise it must be ipv6
 
 			if (ipAddressString.charAt(0) == '[') {

Modified: harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/util/Inet6Util.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/util/Inet6Util.java?view=diff&rev=492101&r1=492100&r2=492101
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/util/Inet6Util.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/util/Inet6Util.java
Wed Jan  3 02:56:10 2007
@@ -465,47 +465,29 @@
 	 *         notation, false otherwise
 	 */
 	public static boolean isValidIPV4Address(String value) {
-
-		int periods = 0;
-		int i = 0;
-		int length = value.length();
-
-		if (length > 15) {
+        // general test
+        if (!value.matches("[\\p{Digit}\\.]*")) {
             return false;
         }
-		char c = 0;
-		String word = "";
-		for (i = 0; i < length; i++) {
-			c = value.charAt(i);
-			if (c == '.') {
-				periods++;
-				if (periods > 3) {
-                    return false;
-                }
-				if (word == "") {
-                    return false;
-                }
-				if (Integer.parseInt(word) > 255) {
-                    return false;
-                }
-				word = "";
-			} else if (!(Character.isDigit(c))) {
-                return false;
-            } else {
-				if (word.length() > 2) {
-                    return false;
-                }
-				word += c;
-			}
-		}
 
-		if (word == "" || Integer.parseInt(word) > 255) {
+        String[] parts = value.split("\\.");
+        int length = parts.length;
+        if (length > 4) {
             return false;
         }
-		if (periods != 3) {
-            return false;
+
+        // for one part
+        if (parts.length == 1) {
+            long longValue = Long.parseLong(parts[0]);
+            return longValue >= 0 && longValue <= 0xFFFFFFFFL;
+        }
+        // test every parts
+        for (int i = 0; i < parts.length; i++) {
+            if (parts[i].length() > 3 || Integer.parseInt(parts[i]) > 255) {
+                return false;
+            }
         }
-		return true;
+        return true;
 	}
 
 }

Modified: harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/luni/tests/java/net/InetAddressTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/luni/tests/java/net/InetAddressTest.java?view=diff&rev=492101&r1=492100&r2=492101
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/luni/tests/java/net/InetAddressTest.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/luni/tests/java/net/InetAddressTest.java
Wed Jan  3 02:56:10 2007
@@ -218,6 +218,15 @@
 //	        ia2.getHostName().startsWith(Support_Configuration.InetTestAddress));
 
         // TODO : Test to ensure all the address formats are recognized
+        InetAddress i = InetAddress.getByName("1.2.3");
+        assertEquals("1.2.0.3",i.getHostAddress());
+        i = InetAddress.getByName("1.2");
+        assertEquals("1.0.0.2",i.getHostAddress());
+        i = InetAddress.getByName(String.valueOf(0xffffffffL));
+        assertEquals("255.255.255.255",i.getHostAddress());
+        String s = "222.222.222.222....";
+        i = InetAddress.getByName(s);
+        assertEquals("222.222.222.222",i.getHostAddress());
     }
 
 	/**



Mime
View raw message