Go to the documentation of this file.00001 import java.net.InetAddress;
00002 import java.net.UnknownHostException;
00003
00004
00005
00006
00007
00008
00009
00010 public class RawIpAddress
00011 {
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 private byte[] addr = null;
00023
00024
00025
00026
00027 public RawIpAddress( RawIpAddress original )
00028 {
00029 this.addr = new byte[ original.addr.length ];
00030 this.set( original );
00031 }
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042 public RawIpAddress( String hostname ) throws UnknownHostException
00043 {
00044 this.addr = InetAddress.getByName( hostname ).getAddress ();
00045 }
00046
00047
00048
00049
00050
00051
00052
00053 public InetAddress getInetAddress () throws UnknownHostException
00054 {
00055 return InetAddress.getByAddress( this.addr );
00056 }
00057
00058
00059
00060
00061 public boolean isSameVersion( RawIpAddress a2 )
00062 {
00063 return this.addr.length == a2.addr.length;
00064 }
00065
00066
00067
00068
00069 public void set( RawIpAddress a2 )
00070 {
00071 assert ! isSameVersion( a2 )
00072 : "We can do arithmetics only with addresses of the same IP version!";
00073
00074 System.arraycopy( a2.addr, 0, this.addr, 0, a2.addr.length );
00075 }
00076
00077
00078
00079
00080 public void increase ()
00081 {
00082 int carry = 1;
00083
00084 for ( int i = addr.length - 1; i >= 0 && carry > 0; --i )
00085 {
00086 int sum = ( ( addr[i] + 0x100 ) & 0xFF ) + carry;
00087 carry = sum >> 8;
00088 addr[i] = (byte)( ( sum & 0xFF ) - 0x100 );
00089 }
00090 }
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112 public long subtract( RawIpAddress a2 )
00113 {
00114 assert ! isSameVersion( a2 )
00115 : "We can do arithmetics only with addresses of the same IP version!";
00116
00117 long delta = 0;
00118
00119 for ( int i = 0; i < this.addr.length; ++i )
00120 {
00121 int b1 = ( this.addr[i] + 0x100 ) & 0xFF;
00122 int b2 = ( a2.addr[i] + 0x100 ) & 0xFF;
00123
00124 delta <<= 8;
00125 delta += ( b1 - b2 );
00126 }
00127
00128 return delta;
00129 }
00130
00131
00132
00133
00134
00135
00136
00137 public int compare( RawIpAddress a2 )
00138 {
00139 assert ! isSameVersion( a2 )
00140 : "We can do arithmetics only with addresses of the same IP version!";
00141
00142 for ( int i = 0; i < this.addr.length; ++i )
00143 {
00144 int b1 = ( this.addr[i] + 0x100 ) & 0xFF;
00145 int b2 = ( a2.addr[i] + 0x100 ) & 0xFF;
00146
00147 if ( b1 < b2 ) {
00148 return -1;
00149 } else if ( b1 > b2 ) {
00150 return +1;
00151 } else {
00152
00153 }
00154 }
00155
00156 return 0;
00157 }
00158
00159
00160
00161
00162 public String toString ()
00163 {
00164 StringBuffer result = new StringBuffer ();
00165
00166 for( int i = 0; i < this.addr.length; i++ )
00167 {
00168 if ( i != 0 ) {
00169 result.append( "." );
00170 }
00171 result.append( ( this.addr[i] + 0x100 ) & 0xFF );
00172 }
00173
00174 return result.toString ();
00175 }
00176 }