boolean and byte array support added
[lwes-java.git] / src / main / java / org / lwes / serializer / Deserializer.java
blob7b6ccedb5e36c82b5df163b4475face7b06728dc
1 package org.lwes.serializer;
3 import org.lwes.Event;
4 import org.lwes.util.EncodedString;
5 import org.lwes.util.Log;
6 import org.lwes.util.NumberCodec;
8 /**
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 {
16 /**
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>
21 * @return a byte.
23 public static byte deserializeBYTE(DeserializerState myState, byte[] bytes) {
24 byte aByte = bytes[myState.currentIndex()];
25 myState.incr(1);
26 return aByte;
29 /**
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>
34 * @return a boolean.
36 public static boolean deserializeBOOLEAN(DeserializerState myState,
37 byte[] bytes) {
38 byte aBooleanAsByte = Deserializer.deserializeBYTE(myState, bytes);
39 boolean aBoolean;
40 if (aBooleanAsByte == (byte) 0x00) {
41 aBoolean = false;
43 else {
44 aBoolean = true;
46 return aBoolean;
49 /**
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>
54 * @return a short.
56 public static short deserializeINT16(DeserializerState myState, byte[] bytes) {
57 /* deserialize in net order (i.e. Big Endian) */
58 short aShort =
59 (short) ((((short) bytes[myState.currentIndex()] << 8) & 0xff00)
60 | (((short) bytes[myState.currentIndex() + 1]) & 0x00ff));
61 myState.incr(2);
62 return aShort;
65 /**
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));
79 myState.incr(2);
80 return anUnsignedShort;
83 /**
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>
88 * @return an int.
90 public static int deserializeINT32(DeserializerState myState, byte[] bytes) {
91 int anInt =
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)
99 myState.incr(4);
100 return anInt;
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) {
112 long anUnsignedInt =
113 ((long)
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)
120 myState.incr(4);
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>
129 * @return a long.
131 public static long deserializeINT64(DeserializerState myState, byte[] bytes) {
132 long aLong = NumberCodec.decodeLongUnchecked(bytes, myState.currentIndex());
134 myState.incr(8);
135 return aLong;
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());
149 myState.incr(8);
150 return aLong;
153 public static String deserializeUINT64toHexString(DeserializerState myState,
154 byte[] bytes) {
155 String aString =
156 NumberCodec.byteArrayToHexString(bytes, myState.currentIndex(), 8);
157 myState.incr(8);
158 return aString;
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()];
174 myState.incr(4);
175 return inetaddr;
178 public static String deserializeIPADDRtoHexString(DeserializerState myState,
179 byte[] bytes) {
180 String aString =
181 NumberCodec.byteArrayToHexString(bytes, myState.currentIndex(), 4);
182 myState.incr(4);
183 return aString;
186 public static String[] deserializeStringArray(DeserializerState state,
187 byte[] bytes,
188 short encoding) {
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);
194 return rtn;
197 public static short[] deserializeInt16Array(DeserializerState state,
198 byte[] bytes) {
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);
204 return rtn;
207 public static int[] deserializeInt32Array(DeserializerState state,
208 byte[] bytes) {
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);
214 return rtn;
217 public static long[] deserializeInt64Array(DeserializerState state,
218 byte[] bytes) {
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);
224 return rtn;
227 public static int[] deserializeUInt16Array(DeserializerState state,
228 byte[] bytes) {
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);
234 return rtn;
237 public static long[] deserializeUInt32Array(DeserializerState state,
238 byte[] bytes) {
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);
244 return rtn;
247 public static long[] deserializeUInt64Array(DeserializerState state,
248 byte[] bytes) {
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);
254 return rtn;
257 public static boolean[] deserializeBooleanArray(DeserializerState state,
258 byte[] bytes) {
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);
264 return rtn;
267 public static byte[] deserializeByteArray(DeserializerState state,
268 byte[] bytes) {
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);
274 return rtn;
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>
282 * @return a String.
283 * @deprecated
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;
292 int len = -1;
293 try {
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]);
305 myState.incr(len);
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);
316 return aString;
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>
326 * @return a String.
328 public static String deserializeEVENTWORD(DeserializerState myState,
329 byte[] bytes) {
330 return deserializeEVENTWORD(myState, bytes, Event.DEFAULT_ENCODING);
333 public static String deserializeEVENTWORD(DeserializerState myState,
334 byte[] bytes, short encoding) {
335 String aString = null;
336 int len = -1;
337 try {
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]);
349 myState.incr(len);
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);
360 return aString;
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>
369 * @return a String.
371 public static String deserializeATTRIBUTEWORD(DeserializerState myState,
372 byte[] bytes) {
373 return deserializeEVENTWORD(myState, bytes, Event.DEFAULT_ENCODING);