1 package org
.lwes
.serializer
;
3 import org
.apache
.commons
.logging
.Log
;
4 import org
.apache
.commons
.logging
.LogFactory
;
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
;
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);
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
35 public static byte deserializeBYTE(DeserializerState myState
, byte[] bytes
) {
36 byte aByte
= bytes
[myState
.currentIndex()];
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
49 public static boolean deserializeBOOLEAN(DeserializerState myState
,
51 byte aBooleanAsByte
= Deserializer
.deserializeBYTE(myState
, bytes
);
52 return aBooleanAsByte
!= (byte) 0x00;
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
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));
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));
88 return anUnsignedShort
;
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
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);
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
) {
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
)
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
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);
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());
168 public static String
deserializeUINT64toHexString(DeserializerState myState
,
171 NumberCodec
.byteArrayToHexString(bytes
, myState
.currentIndex(), 8);
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];
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);
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);
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()];
230 public static String
deserializeIPADDRtoHexString(DeserializerState myState
,
233 NumberCodec
.byteArrayToHexString(bytes
, myState
.currentIndex(), 4);
238 public static List
deserializeStringArray(DeserializerState state
,
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
));
249 public static List
deserializeInt16Array(DeserializerState state
,
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
));
259 public static List
deserializeInt32Array(DeserializerState state
,
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
));
269 public static List
deserializeInt64Array(DeserializerState state
,
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
));
279 public static List
deserializeUInt16Array(DeserializerState state
,
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
));
289 public static List
deserializeUInt32Array(DeserializerState state
,
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
));
299 public static List
deserializeUInt64Array(DeserializerState state
,
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
));
309 public static List
deserializeBooleanArray(DeserializerState state
,
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
));
319 public static List
deserializeByteArray(DeserializerState state
,
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
));
329 public static List
deserializeDoubleArray(DeserializerState state
,
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
));
339 public static List
deserializeFloatArray(DeserializerState state
,
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
));
349 public static List
deserializeIPV4Array(DeserializerState state
,
351 int length
= deserializeUINT16(state
, bytes
);
352 List
<InetAddress
> rtn
= new ArrayList
<InetAddress
>(length
);
353 for (int i
= 0; i
< length
; i
++) {
355 rtn
.add(deserializeIPV4(state
, bytes
));
357 catch (UnknownHostException e
) {
358 log
.error(e
.getMessage(), e
);
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
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;
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
]);
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
);
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
416 public static String
deserializeEVENTWORD(DeserializerState myState
,
418 return deserializeEVENTWORD(myState
, bytes
, Event
.DEFAULT_ENCODING
);
421 public static String
deserializeEVENTWORD(DeserializerState myState
,
422 byte[] bytes
, short encoding
) {
423 String aString
= null;
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
]);
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
);
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
460 public static String
deserializeATTRIBUTEWORD(DeserializerState myState
,
462 return deserializeEVENTWORD(myState
, bytes
, Event
.DEFAULT_ENCODING
);