com.voytechs.jnetstream.primitive.address
Class IpAddress

java.lang.Object
  extended by com.voytechs.jnetstream.primitive.address.Address
      extended by com.voytechs.jnetstream.primitive.address.IpAddress
All Implemented Interfaces:
java.io.Serializable

public class IpAddress
extends Address

A Class for storing IP Addresses Currently is only designed to utilize IPv4 (32bit) addresses. Class also contains utility methods for DNS lookups. There are various functions for converting the IP address to a LONG. Why would you want to use a long for storage of an IP address, in my case I store IP address in a database as an UNSIGNED INT, java does not have unsigned numbers so you have to go to next bigger primitive type to store it or do like other implementations do store the address in a byte array.

See Also:
Serialized Form

Field Summary
 
Fields inherited from class com.voytechs.jnetstream.primitive.address.Address
address, debug, DEFAULT_NUMBER_BASE, format, separator
 
Constructor Summary
IpAddress(byte[] address)
          Main constructor taking the array of bytes as the address.
IpAddress(java.lang.String hostname)
          Constructor taking a hostname.
 
Method Summary
 java.lang.String getCanonicalHostname()
          Gets the fully qualified domain name for this IP address.
 java.lang.String getDomainname()
          Do a domain name lookup by use of DNS reverse lookup on the IP addresses
 java.lang.String getHostname()
          Do a DNS lookup on our IP address by use of DNS reverse lookup.
 java.net.InetAddress getInetObject()
          Returns the java.net.InetAddress object associated with this IpAddress.
static void main(java.lang.String[] args)
          Test function for IpAddress
 java.lang.String toString()
          Convert to dot notation string representation of the address.
 
Methods inherited from class com.voytechs.jnetstream.primitive.address.Address
AND, AND, byteArrayValue, compare, compare, compareTo, EOR, EOR, equals, getByte, getByte, getRadix, INVERT, INVERT, longToByteArray, OR, OR, parseByteArray, setAddress, setByteArrayAddress, setFormat, setFormat, setRadix, setSeparator, stringValue, toByteArray, toString, toString
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

IpAddress

public IpAddress(byte[] address)
Main constructor taking the array of bytes as the address. Automatically sets the separator token between '.' and ':' depending on number of bytes in the address. 4 bytes IPv4 address with '.' separator and 32 bytes for IPv6 address with ':' as separator.

Parameters:
address - array of bytes making up the address.
Throws:
java.lang.IllegalArgumentException - if number of bytes is not 4 or 32.

IpAddress

public IpAddress(java.lang.String hostname)
          throws java.net.UnknownHostException
Constructor taking a hostname. This is a conveniece constructor which converts the hostname to numerical format internally. Currently only IPv4 addresses can be initialized this way due to java.net library limitations.

Parameters:
hostname - hostname or IP address in '.' notation initialize this address with.
Throws:
java.net.UnknownHostException - if hostname can not be converted to an IP address. Addresses in IPv4 '.' notation format always convert and this exception is never thrown.
Method Detail

getHostname

public java.lang.String getHostname()
Do a DNS lookup on our IP address by use of DNS reverse lookup.

Returns:
DNS name of the IP address if exists or null other wise.

getCanonicalHostname

public java.lang.String getCanonicalHostname()
Gets the fully qualified domain name for this IP address. Best effort method, meaning we may not be able to return the FQDN depending on the underlying system configuration. If there is a security manager, this method first calls its checkConnect method with the hostname and -1 as its arguments to see if the calling code is allowed to know the hostname for this IP address, i.e., to connect to the host. If the operation is not allowed, it will return the textual representation of the IP address.

Returns:
the fully qualified domain name for this IP address, or if the operation is not allowed by the security check, the textual representation of the IP address.
Since:
JNetStream 0.2.2

getInetObject

public java.net.InetAddress getInetObject()
Returns the java.net.InetAddress object associated with this IpAddress.

Returns:
InetAddress object associated with this IpAddress. If address does not exist and can not be resolved InetAddress object will be null and null will be returned as well.
Since:
JNetStream 0.2.2

getDomainname

public java.lang.String getDomainname()
Do a domain name lookup by use of DNS reverse lookup on the IP addresses

Returns:
domain name part of the hostname or null other wise.

toString

public java.lang.String toString()
Convert to dot notation string representation of the address. If hostname is known then hostname.domain is returned other wise the appropriate numerical representation of the numerical address is returned as a String.

Overrides:
toString in class Address
Returns:
IP address in the dot notation.

main

public static void main(java.lang.String[] args)
Test function for IpAddress

Parameters:
args - command line arguments