From commits-return-62322-apmail-harmony-commits-archive=harmony.apache.org@harmony.apache.org Fri May 14 05:42:27 2010 Return-Path: Delivered-To: apmail-harmony-commits-archive@www.apache.org Received: (qmail 43989 invoked from network); 14 May 2010 05:42:27 -0000 Received: from unknown (HELO mail.apache.org) (140.211.11.3) by 140.211.11.9 with SMTP; 14 May 2010 05:42:27 -0000 Received: (qmail 12883 invoked by uid 500); 14 May 2010 05:42:27 -0000 Delivered-To: apmail-harmony-commits-archive@harmony.apache.org Received: (qmail 12788 invoked by uid 500); 14 May 2010 05:42:25 -0000 Mailing-List: contact commits-help@harmony.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@harmony.apache.org Delivered-To: mailing list commits@harmony.apache.org Received: (qmail 12781 invoked by uid 99); 14 May 2010 05:42:25 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 14 May 2010 05:42:25 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 14 May 2010 05:42:20 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id 0420723889D2; Fri, 14 May 2010 05:41:59 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r944117 - /harmony/enhanced/java/trunk/classlib/modules/luni/src/main/java/org/apache/harmony/luni/util/Inet6Util.java Date: Fri, 14 May 2010 05:41:58 -0000 To: commits@harmony.apache.org From: regisxu@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20100514054159.0420723889D2@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: regisxu Date: Fri May 14 05:41:58 2010 New Revision: 944117 URL: http://svn.apache.org/viewvc?rev=944117&view=rev Log: format code of Inet6Util.java. Modified: harmony/enhanced/java/trunk/classlib/modules/luni/src/main/java/org/apache/harmony/luni/util/Inet6Util.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=944117&r1=944116&r2=944117&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:41:58 2010 @@ -24,28 +24,28 @@ import java.util.StringTokenizer; */ public class Inet6Util { - /** - * Creates an byte[] based on an ipAddressString. No error handling is - * performed here. - */ - public static byte[] createByteArrayFromIPAddressString( - String ipAddressString) { + /** + * Creates an byte[] based on an ipAddressString. No error handling is + * performed here. + */ + public static byte[] createByteArrayFromIPAddressString( + String ipAddressString) { - if (isValidIPV4Address(ipAddressString)) { + if (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; - } - - return byteAddress; - } - - if (ipAddressString.charAt(0) == '[') { + 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; + } + + return byteAddress; + } + + if (ipAddressString.charAt(0) == '[') { ipAddressString = ipAddressString.substring(1, ipAddressString.length() - 1); } @@ -63,235 +63,235 @@ public class Inet6Util { * or decimal list. In the case where we hit a :: we will save the index * of the hexStrings so we can add zeros in to fill out the string */ - while (tokenizer.hasMoreTokens()) { - prevToken = token; - token = tokenizer.nextToken(); - - if (token.equals(":")) { - if (prevToken.equals(":")) { - doubleColonIndex = hexStrings.size(); - } else if (!prevToken.equals("")) { - hexStrings.add(prevToken); - } - } else if (token.equals(".")) { - decStrings.add(prevToken); - } - } - - if (prevToken.equals(":")) { - if (token.equals(":")) { - doubleColonIndex = hexStrings.size(); - } else { - hexStrings.add(token); - } - } else if (prevToken.equals(".")) { - decStrings.add(token); - } - - // figure out how many hexStrings we should have - // also check if it is a IPv4 address - int hexStringsLength = 8; - - // If we have an IPv4 address tagged on at the end, subtract - // 4 bytes, or 2 hex words from the total - if (decStrings.size() > 0) { - hexStringsLength -= 2; - } - - // if we hit a double Colon add the appropriate hex strings - if (doubleColonIndex != -1) { - int numberToInsert = hexStringsLength - hexStrings.size(); - for (int i = 0; i < numberToInsert; i++) { - hexStrings.add(doubleColonIndex, "0"); - } - } - - byte ipByteArray[] = new byte[16]; - - // Finally convert these strings to bytes... - for (int i = 0; i < hexStrings.size(); i++) { - convertToBytes(hexStrings.get(i), ipByteArray, i * 2); - } + while (tokenizer.hasMoreTokens()) { + prevToken = token; + token = tokenizer.nextToken(); + + if (token.equals(":")) { + if (prevToken.equals(":")) { + doubleColonIndex = hexStrings.size(); + } else if (!prevToken.equals("")) { + hexStrings.add(prevToken); + } + } else if (token.equals(".")) { + decStrings.add(prevToken); + } + } - // Now if there are any decimal values, we know where they go... - for (int i = 0; i < decStrings.size(); i++) { + if (prevToken.equals(":")) { + if (token.equals(":")) { + doubleColonIndex = hexStrings.size(); + } else { + hexStrings.add(token); + } + } else if (prevToken.equals(".")) { + decStrings.add(token); + } + + // figure out how many hexStrings we should have + // also check if it is a IPv4 address + int hexStringsLength = 8; + + // If we have an IPv4 address tagged on at the end, subtract + // 4 bytes, or 2 hex words from the total + if (decStrings.size() > 0) { + hexStringsLength -= 2; + } + + // if we hit a double Colon add the appropriate hex strings + if (doubleColonIndex != -1) { + int numberToInsert = hexStringsLength - hexStrings.size(); + for (int i = 0; i < numberToInsert; i++) { + hexStrings.add(doubleColonIndex, "0"); + } + } + + byte ipByteArray[] = new byte[16]; + + // Finally convert these strings to bytes... + for (int i = 0; i < hexStrings.size(); i++) { + convertToBytes(hexStrings.get(i), ipByteArray, i * 2); + } + + // Now if there are any decimal values, we know where they go... + for (int i = 0; i < decStrings.size(); i++) { ipByteArray[i + 12] = (byte) (Integer.parseInt(decStrings.get(i)) & 255); } - // now check to see if this guy is actually and IPv4 address - // an ipV4 address is ::FFFF:d.d.d.d - boolean ipV4 = true; - for (int i = 0; i < 10; i++) { - if (ipByteArray[i] != 0) { - ipV4 = false; - break; - } - } - - if (ipByteArray[10] != -1 || ipByteArray[11] != -1) { - ipV4 = false; - } - - if (ipV4) { - byte ipv4ByteArray[] = new byte[4]; - for (int i = 0; i < 4; i++) { - ipv4ByteArray[i] = ipByteArray[i + 12]; - } - return ipv4ByteArray; - } - - return ipByteArray; - - } - - static String hexCharacters = "0123456789ABCDEF"; - - public static String createIPAddrStringFromByteArray(byte ipByteArray[]) { - if (ipByteArray.length == 4) { - return addressToString(bytesToInt(ipByteArray, 0)); - } - - if (ipByteArray.length == 16) { - if (isIPv4MappedAddress(ipByteArray)) { - byte ipv4ByteArray[] = new byte[4]; - for (int i = 0; i < 4; i++) { - ipv4ByteArray[i] = ipByteArray[i + 12]; - } - return addressToString(bytesToInt(ipv4ByteArray, 0)); - } + // now check to see if this guy is actually and IPv4 address + // an ipV4 address is ::FFFF:d.d.d.d + boolean ipV4 = true; + for (int i = 0; i < 10; i++) { + if (ipByteArray[i] != 0) { + ipV4 = false; + break; + } + } + + if (ipByteArray[10] != -1 || ipByteArray[11] != -1) { + ipV4 = false; + } + + if (ipV4) { + byte ipv4ByteArray[] = new byte[4]; + for (int i = 0; i < 4; i++) { + ipv4ByteArray[i] = ipByteArray[i + 12]; + } + return ipv4ByteArray; + } + + return ipByteArray; + + } + + static String hexCharacters = "0123456789ABCDEF"; + + public static String createIPAddrStringFromByteArray(byte ipByteArray[]) { + if (ipByteArray.length == 4) { + return addressToString(bytesToInt(ipByteArray, 0)); + } + + if (ipByteArray.length == 16) { + if (isIPv4MappedAddress(ipByteArray)) { + byte ipv4ByteArray[] = new byte[4]; + for (int i = 0; i < 4; i++) { + ipv4ByteArray[i] = ipByteArray[i + 12]; + } + return addressToString(bytesToInt(ipv4ByteArray, 0)); + } StringBuilder buffer = new StringBuilder(); for (int i = 0; i < ipByteArray.length; i++) { - int j = (ipByteArray[i] & 0xf0) >>> 4; - buffer.append(hexCharacters.charAt(j)); - j = ipByteArray[i] & 0x0f; - buffer.append(hexCharacters.charAt(j)); - if (i % 2 != 0 && (i + 1) < ipByteArray.length) { - buffer.append(":"); - } + int j = (ipByteArray[i] & 0xf0) >>> 4; + buffer.append(hexCharacters.charAt(j)); + j = ipByteArray[i] & 0x0f; + buffer.append(hexCharacters.charAt(j)); + if (i % 2 != 0 && (i + 1) < ipByteArray.length) { + buffer.append(":"); + } } return buffer.toString(); - } - return null; - } - - /** Converts a 4 character hex word into a 2 byte word equivalent */ - public static void convertToBytes(String hexWord, byte ipByteArray[], - int byteIndex) { - - int hexWordLength = hexWord.length(); - int hexWordIndex = 0; - ipByteArray[byteIndex] = 0; - ipByteArray[byteIndex + 1] = 0; - int charValue; - - // high order 4 bits of first byte - if (hexWordLength > 3) { - charValue = getIntValue(hexWord.charAt(hexWordIndex++)); - ipByteArray[byteIndex] = (byte) (ipByteArray[byteIndex] | (charValue << 4)); - } - - // low order 4 bits of the first byte - if (hexWordLength > 2) { - charValue = getIntValue(hexWord.charAt(hexWordIndex++)); - ipByteArray[byteIndex] = (byte) (ipByteArray[byteIndex] | charValue); - } - - // high order 4 bits of second byte - if (hexWordLength > 1) { - charValue = getIntValue(hexWord.charAt(hexWordIndex++)); - ipByteArray[byteIndex + 1] = (byte) (ipByteArray[byteIndex + 1] | (charValue << 4)); - } - - // low order 4 bits of the first byte - charValue = getIntValue(hexWord.charAt(hexWordIndex)); - ipByteArray[byteIndex + 1] = (byte) (ipByteArray[byteIndex + 1] | charValue & 15); - } - - static int getIntValue(char c) { - - switch (c) { - case '0': - return 0; - case '1': - return 1; - case '2': - return 2; - case '3': - return 3; - case '4': - return 4; - case '5': - return 5; - case '6': - return 6; - case '7': - return 7; - case '8': - return 8; - case '9': - return 9; - } - - c = Character.toLowerCase(c); - switch (c) { - case 'a': - return 10; - case 'b': - return 11; - case 'c': - return 12; - case 'd': - return 13; - case 'e': - return 14; - case 'f': - return 15; - } - return 0; - } - - 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. - for (int i = 0; i < 10; i++) { - if (ipAddress[i] != 0) { - return false; - } - } - - if (ipAddress[10] != -1 || ipAddress[11] != -1) { - return false; - } - - return true; - - } - - /** - * Takes the byte array and creates an integer out of four bytes starting at - * start as the high-order byte. This method makes no checks on the validity - * of the parameters. - */ - public static int bytesToInt(byte bytes[], int start) { - // First mask the byte with 255, as when a negative - // signed byte converts to an integer, it has bits - // on in the first 3 bytes, we are only concerned - // about the right-most 8 bits. - // Then shift the rightmost byte to align with its - // position in the integer. - int value = ((bytes[start + 3] & 255)) - | ((bytes[start + 2] & 255) << 8) - | ((bytes[start + 1] & 255) << 16) - | ((bytes[start] & 255) << 24); - return value; - } - - public static String addressToString(int value) { - return ((value >> 24) & 0xff) + "." + ((value >> 16) & 0xff) + "." - + ((value >> 8) & 0xff) + "." + (value & 0xff); - } + } + return null; + } + + /** Converts a 4 character hex word into a 2 byte word equivalent */ + public static void convertToBytes(String hexWord, byte ipByteArray[], + int byteIndex) { + + int hexWordLength = hexWord.length(); + int hexWordIndex = 0; + ipByteArray[byteIndex] = 0; + ipByteArray[byteIndex + 1] = 0; + int charValue; + + // high order 4 bits of first byte + if (hexWordLength > 3) { + charValue = getIntValue(hexWord.charAt(hexWordIndex++)); + ipByteArray[byteIndex] = (byte) (ipByteArray[byteIndex] | (charValue << 4)); + } + + // low order 4 bits of the first byte + if (hexWordLength > 2) { + charValue = getIntValue(hexWord.charAt(hexWordIndex++)); + ipByteArray[byteIndex] = (byte) (ipByteArray[byteIndex] | charValue); + } + + // high order 4 bits of second byte + if (hexWordLength > 1) { + charValue = getIntValue(hexWord.charAt(hexWordIndex++)); + ipByteArray[byteIndex + 1] = (byte) (ipByteArray[byteIndex + 1] | (charValue << 4)); + } + + // low order 4 bits of the first byte + charValue = getIntValue(hexWord.charAt(hexWordIndex)); + ipByteArray[byteIndex + 1] = (byte) (ipByteArray[byteIndex + 1] | charValue & 15); + } + + static int getIntValue(char c) { + + switch (c) { + case '0': + return 0; + case '1': + return 1; + case '2': + return 2; + case '3': + return 3; + case '4': + return 4; + case '5': + return 5; + case '6': + return 6; + case '7': + return 7; + case '8': + return 8; + case '9': + return 9; + } + + c = Character.toLowerCase(c); + switch (c) { + case 'a': + return 10; + case 'b': + return 11; + case 'c': + return 12; + case 'd': + return 13; + case 'e': + return 14; + case 'f': + return 15; + } + return 0; + } + + 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. + for (int i = 0; i < 10; i++) { + if (ipAddress[i] != 0) { + return false; + } + } + + if (ipAddress[10] != -1 || ipAddress[11] != -1) { + return false; + } + + return true; + + } + + /** + * Takes the byte array and creates an integer out of four bytes starting at + * start as the high-order byte. This method makes no checks on the validity + * of the parameters. + */ + public static int bytesToInt(byte bytes[], int start) { + // First mask the byte with 255, as when a negative + // signed byte converts to an integer, it has bits + // on in the first 3 bytes, we are only concerned + // about the right-most 8 bits. + // Then shift the rightmost byte to align with its + // position in the integer. + int value = ((bytes[start + 3] & 255)) + | ((bytes[start + 2] & 255) << 8) + | ((bytes[start + 1] & 255) << 16) + | ((bytes[start] & 255) << 24); + return value; + } + + public static String addressToString(int value) { + return ((value >> 24) & 0xff) + "." + ((value >> 16) & 0xff) + "." + + ((value >> 8) & 0xff) + "." + (value & 0xff); + } public static boolean isIP6AddressInFullForm(String ipAddress) { if (isValidIP6Address(ipAddress)) { @@ -304,179 +304,179 @@ public class Inet6Util { } return false; } - - public static boolean isValidIP6Address(String ipAddress) { - int length = ipAddress.length(); - boolean doubleColon = false; - int numberOfColons = 0; - int numberOfPeriods = 0; - int numberOfPercent = 0; - String word = ""; - char c = 0; - char prevChar = 0; - int offset = 0; // offset for [] IP addresses - if (length < 2) { + public static boolean isValidIP6Address(String ipAddress) { + int length = ipAddress.length(); + boolean doubleColon = false; + int numberOfColons = 0; + int numberOfPeriods = 0; + int numberOfPercent = 0; + String word = ""; + char c = 0; + char prevChar = 0; + int offset = 0; // offset for [] IP addresses + + if (length < 2) { return false; } - for (int i = 0; i < length; i++) { - prevChar = c; - c = ipAddress.charAt(i); - switch (c) { - - // case for an open bracket [x:x:x:...x] - case '[': - if (i != 0) { + for (int i = 0; i < length; i++) { + prevChar = c; + c = ipAddress.charAt(i); + switch (c) { + + // case for an open bracket [x:x:x:...x] + case '[': + if (i != 0) { return false; // must be first character } - if (ipAddress.charAt(length - 1) != ']') { + if (ipAddress.charAt(length - 1) != ']') { return false; // must have a close ] } - offset = 1; - if (length < 4) { + offset = 1; + if (length < 4) { return false; } - break; + break; - // case for a closed bracket at end of IP [x:x:x:...x] - case ']': - if (i != length - 1) { + // case for a closed bracket at end of IP [x:x:x:...x] + case ']': + if (i != length - 1) { return false; // must be last character } - if (ipAddress.charAt(0) != '[') { + if (ipAddress.charAt(0) != '[') { return false; // must have a open [ } - break; + break; - // case for the last 32-bits represented as IPv4 x:x:x:x:x:x:d.d.d.d - case '.': - numberOfPeriods++; - if (numberOfPeriods > 3) { + // case for the last 32-bits represented as IPv4 x:x:x:x:x:x:d.d.d.d + case '.': + numberOfPeriods++; + if (numberOfPeriods > 3) { return false; } - if (!isValidIP4Word(word)) { + if (!isValidIP4Word(word)) { return false; } - if (numberOfColons != 6 && !doubleColon) { + if (numberOfColons != 6 && !doubleColon) { return false; } - // a special case ::1:2:3:4:5:d.d.d.d allows 7 colons with an - // IPv4 ending, otherwise 7 :'s is bad - if (numberOfColons == 7 && ipAddress.charAt(0 + offset) != ':' - && ipAddress.charAt(1 + offset) != ':') { + // a special case ::1:2:3:4:5:d.d.d.d allows 7 colons with an + // IPv4 ending, otherwise 7 :'s is bad + if (numberOfColons == 7 && ipAddress.charAt(0 + offset) != ':' + && ipAddress.charAt(1 + offset) != ':') { return false; } - word = ""; - break; + word = ""; + break; - case ':': - numberOfColons++; - if (numberOfColons > 7) { + case ':': + numberOfColons++; + if (numberOfColons > 7) { return false; } - if (numberOfPeriods > 0) { + if (numberOfPeriods > 0) { return false; } - if (prevChar == ':') { - if (doubleColon) { + if (prevChar == ':') { + if (doubleColon) { return false; } - doubleColon = true; - } - word = ""; - break; - case '%': - if (numberOfColons == 0) { - return false; - } - numberOfPercent++; - - // validate that the stuff after the % is valid - if ((i + 1) >= length) { - // in this case the percent is there but no number is - // available - return false; - } - try { - Integer.parseInt(ipAddress.substring(i + 1)); - } catch (NumberFormatException e) { - // right now we just support an integer after the % so if - // this is not - // what is there then return - return false; - } - break; - - default: - if (numberOfPercent == 0) { - if (word.length() > 3) { + doubleColon = true; + } + word = ""; + break; + case '%': + if (numberOfColons == 0) { + return false; + } + numberOfPercent++; + + // validate that the stuff after the % is valid + if ((i + 1) >= length) { + // in this case the percent is there but no number is + // available + return false; + } + try { + Integer.parseInt(ipAddress.substring(i + 1)); + } catch (NumberFormatException e) { + // right now we just support an integer after the % so if + // this is not + // what is there then return + return false; + } + break; + + default: + if (numberOfPercent == 0) { + if (word.length() > 3) { return false; } - if (!isValidHexChar(c)) { + if (!isValidHexChar(c)) { return false; } - } - word += c; - } - } - - // Check if we have an IPv4 ending - if (numberOfPeriods > 0) { - if (numberOfPeriods != 3 || !isValidIP4Word(word)) { + } + word += c; + } + } + + // Check if we have an IPv4 ending + if (numberOfPeriods > 0) { + if (numberOfPeriods != 3 || !isValidIP4Word(word)) { + return false; + } + } else { + // If we're at then end and we haven't had 7 colons then there is a + // problem unless we encountered a doubleColon + if (numberOfColons != 7 && !doubleColon) { return false; } - } else { - // If we're at then end and we haven't had 7 colons then there is a - // problem unless we encountered a doubleColon - if (numberOfColons != 7 && !doubleColon) { - return false; - } - - // If we have an empty word at the end, it means we ended in either - // a : or a . - // If we did not end in :: then this is invalid - if (numberOfPercent == 0) { - if (word == "" && ipAddress.charAt(length - 1 - offset) == ':' - && ipAddress.charAt(length - 2 - offset) != ':') { - return false; - } - } - } - - return true; - } - - public static boolean isValidIP4Word(String word) { - char c; - if (word.length() < 1 || word.length() > 3) { + + // If we have an empty word at the end, it means we ended in either + // a : or a . + // If we did not end in :: then this is invalid + if (numberOfPercent == 0) { + if (word == "" && ipAddress.charAt(length - 1 - offset) == ':' + && ipAddress.charAt(length - 2 - offset) != ':') { + return false; + } + } + } + + return true; + } + + public static boolean isValidIP4Word(String word) { + char c; + if (word.length() < 1 || word.length() > 3) { return false; } - for (int i = 0; i < word.length(); i++) { - c = word.charAt(i); - if (!(c >= '0' && c <= '9')) { + for (int i = 0; i < word.length(); i++) { + c = word.charAt(i); + if (!(c >= '0' && c <= '9')) { return false; } - } - if (Integer.parseInt(word) > 255) { + } + if (Integer.parseInt(word) > 255) { return false; } - return true; - } + return true; + } - static boolean isValidHexChar(char c) { + static boolean isValidHexChar(char c) { - return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'F') - || (c >= 'a' && c <= 'f'); - } - - /** - * Takes a string and parses it to see if it is a valid IPV4 address. - * - * @return true, if the string represents an IPV4 address in dotted - * notation, false otherwise - */ - public static boolean isValidIPV4Address(String value) { + return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'F') + || (c >= 'a' && c <= 'f'); + } + + /** + * Takes a string and parses it to see if it is a valid IPV4 address. + * + * @return true, if the string represents an IPV4 address in dotted + * notation, false otherwise + */ + public static boolean isValidIPV4Address(String value) { // general test if (!value.matches("[\\p{Digit}\\.]*")) { return false; @@ -500,6 +500,6 @@ public class Inet6Util { } } return true; - } + } }