1 package org
.lwes
.serializer
;
4 import org
.lwes
.util
.EncodedString
;
5 import org
.lwes
.util
.Log
;
6 import org
.lwes
.util
.NumberCodec
;
9 * This encapuslates the information needed to deserialize the base types
10 * of the event system.
12 * @author Anthony Molinaro
13 * @author Michael P. Lum
15 public class Deserializer
{
17 * Deserialize a byte out of the byte array <tt>bytes</tt>
19 * @param myState the DeserializeState object giving the current index
20 * in the byte array <tt>bytes</tt>
23 public static byte deserializeBYTE(DeserializerState myState
, byte[] bytes
) {
24 byte aByte
= bytes
[myState
.currentIndex()];
30 * Deserialize a boolean value out of the byte array <tt>bytes</tt>
32 * @param myState the DeserializeState object giving the current index
33 * in the byte array <tt>bytes</tt>
36 public static boolean deserializeBOOLEAN(DeserializerState myState
,
38 byte aBooleanAsByte
= Deserializer
.deserializeBYTE(myState
, bytes
);
40 if (aBooleanAsByte
== (byte) 0x00) {
50 * Deserialize an int16 out of the byte array <tt>bytes</tt>
52 * @param myState the DeserializeState object giving the current index
53 * in the byte array <tt>bytes</tt>
56 public static short deserializeINT16(DeserializerState myState
, byte[] bytes
) {
57 /* deserialize in net order (i.e. Big Endian) */
59 (short) ((((short) bytes
[myState
.currentIndex()] << 8) & 0xff00)
60 | (((short) bytes
[myState
.currentIndex() + 1]) & 0x00ff));
66 * Deserialize a uint16 out of the byte array <tt>bytes</tt>
68 * @param myState the DeserializeState object giving the current index
69 * in the byte array <tt>bytes</tt>
70 * @return an int containing the unsigned short.
72 public static int deserializeUINT16(DeserializerState myState
, byte[] bytes
) {
74 /* deserialize in net order (i.e. Big Endian) */
75 int anUnsignedShort
= (int)
76 ((((int) bytes
[myState
.currentIndex()] << 8) & 0x0000ff00)
77 | (((int) bytes
[myState
.currentIndex() + 1] << 0) & 0x000000ff));
80 return anUnsignedShort
;
84 * Deserialize an int32 out of the byte array <tt>bytes</tt>
86 * @param myState the DeserializeState object giving the current index
87 * in the byte array <tt>bytes</tt>
90 public static int deserializeINT32(DeserializerState myState
, byte[] bytes
) {
92 ((int) ((((int) bytes
[myState
.currentIndex()] << 24) & 0xff000000)
93 | (((int) bytes
[myState
.currentIndex() + 1] << 16) & 0x00ff0000)
94 | (((int) bytes
[myState
.currentIndex() + 2] << 8) & 0x0000ff00)
95 | (((int) bytes
[myState
.currentIndex() + 3] << 0) & 0x000000ff)
105 * Deserialize a uint32 out of the byte array <tt>bytes</tt>
107 * @param myState the DeserializeState object giving the current index
108 * in the byte array <tt>bytes</tt>
109 * @return a long because java doesn't have unsigned types.
111 public static long deserializeUINT32(DeserializerState myState
, byte[] bytes
) {
114 ((((long) bytes
[myState
.currentIndex()] << 24) & 0x00000000ff000000L
)
115 | (((long) bytes
[myState
.currentIndex() + 1] << 16) & 0x0000000000ff0000L
)
116 | (((long) bytes
[myState
.currentIndex() + 2] << 8) & 0x000000000000ff00L
)
117 | (((long) bytes
[myState
.currentIndex() + 3] << 0) & 0x00000000000000ffL
)
121 return anUnsignedInt
;
125 * Deserialize a int64 out of the byte array <tt>bytes</tt>
127 * @param myState the DeserializeState object giving the current index
128 * in the byte array <tt>bytes</tt>
131 public static long deserializeINT64(DeserializerState myState
, byte[] bytes
) {
132 long aLong
= NumberCodec
.decodeLongUnchecked(bytes
, myState
.currentIndex());
140 * Deserialize a uint64 out of the byte array <tt>bytes</tt>
142 * @param myState the DeserializeState object giving the current index
143 * in the byte array <tt>bytes</tt>
144 * @return a long (because java doesn't have unsigned types do not expect
145 * to do any math on this).
147 public static long deserializeUINT64(DeserializerState myState
, byte[] bytes
) {
148 long aLong
= NumberCodec
.decodeLongUnchecked(bytes
, myState
.currentIndex());
153 public static String
deserializeUINT64toHexString(DeserializerState myState
,
156 NumberCodec
.byteArrayToHexString(bytes
, myState
.currentIndex(), 8);
162 * Deserialize an ip_addr out of the byte array <tt>bytes</tt>
164 * @param myState the DeserializeState object giving the current index
165 * in the byte array <tt>bytes</tt>
166 * @return a byte array with the ip_addr with byte order 1234.
168 public static byte[] deserializeIPADDR(DeserializerState myState
, byte[] bytes
) {
169 byte[] inetaddr
= new byte[4];
170 inetaddr
[0] = bytes
[myState
.currentIndex() + 3];
171 inetaddr
[1] = bytes
[myState
.currentIndex() + 2];
172 inetaddr
[2] = bytes
[myState
.currentIndex() + 1];
173 inetaddr
[3] = bytes
[myState
.currentIndex()];
178 public static String
deserializeIPADDRtoHexString(DeserializerState myState
,
181 NumberCodec
.byteArrayToHexString(bytes
, myState
.currentIndex(), 4);
186 public static String
[] deserializeStringArray(DeserializerState state
,
189 int length
= deserializeUINT16(state
, bytes
);
190 String
[] rtn
= new String
[length
];
191 for (int i
= 0; i
< length
; i
++) {
192 rtn
[i
] = deserializeSTRING(state
, bytes
, encoding
);
197 public static short[] deserializeInt16Array(DeserializerState state
,
199 int length
= deserializeUINT16(state
, bytes
);
200 short[] rtn
= new short[length
];
201 for (int i
= 0; i
< length
; i
++) {
202 rtn
[i
] = deserializeINT16(state
, bytes
);
207 public static int[] deserializeInt32Array(DeserializerState state
,
209 int length
= deserializeUINT16(state
, bytes
);
210 int[] rtn
= new int[length
];
211 for (int i
= 0; i
< length
; i
++) {
212 rtn
[i
] = deserializeINT32(state
, bytes
);
217 public static long[] deserializeInt64Array(DeserializerState state
,
219 int length
= deserializeUINT16(state
, bytes
);
220 long[] rtn
= new long[length
];
221 for (int i
= 0; i
< length
; i
++) {
222 rtn
[i
] = deserializeINT64(state
, bytes
);
227 public static int[] deserializeUInt16Array(DeserializerState state
,
229 int length
= deserializeUINT16(state
, bytes
);
230 int[] rtn
= new int[length
];
231 for (int i
= 0; i
< length
; i
++) {
232 rtn
[i
] = deserializeUINT16(state
, bytes
);
237 public static long[] deserializeUInt32Array(DeserializerState state
,
239 int length
= deserializeUINT16(state
, bytes
);
240 long[] rtn
= new long[length
];
241 for (int i
= 0; i
< length
; i
++) {
242 rtn
[i
] = deserializeUINT32(state
, bytes
);
247 public static long[] deserializeUInt64Array(DeserializerState state
,
249 int length
= deserializeUINT16(state
, bytes
);
250 long[] rtn
= new long[length
];
251 for (int i
= 0; i
< length
; i
++) {
252 rtn
[i
] = deserializeUINT64(state
, bytes
);
257 public static boolean[] deserializeBooleanArray(DeserializerState state
,
259 int length
= deserializeUINT16(state
, bytes
);
260 boolean[] rtn
= new boolean[length
];
261 for (int i
= 0; i
< length
; i
++) {
262 rtn
[i
] = deserializeBOOLEAN(state
, bytes
);
267 public static byte[] deserializeByteArray(DeserializerState state
,
269 int length
= deserializeUINT16(state
, bytes
);
270 byte[] rtn
= new byte[length
];
271 for (int i
= 0; i
< length
; i
++) {
272 rtn
[i
] = deserializeBYTE(state
, bytes
);
278 * Deserialize a String out of the byte array <tt>bytes</tt>
280 * @param myState the DeserializeState object giving the current index
281 * in the byte array <tt>bytes</tt>
285 public static String
deserializeSTRING(DeserializerState myState
, byte[] bytes
) {
286 return deserializeSTRING(myState
, bytes
, Event
.DEFAULT_ENCODING
);
289 public static String
deserializeSTRING(DeserializerState myState
,
290 byte[] bytes
, short encoding
) {
291 String aString
= null;
294 len
= deserializeUINT16(myState
, bytes
);
296 if (Log
.isLogDebug()) {
297 Log
.debug("Datagram Bytes: " +
298 NumberCodec
.byteArrayToHexString(bytes
, 0, bytes
.length
));
299 Log
.debug("String Length: " + len
);
300 Log
.debug("State: " + myState
);
303 aString
= EncodedString
.bytesToString(bytes
, myState
.currentIndex(), len
,
304 Event
.ENCODING_STRINGS
[encoding
]);
307 catch (ArrayIndexOutOfBoundsException aioobe
) {
308 if (Log
.isLogInfo()) {
309 Log
.info("Exception: " + aioobe
.toString());
310 Log
.info("Datagram Bytes: " +
311 NumberCodec
.byteArrayToHexString(bytes
, 0, bytes
.length
));
312 Log
.info("String Length: " + len
);
313 Log
.info("State: " + myState
);
321 * Deserialize a String out of the byte array <tt>bytes</tt> which
322 * represents an Event name.
324 * @param myState the DeserializeState object giving the current index
325 * in the byte array <tt>bytes</tt>
328 public static String
deserializeEVENTWORD(DeserializerState myState
,
330 return deserializeEVENTWORD(myState
, bytes
, Event
.DEFAULT_ENCODING
);
333 public static String
deserializeEVENTWORD(DeserializerState myState
,
334 byte[] bytes
, short encoding
) {
335 String aString
= null;
338 len
= (int) deserializeBYTE(myState
, bytes
);
340 if (Log
.isLogDebug()) {
341 Log
.debug("Datagram Bytes: " +
342 NumberCodec
.byteArrayToHexString(bytes
, 0, bytes
.length
));
343 Log
.debug("String Length: " + len
);
344 Log
.debug("State: " + myState
);
347 aString
= EncodedString
.bytesToString(bytes
, myState
.currentIndex(), len
,
348 Event
.ENCODING_STRINGS
[encoding
]);
351 catch (ArrayIndexOutOfBoundsException aioobe
) {
352 if (Log
.isLogInfo()) {
353 Log
.info("Exception: " + aioobe
.toString());
354 Log
.info("Datagram Bytes: " +
355 NumberCodec
.byteArrayToHexString(bytes
, 0, bytes
.length
));
356 Log
.info("String Length: " + len
);
357 Log
.info("State: " + myState
);
364 * Deserialize a String out of the byte array <tt>bytes</tt> which
365 * represents an Attribute name.
367 * @param myState the DeserializeState object giving the current index
368 * in the byte array <tt>bytes</tt>
371 public static String
deserializeATTRIBUTEWORD(DeserializerState myState
,
373 return deserializeEVENTWORD(myState
, bytes
, Event
.DEFAULT_ENCODING
);