added ipv4 type
[lwes-java.git] / src / main / java / org / lwes / serializer / Deserializer.java
blobf57a66c3d2399819d3e4d8f511baaf840ec31f36
1 package org.lwes.serializer;
3 import org.apache.commons.logging.Log;
4 import org.apache.commons.logging.LogFactory;
5 import org.lwes.Event;
6 import org.lwes.util.EncodedString;
7 import org.lwes.util.NumberCodec;
9 import java.net.Inet4Address;
10 import java.net.InetAddress;
11 import java.net.UnknownHostException;
12 import java.util.ArrayList;
13 import java.util.List;
15 /**
16 * This encapuslates the information needed to deserialize the base types
17 * of the event system.
19 * @author Anthony Molinaro
20 * @author Michael P. Lum
21 * @author Frank Maritato
23 public class Deserializer {
25 private static transient Log log = LogFactory.getLog(Deserializer.class);
27 /**
28 * Deserialize a byte out of the byte array <tt>bytes</tt>
30 * @param myState the DeserializeState object giving the current index
31 * in the byte array <tt>bytes</tt>
32 * @param bytes the bytes to deserialize
33 * @return a byte.
35 public static byte deserializeBYTE(DeserializerState myState, byte[] bytes) {
36 byte aByte = bytes[myState.currentIndex()];
37 myState.incr(1);
38 return aByte;
41 /**
42 * Deserialize a boolean value out of the byte array <tt>bytes</tt>
44 * @param myState the DeserializeState object giving the current index
45 * in the byte array <tt>bytes</tt>
46 * @param bytes the bytes to deserialize
47 * @return a boolean.
49 public static boolean deserializeBOOLEAN(DeserializerState myState,
50 byte[] bytes) {
51 byte aBooleanAsByte = Deserializer.deserializeBYTE(myState, bytes);
52 return aBooleanAsByte != (byte) 0x00;
55 /**
56 * Deserialize an int16 out of the byte array <tt>bytes</tt>
58 * @param myState the DeserializeState object giving the current index
59 * in the byte array <tt>bytes</tt>
60 * @param bytes the bytes to deserialize
61 * @return a short.
63 public static short deserializeINT16(DeserializerState myState, byte[] bytes) {
64 /* deserialize in net order (i.e. Big Endian) */
65 int off = myState.currentIndex();
66 short aShort = (short) (((bytes[off + 1] & 0xFF) << 0) +
67 ((bytes[off + 0]) << 8));
68 myState.incr(2);
69 return aShort;
72 /**
73 * Deserialize a uint16 out of the byte array <tt>bytes</tt>
75 * @param myState the DeserializeState object giving the current index
76 * in the byte array <tt>bytes</tt>
77 * @param bytes the bytes to deserialize
78 * @return an int containing the unsigned short.
80 public static int deserializeUINT16(DeserializerState myState, byte[] bytes) {
82 /* deserialize in net order (i.e. Big Endian) */
83 int anUnsignedShort = (int)
84 ((((int) bytes[myState.currentIndex()] << 8) & 0x0000ff00)
85 | (((int) bytes[myState.currentIndex() + 1] << 0) & 0x000000ff));
87 myState.incr(2);
88 return anUnsignedShort;
91 /**
92 * Deserialize an int32 out of the byte array <tt>bytes</tt>
94 * @param myState the DeserializeState object giving the current index
95 * in the byte array <tt>bytes</tt>
96 * @param bytes the bytes to deserialize
97 * @return an int.
99 public static int deserializeINT32(DeserializerState myState, byte[] bytes) {
100 int off = myState.currentIndex();
101 int anInt = ((bytes[off + 3] & 0xFF) << 0) +
102 ((bytes[off + 2] & 0xFF) << 8) +
103 ((bytes[off + 1] & 0xFF) << 16) +
104 ((bytes[off + 0]) << 24);
105 myState.incr(4);
106 return anInt;
111 * Deserialize a uint32 out of the byte array <tt>bytes</tt>
113 * @param myState the DeserializeState object giving the current index
114 * in the byte array <tt>bytes</tt>
115 * @param bytes the bytes to deserialize
116 * @return a long because java doesn't have unsigned types.
118 public static long deserializeUINT32(DeserializerState myState, byte[] bytes) {
119 long anUnsignedInt =
120 ((long)
121 ((((long) bytes[myState.currentIndex()] << 24) & 0x00000000ff000000L)
122 | (((long) bytes[myState.currentIndex() + 1] << 16) & 0x0000000000ff0000L)
123 | (((long) bytes[myState.currentIndex() + 2] << 8) & 0x000000000000ff00L)
124 | (((long) bytes[myState.currentIndex() + 3] << 0) & 0x00000000000000ffL)
127 myState.incr(4);
128 return anUnsignedInt;
132 * Deserialize a int64 out of the byte array <tt>bytes</tt>
134 * @param myState the DeserializeState object giving the current index
135 * in the byte array <tt>bytes</tt>
136 * @param bytes the bytes to deserialize
137 * @return a long.
139 public static long deserializeINT64(DeserializerState myState, byte[] bytes) {
140 int off = myState.currentIndex();
141 long aLong = ((bytes[off + 7] & 0xFFL) << 0) +
142 ((bytes[off + 6] & 0xFFL) << 8) +
143 ((bytes[off + 5] & 0xFFL) << 16) +
144 ((bytes[off + 4] & 0xFFL) << 24) +
145 ((bytes[off + 3] & 0xFFL) << 32) +
146 ((bytes[off + 2] & 0xFFL) << 40) +
147 ((bytes[off + 1] & 0xFFL) << 48) +
148 (((long) bytes[off + 0]) << 56);
149 myState.incr(8);
150 return aLong;
154 * Deserialize a uint64 out of the byte array <tt>bytes</tt>
156 * @param myState the DeserializeState object giving the current index
157 * in the byte array <tt>bytes</tt>
158 * @param bytes the bytes to deserialize
159 * @return a long (because java doesn't have unsigned types do not expect
160 * to do any math on this).
162 public static long deserializeUINT64(DeserializerState myState, byte[] bytes) {
163 long aLong = NumberCodec.decodeLongUnchecked(bytes, myState.currentIndex());
164 myState.incr(8);
165 return aLong;
168 public static String deserializeUINT64toHexString(DeserializerState myState,
169 byte[] bytes) {
170 String aString =
171 NumberCodec.byteArrayToHexString(bytes, myState.currentIndex(), 8);
172 myState.incr(8);
173 return aString;
176 public static InetAddress deserializeIPV4(DeserializerState myState, byte[] bytes)
177 throws UnknownHostException {
178 int offset = myState.currentIndex();
179 byte[] b = new byte[4];
180 b[0] = bytes[offset];
181 b[1] = bytes[offset + 1];
182 b[2] = bytes[offset + 2];
183 b[3] = bytes[offset + 3];
184 myState.incr(4);
185 return Inet4Address.getByAddress(b);
188 public static Double deserializeDOUBLE(DeserializerState myState, byte[] bytes) {
189 int off = myState.currentIndex();
190 long j = ((bytes[off + 7] & 0xFFL) << 0) +
191 ((bytes[off + 6] & 0xFFL) << 8) +
192 ((bytes[off + 5] & 0xFFL) << 16) +
193 ((bytes[off + 4] & 0xFFL) << 24) +
194 ((bytes[off + 3] & 0xFFL) << 32) +
195 ((bytes[off + 2] & 0xFFL) << 40) +
196 ((bytes[off + 1] & 0xFFL) << 48) +
197 (((long) bytes[off + 0]) << 56);
198 myState.incr(8);
199 return Double.longBitsToDouble(j);
202 public static Float deserializeFLOAT(DeserializerState myState, byte[] bytes) {
203 int off = myState.currentIndex();
204 int i = ((bytes[off + 3] & 0xFF) << 0) +
205 ((bytes[off + 2] & 0xFF) << 8) +
206 ((bytes[off + 1] & 0xFF) << 16) +
207 ((bytes[off + 0]) << 24);
208 myState.incr(4);
209 return Float.intBitsToFloat(i);
213 * Deserialize an ip_addr out of the byte array <tt>bytes</tt>
215 * @param myState the DeserializeState object giving the current index
216 * in the byte array <tt>bytes</tt>
217 * @param bytes the bytes to deserialize
218 * @return a byte array with the ip_addr with byte order 1234.
220 public static byte[] deserializeIPADDR(DeserializerState myState, byte[] bytes) {
221 byte[] inetaddr = new byte[4];
222 inetaddr[0] = bytes[myState.currentIndex() + 3];
223 inetaddr[1] = bytes[myState.currentIndex() + 2];
224 inetaddr[2] = bytes[myState.currentIndex() + 1];
225 inetaddr[3] = bytes[myState.currentIndex()];
226 myState.incr(4);
227 return inetaddr;
230 public static String deserializeIPADDRtoHexString(DeserializerState myState,
231 byte[] bytes) {
232 String aString =
233 NumberCodec.byteArrayToHexString(bytes, myState.currentIndex(), 4);
234 myState.incr(4);
235 return aString;
238 public static List deserializeStringArray(DeserializerState state,
239 byte[] bytes,
240 short encoding) {
241 int length = deserializeUINT16(state, bytes);
242 List<String> rtn = new ArrayList<String>(length);
243 for (int i = 0; i < length; i++) {
244 rtn.add(deserializeSTRING(state, bytes, encoding));
246 return rtn;
249 public static List deserializeInt16Array(DeserializerState state,
250 byte[] bytes) {
251 int length = deserializeUINT16(state, bytes);
252 List<Short> rtn = new ArrayList<Short>(length);
253 for (int i = 0; i < length; i++) {
254 rtn.add(deserializeINT16(state, bytes));
256 return rtn;
259 public static List deserializeInt32Array(DeserializerState state,
260 byte[] bytes) {
261 int length = deserializeUINT16(state, bytes);
262 List<Integer> rtn = new ArrayList<Integer>(length);
263 for (int i = 0; i < length; i++) {
264 rtn.add(deserializeINT32(state, bytes));
266 return rtn;
269 public static List deserializeInt64Array(DeserializerState state,
270 byte[] bytes) {
271 int length = deserializeUINT16(state, bytes);
272 List<Long> rtn = new ArrayList<Long>(length);
273 for (int i = 0; i < length; i++) {
274 rtn.add(deserializeINT64(state, bytes));
276 return rtn;
279 public static List deserializeUInt16Array(DeserializerState state,
280 byte[] bytes) {
281 int length = deserializeUINT16(state, bytes);
282 List<Integer> rtn = new ArrayList<Integer>(length);
283 for (int i = 0; i < length; i++) {
284 rtn.add(deserializeUINT16(state, bytes));
286 return rtn;
289 public static List deserializeUInt32Array(DeserializerState state,
290 byte[] bytes) {
291 int length = deserializeUINT16(state, bytes);
292 List<Long> rtn = new ArrayList<Long>(length);
293 for (int i = 0; i < length; i++) {
294 rtn.add(deserializeUINT32(state, bytes));
296 return rtn;
299 public static List deserializeUInt64Array(DeserializerState state,
300 byte[] bytes) {
301 int length = deserializeUINT16(state, bytes);
302 List<Long> rtn = new ArrayList<Long>(length);
303 for (int i = 0; i < length; i++) {
304 rtn.add(deserializeUINT64(state, bytes));
306 return rtn;
309 public static List deserializeBooleanArray(DeserializerState state,
310 byte[] bytes) {
311 int length = deserializeUINT16(state, bytes);
312 List<Boolean> rtn = new ArrayList<Boolean>(length);
313 for (int i = 0; i < length; i++) {
314 rtn.add(deserializeBOOLEAN(state, bytes));
316 return rtn;
319 public static List deserializeByteArray(DeserializerState state,
320 byte[] bytes) {
321 int length = deserializeUINT16(state, bytes);
322 List<Byte> rtn = new ArrayList<Byte>(length);
323 for (int i = 0; i < length; i++) {
324 rtn.add(deserializeBYTE(state, bytes));
326 return rtn;
329 public static List deserializeDoubleArray(DeserializerState state,
330 byte[] bytes) {
331 int length = deserializeUINT16(state, bytes);
332 List<Double> rtn = new ArrayList<Double>(length);
333 for (int i = 0; i < length; i++) {
334 rtn.add(deserializeDOUBLE(state, bytes));
336 return rtn;
339 public static List deserializeFloatArray(DeserializerState state,
340 byte[] bytes) {
341 int length = deserializeUINT16(state, bytes);
342 List<Float> rtn = new ArrayList<Float>(length);
343 for (int i = 0; i < length; i++) {
344 rtn.add(deserializeFLOAT(state, bytes));
346 return rtn;
349 public static List deserializeIPV4Array(DeserializerState state,
350 byte[] bytes) {
351 int length = deserializeUINT16(state, bytes);
352 List<InetAddress> rtn = new ArrayList<InetAddress>(length);
353 for (int i = 0; i < length; i++) {
354 try {
355 rtn.add(deserializeIPV4(state, bytes));
357 catch (UnknownHostException e) {
358 log.error(e.getMessage(), e);
361 return rtn;
365 * Deserialize a String out of the byte array <tt>bytes</tt>
367 * @param myState the DeserializeState object giving the current index
368 * in the byte array <tt>bytes</tt>
369 * @param bytes the bytes to deserialize
370 * @return a String.
371 * @deprecated
373 public static String deserializeSTRING(DeserializerState myState, byte[] bytes) {
374 return deserializeSTRING(myState, bytes, Event.DEFAULT_ENCODING);
377 public static String deserializeSTRING(DeserializerState myState,
378 byte[] bytes, short encoding) {
379 String aString = null;
380 int len = -1;
381 try {
382 len = deserializeUINT16(myState, bytes);
384 if (log.isDebugEnabled()) {
385 log.debug("Datagram Bytes: " +
386 NumberCodec.byteArrayToHexString(bytes, 0, bytes.length));
387 log.debug("String Length: " + len);
388 log.debug("State: " + myState);
391 aString = EncodedString.bytesToString(bytes, myState.currentIndex(), len,
392 Event.ENCODING_STRINGS[encoding]);
393 myState.incr(len);
395 catch (ArrayIndexOutOfBoundsException aioobe) {
396 if (log.isInfoEnabled()) {
397 log.info("Exception: " + aioobe.toString());
398 log.info("Datagram Bytes: " +
399 NumberCodec.byteArrayToHexString(bytes, 0, bytes.length));
400 log.info("String Length: " + len);
401 log.info("State: " + myState);
404 return aString;
408 * Deserialize a String out of the byte array <tt>bytes</tt> which
409 * represents an Event name.
411 * @param myState the DeserializeState object giving the current index
412 * in the byte array <tt>bytes</tt>
413 * @param bytes the bytes to deserialize
414 * @return a String.
416 public static String deserializeEVENTWORD(DeserializerState myState,
417 byte[] bytes) {
418 return deserializeEVENTWORD(myState, bytes, Event.DEFAULT_ENCODING);
421 public static String deserializeEVENTWORD(DeserializerState myState,
422 byte[] bytes, short encoding) {
423 String aString = null;
424 int len = -1;
425 try {
426 len = (int) deserializeBYTE(myState, bytes);
428 if (log.isDebugEnabled()) {
429 log.debug("Datagram Bytes: " +
430 NumberCodec.byteArrayToHexString(bytes, 0, bytes.length));
431 log.debug("String Length: " + len);
432 log.debug("State: " + myState);
435 aString = EncodedString.bytesToString(bytes, myState.currentIndex(), len,
436 Event.ENCODING_STRINGS[encoding]);
437 myState.incr(len);
439 catch (ArrayIndexOutOfBoundsException aioobe) {
440 if (log.isInfoEnabled()) {
441 log.info("Exception: " + aioobe.toString());
442 log.info("Datagram Bytes: " +
443 NumberCodec.byteArrayToHexString(bytes, 0, bytes.length));
444 log.info("String Length: " + len);
445 log.info("State: " + myState);
448 return aString;
452 * Deserialize a String out of the byte array <tt>bytes</tt> which
453 * represents an Attribute name.
455 * @param myState the DeserializeState object giving the current index
456 * in the byte array <tt>bytes</tt>
457 * @param bytes the bytes to deserialize
458 * @return a String.
460 public static String deserializeATTRIBUTEWORD(DeserializerState myState,
461 byte[] bytes) {
462 return deserializeEVENTWORD(myState, bytes, Event.DEFAULT_ENCODING);