Class IPv4Address

java.lang.Object
org.opennms.protocols.ip.IPv4Address
All Implemented Interfaces:
Serializable

public class IPv4Address extends Object implements 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

    Constructors
    Constructor
    Description
    Constructs 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.
    Creates a new IPv4Address from the passed InetAddress object.
    Constructs a new address object based upon the value of the first object.
  • Method Summary

    Modifier and Type
    Method
    Description
    static String
    addressToString(byte[] buf)
    Converts the passed IPv4 address buffer to a dotted decimal IP address string.
    static String
    addressToString(int ipv4Addr)
    Converts the passed 32-bit IPv4 address to a dotted decimal IP address string.
    boolean
    Test to determine if the passed object is equal to self.
    int
    Returns the 32-bit IPv4 address.
    byte[]
    Returns the IPv4 address in the form of an array of 4 bytes
    int
    If you override equals() you need to make a hashCode implementation as well
    void
    setAddress(byte[] addr)
    Sets the object based on the value stored in the passed array.
    void
    setAddress(int ipv4Addr)
    Sets the address object based on the 32-bit passed value.
    void
    setAddress(String ipv4Addr)
    Sets the object by decomposing the passed string into it four components.
    void
    Sets the IPv4Address from the passed InetAddress object.
    void
    Sets the current address based upon the value of the passed object.
    Converts the object to a string and returns the string to the caller.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • IPv4Address

      public IPv4Address()
      Constructs a new IPv4Address object. The default value for the object is "0.0.0.0"
    • IPv4Address

      public IPv4Address(IPv4Address second)
      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

      public IPv4Address(String ipv4Addr)
      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

      public IPv4Address(InetAddress addr)
      Creates a new IPv4Address from the passed InetAddress object.
      Parameters:
      addr - The Internet Address containing the IPv4 address.
  • Method Details

    • addressToString

      public static String addressToString(int ipv4Addr)
      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

      public static String addressToString(byte[] buf)
      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

      public void setAddress(IPv4Address second)
      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

      public void setAddress(String ipv4Addr)
      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

      public void setAddress(InetAddress addr)
      Sets the IPv4Address from the passed InetAddress object.
      Parameters:
      addr - The Internet Address containing the IPv4 address.
    • equals

      public boolean equals(Object obj)
      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.
      Overrides:
      equals in class Object
      Parameters:
      obj - The object to use in the comparison.
      Returns:
      True if equal, false if not equal.
    • hashCode

      public int hashCode()
      If you override equals() you need to make a hashCode implementation as well
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Converts the object to a string and returns the string to the caller.
      Overrides:
      toString in class Object
      Returns:
      The dotted decimal string for the address