Package org.opennms.protocols.ip
Class IPv4Address
java.lang.Object
org.opennms.protocols.ip.IPv4Address
- All Implemented Interfaces:
Serializable
Represents an Internet Protocol version 4 address. An IPv4 address is a
32-bit address that can be considered four eight bit octets. Each octet
represents a number in the range of [0..256). A string representation is a
dotted decimal address in the form of "xxx.xxx.xxx.xxx" where xxx is a single
octet.
The main purpose of the class is to represent an IPv4 Address without the
associated lookup baggage in the java.net.InetAddress class.
- Author:
- Brian Weaver
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionConstructs a new IPv4Address object.IPv4Address(byte[] addr) Constructs a new object based on the value stored in the passed array.IPv4Address(int ipv4Addr) Constructs a new address object based on the 32-bit passed value.IPv4Address(String ipv4Addr) Creates a new object by decomposing the passed string into it four components.IPv4Address(InetAddress addr) Creates a new IPv4Address from the passed InetAddress object.IPv4Address(IPv4Address second) Constructs a new address object based upon the value of the first object. -
Method Summary
Modifier and TypeMethodDescriptionstatic StringaddressToString(byte[] buf) Converts the passed IPv4 address buffer to a dotted decimal IP address string.static StringaddressToString(int ipv4Addr) Converts the passed 32-bit IPv4 address to a dotted decimal IP address string.booleanTest to determine if the passed object is equal to self.intReturns the 32-bit IPv4 address.byte[]Returns the IPv4 address in the form of an array of 4 bytesinthashCode()If you override equals() you need to make a hashCode implementation as wellvoidsetAddress(byte[] addr) Sets the object based on the value stored in the passed array.voidsetAddress(int ipv4Addr) Sets the address object based on the 32-bit passed value.voidsetAddress(String ipv4Addr) Sets the object by decomposing the passed string into it four components.voidsetAddress(InetAddress addr) Sets the IPv4Address from the passed InetAddress object.voidsetAddress(IPv4Address second) Sets the current address based upon the value of the passed object.toString()Converts the object to a string and returns the string to the caller.
-
Constructor Details
-
IPv4Address
public IPv4Address()Constructs a new IPv4Address object. The default value for the object is "0.0.0.0" -
IPv4Address
Constructs a new address object based upon the value of the first object.- Parameters:
second- The object to copy the address from.
-
IPv4Address
public IPv4Address(byte[] addr) Constructs a new object based on the value stored in the passed array. An IPv4 Address is 32-bits in length, thus the array must have exactly 4 elements.- Parameters:
addr- The IPv4Address data- Throws:
IllegalArgumentException- Thrown if the passed buffer is not in the correct format for an IPv4Address.
-
IPv4Address
public IPv4Address(int ipv4Addr) Constructs a new address object based on the 32-bit passed value. The 32-bit integer is split into four eight bit values that represent the IPv4 address.- Parameters:
ipv4Addr- The 32-bit IP address.
-
IPv4Address
Creates a new object by decomposing the passed string into it four components. The string must be in the format of "xxx.xxx.xxx.xxx" where xxx is in the range of [0..256).- Parameters:
ipv4Addr- The dotted decimal address.- Throws:
IllegalArgumentException- Thrown if the string is a malformed dotted decimal address.
-
IPv4Address
Creates a new IPv4Address from the passed InetAddress object.- Parameters:
addr- The Internet Address containing the IPv4 address.
-
-
Method Details
-
addressToString
Converts the passed 32-bit IPv4 address to a dotted decimal IP address string.- Parameters:
ipv4Addr- The 32-bit address- Returns:
- The dotted decimal address in the format "xxx.xxx.xxx.xxx" where 0 <= xxx < 256
-
addressToString
Converts the passed IPv4 address buffer to a dotted decimal IP address string.- Parameters:
buf- The 4 byte buffer- Returns:
- The dotted decimal address in the format "xxx.xxx.xxx.xxx" where 0 <= xxx < 256
- Throws:
IllegalArgumentException- Thrown if the buffer is not exactly 4 bytes in length.
-
getAddress
public int getAddress()Returns the 32-bit IPv4 address.- Returns:
- 32-bit IPv4 address
-
getAddressBytes
public byte[] getAddressBytes()Returns the IPv4 address in the form of an array of 4 bytes- Returns:
- the IPv4 address bytes
-
setAddress
Sets the current address based upon the value of the passed object.- Parameters:
second- The new address.
-
setAddress
public void setAddress(byte[] addr) Sets the object based on the value stored in the passed array. An IPv4 Address is 32-bits in length, thus the array must have exactly 4 elements.- Parameters:
addr- The IPv4Address data- Throws:
IllegalArgumentException- Thrown if the passed buffer is not in the correct format for an IPv4Address.
-
setAddress
public void setAddress(int ipv4Addr) Sets the address object based on the 32-bit passed value. The 32-bit integer is split into four eight bit values that represent the IPv4 address.- Parameters:
ipv4Addr- The 32-bit IP address.
-
setAddress
Sets the object by decomposing the passed string into it four components. The string must be in the format of "xxx.xxx.xxx.xxx" where xxx is in the range of [0..256).- Parameters:
ipv4Addr- The dotted decimal address.- Throws:
IllegalArgumentException- Thrown if the string is a malformed dotted decimal address.
-
setAddress
Sets the IPv4Address from the passed InetAddress object.- Parameters:
addr- The Internet Address containing the IPv4 address.
-
equals
Test to determine if the passed object is equal to self. The object may be an Integer, String, or IPv4Address. If the object is a String then it must be in the dotted decimal string format. -
hashCode
public int hashCode()If you override equals() you need to make a hashCode implementation as well -
toString
Converts the object to a string and returns the string to the caller.
-