added ESF parser, serializer and event types
[lwes-java.git] / src / org / lwes / util / IPAddress.java
blob021d5869c28e67cb93a57dead969e92a991eb448
1 package org.lwes.util;
3 import java.net.InetAddress;
4 import java.net.UnknownHostException;
6 /**
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;
20 /**
21 * Defaut constructor
23 public IPAddress()
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;
33 /**
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
37 * given.
39 * @param anIPAddress the string representing the IPAddress
41 public IPAddress(String anIPAddress)
43 try {
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;
56 /**
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");
67 System.exit(-1);
69 inet_addr = anIPAddress;
72 /**
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();
83 /**
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;
94 else
96 try {
97 return InetAddress.getByName(toString());
98 } catch (UnknownHostException e) {}
99 return null;
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()
110 return inet_addr;
114 * Return the IPAddress in as an integer
116 * @return an integer representing the IPAddress in network order
118 public int toInt()
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()
145 return 4;
148 public boolean equals(Object o) {
149 if (! (o instanceof IPAddress)) {
150 /* "o" is not an IPAddress object (note: it may be null). */
151 return false;
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 */
158 return false;
160 for (int i=0; i<theseBytes.length; ++i) {
161 if (theseBytes[i] != otherBytes[i]) {
162 /* found a difference; not equal. */
163 return false;
166 return true; /* passed all tests; return true */
169 public int hashcode() {
170 int hash = 0;
171 final byte[] theseBytes = this.getInetAddressAsBytes();
172 for (int i=0; i<theseBytes.length; ++i) {
173 hash ^= theseBytes[i];
175 return hash;