Return-Path: Delivered-To: apmail-harmony-commits-archive@www.apache.org Received: (qmail 37960 invoked from network); 14 Jun 2007 10:21:42 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 14 Jun 2007 10:21:42 -0000 Received: (qmail 1717 invoked by uid 500); 14 Jun 2007 10:21:45 -0000 Delivered-To: apmail-harmony-commits-archive@harmony.apache.org Received: (qmail 1707 invoked by uid 500); 14 Jun 2007 10:21:45 -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 1698 invoked by uid 99); 14 Jun 2007 10:21:45 -0000 Received: from herse.apache.org (HELO herse.apache.org) (140.211.11.133) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 14 Jun 2007 03:21:45 -0700 X-ASF-Spam-Status: No, hits=-99.5 required=10.0 tests=ALL_TRUSTED,NO_REAL_NAME X-Spam-Check-By: apache.org Received: from [140.211.11.3] (HELO eris.apache.org) (140.211.11.3) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 14 Jun 2007 03:21:41 -0700 Received: by eris.apache.org (Postfix, from userid 65534) id 4E34E1A981D; Thu, 14 Jun 2007 03:21:21 -0700 (PDT) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r547200 - /harmony/enhanced/classlib/branches/java6/modules/luni/src/main/java/java/net/InterfaceAddress.java Date: Thu, 14 Jun 2007 10:21:21 -0000 To: commits@harmony.apache.org From: tellison@apache.org X-Mailer: svnmailer-1.1.0 Message-Id: <20070614102121.4E34E1A981D@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: tellison Date: Thu Jun 14 03:21:20 2007 New Revision: 547200 URL: http://svn.apache.org/viewvc?view=rev&rev=547200 Log: Apply patch HARMONY-4153 ([classlib][luni][java6] New Class java.net.InterfaceAddress for java6) Added: harmony/enhanced/classlib/branches/java6/modules/luni/src/main/java/java/net/InterfaceAddress.java (with props) Added: harmony/enhanced/classlib/branches/java6/modules/luni/src/main/java/java/net/InterfaceAddress.java URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/branches/java6/modules/luni/src/main/java/java/net/InterfaceAddress.java?view=auto&rev=547200 ============================================================================== --- harmony/enhanced/classlib/branches/java6/modules/luni/src/main/java/java/net/InterfaceAddress.java (added) +++ harmony/enhanced/classlib/branches/java6/modules/luni/src/main/java/java/net/InterfaceAddress.java Thu Jun 14 03:21:20 2007 @@ -0,0 +1,178 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package java.net; + +/** + * This class is a Network Interface address. If the address is an IPv4 one, it + * contains an IP address, a subnet mask and a broadcast address. If it is an + * IPv6 one, there is a network prefix length, an IPv6 prefix and an IP address. + * + * @since 1.6 + */ +public class InterfaceAddress { + + private InetAddress addr; + + // the subnet mask when IPv4 or null when IPv6 + private InetAddress mask; + + // cache broadcast address for performance concern + private InetAddress cacheBroadcast = new InetAddress(null); + + private short prefixLength; + + InterfaceAddress(InetAddress address, short prefix) { + addr = address; + prefixLength = prefix; + + if (addr != null) { + mask = calSubnetMask(); + } + } + + private InetAddress calSubnetMask() { + // if the address is ipv4, calculate its subnet mask address + if (addr instanceof Inet4Address) { + byte[] maskAddr = new byte[4]; + for (int i = 0; i < prefixLength; i++) { + maskAddr[i / 8] |= 1 << (7 - i % 8); + } + return new Inet4Address(maskAddr); + } + return null; + } + + /** + * Answers whether this object is equal to another one. Returns true when + * the address, broadcast address and prefix length are all equal. + * + * @param obj + * the object to be compared. + * @return true if two interface addresses equals, false otherwise + */ + @Override + public boolean equals(Object obj) { + if(obj == this){ + return true; + } + if (obj instanceof InterfaceAddress) { + InterfaceAddress anotherInterAddr = (InterfaceAddress) obj; + boolean equals = addr == null ? anotherInterAddr.getAddress() == null + : addr.equals(anotherInterAddr.getAddress()); + if (equals + && (anotherInterAddr.getNetworkPrefixLength() == prefixLength)) { + InetAddress boardcast = cacheBroadcast.ipaddress == null ? + getBroadcast() : cacheBroadcast; + InetAddress anotherBoardcast = anotherInterAddr.getBroadcast(); + equals = boardcast == null ? anotherBoardcast == null + : boardcast.equals(anotherBoardcast); + } + return equals; + } + return false; + } + + /** + * Answers a hashcode for this Interface address. + * + * @return a hash code value. + */ + @Override + public int hashCode() { + int hashCode = addr == null ? 0 : -addr.hashCode(); + InetAddress boardcast = cacheBroadcast.ipaddress == null ? + getBroadcast() : cacheBroadcast; + hashCode += boardcast == null ? 0 : boardcast.hashCode(); + hashCode += prefixLength; + return hashCode; + } + + /** + * Answers the string representation for this interface address. The string + * follows the form: InetAddress / prefix length [ broadcast address ]. + * + * @return a string representation of this interface address. + */ + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + if (addr != null) { + builder.append(addr.toString()); + } + builder.append("/"); //$NON-NLS-1$ + builder.append(prefixLength); + builder.append(" ["); //$NON-NLS-1$ + InetAddress broadcast = cacheBroadcast.ipaddress == null ? + getBroadcast() : cacheBroadcast; + if (broadcast != null) { + builder.append(broadcast.toString()); + } else { + builder.append("null"); //$NON-NLS-1$ + } + builder.append("]"); //$NON-NLS-1$ + return builder.toString(); + } + + /** + * Answers an InetAddress for this address. + * + * @return the InetAddress for this address. + */ + public InetAddress getAddress() { + return addr; + } + + /** + * Answers an InetAddress for the brodcast address. + * + * If the address is an IPv6 one, returns null. + * + * @return the InetAddress that represents the broadcast address or null if + * there is no broadcast address. + */ + public InetAddress getBroadcast() { + if (addr instanceof Inet4Address && mask instanceof Inet4Address) { + if (cacheBroadcast.ipaddress == null) { + byte[] broadcast = new byte[4]; + if (prefixLength > 0) { + byte[] maskBytes = mask.getAddress(); + byte[] addrBytes = addr.getAddress(); + for (int i = 0; i < broadcast.length; i++) { + broadcast[i] = (byte) (addrBytes[i] | ~maskBytes[i]); + } + } + cacheBroadcast = new InetAddress(broadcast); + } + return cacheBroadcast; + } + return null; + } + + /** + * Answers the network prefix length for the InetAddress. + * + * If the address is an IPv4 one, returns the length of the subnet mask. + * + * @return a short representing the prefix length for the subnet mask of + * this address. + * + */ + public short getNetworkPrefixLength() { + return prefixLength; + } +} Propchange: harmony/enhanced/classlib/branches/java6/modules/luni/src/main/java/java/net/InterfaceAddress.java ------------------------------------------------------------------------------ svn:eol-style = native