Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

com.voytechs.jnetstream.primitive.address
Class IpNumber  view IpNumber download IpNumber.java

java.lang.Object
  extended bycom.voytechs.jnetstream.primitive.address.IpNumber
All Implemented Interfaces:
java.lang.Comparable, java.io.Serializable

public class IpNumber
extends java.lang.Object
implements java.io.Serializable, java.lang.Comparable

A Class for storing IP Addresses Currently is only designed to utilize IPv4 (32bit) byteArrayAddresses. There are various functions for converting the IP byteArrayAddress to a LONG. Why would you want to use a long for storage of an IP byteArrayAddress, in my case I store IP byteArrayAddress 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 byteArrayAddress in a byte array.


Field Summary
protected  byte[] byteArrayAddress
          Array which stores the IP byteArrayAddress bytes.
static boolean debug
           
private static int DEFAULT_NUMBER_BASE
           
private  int defaultNumberBase
           
 
Constructor Summary
protected IpNumber()
          Special internal use only constructor that does not initialize the IpNumber right away but later.
  IpNumber(byte[] byteArrayAddress)
          Main constructor taking the array of bytes as the byteArrayAddress
  IpNumber(IpNumber ip)
          Main constructor taking another IpNumber object
  IpNumber(long longAddress)
          Constructor taking a integer IPv4 value
  IpNumber(java.lang.String stringAddress)
          Constructor taking a integer IPv4 value
 
Method Summary
static byte[] AND(byte[] a, byte[] b)
          This method ANDs IpNumber object to another IpNumber object.
 byte[] AND(IpNumber b)
          This method ANDs IpNumber object to another IpNumber object.
 byte[] byteArrayValue()
          Gets the byte array representation of this address.
static int compare(IpNumber a, IpNumber b)
          Compare our byte values for IP address to the object's.
 int compareTo(java.lang.Object o)
          Compares this object with another, and returns a numerical result based on the comparison.
static byte[] EOR(byte[] a, byte[] b)
          This method ORs IpNumber object to another IpNumber object.
 byte[] EOR(IpNumber b)
          This method EORs (Exclusive OR) IpNumber object to another IpNumber object.
 boolean equals(java.lang.Object o)
          Compare our byte values for IP address to the object's.
static int getByte(byte b)
           
protected  int getByte(int index)
           
 byte[] INVERT()
          This method INVERTs (Java statment ~) IpNumber object to another IpNumber object.
static byte[] INVERT(byte[] a)
          This method ORs IpNumber object to another IpNumber object.
 long longValue()
          Return the value of this IpNumber object as a long.
static void main(java.lang.String[] args)
          Test function for IpNumber
static byte[] OR(byte[] a, byte[] b)
          This method ORs IpNumber object to another IpNumber object.
 byte[] OR(IpNumber b)
          This method ORs IpNumber object to another IpNumber object.
static byte[] parseByteArray(java.lang.String address)
          Return current IP address as byte array, for V4 that will be 4 bytes for V6 16.
static long parseLong(java.lang.String byteArrayAddress)
          Parse string containing a string representation of and IP byteArrayAddress in the dot notatio such as 1.2.3.4 to long.
protected  void setByteArrayAddress(byte[] address)
          Set the internal address array.
 int setDefaultNumberBase(int radix)
           
static int setGlobalDefaultNumberBase(int radix)
           
 java.lang.String stringValue()
          Get the IP number value in a dot notation.
 byte[] toByteArray()
          Returns a byte[] representing this IpNumber.
static byte[] toByteArray(long longAddress)
          Convert a long (32 bits) to byte array (4 bytes).
static long toLong(byte[] byteArray)
          Convert a byte array to a long.
 java.lang.String toString()
          Convert to dot notation string representation of the byteArrayAddress
static java.lang.String toString(byte[] byteArrayAddress)
          Convert internal byteArrayAddress to a string.
static java.lang.String toString(byte[] byteArrayAddress, int radix)
          Convert internal byteArrayAddress to a string.
static java.lang.String toString(long longAddress)
          Convert internal byteArrayAddress to a string.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

DEFAULT_NUMBER_BASE

private static int DEFAULT_NUMBER_BASE

defaultNumberBase

private int defaultNumberBase

debug

public static final boolean debug
See Also:
Constant Field Values

byteArrayAddress

protected byte[] byteArrayAddress
Array which stores the IP byteArrayAddress bytes. This is a variable array so that both v4 and v6 byteArrayAddresses.

Constructor Detail

IpNumber

protected IpNumber()
Special internal use only constructor that does not initialize the IpNumber right away but later.


IpNumber

public IpNumber(java.lang.String stringAddress)
Constructor taking a integer IPv4 value


IpNumber

public IpNumber(long longAddress)
Constructor taking a integer IPv4 value


IpNumber

public IpNumber(byte[] byteArrayAddress)
Main constructor taking the array of bytes as the byteArrayAddress


IpNumber

public IpNumber(IpNumber ip)
Main constructor taking another IpNumber object

Method Detail

setDefaultNumberBase

public int setDefaultNumberBase(int radix)

setGlobalDefaultNumberBase

public static int setGlobalDefaultNumberBase(int radix)

INVERT

public byte[] INVERT()
This method INVERTs (Java statment ~) IpNumber object to another IpNumber object. Every byte is ANDed with every other byte in the corresponding object. If number of bytes in the first number does not match the number in the second object then an IllegalArgumentException is thrown.


INVERT

public static byte[] INVERT(byte[] a)
This method ORs IpNumber object to another IpNumber object. Every byte is ANDed with every other byte in the corresponding object. If number of bytes in the first number does not match the number in the second object then an IllegalArgumentException is thrown.


EOR

public byte[] EOR(IpNumber b)
This method EORs (Exclusive OR) IpNumber object to another IpNumber object. Every byte is ANDed with every other byte in the corresponding object. If number of bytes in the first number does not match the number in the second object then an IllegalArgumentException is thrown.


EOR

public static byte[] EOR(byte[] a,
                         byte[] b)
This method ORs IpNumber object to another IpNumber object. Every byte is ANDed with every other byte in the corresponding object. If number of bytes in the first number does not match the number in the second object then an IllegalArgumentException is thrown.


OR

public byte[] OR(IpNumber b)
This method ORs IpNumber object to another IpNumber object. Every byte is ANDed with every other byte in the corresponding object. If number of bytes in the first number does not match the number in the second object then an IllegalArgumentException is thrown.


OR

public static byte[] OR(byte[] a,
                        byte[] b)
This method ORs IpNumber object to another IpNumber object. Every byte is ANDed with every other byte in the corresponding object. If number of bytes in the first number does not match the number in the second object then an IllegalArgumentException is thrown.


AND

public byte[] AND(IpNumber b)
This method ANDs IpNumber object to another IpNumber object. Every byte is ANDed with every other byte in the corresponding object. If number of bytes in the first number does not match the number in the second object then an IllegalArgumentException is thrown.


AND

public static byte[] AND(byte[] a,
                         byte[] b)
This method ANDs IpNumber object to another IpNumber object. Every byte is ANDed with every other byte in the corresponding object. If number of bytes in the first number does not match the number in the second object then an IllegalArgumentException is thrown.


getByte

protected int getByte(int index)

getByte

public static int getByte(byte b)

equals

public boolean equals(java.lang.Object o)
Compare our byte values for IP address to the object's. If length do not match then the objects are considered unequal. If byte counts are the same then a byte for byte comparison is made and appropriate result return.


compareTo

public int compareTo(java.lang.Object o)
Description copied from interface: java.lang.Comparable
Compares this object with another, and returns a numerical result based on the comparison. If the result is negative, this object sorts less than the other; if 0, the two are equal, and if positive, this object sorts greater than the other. To translate this into boolean, simply perform o1.compareTo(o2) <op> 0, where op is one of <, <=, =, !=, >, or >=.

You must make sure that the comparison is mutual, ie. sgn(x.compareTo(y)) == -sgn(y.compareTo(x)) (where sgn() is defined as -1, 0, or 1 based on the sign). This includes throwing an exception in either direction if the two are not comparable; hence, compareTo(null) should always throw an Exception.

You should also ensure transitivity, in two forms: x.compareTo(y) > 0 && y.compareTo(z) > 0 implies x.compareTo(z) > 0; and x.compareTo(y) == 0 implies x.compareTo(z) == y.compareTo(z).

Specified by:
compareTo in interface java.lang.Comparable

compare

public static int compare(IpNumber a,
                          IpNumber b)
                   throws java.lang.IllegalArgumentException
Compare our byte values for IP address to the object's. If length do not match then the one with smaller number of bytes is considered less then the one with more bytes (V6 is always > V4). If byte counts are the same then a byte for byte comparison is made and appropriate result return.


toByteArray

public static byte[] toByteArray(long longAddress)
Convert a long (32 bits) to byte array (4 bytes).


toByteArray

public byte[] toByteArray()
Returns a byte[] representing this IpNumber.


toLong

public static long toLong(byte[] byteArray)
                   throws java.lang.IllegalArgumentException
Convert a byte array to a long. The byte array has to be 4 bytes in size other wise an IllegalArgumentException is thrown.


longValue

public long longValue()
Return the value of this IpNumber object as a long. An IllegalArgumentException is thrown if address is not V4 type address and can not fit into a 32bit long.


byteArrayValue

public byte[] byteArrayValue()
Gets the byte array representation of this address.


stringValue

public java.lang.String stringValue()
Get the IP number value in a dot notation.


setByteArrayAddress

protected void setByteArrayAddress(byte[] address)
Set the internal address array.


parseByteArray

public static byte[] parseByteArray(java.lang.String address)
                             throws java.lang.IllegalArgumentException
Return current IP address as byte array, for V4 that will be 4 bytes for V6 16.


toString

public java.lang.String toString()
Convert to dot notation string representation of the byteArrayAddress


parseLong

public static long parseLong(java.lang.String byteArrayAddress)
Parse string containing a string representation of and IP byteArrayAddress in the dot notatio such as 1.2.3.4 to long.


toString

public static java.lang.String toString(long longAddress)
Convert internal byteArrayAddress to a string. Use appropriate notation based on the byteArrayAddress type. Dot notation for v4 and colon notation for v6.


toString

public static java.lang.String toString(byte[] byteArrayAddress)
Convert internal byteArrayAddress to a string. Use appropriate notation based on the byteArrayAddress type. Dot notation for v4 and colon notation for v6.


toString

public static java.lang.String toString(byte[] byteArrayAddress,
                                        int radix)
Convert internal byteArrayAddress to a string. Use appropriate notation based on the byteArrayAddress type. Dot notation for v4 and colon notation for v6.


main

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