3 import java
.net
.InetAddress
;
4 import java
.net
.UnknownHostException
;
7 * This is a wrapper class for InetAddress, which allows the setting of
8 * an InetAddress with a byte arrays. As well as other useful functions.
9 * @author Anthony Molinaro
11 public class IPAddress
implements java
.io
.Serializable
13 /* serializable UID */
14 static final long serialVersionUID
= -1;
16 /* inet address in network byte order */
17 private byte[] inet_addr
;
18 private InetAddress java_inet_addr
;
25 inet_addr
= new byte[4];
26 inet_addr
[0] = (byte)0;
27 inet_addr
[1] = (byte)0;
28 inet_addr
[2] = (byte)0;
29 inet_addr
[3] = (byte)0;
30 java_inet_addr
= null;
34 * Construct an IPAddress object with a string of the form
35 * "%d.%d.%d.%d" representing an InetAddress.
36 * The address defaults to 0.0.0.0 if an invalid address is
39 * @param anIPAddress the string representing the IPAddress
41 public IPAddress(String anIPAddress
)
44 java_inet_addr
= InetAddress
.getByName(anIPAddress
);
45 inet_addr
= java_inet_addr
.getAddress();
46 } catch (UnknownHostException e
) {
47 java_inet_addr
= null;
48 inet_addr
= new byte[4];
49 inet_addr
[0] = (byte)0;
50 inet_addr
[1] = (byte)0;
51 inet_addr
[2] = (byte)0;
52 inet_addr
[3] = (byte)0;
57 * Construct an IPAddress object with a byte array containing
58 * the InetAddress in little-endian notation.
60 * @param anIPAddress the byte array representing the IPAddress
62 public IPAddress(byte[] anIPAddress
)
64 if ( anIPAddress
.length
!= 4 )
66 System
.err
.println("ERROR : bad inet addr\n");
69 inet_addr
= anIPAddress
;
73 * Construct an IPAddress object with an InetAddress.
75 * @param anIPAddress the InetAddress to set this IPAddress to
77 public IPAddress(InetAddress anIPAddress
)
79 java_inet_addr
= anIPAddress
;
80 inet_addr
= java_inet_addr
.getAddress();
84 * Get the InetAddress representing this IPAddress
86 * @return the InetAddress representing this IPAddress
88 public InetAddress
toInetAddress()
90 if ( java_inet_addr
!= null )
92 return java_inet_addr
;
97 return InetAddress
.getByName(toString());
98 } catch (UnknownHostException e
) {}
104 * Return the IPAddress in a byte array in network order
106 * @return a byte array containing the IPAddress in network order
108 public byte[] getInetAddressAsBytes()
114 * Return the IPAddress in as an integer
116 * @return an integer representing the IPAddress in network order
120 return (int)((inet_addr
[0] << 24)
121 | (inet_addr
[1] << 16)
122 | (inet_addr
[2] << 8)
123 | (inet_addr
[3] << 0) );
127 * return a string representation of an IPAddress in the following
128 * format "%d.%d.%d.%d".
130 * @return a string representation of an IPAddress.
132 public String
toString()
134 return (((int)(inet_addr
[0]) & 0xff)
135 +"."+((int)(inet_addr
[1]) & 0xff)
136 +"."+((int)(inet_addr
[2]) & 0xff)
137 +"."+((int)(inet_addr
[3]) & 0xff));
141 * return the size that an IPAddress takes up in a serialized byte array
143 public int bytesStoreSize()
148 public boolean equals(Object o
) {
149 if (! (o
instanceof IPAddress
)) {
150 /* "o" is not an IPAddress object (note: it may be null). */
153 final IPAddress other
= (IPAddress
) o
;
154 final byte[] theseBytes
= this.getInetAddressAsBytes();
155 final byte[] otherBytes
= other
.getInetAddressAsBytes();
156 if (theseBytes
.length
!= otherBytes
.length
) {
157 /* same length or else not equal */
160 for (int i
=0; i
<theseBytes
.length
; ++i
) {
161 if (theseBytes
[i
] != otherBytes
[i
]) {
162 /* found a difference; not equal. */
166 return true; /* passed all tests; return true */
169 public int hashcode() {
171 final byte[] theseBytes
= this.getInetAddressAsBytes();
172 for (int i
=0; i
<theseBytes
.length
; ++i
) {
173 hash ^
= theseBytes
[i
];