Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/java/net/InterfaceAddress.java
38829 views
/*1* Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.net;2627/**28* This class represents a Network Interface address. In short it's an29* IP address, a subnet mask and a broadcast address when the address is30* an IPv4 one. An IP address and a network prefix length in the case31* of IPv6 address.32*33* @see java.net.NetworkInterface34* @since 1.635*/36public class InterfaceAddress {37private InetAddress address = null;38private Inet4Address broadcast = null;39private short maskLength = 0;4041/*42* Package private constructor. Can't be built directly, instances are43* obtained through the NetworkInterface class.44*/45InterfaceAddress() {46}4748/**49* Returns an {@code InetAddress} for this address.50*51* @return the {@code InetAddress} for this address.52*/53public InetAddress getAddress() {54return address;55}5657/**58* Returns an {@code InetAddress} for the broadcast address59* for this InterfaceAddress.60* <p>61* Only IPv4 networks have broadcast address therefore, in the case62* of an IPv6 network, {@code null} will be returned.63*64* @return the {@code InetAddress} representing the broadcast65* address or {@code null} if there is no broadcast address.66*/67public InetAddress getBroadcast() {68return broadcast;69}7071/**72* Returns the network prefix length for this address. This is also known73* as the subnet mask in the context of IPv4 addresses.74* Typical IPv4 values would be 8 (255.0.0.0), 16 (255.255.0.0)75* or 24 (255.255.255.0). <p>76* Typical IPv6 values would be 128 (::1/128) or 10 (fe80::203:baff:fe27:1243/10)77*78* @return a {@code short} representing the prefix length for the79* subnet of that address.80*/81public short getNetworkPrefixLength() {82return maskLength;83}8485/**86* Compares this object against the specified object.87* The result is {@code true} if and only if the argument is88* not {@code null} and it represents the same interface address as89* this object.90* <p>91* Two instances of {@code InterfaceAddress} represent the same92* address if the InetAddress, the prefix length and the broadcast are93* the same for both.94*95* @param obj the object to compare against.96* @return {@code true} if the objects are the same;97* {@code false} otherwise.98* @see java.net.InterfaceAddress#hashCode()99*/100public boolean equals(Object obj) {101if (!(obj instanceof InterfaceAddress)) {102return false;103}104InterfaceAddress cmp = (InterfaceAddress) obj;105if ( !(address == null ? cmp.address == null : address.equals(cmp.address)) )106return false;107if ( !(broadcast == null ? cmp.broadcast == null : broadcast.equals(cmp.broadcast)) )108return false;109if (maskLength != cmp.maskLength)110return false;111return true;112}113114/**115* Returns a hashcode for this Interface address.116*117* @return a hash code value for this Interface address.118*/119public int hashCode() {120return address.hashCode() + ((broadcast != null) ? broadcast.hashCode() : 0) + maskLength;121}122123/**124* Converts this Interface address to a {@code String}. The125* string returned is of the form: InetAddress / prefix length [ broadcast address ].126*127* @return a string representation of this Interface address.128*/129public String toString() {130return address + "/" + maskLength + " [" + broadcast + "]";131}132133}134135136