3 import java.io.ByteArrayInputStream;
4 import java.io.ByteArrayOutputStream;
5 import java.io.IOException;
6 import java.io.ObjectInputStream;
7 import java.io.ObjectOutputStream;
8 import java.nio.ByteBuffer;
9 import java.util.Arrays;
10 import java.util.ArrayList;
11 import java.util.Collections;
12 import java.util.HashMap;
13 import java.util.HashSet;
14 import java.util.Iterator;
15 import java.util.List;
20 /** Class IoTRMI provides utility services.
22 * It provides miscellaneous (data type/value) translations.
24 * @author Rahmadi Trimananda <rtrimana @ uci.edu>
28 public class IoTRMIUtil {
33 private Map<String,String> mapPrimitives;
34 private Map<String,Integer> mapPrimitiveSizes;
35 private Map<String,String> mapNonPrimitives;
40 public final static int METHOD_ID_LEN = 4; // 4 bytes = 32 bits
41 public final static int PARAM_LEN = 4; // 4 bytes = 32 bits (4-byte field that stores the length of the param)
43 public final static int SHT_LEN = 2;
44 public final static int INT_LEN = 4;
45 public final static int LNG_LEN = 8;
46 public final static int FLT_LEN = 4;
47 public final static int DBL_LEN = 8;
48 public final static int CHR_LEN = 2;
49 public final static int BYT_LEN = 1;
50 public final static int BOL_LEN = 1;
57 mapPrimitives = new HashMap<String,String>();
58 IoTRMITypes.arraysToMap(mapPrimitives,
59 IoTRMITypes.primitivesJava, IoTRMITypes.primitivesCplus);
60 mapPrimitiveSizes = new HashMap<String,Integer>();
61 IoTRMITypes.arraysToMap(mapPrimitiveSizes,
62 IoTRMITypes.primitivesJava, IoTRMITypes.primitivesSizes);
63 mapNonPrimitives = new HashMap<String,String>();
64 IoTRMITypes.arraysToMap(mapNonPrimitives,
65 IoTRMITypes.nonPrimitivesJava, IoTRMITypes.nonPrimitivesCplus);
70 * getHashCodeBytes() gets hash value (in bytes) from method name
72 public static byte[] getHashCodeBytes(String string) {
74 int hash = string.hashCode();
75 byte[] hashBytes = ByteBuffer.allocate(4).putInt(hash).array();
85 * translateType() try to translate a type
87 * It returns the original type when fails.
89 public String translateType(String type) {
91 if (mapPrimitives.containsKey(type))
92 return mapPrimitives.get(type);
93 else if (mapNonPrimitives.containsKey(type))
94 return mapNonPrimitives.get(type);
101 * getTypeSize() gets the size of a type
104 public int getTypeSize(String type) {
106 if (mapPrimitiveSizes.containsKey(type))
107 return mapPrimitiveSizes.get(type);
109 return -1; // Size is unknown (variable length)
114 * getTypeSize() gets the size of a type
117 public static int getTypeSize(Class<?> type) {
120 if (type == byte.class) {
122 } else if (type == Byte.class) {
124 } else if (type == short.class) {
126 } else if (type == Short.class) {
128 } else if ( type == int.class) {
130 } else if ( type == Integer.class) {
132 } else if ( type == long.class) {
134 } else if ( type == Long.class) {
136 } else if ( type == float.class) {
138 } else if ( type == Float.class) {
140 } else if ( type == double.class) {
142 } else if ( type == Double.class) {
144 } else if ( type == boolean.class) {
146 } else if ( type == Boolean.class) {
148 } else if ( type == char.class) {
150 } else if ( type == Character[].class) {
152 } else if (type == String[].class) {
155 throw new Error("IoTRMIUtil: Unrecognizable type: " + type.getName());
162 * getParamObject() converts byte array of certain object type into Object
164 public static Object getParamObject(Class<?> type, Class<?> genTypeKey, Class<?> genTypeVal, byte[] paramBytes) {
166 Object retObj = null;
167 if (type == byte.class ||
168 type == Byte.class) {
169 retObj = (Object) paramBytes[0];
170 } else if ( type == short.class ||
171 type == Short.class) {
172 retObj = (Object) byteArrayToShort(paramBytes);
173 } else if ( type == int.class ||
174 type == Integer.class) {
175 retObj = (Object) byteArrayToInt(paramBytes);
176 } else if ( type == long.class ||
177 type == Long.class) {
178 retObj = (Object) byteArrayToLong(paramBytes);
179 } else if ( type == float.class ||
180 type == Float.class) {
181 retObj = (Object) byteArrayToFloat(paramBytes);
182 } else if ( type == double.class ||
183 type == Double.class) {
184 retObj = (Object) byteArrayToDouble(paramBytes);
185 } else if ( type == boolean.class ||
186 type == Boolean.class) {
187 retObj = (Object) byteArrayToBoolean(paramBytes);
188 } else if ( type == char.class ||
189 type == Character.class) {
190 retObj = (Object) byteArrayToChar(paramBytes);
191 } else if (type == String.class) {
192 retObj = (Object) byteArrayToString(paramBytes);
194 } else if (type.isArray()) {
195 retObj = getParamObjectArray(type, paramBytes);
197 // e.g. Set<String> - type = Set.class, genTypeVal = String.class
198 /*} else if (type == Set.class) {
199 retObj = getParamSetObject(genTypeVal, paramBytes);*/
201 } else if (type == List.class) {
202 retObj = getParamListObject(genTypeVal, paramBytes);
204 // e.g. Map<String,Integer> - type = Map.class, genTypeKey = String.class, genTypeVal = Integer.class
205 /*} else if (type == Map.class) {
206 retObj = getParamMapObject(genTypeKey, genTypeVal, paramBytes);*/
208 throw new Error("IoTRMIUtil: Unrecognizable type: " + type.getName());
215 * getParamObjectArray() converts byte array of certain object type into array of Objects
217 public static Object getParamObjectArray(Class<?> type, byte[] paramBytes) {
219 Object retObj = null;
220 if ((type == byte[].class) ||
221 (type == byte.class)) {
222 retObj = (Object) paramBytes;
223 } else if ( (type == Byte[].class) ||
224 (type == Byte.class)) {
225 retObj = (Object) byteArrayToByteArray(paramBytes);
226 } else if ( (type == short[].class) ||
227 (type == short.class)) {
228 retObj = (Object) byteArrayToShtArray(paramBytes);
229 } else if ( (type == Short[].class) ||
230 (type == Short.class)) {
231 retObj = (Object) byteArrayToShortArray(paramBytes);
232 } else if ( (type == int[].class) ||
233 (type == int.class)) {
234 retObj = (Object) byteArrayToIntArray(paramBytes);
235 } else if ( (type == Integer[].class) ||
236 (type == Integer.class)) {
237 retObj = (Object) byteArrayToIntegerArray(paramBytes);
238 } else if ( (type == long[].class) ||
239 (type == long.class)) {
240 retObj = (Object) byteArrayToLngArray(paramBytes);
241 } else if ( (type == Long[].class) ||
242 (type == Long.class)) {
243 retObj = (Object) byteArrayToLongArray(paramBytes);
244 } else if ( (type == float[].class) ||
245 (type == float.class)) {
246 retObj = (Object) byteArrayToFltArray(paramBytes);
247 } else if ( (type == Float[].class) ||
248 (type == Float.class)) {
249 retObj = (Object) byteArrayToFloatArray(paramBytes);
250 } else if ( (type == double[].class) ||
251 (type == double.class)) {
252 retObj = (Object) byteArrayToDblArray(paramBytes);
253 } else if ( (type == Double[].class) ||
254 (type == Double.class)) {
255 retObj = (Object) byteArrayToDoubleArray(paramBytes);
256 } else if ( (type == boolean[].class) ||
257 (type == boolean.class)) {
258 retObj = (Object) byteArrayToBolArray(paramBytes);
259 } else if ( (type == Boolean[].class) ||
260 (type == Boolean.class)) {
261 retObj = (Object) byteArrayToBooleanArray(paramBytes);
262 } else if ( (type == char[].class) ||
263 (type == char.class)) {
264 retObj = (Object) byteArrayToChrArray(paramBytes);
265 } else if ( (type == Character[].class) ||
266 (type == Character.class)) {
267 retObj = (Object) byteArrayToCharacterArray(paramBytes);
268 } else if ( (type == String[].class) ||
269 (type == String.class)) {
270 retObj = (Object) byteArrayToStringArray(paramBytes);
271 } else if (type.isArray()) {
272 // This is an array but it's more than 1 dimension, e.g. 2-dimensional,
273 // 3-dimensional, etc.
274 // for loop to check inner array perhaps using object
275 // then call this function recursively
276 // combine the result altogether
279 throw new Error("IoTRMIUtil: Unrecognizable type: " + type.getName());
286 * getObjectBytes() converts an object into byte array
288 public static byte[] getObjectBytes(Object obj) {
290 byte[] retObjBytes = null;
291 if (obj instanceof Byte) {
292 retObjBytes = (byte[]) obj;
293 } else if (obj instanceof Short) {
294 retObjBytes = shortToByteArray((short) obj);
295 } else if (obj instanceof Integer) {
296 retObjBytes = intToByteArray((int) obj);
297 } else if (obj instanceof Long) {
298 retObjBytes = longToByteArray((long) obj);
299 } else if (obj instanceof Float) {
300 retObjBytes = floatToByteArray((float) obj);
301 } else if (obj instanceof Double) {
302 retObjBytes = doubleToByteArray((double) obj);
303 } else if (obj instanceof Character) {
304 retObjBytes = charToByteArray((char) obj);
305 } else if (obj instanceof Boolean) {
306 retObjBytes = booleanToByteArray((boolean) obj);
307 } else if (obj instanceof String) {
308 retObjBytes = stringToByteArray((String) obj);
310 } else if (obj.getClass().isArray()) {
311 retObjBytes = getArrayObjectBytes(obj);
312 // Set and its implementations
313 /*} else if (obj instanceof Set<?>) {
314 retObjBytes = setToByteArray((Set<?>) obj);*/
315 // List and its implementations
316 } else if (obj instanceof List<?>) {
317 retObjBytes = listToByteArray((List<?>) obj);
318 // Map and its implementations
319 /*} else if (obj instanceof Map<?,?>) {
320 retObjBytes = mapToByteArray((Map<?,?>) obj);*/
322 throw new Error("IoTRMIUtil: Unrecognizable object: " + obj.getClass());
329 * getArrayObjectBytes() converts array of objects into bytes array
331 public static byte[] getArrayObjectBytes(Object obj) {
333 byte[] retObjBytes = null;
334 if (obj instanceof byte[]) {
335 retObjBytes = (byte[]) obj;
336 } else if (obj instanceof Byte[]) {
337 retObjBytes = arrByteToByteArray((Byte[]) obj);
338 } else if (obj instanceof short[]) {
339 retObjBytes = arrShortToByteArray((short[]) obj);
340 } else if (obj instanceof Short[]) {
341 retObjBytes = arrShortToByteArray((Short[]) obj);
342 } else if (obj instanceof int[]) {
343 retObjBytes = arrIntToByteArray((int[]) obj);
344 } else if (obj instanceof Integer[]) {
345 retObjBytes = arrIntToByteArray((Integer[]) obj);
346 } else if (obj instanceof long[]) {
347 retObjBytes = arrLongToByteArray((long[]) obj);
348 } else if (obj instanceof Long[]) {
349 retObjBytes = arrLongToByteArray((Long[]) obj);
350 } else if (obj instanceof float[]) {
351 retObjBytes = arrFloatToByteArray((float[]) obj);
352 } else if (obj instanceof Float[]) {
353 retObjBytes = arrFloatToByteArray((Float[]) obj);
354 } else if (obj instanceof double[]) {
355 retObjBytes = arrDoubleToByteArray((double[]) obj);
356 } else if (obj instanceof Double[]) {
357 retObjBytes = arrDoubleToByteArray((Double[]) obj);
358 } else if (obj instanceof char[]) {
359 retObjBytes = arrCharToByteArray((char[]) obj);
360 } else if (obj instanceof Character[]) {
361 retObjBytes = arrCharToByteArray((Character[]) obj);
362 } else if (obj instanceof boolean[]) {
363 retObjBytes = arrBooleanToByteArray((boolean[]) obj);
364 } else if (obj instanceof Boolean[]) {
365 retObjBytes = arrBooleanToByteArray((Boolean[]) obj);
366 } else if (obj instanceof String[]) {
367 retObjBytes = arrStringToByteArray((String[]) obj);
369 throw new Error("IoTRMIUtil: Unrecognizable object: " + obj.getClass());
375 // Collection data structures
376 /*public static byte[] setToByteArray(Set<?> set) {
378 // Find out the class of the type
379 Iterator<?> it = set.iterator();
380 Object[] arrObj = null;
381 Object obj = it.next();
383 if (obj instanceof Byte) {
384 arrObj = set.toArray(new Byte[set.size()]);
385 } else if (obj instanceof Short) {
386 arrObj = set.toArray(new Short[set.size()]);
387 } else if (obj instanceof Integer) {
388 arrObj = set.toArray(new Integer[set.size()]);
389 } else if (obj instanceof Long) {
390 arrObj = set.toArray(new Long[set.size()]);
391 } else if (obj instanceof Float) {
392 arrObj = set.toArray(new Float[set.size()]);
393 } else if (obj instanceof Double) {
394 arrObj = set.toArray(new Double[set.size()]);
395 } else if (obj instanceof Character) {
396 arrObj = set.toArray(new Character[set.size()]);
397 } else if (obj instanceof Boolean) {
398 arrObj = set.toArray(new Boolean[set.size()]);
399 } else if (obj instanceof String) {
400 arrObj = set.toArray(new String[set.size()]);
402 throw new Error("IoTRMIUtil: Unrecognizable object: " + obj.getClass());
404 byte[] arrObjBytes = getArrayObjectBytes(arrObj);
409 public static byte[] listToByteArray(List<?> list) {
411 // Find out the class of the type
412 Iterator<?> it = list.iterator();
413 Object[] arrObj = null;
414 Object obj = it.next();
416 if (obj instanceof Byte) {
417 arrObj = list.toArray(new Byte[list.size()]);
418 } else if (obj instanceof Short) {
419 arrObj = list.toArray(new Short[list.size()]);
420 } else if (obj instanceof Integer) {
421 arrObj = list.toArray(new Integer[list.size()]);
422 } else if (obj instanceof Long) {
423 arrObj = list.toArray(new Long[list.size()]);
424 } else if (obj instanceof Float) {
425 arrObj = list.toArray(new Float[list.size()]);
426 } else if (obj instanceof Double) {
427 arrObj = list.toArray(new Double[list.size()]);
428 } else if (obj instanceof Character) {
429 arrObj = list.toArray(new Character[list.size()]);
430 } else if (obj instanceof Boolean) {
431 arrObj = list.toArray(new Boolean[list.size()]);
432 } else if (obj instanceof String) {
433 arrObj = list.toArray(new String[list.size()]);
435 throw new Error("IoTRMIUtil: Unrecognizable object: " + obj.getClass());
437 byte[] arrObjBytes = getArrayObjectBytes(arrObj);
442 // Convert keySet of a Map
443 /*public static byte[] mapKeyToByteArray(Map<?,?> map) {
446 // Find out the class of the type for K
447 Iterator<?> it = map.keySet().iterator();
448 Object[] arrObj = null;
449 Object obj = it.next();
451 if (obj instanceof Byte) {
452 arrObj = map.keySet().toArray(new Byte[map.size()]);
453 } else if (obj instanceof Short) {
454 arrObj = map.keySet().toArray(new Short[map.size()]);
455 } else if (obj instanceof Integer) {
456 arrObj = map.keySet().toArray(new Integer[map.size()]);
457 } else if (obj instanceof Long) {
458 arrObj = map.keySet().toArray(new Long[map.size()]);
459 } else if (obj instanceof Float) {
460 arrObj = map.keySet().toArray(new Float[map.size()]);
461 } else if (obj instanceof Double) {
462 arrObj = map.keySet().toArray(new Double[map.size()]);
463 } else if (obj instanceof Character) {
464 arrObj = map.keySet().toArray(new Character[map.size()]);
465 } else if (obj instanceof Boolean) {
466 arrObj = map.keySet().toArray(new Boolean[map.size()]);
467 } else if (obj instanceof String) {
468 arrObj = map.keySet().toArray(new String[map.size()]);
470 throw new Error("IoTRMIUtil: Unrecognizable object: " + obj.getClass());
471 byte[] arrObjBytes = getArrayObjectBytes(arrObj);
477 // Convert entrySet of a Map
478 public static byte[] mapEntryToByteArray(Map<?,?> map) {
481 // Find out the class of the type for V
482 Iterator<?> it = map.values().iterator();
483 Object[] arrObj = null;
484 Object obj = it.next();
486 if (obj instanceof Byte) {
487 arrObj = map.values().toArray(new Byte[map.size()]);
488 } else if (obj instanceof Short) {
489 arrObj = map.values().toArray(new Short[map.size()]);
490 } else if (obj instanceof Integer) {
491 arrObj = map.values().toArray(new Integer[map.size()]);
492 } else if (obj instanceof Long) {
493 arrObj = map.values().toArray(new Long[map.size()]);
494 } else if (obj instanceof Float) {
495 arrObj = map.values().toArray(new Float[map.size()]);
496 } else if (obj instanceof Double) {
497 arrObj = map.values().toArray(new Double[map.size()]);
498 } else if (obj instanceof Character) {
499 arrObj = map.values().toArray(new Character[map.size()]);
500 } else if (obj instanceof Boolean) {
501 arrObj = map.values().toArray(new Boolean[map.size()]);
502 } else if (obj instanceof String) {
503 arrObj = map.values().toArray(new String[map.size()]);
505 throw new Error("IoTRMIUtil: Unrecognizable object: " + obj.getClass());
507 byte[] arrObjBytes = getArrayObjectBytes(arrObj);
512 // Merge keySet and entrySet of a Map into one long byte array
513 public static byte[] mapToByteArray(Map<?,?> map) {
515 // Put map size in the packet
516 byte[] numEntries = intToByteArray(map.size());
517 byte[] keySetBytes = mapKeyToByteArray(map);
518 byte[] entrySetBytes = mapEntryToByteArray(map);
519 byte[] mapBytes = new byte[INT_LEN + keySetBytes.length + entrySetBytes.length];
521 System.arraycopy(numEntries, 0, mapBytes, 0, INT_LEN);
522 System.arraycopy(keySetBytes, 0, mapBytes, INT_LEN, keySetBytes.length);
523 System.arraycopy(entrySetBytes, 0, mapBytes, (INT_LEN + keySetBytes.length), entrySetBytes.length);
529 // Get a Set object from bytes
530 public static Object getParamSetObject(Class<?> genericType, byte[] paramBytes) {
532 Set<Object> retSet = new HashSet<Object>();
533 Object retObj = null;
534 if (genericType == Byte.class) {
535 Byte[] retArr = byteArrayToByteArray(paramBytes);
536 Collections.addAll(retSet, retArr);
537 } else if (genericType == Short.class) {
538 Short[] retArr = byteArrayToShortArray(paramBytes);
539 Collections.addAll(retSet, retArr);
540 } else if (genericType == Integer.class) {
541 Integer[] retArr = byteArrayToIntegerArray(paramBytes);
542 Collections.addAll(retSet, retArr);
543 } else if (genericType == Long.class) {
544 Long[] retArr = byteArrayToLongArray(paramBytes);
545 Collections.addAll(retSet, retArr);
546 } else if (genericType == Float.class) {
547 Float[] retArr = byteArrayToFloatArray(paramBytes);
548 Collections.addAll(retSet, retArr);
549 } else if (genericType == Double.class) {
550 Double[] retArr = byteArrayToDoubleArray(paramBytes);
551 Collections.addAll(retSet, retArr);
552 } else if (genericType == Boolean.class) {
553 Boolean[] retArr = byteArrayToBooleanArray(paramBytes);
554 Collections.addAll(retSet, retArr);
555 } else if (genericType == Character.class) {
556 Character[] retArr = byteArrayToCharacterArray(paramBytes);
557 Collections.addAll(retSet, retArr);
558 } else if (genericType == String.class) {
559 String[] retArr = byteArrayToStringArray(paramBytes);
560 Collections.addAll(retSet, retArr);
562 throw new Error("IoTRMIUtil: Unrecognizable object: " + genericType.getSimpleName());
568 // Get a List object from bytes
569 public static Object getParamListObject(Class<?> genericType, byte[] paramBytes) {
571 List<Object> retList = new ArrayList<Object>();
572 Object retObj = null;
573 if (genericType == Byte.class) {
574 Byte[] retArr = byteArrayToByteArray(paramBytes);
575 Collections.addAll(retList, retArr);
576 } else if (genericType == Short.class) {
577 Short[] retArr = byteArrayToShortArray(paramBytes);
578 Collections.addAll(retList, retArr);
579 } else if (genericType == Integer.class) {
580 Integer[] retArr = byteArrayToIntegerArray(paramBytes);
581 Collections.addAll(retList, retArr);
582 } else if (genericType == Long.class) {
583 Long[] retArr = byteArrayToLongArray(paramBytes);
584 Collections.addAll(retList, retArr);
585 } else if (genericType == Float.class) {
586 Float[] retArr = byteArrayToFloatArray(paramBytes);
587 Collections.addAll(retList, retArr);
588 } else if (genericType == Double.class) {
589 Double[] retArr = byteArrayToDoubleArray(paramBytes);
590 Collections.addAll(retList, retArr);
591 } else if (genericType == Boolean.class) {
592 Boolean[] retArr = byteArrayToBooleanArray(paramBytes);
593 Collections.addAll(retList, retArr);
594 } else if (genericType == Character.class) {
595 Character[] retArr = byteArrayToCharacterArray(paramBytes);
596 Collections.addAll(retList, retArr);
597 } else if (genericType == String.class) {
598 String[] retArr = byteArrayToStringArray(paramBytes);
599 Collections.addAll(retList, retArr);
601 throw new Error("IoTRMIUtil: Unrecognizable object: " + genericType.getSimpleName());
607 // Get a Key array for Map object from bytes
608 /*public static Object getParamMapObject(Class<?> genTypeKey, Class<?> genTypeVal, byte[] paramBytes) {
610 // The complete set of bytes always consists of all keys followed by all values - <K,V> pairs
611 // Calculate number of elements
612 byte[] numElBytes = new byte[INT_LEN];
613 System.arraycopy(paramBytes, 0, numElBytes, 0, INT_LEN);
614 int numEl = byteArrayToInt(numElBytes);
615 int keyLen = numEl * getTypeSize(genTypeKey);
616 int valLen = numEl * getTypeSize(genTypeVal);
617 byte[] prmKeyBytes = new byte[keyLen];
618 byte[] prmValBytes = new byte[valLen];
620 System.arraycopy(paramBytes, INT_LEN, prmKeyBytes, 0, keyLen);
621 System.arraycopy(paramBytes, (INT_LEN + keyLen), prmValBytes, 0, valLen);
623 Object[] retObjKey = (Object[]) getParamObjectArray(genTypeKey, prmKeyBytes);
624 Object[] retObjVal = (Object[]) getParamObjectArray(genTypeVal, prmValBytes);
625 // Put everything back to a Map
626 Map<Object,Object> retMap = new HashMap<Object,Object>();
627 IoTRMITypes.arraysToMap(retMap, retObjKey, retObjVal);
634 * Converters to byte array
637 public static byte[] shortToByteArray(short s) {
639 ByteBuffer bb = ByteBuffer.allocate(SHT_LEN);
646 public static byte[] intToByteArray(int i) {
648 ByteBuffer bb = ByteBuffer.allocate(INT_LEN);
655 public static byte[] longToByteArray(long l) {
657 ByteBuffer bb = ByteBuffer.allocate(LNG_LEN);
664 public static byte[] floatToByteArray(float f) {
666 ByteBuffer bb = ByteBuffer.allocate(FLT_LEN);
673 public static byte[] doubleToByteArray(double d) {
675 ByteBuffer bb = ByteBuffer.allocate(DBL_LEN);
682 public static byte[] charToByteArray(char c) {
684 ByteBuffer bb = ByteBuffer.allocate(CHR_LEN);
691 public static byte[] booleanToByteArray(boolean b) {
693 ByteBuffer bb = ByteBuffer.allocate(BOL_LEN);
703 public static byte[] stringToByteArray(String str) {
705 return str.getBytes();
710 public static byte[] arrByteToByteArray(Byte[] arrByte) {
712 byte[] arrByt = new byte[arrByte.length];
713 for(int i = 0; i < arrByte.length; i++) {
714 arrByt[i] = arrByte[i];
721 public static byte[] arrShortToByteArray(short[] arrShort) {
723 ByteBuffer bb = ByteBuffer.allocate(SHT_LEN * arrShort.length);
724 for(short s : arrShort) {
732 public static byte[] arrShortToByteArray(Short[] arrShort) {
734 ByteBuffer bb = ByteBuffer.allocate(SHT_LEN * arrShort.length);
735 for(Short s : arrShort) {
743 public static byte[] arrIntToByteArray(int[] arrInt) {
745 ByteBuffer bb = ByteBuffer.allocate(INT_LEN * arrInt.length);
746 for(int i : arrInt) {
754 public static byte[] arrIntToByteArray(Integer[] arrInt) {
756 ByteBuffer bb = ByteBuffer.allocate(INT_LEN * arrInt.length);
757 for(Integer i : arrInt) {
765 public static byte[] arrLongToByteArray(long[] arrLong) {
767 ByteBuffer bb = ByteBuffer.allocate(LNG_LEN * arrLong.length);
768 for(long l : arrLong) {
776 public static byte[] arrLongToByteArray(Long[] arrLong) {
778 ByteBuffer bb = ByteBuffer.allocate(LNG_LEN * arrLong.length);
779 for(Long l : arrLong) {
787 public static byte[] arrFloatToByteArray(float[] arrFloat) {
789 ByteBuffer bb = ByteBuffer.allocate(FLT_LEN * arrFloat.length);
790 for(float f : arrFloat) {
798 public static byte[] arrFloatToByteArray(Float[] arrFloat) {
800 ByteBuffer bb = ByteBuffer.allocate(FLT_LEN * arrFloat.length);
801 for(Float f : arrFloat) {
809 public static byte[] arrDoubleToByteArray(double[] arrDouble) {
811 ByteBuffer bb = ByteBuffer.allocate(DBL_LEN * arrDouble.length);
812 for(double d : arrDouble) {
820 public static byte[] arrDoubleToByteArray(Double[] arrDouble) {
822 ByteBuffer bb = ByteBuffer.allocate(DBL_LEN * arrDouble.length);
823 for(Double d : arrDouble) {
831 public static byte[] arrCharToByteArray(char[] arrChar) {
833 ByteBuffer bb = ByteBuffer.allocate(CHR_LEN * arrChar.length);
834 for(char c : arrChar) {
842 public static byte[] arrCharToByteArray(Character[] arrChar) {
844 ByteBuffer bb = ByteBuffer.allocate(CHR_LEN * arrChar.length);
845 for(Character c : arrChar) {
853 public static byte[] arrBooleanToByteArray(boolean[] arrBool) {
855 ByteBuffer bb = ByteBuffer.allocate(BOL_LEN * arrBool.length);
856 for(boolean b : arrBool) {
867 public static byte[] arrBooleanToByteArray(Boolean[] arrBool) {
869 ByteBuffer bb = ByteBuffer.allocate(BOL_LEN * arrBool.length);
870 for(Boolean b : arrBool) {
881 public static byte[] arrStringToByteArray(String[] arrString) {
883 // Format of bytes: | array length | length #1 | string #1 | length #2 | string #2 | ...
884 // Prepare array of bytes
885 int arrLen = INT_LEN; // First allocation for array length
886 for (int i = 0; i < arrString.length; i++) {
887 arrLen = arrLen + INT_LEN + arrString[i].length();
889 byte[] arrStrBytes = new byte[arrLen];
892 byte[] strArrLenBytes = intToByteArray(arrString.length);
893 System.arraycopy(strArrLenBytes, 0, arrStrBytes, pos, INT_LEN);
895 for (String str : arrString) {
897 // Copy string length
898 int strLen = str.length();
899 byte[] strLenBytes = intToByteArray(strLen);
900 System.arraycopy(strLenBytes, 0, arrStrBytes, pos, INT_LEN);
903 byte[] strBytes = stringToByteArray(str);
904 System.arraycopy(strBytes, 0, arrStrBytes, pos, strLen);
913 * Converters from byte array
916 public static short byteArrayToShort(byte[] bytes) {
918 return ByteBuffer.wrap(bytes).getShort();
922 public static int byteArrayToInt(byte[] bytes) {
924 return ByteBuffer.wrap(bytes).getInt();
928 public static long byteArrayToLong(byte[] bytes) {
930 return ByteBuffer.wrap(bytes).getLong();
934 public static float byteArrayToFloat(byte[] bytes) {
936 return ByteBuffer.wrap(bytes).getFloat();
940 public static double byteArrayToDouble(byte[] bytes) {
942 return ByteBuffer.wrap(bytes).getDouble();
946 public static char byteArrayToChar(byte[] bytes) {
948 return ByteBuffer.wrap(bytes).getChar();
952 public static boolean byteArrayToBoolean(byte[] bytes) {
954 Byte boolValByte = ByteBuffer.wrap(bytes).get();
955 short boolVal = boolValByte.shortValue();
963 public static String byteArrayToString(byte[] bytes) {
964 return new String(bytes);
969 public static Byte[] byteArrayToByteArray(byte[] arrByt) {
971 Byte[] arrByte = new Byte[arrByt.length];
972 for(int i = 0; i < arrByt.length; i++) {
973 arrByte[i] = arrByt[i];
980 public static short[] byteArrayToShtArray(byte[] bytes) {
982 // Single element bytes
983 byte[] elmt = new byte[SHT_LEN];
985 int arrLen = bytes.length / SHT_LEN;
986 short[] arr = new short[arrLen];
987 for(int i = 0; i < arrLen; i++) {
988 int offset = i * SHT_LEN;
989 System.arraycopy(bytes, offset, elmt, 0, SHT_LEN);
990 arr[i] = byteArrayToShort(elmt);
997 public static Short[] byteArrayToShortArray(byte[] bytes) {
999 // Single element bytes
1000 byte[] elmt = new byte[SHT_LEN];
1002 int arrLen = bytes.length / SHT_LEN;
1003 Short[] arr = new Short[arrLen];
1004 for(int i = 0; i < arrLen; i++) {
1005 int offset = i * SHT_LEN;
1006 System.arraycopy(bytes, offset, elmt, 0, SHT_LEN);
1007 arr[i] = byteArrayToShort(elmt);
1014 public static int[] byteArrayToIntArray(byte[] bytes) {
1016 // Single element bytes
1017 byte[] elmt = new byte[INT_LEN];
1019 int arrLen = bytes.length / INT_LEN;
1020 int[] arr = new int[arrLen];
1021 for(int i = 0; i < arrLen; i++) {
1022 int offset = i * INT_LEN;
1023 System.arraycopy(bytes, offset, elmt, 0, INT_LEN);
1024 arr[i] = byteArrayToInt(elmt);
1031 public static Integer[] byteArrayToIntegerArray(byte[] bytes) {
1033 // Single element bytes
1034 byte[] elmt = new byte[INT_LEN];
1036 int arrLen = bytes.length / INT_LEN;
1037 Integer[] arr = new Integer[arrLen];
1038 for(int i = 0; i < arrLen; i++) {
1039 int offset = i * INT_LEN;
1040 System.arraycopy(bytes, offset, elmt, 0, INT_LEN);
1041 arr[i] = byteArrayToInt(elmt);
1048 public static long[] byteArrayToLngArray(byte[] bytes) {
1050 // Single element bytes
1051 byte[] elmt = new byte[LNG_LEN];
1053 int arrLen = bytes.length / LNG_LEN;
1054 long[] arr = new long[arrLen];
1055 for(int i = 0; i < arrLen; i++) {
1056 int offset = i * LNG_LEN;
1057 System.arraycopy(bytes, offset, elmt, 0, LNG_LEN);
1058 arr[i] = byteArrayToLong(elmt);
1065 public static Long[] byteArrayToLongArray(byte[] bytes) {
1067 // Single element bytes
1068 byte[] elmt = new byte[LNG_LEN];
1070 int arrLen = bytes.length / LNG_LEN;
1071 Long[] arr = new Long[arrLen];
1072 for(int i = 0; i < arrLen; i++) {
1073 int offset = i * LNG_LEN;
1074 System.arraycopy(bytes, offset, elmt, 0, LNG_LEN);
1075 arr[i] = byteArrayToLong(elmt);
1082 public static float[] byteArrayToFltArray(byte[] bytes) {
1084 // Single element bytes
1085 byte[] elmt = new byte[FLT_LEN];
1087 int arrLen = bytes.length / FLT_LEN;
1088 float[] arr = new float[arrLen];
1089 for(int i = 0; i < arrLen; i++) {
1090 int offset = i * FLT_LEN;
1091 System.arraycopy(bytes, offset, elmt, 0, FLT_LEN);
1092 arr[i] = byteArrayToFloat(elmt);
1099 public static Float[] byteArrayToFloatArray(byte[] bytes) {
1101 // Single element bytes
1102 byte[] elmt = new byte[FLT_LEN];
1104 int arrLen = bytes.length / FLT_LEN;
1105 Float[] arr = new Float[arrLen];
1106 for(int i = 0; i < arrLen; i++) {
1107 int offset = i * FLT_LEN;
1108 System.arraycopy(bytes, offset, elmt, 0, FLT_LEN);
1109 arr[i] = byteArrayToFloat(elmt);
1116 public static double[] byteArrayToDblArray(byte[] bytes) {
1118 // Single element bytes
1119 byte[] elmt = new byte[DBL_LEN];
1121 int arrLen = bytes.length / DBL_LEN;
1122 double[] arr = new double[arrLen];
1123 for(int i = 0; i < arrLen; i++) {
1124 int offset = i * DBL_LEN;
1125 System.arraycopy(bytes, offset, elmt, 0, DBL_LEN);
1126 arr[i] = byteArrayToDouble(elmt);
1133 public static Double[] byteArrayToDoubleArray(byte[] bytes) {
1135 // Single element bytes
1136 byte[] elmt = new byte[DBL_LEN];
1138 int arrLen = bytes.length / DBL_LEN;
1139 Double[] arr = new Double[arrLen];
1140 for(int i = 0; i < arrLen; i++) {
1141 int offset = i * DBL_LEN;
1142 System.arraycopy(bytes, offset, elmt, 0, DBL_LEN);
1143 arr[i] = byteArrayToDouble(elmt);
1150 public static char[] byteArrayToChrArray(byte[] bytes) {
1152 // Single element bytes
1153 byte[] elmt = new byte[CHR_LEN];
1155 int arrLen = bytes.length / CHR_LEN;
1156 char[] arr = new char[arrLen];
1157 for(int i = 0; i < arrLen; i++) {
1158 int offset = i * CHR_LEN;
1159 System.arraycopy(bytes, offset, elmt, 0, CHR_LEN);
1160 arr[i] = byteArrayToChar(elmt);
1167 public static Character[] byteArrayToCharacterArray(byte[] bytes) {
1169 // Single element bytes
1170 byte[] elmt = new byte[CHR_LEN];
1172 int arrLen = bytes.length / CHR_LEN;
1173 Character[] arr = new Character[arrLen];
1174 for(int i = 0; i < arrLen; i++) {
1175 int offset = i * CHR_LEN;
1176 System.arraycopy(bytes, offset, elmt, 0, CHR_LEN);
1177 arr[i] = byteArrayToChar(elmt);
1184 public static boolean[] byteArrayToBolArray(byte[] bytes) {
1186 // Single element bytes
1187 byte[] elmt = new byte[BOL_LEN];
1189 int arrLen = bytes.length / BOL_LEN;
1190 boolean[] arr = new boolean[arrLen];
1191 for(int i = 0; i < arrLen; i++) {
1192 int offset = i * BOL_LEN;
1193 System.arraycopy(bytes, offset, elmt, 0, BOL_LEN);
1194 arr[i] = byteArrayToBoolean(elmt);
1201 public static Boolean[] byteArrayToBooleanArray(byte[] bytes) {
1203 // Single element bytes
1204 byte[] elmt = new byte[BOL_LEN];
1206 int arrLen = bytes.length / BOL_LEN;
1207 Boolean[] arr = new Boolean[arrLen];
1208 for(int i = 0; i < arrLen; i++) {
1209 int offset = i * BOL_LEN;
1210 System.arraycopy(bytes, offset, elmt, 0, BOL_LEN);
1211 arr[i] = byteArrayToBoolean(elmt);
1218 public static String[] byteArrayToStringArray(byte[] bytes) {
1220 // Format of bytes: | array length | length #1 | string #1 | length #2 | string #2 | ...
1221 // Get string array length
1223 byte[] strArrLenBytes = new byte[INT_LEN];
1224 System.arraycopy(bytes, pos, strArrLenBytes, 0, INT_LEN);
1225 int strArrLen = byteArrayToInt(strArrLenBytes);
1226 pos = pos + INT_LEN;
1227 // Prepare string array
1228 String[] strArray = new String[strArrLen];
1229 // Extract array of strings
1230 for(int i = 0; i < strArrLen; i++) {
1232 // Extract string length
1233 byte[] strLenBytes = new byte[INT_LEN];
1234 System.arraycopy(bytes, pos, strLenBytes, 0, INT_LEN);
1235 int strLen = byteArrayToInt(strLenBytes);
1236 pos = pos + INT_LEN;
1238 byte[] strBytes = new byte[strLen];
1239 System.arraycopy(bytes, pos, strBytes, 0, strLen);
1241 strArray[i] = byteArrayToString(strBytes);
1249 * toByteArray() gets Object and return its byte array
1251 * Adapted from http://www.java2s.com/
1252 * @see <a href="http://www.java2s.com/Code/Java/File-Input-
1253 * Output/Convertobjecttobytearrayandconvertbytearraytoobject.htm"</a>
1255 // toByteArray and toObject are taken from: http://tinyurl.com/69h8l7x
1256 public static byte[] toByteArray(Object obj) throws IOException {
1258 byte[] bytes = null;
1259 ByteArrayOutputStream bos = null;
1260 ObjectOutputStream oos = null;
1263 bos = new ByteArrayOutputStream();
1264 oos = new ObjectOutputStream(bos);
1265 oos.writeObject(obj);
1267 bytes = bos.toByteArray();
1282 * toObject() gets byte array and return its Object
1284 * Adapted from http://www.java2s.com/
1285 * @see <a href="http://www.java2s.com/Code/Java/File-Input-
1286 * Output/Convertobjecttobytearrayandconvertbytearraytoobject.htm"</a>
1288 public static Object toObject(byte[] bytes) throws IOException, ClassNotFoundException {
1291 ByteArrayInputStream bis = null;
1292 ObjectInputStream ois = null;
1295 bis = new ByteArrayInputStream(bytes);
1296 ois = new ObjectInputStream(bis);
1297 obj = ois.readObject();
1311 public static void main(String[] args) {
1313 //boolean data = false;
1315 // float data = 1234.123f;
1316 //double data = 12.51231234;
1317 //long data = 1234l;
1318 //short data = 1234;
1319 //int data = 12345678;
1320 // byte[] result = floatToByteArray(data);
1321 // System.out.println("Result: " + Arrays.toString(result));
1322 // System.out.println("Converted back: " + byteArrayToFloat(result));
1324 //String str = "methodA(int,string,float,double,double)";
1325 //int hash = str.hashCode();
1326 //System.out.println("Hash value: " + hash);
1328 int[][] multi = new int[][] {
1334 for (int[] inner : multi ) {
1335 System.out.println("New row!");
1336 for (Object i : inner) {
1337 System.out.println("Element i: " + i);
1339 System.out.println("Change row!\n");
1342 int[] int1 = { 1, 2, 3 };
1343 int[] int2 = { 6, 5, 4 };
1344 int[] result = new int[int1.length + int2.length];
1345 System.arraycopy(int1, 0, result, 0, int1.length);
1346 System.arraycopy(int2, 0, result, int1.length, int2.length);
1348 System.out.println("Combined array: " + Arrays.toString(result));