You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by re...@apache.org on 2010/05/14 07:42:20 UTC

svn commit: r944118 - in /harmony/enhanced/java/trunk/classlib/modules/luni/src: main/java/org/apache/harmony/luni/util/Inet6Util.java test/api/common/org/apache/harmony/luni/tests/java/net/Inet6AddressTest.java

Author: regisxu
Date: Fri May 14 05:42:20 2010
New Revision: 944118

URL: http://svn.apache.org/viewvc?rev=944118&view=rev
Log:
According to Java6 spec, remove unnecessary leading zeros in an individual field
of IPv6 address.

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

Modified: harmony/enhanced/java/trunk/classlib/modules/luni/src/main/java/org/apache/harmony/luni/util/Inet6Util.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/java/trunk/classlib/modules/luni/src/main/java/org/apache/harmony/luni/util/Inet6Util.java?rev=944118&r1=944117&r2=944118&view=diff
==============================================================================
--- harmony/enhanced/java/trunk/classlib/modules/luni/src/main/java/org/apache/harmony/luni/util/Inet6Util.java (original)
+++ harmony/enhanced/java/trunk/classlib/modules/luni/src/main/java/org/apache/harmony/luni/util/Inet6Util.java Fri May 14 05:42:20 2010
@@ -160,13 +160,26 @@ public class Inet6Util {
                 return addressToString(bytesToInt(ipv4ByteArray, 0));
             }
             StringBuilder buffer = new StringBuilder();
+            boolean isFirst = true;
             for (int i = 0; i < ipByteArray.length; i++) {
+                if ((i & 1) == 0) {
+                    isFirst = true;
+                }
                 int j = (ipByteArray[i] & 0xf0) >>> 4;
-                buffer.append(hexCharacters.charAt(j));
+                if (j != 0 || !isFirst) {
+                    buffer.append(hexCharacters.charAt(j));
+                    isFirst = false;
+                }
                 j = ipByteArray[i] & 0x0f;
-                buffer.append(hexCharacters.charAt(j));
-                if (i % 2 != 0 && (i + 1) < ipByteArray.length) {
-                    buffer.append(":");
+                if (j != 0 || !isFirst) {
+                    buffer.append(hexCharacters.charAt(j));
+                    isFirst = false;
+                }
+                if ((i & 1) != 0 && (i + 1) < ipByteArray.length) {
+                    if (isFirst) {
+                        buffer.append('0');
+                    }
+                    buffer.append(':');
                 }
             }
             return buffer.toString();

Modified: harmony/enhanced/java/trunk/classlib/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/net/Inet6AddressTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/java/trunk/classlib/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/net/Inet6AddressTest.java?rev=944118&r1=944117&r2=944118&view=diff
==============================================================================
--- harmony/enhanced/java/trunk/classlib/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/net/Inet6AddressTest.java (original)
+++ harmony/enhanced/java/trunk/classlib/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/net/Inet6AddressTest.java Fri May 14 05:42:20 2010
@@ -22,6 +22,7 @@ import java.net.Inet6Address;
 import java.net.InetAddress;
 import java.net.NetworkInterface;
 import java.net.UnknownHostException;
+import java.util.Locale;
 
 import org.apache.harmony.testframework.serialization.SerializationTest;
 import org.apache.harmony.testframework.serialization.SerializationTest.SerializableAssert;
@@ -822,6 +823,16 @@ public class Inet6AddressTest extends ju
 		Inet6Address.getByAddress("123", addr2, nif);
 	}
 
+    public void test_getHostAddress_() throws Exception {
+        byte[] ipAddress = new byte[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1};
+        InetAddress ia = InetAddress.getByAddress(ipAddress);
+        assertEquals("0:0:0:0:0:0:0:1", ia.getHostAddress().toLowerCase(Locale.US));
+
+        ipAddress = new byte[] {-2, -128, 0, 0, 0, 0, 0, 0, 2, 17, 37, -1, -2, -8, 124, -79};
+        ia = InetAddress.getByAddress(ipAddress);
+        assertEquals("fe80:0:0:0:211:25ff:fef8:7cb1", ia.getHostAddress().toLowerCase(Locale.US));
+    }
+
 	/**
 	 * @throws UnknownHostException
 	 * @tests java.net.Inet6Address#getScopeID()