Class IPAddress

java.lang.Object
org.n52.janmayen.net.IPAddress
All Implemented Interfaces:
Comparable<IPAddress>

public class IPAddress extends Object implements Comparable<IPAddress>
Encapsulation of an IP address.
  • Field Details

  • Constructor Details

    • IPAddress

      public IPAddress(int address)
      Creates a new IPAddress from an 32-Bit integer.
      Parameters:
      address - the address
    • IPAddress

      public IPAddress(long upperBits, long lowerBits)
      Creates a new IPAddress from two 64-Bit integers.
      Parameters:
      upperBits - the 64 upper bits of the address
      lowerBits - the 64 lower bits of the address
    • IPAddress

      public IPAddress(byte[] address)
      Creates a new IPAddress from an four element byte array.
      Parameters:
      address - the address
    • IPAddress

      public IPAddress(String address)
      Creates a new IPAddress from its string representation.
      Parameters:
      address - the address
    • IPAddress

      public IPAddress(InetAddress address)
      Creates a new IPAddress from an InetAddress.
      Parameters:
      address - the address
  • Method Details

    • isIPv4

      public boolean isIPv4()
      Checks if this address is IPv4.
      Returns:
      if it is IPv4
    • getBitSize

      public int getBitSize()
      Get the number of bits the address contains.
      Returns:
      the number of bits
    • getByteSize

      public int getByteSize()
      Get the number of bytes the address contains.
      Returns:
      the number of bytes
    • isIPv6

      public boolean isIPv6()
      Checks if this address is IPv6.
      Returns:
      if it is IPv6
    • getInetAddress

      public InetAddress getInetAddress()
      Returns:
      the IP address as an Inet4Address
    • getBytes

      public byte[] getBytes()
      Returns:
      the IP address as an byte array.
    • compareTo

      public int compareTo(IPAddress o)
      Specified by:
      compareTo in interface Comparable<IPAddress>
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • compareLittleEndianUnsignedByte

      static int compareLittleEndianUnsignedByte(byte[] a, byte[] b)
      Compares to byte arrays. Each element is compared with the corresponding element in the other array. If the array sizes differ the shorter array is treated as it would contain leading zeros.
      Parameters:
      a - the first byte array
      b - the second byte array
      Returns:
      the value 0 if a == b; a value less than 0 if a < b; and a value greater than 0 if a > b
    • getV4Pattern

      public static Pattern getV4Pattern()
      Get the regular expression matching an IPv4. No capturing groups are included in the pattern.
      Returns:
      the pattern
    • getV6Pattern

      public static Pattern getV6Pattern()
      Get the regular expression matching an IPv6. No capturing groups are included in the pattern.
      Returns:
      the pattern