/src/main/java/org/lwes/serializer/Deserializer.java

http://github.com/lwes/lwes-java · Java · 791 lines · 573 code · 63 blank · 155 comment · 57 complexity · e08803a10f178e6cc55ae301d23aeef9 MD5 · raw file

  1. /*======================================================================*
  2. * Copyright (c) 2008, Yahoo! Inc. All rights reserved. *
  3. * *
  4. * Licensed under the New BSD License (the "License"); you may not use *
  5. * this file except in compliance with the License. Unless required *
  6. * by applicable law or agreed to in writing, software distributed *
  7. * under the License is distributed on an "AS IS" BASIS, WITHOUT *
  8. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
  9. * See the License for the specific language governing permissions and *
  10. * limitations under the License. See accompanying LICENSE file. *
  11. *======================================================================*/
  12. package org.lwes.serializer;
  13. import java.math.BigInteger;
  14. import java.net.Inet4Address;
  15. import java.net.InetAddress;
  16. import java.net.UnknownHostException;
  17. import java.util.BitSet;
  18. import org.apache.commons.logging.Log;
  19. import org.apache.commons.logging.LogFactory;
  20. import org.lwes.Event;
  21. import org.lwes.EventSystemException;
  22. import org.lwes.FieldType;
  23. import org.lwes.util.EncodedString;
  24. import org.lwes.util.IPAddress;
  25. import org.lwes.util.NumberCodec;
  26. /**
  27. * This encapuslates the information needed to deserialize the base types
  28. * of the event system.
  29. *
  30. * @author Anthony Molinaro
  31. * @author Michael P. Lum
  32. * @author Frank Maritato
  33. */
  34. public class Deserializer {
  35. private static final BigInteger UINT64_MASK = new BigInteger("ffffffffffffffff", 16);
  36. private static transient Log log = LogFactory.getLog(Deserializer.class);
  37. /**
  38. * Deserialize a byte out of the byte array <tt>bytes</tt>
  39. *
  40. * @param myState the DeserializeState object giving the current index
  41. * in the byte array <tt>bytes</tt>
  42. * @param bytes the bytes to deserialize
  43. * @return a byte.
  44. */
  45. public static byte deserializeBYTE(DeserializerState myState, byte[] bytes) {
  46. byte aByte = bytes[myState.currentIndex()];
  47. myState.incr(1);
  48. return aByte;
  49. }
  50. /**
  51. * Deserialize a byte out of the byte array <tt>bytes</tt> and
  52. * interpret as a number in the range 0..255.
  53. *
  54. * @param myState the DeserializeState object giving the current index
  55. * in the byte array <tt>bytes</tt>
  56. * @param bytes the bytes to deserialize
  57. * @return a short containing the unsigned byte value.
  58. */
  59. public static short deserializeUBYTE(DeserializerState myState, byte[] bytes) {
  60. return (short) (deserializeBYTE(myState, bytes) & 0xff);
  61. }
  62. /**
  63. * Deserialize a boolean value out of the byte array <tt>bytes</tt>
  64. *
  65. * @param myState the DeserializeState object giving the current index
  66. * in the byte array <tt>bytes</tt>
  67. * @param bytes the bytes to deserialize
  68. * @return a boolean.
  69. */
  70. public static boolean deserializeBOOLEAN(DeserializerState myState,
  71. byte[] bytes) {
  72. byte aBooleanAsByte = Deserializer.deserializeBYTE(myState, bytes);
  73. return aBooleanAsByte != (byte) 0x00;
  74. }
  75. /**
  76. * Deserialize an int16 out of the byte array <tt>bytes</tt>
  77. *
  78. * @param myState the DeserializeState object giving the current index
  79. * in the byte array <tt>bytes</tt>
  80. * @param bytes the bytes to deserialize
  81. * @return a short.
  82. */
  83. public static short deserializeINT16(DeserializerState myState, byte[] bytes) {
  84. /* deserialize in net order (i.e. Big Endian) */
  85. int off = myState.currentIndex();
  86. short aShort = (short) (((bytes[off + 1] & 0xFF) << 0) +
  87. ((bytes[off + 0]) << 8));
  88. myState.incr(2);
  89. return aShort;
  90. }
  91. /**
  92. * Deserialize a uint16 out of the byte array <tt>bytes</tt>
  93. *
  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 containing the unsigned short.
  98. */
  99. public static int deserializeUINT16(DeserializerState myState, byte[] bytes) {
  100. /* deserialize in net order (i.e. Big Endian) */
  101. int anUnsignedShort =
  102. (((bytes[myState.currentIndex()] << 8) & 0x0000ff00)
  103. | (bytes[myState.currentIndex() + 1] & 0x000000ff));
  104. myState.incr(2);
  105. return anUnsignedShort;
  106. }
  107. /**
  108. * Deserialize an int32 out of the byte array <tt>bytes</tt>
  109. *
  110. * @param myState the DeserializeState object giving the current index
  111. * in the byte array <tt>bytes</tt>
  112. * @param bytes the bytes to deserialize
  113. * @return an int.
  114. */
  115. public static int deserializeINT32(DeserializerState myState, byte[] bytes) {
  116. int off = myState.currentIndex();
  117. int anInt = ((bytes[off + 3] & 0xFF) << 0) +
  118. ((bytes[off + 2] & 0xFF) << 8) +
  119. ((bytes[off + 1] & 0xFF) << 16) +
  120. ((bytes[off + 0]) << 24);
  121. myState.incr(4);
  122. return anInt;
  123. }
  124. /**
  125. * Deserialize a uint32 out of the byte array <tt>bytes</tt>
  126. *
  127. * @param myState the DeserializeState object giving the current index
  128. * in the byte array <tt>bytes</tt>
  129. * @param bytes the bytes to deserialize
  130. * @return a long because java doesn't have unsigned types.
  131. */
  132. public static long deserializeUINT32(DeserializerState myState, byte[] bytes) {
  133. long anUnsignedInt =
  134. ((((long) bytes[myState.currentIndex()] << 24) & 0x00000000ff000000L)
  135. | (((long) bytes[myState.currentIndex() + 1] << 16) & 0x0000000000ff0000L)
  136. | (((long) bytes[myState.currentIndex() + 2] << 8) & 0x000000000000ff00L)
  137. | (bytes[myState.currentIndex() + 3] & 0x00000000000000ffL));
  138. myState.incr(4);
  139. return anUnsignedInt;
  140. }
  141. /**
  142. * Deserialize a int64 out of the byte array <tt>bytes</tt>
  143. *
  144. * @param myState the DeserializeState object giving the current index
  145. * in the byte array <tt>bytes</tt>
  146. * @param bytes the bytes to deserialize
  147. * @return a long.
  148. */
  149. public static long deserializeINT64(DeserializerState myState, byte[] bytes) {
  150. int off = myState.currentIndex();
  151. long aLong = ((bytes[off + 7] & 0xFFL) << 0) +
  152. ((bytes[off + 6] & 0xFFL) << 8) +
  153. ((bytes[off + 5] & 0xFFL) << 16) +
  154. ((bytes[off + 4] & 0xFFL) << 24) +
  155. ((bytes[off + 3] & 0xFFL) << 32) +
  156. ((bytes[off + 2] & 0xFFL) << 40) +
  157. ((bytes[off + 1] & 0xFFL) << 48) +
  158. (((long) bytes[off + 0]) << 56);
  159. myState.incr(8);
  160. return aLong;
  161. }
  162. /**
  163. * Deserialize a uint64 out of the byte array <tt>bytes</tt>
  164. *
  165. * @param myState the DeserializeState object giving the current index
  166. * in the byte array <tt>bytes</tt>
  167. * @param bytes the bytes to deserialize
  168. * @return a long (because java doesn't have unsigned types do not expect
  169. * to do any math on this).
  170. */
  171. public static long deserializeUINT64(DeserializerState myState, byte[] bytes) {
  172. long aLong = NumberCodec.decodeLongUnchecked(bytes, myState.currentIndex());
  173. myState.incr(8);
  174. return aLong;
  175. }
  176. public static BigInteger deserializeUInt64ToBigInteger(DeserializerState state,
  177. byte[] bytes) {
  178. final long l = NumberCodec.decodeLongUnchecked(bytes, state.currentIndex());
  179. state.incr(8);
  180. return BigInteger.valueOf(l).and(UINT64_MASK);
  181. }
  182. public static String deserializeUINT64toHexString(DeserializerState myState,
  183. byte[] bytes) {
  184. String aString =
  185. NumberCodec.byteArrayToHexString(bytes, myState.currentIndex(), 8);
  186. myState.incr(8);
  187. return aString;
  188. }
  189. public static InetAddress deserializeIPV4(DeserializerState myState, byte[] bytes)
  190. throws UnknownHostException {
  191. int offset = myState.currentIndex();
  192. byte[] b = new byte[4];
  193. b[0] = bytes[offset];
  194. b[1] = bytes[offset + 1];
  195. b[2] = bytes[offset + 2];
  196. b[3] = bytes[offset + 3];
  197. myState.incr(4);
  198. return Inet4Address.getByAddress(b);
  199. }
  200. public static Double deserializeDOUBLE(DeserializerState myState, byte[] bytes) {
  201. int off = myState.currentIndex();
  202. long j = ((bytes[off + 7] & 0xFFL) << 0) +
  203. ((bytes[off + 6] & 0xFFL) << 8) +
  204. ((bytes[off + 5] & 0xFFL) << 16) +
  205. ((bytes[off + 4] & 0xFFL) << 24) +
  206. ((bytes[off + 3] & 0xFFL) << 32) +
  207. ((bytes[off + 2] & 0xFFL) << 40) +
  208. ((bytes[off + 1] & 0xFFL) << 48) +
  209. (((long) bytes[off + 0]) << 56);
  210. myState.incr(8);
  211. return Double.longBitsToDouble(j);
  212. }
  213. public static Float deserializeFLOAT(DeserializerState myState, byte[] bytes) {
  214. int off = myState.currentIndex();
  215. int i = ((bytes[off + 3] & 0xFF) << 0) +
  216. ((bytes[off + 2] & 0xFF) << 8) +
  217. ((bytes[off + 1] & 0xFF) << 16) +
  218. ((bytes[off + 0]) << 24);
  219. myState.incr(4);
  220. return Float.intBitsToFloat(i);
  221. }
  222. /**
  223. * Deserialize an ip_addr out of the byte array <tt>bytes</tt>
  224. *
  225. * @param myState the DeserializeState object giving the current index
  226. * in the byte array <tt>bytes</tt>
  227. * @param bytes the bytes to deserialize
  228. * @return a byte array with the ip_addr with byte order 1234.
  229. */
  230. public static IPAddress deserializeIPADDR(DeserializerState myState, byte[] bytes) {
  231. byte[] inetaddr = new byte[4];
  232. inetaddr[0] = bytes[myState.currentIndex() + 3];
  233. inetaddr[1] = bytes[myState.currentIndex() + 2];
  234. inetaddr[2] = bytes[myState.currentIndex() + 1];
  235. inetaddr[3] = bytes[myState.currentIndex()];
  236. myState.incr(4);
  237. return new IPAddress(inetaddr);
  238. }
  239. public static String deserializeIPADDRtoHexString(DeserializerState myState,
  240. byte[] bytes) {
  241. String aString =
  242. NumberCodec.byteArrayToHexString(bytes, myState.currentIndex(), 4);
  243. myState.incr(4);
  244. return aString;
  245. }
  246. public static String[] deserializeStringArray(DeserializerState state,
  247. byte[] bytes,
  248. short encoding) {
  249. int length = deserializeUINT16(state, bytes);
  250. String[] rtn = new String[length];
  251. for (int i = 0; i < length; i++) {
  252. rtn[i] = deserializeSTRING(state, bytes, encoding);
  253. }
  254. return rtn;
  255. }
  256. public static IPAddress[] deserializeIPADDRArray(DeserializerState state,
  257. byte[] bytes) {
  258. int length = deserializeUINT16(state, bytes);
  259. IPAddress[] rtn = new IPAddress[length];
  260. for (int i = 0; i < length; i++) {
  261. rtn[i] = deserializeIPADDR(state, bytes);
  262. }
  263. return rtn;
  264. }
  265. public static short[] deserializeInt16Array(DeserializerState state,
  266. byte[] bytes) {
  267. int length = deserializeUINT16(state, bytes);
  268. short[] rtn = new short[length];
  269. for (int i = 0; i < length; i++) {
  270. rtn[i] = deserializeINT16(state, bytes);
  271. }
  272. return rtn;
  273. }
  274. public static int[] deserializeInt32Array(DeserializerState state,
  275. byte[] bytes) {
  276. int length = deserializeUINT16(state, bytes);
  277. int[] rtn = new int[length];
  278. for (int i = 0; i < length; i++) {
  279. rtn[i] = deserializeINT32(state, bytes);
  280. }
  281. return rtn;
  282. }
  283. public static long[] deserializeInt64Array(DeserializerState state,
  284. byte[] bytes) {
  285. int length = deserializeUINT16(state, bytes);
  286. long[] rtn = new long[length];
  287. for (int i = 0; i < length; i++) {
  288. rtn[i] = deserializeINT64(state, bytes);
  289. }
  290. return rtn;
  291. }
  292. public static int[] deserializeUInt16Array(DeserializerState state,
  293. byte[] bytes) {
  294. int length = deserializeUINT16(state, bytes);
  295. int[] rtn = new int[length];
  296. for (int i = 0; i < length; i++) {
  297. rtn[i] = deserializeUINT16(state, bytes);
  298. }
  299. return rtn;
  300. }
  301. public static long[] deserializeUInt32Array(DeserializerState state,
  302. byte[] bytes) {
  303. int length = deserializeUINT16(state, bytes);
  304. long[] rtn = new long[length];
  305. for (int i = 0; i < length; i++) {
  306. rtn[i] = deserializeUINT32(state, bytes);
  307. }
  308. return rtn;
  309. }
  310. public static BigInteger[] deserializeUInt64Array(DeserializerState state,
  311. byte[] bytes) {
  312. int length = deserializeUINT16(state, bytes);
  313. BigInteger[] rtn = new BigInteger[length];
  314. for (int i = 0; i < length; i++) {
  315. rtn[i] = deserializeUInt64ToBigInteger(state, bytes);
  316. }
  317. return rtn;
  318. }
  319. public static boolean[] deserializeBooleanArray(DeserializerState state,
  320. byte[] bytes) {
  321. int length = deserializeUINT16(state, bytes);
  322. boolean[] rtn = new boolean[length];
  323. for (int i = 0; i < length; i++) {
  324. rtn[i] = deserializeBOOLEAN(state, bytes);
  325. }
  326. return rtn;
  327. }
  328. public static byte[] deserializeByteArray(DeserializerState state,
  329. byte[] bytes) {
  330. int length = deserializeUINT16(state, bytes);
  331. byte[] rtn = new byte[length];
  332. for (int i = 0; i < length; i++) {
  333. rtn[i] = deserializeBYTE(state, bytes);
  334. }
  335. return rtn;
  336. }
  337. public static double[] deserializeDoubleArray(DeserializerState state,
  338. byte[] bytes) {
  339. int length = deserializeUINT16(state, bytes);
  340. double[] rtn = new double[length];
  341. for (int i = 0; i < length; i++) {
  342. rtn[i] = deserializeDOUBLE(state, bytes);
  343. }
  344. return rtn;
  345. }
  346. public static float[] deserializeFloatArray(DeserializerState state,
  347. byte[] bytes) {
  348. int length = deserializeUINT16(state, bytes);
  349. float[] rtn = new float[length];
  350. for (int i = 0; i < length; i++) {
  351. rtn[i] = deserializeFLOAT(state, bytes);
  352. }
  353. return rtn;
  354. }
  355. /**
  356. * Deserialize a String out of the byte array <tt>bytes</tt>
  357. *
  358. * @param myState the DeserializeState object giving the current index
  359. * in the byte array <tt>bytes</tt>
  360. * @param bytes the bytes to deserialize
  361. * @return a String.
  362. * @deprecated
  363. */
  364. @Deprecated
  365. public static String deserializeSTRING(DeserializerState myState, byte[] bytes) {
  366. return deserializeSTRING(myState, bytes, Event.DEFAULT_ENCODING);
  367. }
  368. public static String deserializeSTRING(DeserializerState myState,
  369. byte[] bytes, short encoding) {
  370. String aString = null;
  371. int len = -1;
  372. try {
  373. len = deserializeUINT16(myState, bytes);
  374. // if (log.isTraceEnabled()) {
  375. // log.trace("Datagram Bytes: " +
  376. // NumberCodec.byteArrayToHexString(bytes, 0, bytes.length));
  377. // log.trace("String Length: " + len);
  378. // log.trace("State: " + myState);
  379. // }
  380. aString = EncodedString.bytesToString(bytes, myState.currentIndex(), len,
  381. Event.ENCODING_STRINGS[encoding]);
  382. myState.incr(len);
  383. }
  384. catch (ArrayIndexOutOfBoundsException aioobe) {
  385. if (log.isInfoEnabled()) {
  386. log.info("Exception: " + aioobe.toString());
  387. log.info("Datagram Bytes: " +
  388. NumberCodec.byteArrayToHexString(bytes, 0, bytes.length));
  389. log.info("String Length: " + len);
  390. log.info("State: " + myState);
  391. }
  392. }
  393. return aString;
  394. }
  395. /**
  396. * Deserialize a String out of the byte array <tt>bytes</tt> which
  397. * represents an Event name.
  398. *
  399. * @param myState the DeserializeState object giving the current index
  400. * in the byte array <tt>bytes</tt>
  401. * @param bytes the bytes to deserialize
  402. * @return a String.
  403. */
  404. public static String deserializeEVENTWORD(DeserializerState myState,
  405. byte[] bytes) {
  406. return deserializeEVENTWORD(myState, bytes, Event.DEFAULT_ENCODING);
  407. }
  408. public static String deserializeEVENTWORD(DeserializerState myState,
  409. byte[] bytes, short encoding) {
  410. String aString = null;
  411. int len = -1;
  412. try {
  413. len = deserializeUBYTE(myState, bytes);
  414. // if (log.isTraceEnabled()) {
  415. // log.trace("Datagram Bytes: " +
  416. // NumberCodec.byteArrayToHexString(bytes, 0, bytes.length));
  417. // log.trace("String Length: " + len);
  418. // log.trace("State: " + myState);
  419. // }
  420. aString = EncodedString.bytesToString(bytes, myState.currentIndex(), len,
  421. Event.ENCODING_STRINGS[encoding]);
  422. myState.incr(len);
  423. }
  424. catch (ArrayIndexOutOfBoundsException aioobe) {
  425. if (log.isInfoEnabled()) {
  426. log.info("Exception: " + aioobe.toString());
  427. log.info("Datagram Bytes: " +
  428. NumberCodec.byteArrayToHexString(bytes, 0, bytes.length));
  429. log.info("String Length: " + len);
  430. log.info("State: " + myState);
  431. }
  432. }
  433. return aString;
  434. }
  435. /**
  436. * Deserialize a String out of the byte array <tt>bytes</tt> which
  437. * represents an Attribute name.
  438. *
  439. * @param myState the DeserializeState object giving the current index
  440. * in the byte array <tt>bytes</tt>
  441. * @param bytes the bytes to deserialize
  442. * @return a String.
  443. */
  444. public static String deserializeATTRIBUTEWORD(DeserializerState myState,
  445. byte[] bytes) {
  446. return deserializeEVENTWORD(myState, bytes, Event.DEFAULT_ENCODING);
  447. }
  448. public static BitSet deserializeBitSet(DeserializerState myState, byte[] bytes) {
  449. int size = deserializeUINT16(myState, bytes);
  450. int numBytes = (int) Math.ceil(size / 8.0);
  451. BitSet bitSet = new BitSet(size);
  452. int offset = myState.currentIndex();
  453. int index = 0;
  454. for (int i = 0; i < numBytes; i++) {
  455. int val = bytes[offset + i];
  456. for (int j = 0; j < 8; j++) {
  457. bitSet.set(index++, ((val & (1 << j)) != 0));
  458. }
  459. }
  460. myState.incr(numBytes);
  461. return bitSet;
  462. }
  463. /**
  464. * Instead of deserializing the whole bit set, just determine the number of
  465. * bits which are set, while moving "myState" past the bit set. This is only
  466. * useful if it is faster than deserializeBitSet(myState,bytes).cardinality().
  467. */
  468. public static int deserializeBitSetCount(DeserializerState myState, byte[] bytes) {
  469. int size = deserializeUINT16(myState, bytes);
  470. int numBytes = (size / 8) + (size % 8 == 0 ? 0 : 1);
  471. int offset = myState.currentIndex();
  472. int bitsSet = 0;
  473. for (int i = 0; i < numBytes; i++) {
  474. int val = bytes[offset + i];
  475. for (int j = 0; j < 8; j++) {
  476. if ((val & (1 << j)) != 0) {
  477. ++bitsSet;
  478. }
  479. }
  480. }
  481. myState.incr(numBytes);
  482. return bitsSet;
  483. }
  484. public static String[] deserializeNStringArray(DeserializerState state,
  485. byte[] bytes,
  486. short encoding) {
  487. int length = deserializeUINT16(state, bytes);
  488. BitSet bs = deserializeBitSet(state, bytes);
  489. String[] rtn = new String[length];
  490. for (int i = 0; i < length; i++) {
  491. if (bs.get(i)) {
  492. rtn[i] = deserializeSTRING(state, bytes, encoding);
  493. }
  494. else {
  495. rtn[i] = null;
  496. }
  497. }
  498. return rtn;
  499. }
  500. public static Float[] deserializeNFloatArray(DeserializerState state, byte[] bytes) {
  501. // get the number of items in the array
  502. int length = deserializeUINT16(state, bytes); // 2 bytes
  503. BitSet bs = deserializeBitSet(state, bytes); // 2 bytes * length worst case
  504. Float[] rtn = new Float[length];
  505. for (int i = 0; i < length; i++) {
  506. if (bs.get(i)) {
  507. rtn[i] = deserializeFLOAT(state, bytes);
  508. }
  509. else {
  510. rtn[i] = null;
  511. }
  512. }
  513. return rtn;
  514. }
  515. public static Double[] deserializeNDoubleArray(DeserializerState state, byte[] bytes) {
  516. // get the number of items in the array
  517. int length = deserializeUINT16(state, bytes); // 2 bytes
  518. BitSet bs = deserializeBitSet(state, bytes); // 2 bytes * length worst case
  519. Double[] rtn = new Double[length];
  520. for (int i = 0; i < length; i++) {
  521. if (bs.get(i)) {
  522. rtn[i] = deserializeDOUBLE(state, bytes);
  523. }
  524. else {
  525. rtn[i] = null;
  526. }
  527. }
  528. return rtn;
  529. }
  530. public static Integer[] deserializeNUInt16Array(DeserializerState state, byte[] bytes) {
  531. // get the number of items in the array
  532. int length = deserializeUINT16(state, bytes); // 2 bytes
  533. BitSet bs = deserializeBitSet(state, bytes); // 2 bytes * length worst case
  534. Integer[] rtn = new Integer[length];
  535. for (int i = 0; i < length; i++) {
  536. if (bs.get(i)) {
  537. rtn[i] = deserializeUINT16(state, bytes);
  538. }
  539. else {
  540. rtn[i] = null;
  541. }
  542. }
  543. return rtn;
  544. }
  545. public static Short[] deserializeNInt16Array(DeserializerState state, byte[] bytes) {
  546. // get the number of items in the array
  547. int length = deserializeUINT16(state, bytes); // 2 bytes
  548. BitSet bs = deserializeBitSet(state, bytes); // 2 bytes * length worst case
  549. Short[] rtn = new Short[length];
  550. for (int i = 0; i < length; i++) {
  551. if (bs.get(i)) {
  552. rtn[i] = deserializeINT16(state, bytes);
  553. }
  554. else {
  555. rtn[i] = null;
  556. }
  557. }
  558. return rtn;
  559. }
  560. public static Long[] deserializeNUInt32Array(DeserializerState state, byte[] bytes) {
  561. // get the number of items in the array
  562. int length = deserializeUINT16(state, bytes); // 2 bytes
  563. BitSet bs = deserializeBitSet(state, bytes); // 2 bytes * length worst case
  564. Long[] rtn = new Long[length];
  565. for (int i = 0; i < length; i++) {
  566. if (bs.get(i)) {
  567. rtn[i] = deserializeUINT32(state, bytes);
  568. }
  569. else {
  570. rtn[i] = null;
  571. }
  572. }
  573. return rtn;
  574. }
  575. public static Integer[] deserializeNInt32Array(DeserializerState state, byte[] bytes) {
  576. // get the number of items in the array
  577. int length = deserializeUINT16(state, bytes); // 2 bytes
  578. BitSet bs = deserializeBitSet(state, bytes); // 2 bytes * length worst case
  579. Integer[] rtn = new Integer[length];
  580. for (int i = 0; i < length; i++) {
  581. if (bs.get(i)) {
  582. rtn[i] = deserializeINT32(state, bytes);
  583. }
  584. else {
  585. rtn[i] = null;
  586. }
  587. }
  588. return rtn;
  589. }
  590. public static BigInteger[] deserializeNUInt64Array(DeserializerState state,
  591. byte[] bytes) {
  592. int length = deserializeUINT16(state, bytes);
  593. BitSet bs = deserializeBitSet(state, bytes);
  594. BigInteger[] rtn = new BigInteger[length];
  595. for (int i = 0; i < length; i++) {
  596. if (bs.get(i)) {
  597. rtn[i] = deserializeUInt64ToBigInteger(state, bytes);
  598. }
  599. else {
  600. rtn[i] = null;
  601. }
  602. }
  603. return rtn;
  604. }
  605. public static Long[] deserializeNInt64Array(DeserializerState state, byte[] bytes) {
  606. // get the number of items in the array
  607. int length = deserializeUINT16(state, bytes); // 2 bytes
  608. BitSet bs = deserializeBitSet(state, bytes); // 2 bytes * length worst case
  609. Long[] rtn = new Long[length];
  610. for (int i = 0; i < length; i++) {
  611. if (bs.get(i)) {
  612. rtn[i] = deserializeINT64(state, bytes);
  613. }
  614. else {
  615. rtn[i] = null;
  616. }
  617. }
  618. return rtn;
  619. }
  620. public static Boolean[] deserializeNBooleanArray(DeserializerState state, byte[] bytes) {
  621. // get the number of items in the array
  622. int length = deserializeUINT16(state, bytes); // 2 bytes
  623. BitSet bs = deserializeBitSet(state, bytes); // 2 bytes * length worst case
  624. Boolean[] rtn = new Boolean[length];
  625. for (int i = 0; i < length; i++) {
  626. if (bs.get(i)) {
  627. rtn[i] = deserializeBOOLEAN(state, bytes);
  628. }
  629. else {
  630. rtn[i] = null;
  631. }
  632. }
  633. return rtn;
  634. }
  635. public static Byte[] deserializeNByteArray(DeserializerState state,
  636. byte[] bytes) {
  637. int length = deserializeUINT16(state, bytes);
  638. BitSet bs = deserializeBitSet(state, bytes);
  639. Byte[] rtn = new Byte[length];
  640. for (int i = 0; i < length; i++) {
  641. if (bs.get(i)) {
  642. rtn[i] = deserializeBYTE(state, bytes);
  643. }
  644. else {
  645. rtn[i] = null;
  646. }
  647. }
  648. return rtn;
  649. }
  650. public static Object deserializeValue(DeserializerState state,
  651. byte[] bytes,
  652. FieldType type,
  653. short encoding) throws EventSystemException {
  654. switch (type) {
  655. case BOOLEAN:
  656. return Deserializer.deserializeBOOLEAN(state, bytes);
  657. case BYTE:
  658. return Deserializer.deserializeBYTE(state, bytes);
  659. case UINT16:
  660. return Deserializer.deserializeUINT16(state, bytes);
  661. case INT16:
  662. return Deserializer.deserializeINT16(state, bytes);
  663. case UINT32:
  664. return Deserializer.deserializeUINT32(state, bytes);
  665. case INT32:
  666. return Deserializer.deserializeINT32(state, bytes);
  667. case FLOAT:
  668. return Deserializer.deserializeFLOAT(state, bytes);
  669. case UINT64:
  670. return Deserializer.deserializeUInt64ToBigInteger(state, bytes);
  671. case INT64:
  672. return Deserializer.deserializeINT64(state, bytes);
  673. case DOUBLE:
  674. return Deserializer.deserializeDOUBLE(state, bytes);
  675. case STRING:
  676. return Deserializer.deserializeSTRING(state, bytes, encoding);
  677. case IPADDR:
  678. return Deserializer.deserializeIPADDR(state, bytes);
  679. case STRING_ARRAY:
  680. return Deserializer.deserializeStringArray(state, bytes, encoding);
  681. case INT16_ARRAY:
  682. return Deserializer.deserializeInt16Array(state, bytes);
  683. case INT32_ARRAY:
  684. return Deserializer.deserializeInt32Array(state, bytes);
  685. case INT64_ARRAY:
  686. return Deserializer.deserializeInt64Array(state, bytes);
  687. case UINT16_ARRAY:
  688. return Deserializer.deserializeUInt16Array(state, bytes);
  689. case UINT32_ARRAY:
  690. return Deserializer.deserializeUInt32Array(state, bytes);
  691. case UINT64_ARRAY:
  692. return Deserializer.deserializeUInt64Array(state, bytes);
  693. case BOOLEAN_ARRAY:
  694. return Deserializer.deserializeBooleanArray(state, bytes);
  695. case BYTE_ARRAY:
  696. return Deserializer.deserializeByteArray(state, bytes);
  697. case DOUBLE_ARRAY:
  698. return Deserializer.deserializeDoubleArray(state, bytes);
  699. case FLOAT_ARRAY:
  700. return Deserializer.deserializeFloatArray(state, bytes);
  701. case IP_ADDR_ARRAY:
  702. return Deserializer.deserializeIPADDRArray(state, bytes);
  703. case NDOUBLE_ARRAY:
  704. return Deserializer.deserializeNDoubleArray(state, bytes);
  705. case NFLOAT_ARRAY:
  706. return Deserializer.deserializeNFloatArray(state, bytes);
  707. case NINT16_ARRAY:
  708. return Deserializer.deserializeNInt16Array(state, bytes);
  709. case NUINT16_ARRAY:
  710. return Deserializer.deserializeNUInt16Array(state, bytes);
  711. case NUINT32_ARRAY:
  712. return Deserializer.deserializeNUInt32Array(state, bytes);
  713. case NINT64_ARRAY:
  714. return Deserializer.deserializeNInt64Array(state, bytes);
  715. case NINT32_ARRAY:
  716. return Deserializer.deserializeNInt32Array(state, bytes);
  717. case NBOOLEAN_ARRAY:
  718. return Deserializer.deserializeNBooleanArray(state, bytes);
  719. case NSTRING_ARRAY:
  720. return Deserializer.deserializeNStringArray(state, bytes, encoding);
  721. case NUINT64_ARRAY:
  722. return Deserializer.deserializeNUInt64Array(state, bytes);
  723. case NBYTE_ARRAY:
  724. return Deserializer.deserializeNByteArray(state, bytes);
  725. }
  726. throw new EventSystemException("Unrecognized type: " + type);
  727. }
  728. }