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 OBJECT_ID_LEN = 4; // 4 bytes = 32 bits
41 public final static int METHOD_ID_LEN = 4; // 4 bytes = 32 bits
42 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 RETURN_LEN = 4; // 4 bytes = 32 bits (4-byte field that stores the length of the return object)
45 public final static int SHT_LEN = 2;
46 public final static int INT_LEN = 4;
47 public final static int LNG_LEN = 8;
48 public final static int FLT_LEN = 4;
49 public final static int DBL_LEN = 8;
50 public final static int CHR_LEN = 2;
51 public final static int BYT_LEN = 1;
52 public final static int BOL_LEN = 1;
59 mapPrimitives = new HashMap<String,String>();
60 IoTRMITypes.arraysToMap(mapPrimitives,
61 IoTRMITypes.primitivesJava, IoTRMITypes.primitivesCplus);
62 mapPrimitiveSizes = new HashMap<String,Integer>();
63 IoTRMITypes.arraysToMap(mapPrimitiveSizes,
64 IoTRMITypes.primitivesJava, IoTRMITypes.primitivesSizes);
65 mapNonPrimitives = new HashMap<String,String>();
66 IoTRMITypes.arraysToMap(mapNonPrimitives,
67 IoTRMITypes.nonPrimitivesJava, IoTRMITypes.nonPrimitivesCplus);
72 * getHashCodeBytes() gets hash value (in bytes) from method name
74 public static byte[] getHashCodeBytes(String string) {
76 int hash = string.hashCode();
77 byte[] hashBytes = ByteBuffer.allocate(4).putInt(hash).array();
87 * translateType() try to translate a type
89 * It returns the original type when fails.
91 public String translateType(String type) {
93 if (mapPrimitives.containsKey(type))
94 return mapPrimitives.get(type);
95 else if (mapNonPrimitives.containsKey(type))
96 return mapNonPrimitives.get(type);
103 * getTypeSize() gets the size of a type
106 public int getTypeSize(String type) {
108 if (mapPrimitiveSizes.containsKey(type))
109 return mapPrimitiveSizes.get(type);
111 return -1; // Size is unknown (variable length)
116 * getTypeSize() gets the size of a type
119 public static int getTypeSize(Class<?> type) {
122 if (type == byte.class) {
124 } else if (type == Byte.class) {
126 } else if (type == short.class) {
128 } else if (type == Short.class) {
130 } else if ( type == int.class) {
132 } else if ( type == Integer.class) {
134 } else if ( type == long.class) {
136 } else if ( type == Long.class) {
138 } else if ( type == float.class) {
140 } else if ( type == Float.class) {
142 } else if ( type == double.class) {
144 } else if ( type == Double.class) {
146 } else if ( type == boolean.class) {
148 } else if ( type == Boolean.class) {
150 } else if ( type == char.class) {
152 } else if ( type == Character[].class) {
154 } else if (type == String[].class) {
157 throw new Error("IoTRMIUtil: Unrecognizable type: " + type.getName());
164 * getParamObject() converts byte array of certain object type into Object
166 public static Object getParamObject(Class<?> type, Class<?> genTypeVal, byte[] paramBytes) {
168 Object retObj = null;
169 if (type == byte.class ||
170 type == Byte.class) {
171 retObj = (Object) paramBytes[0];
172 } else if ( type == short.class ||
173 type == Short.class) {
174 retObj = (Object) byteArrayToShort(paramBytes);
175 } else if ( type == int.class ||
176 type == Integer.class) {
177 retObj = (Object) byteArrayToInt(paramBytes);
178 } else if ( type == long.class ||
179 type == Long.class) {
180 retObj = (Object) byteArrayToLong(paramBytes);
181 } else if ( type == float.class ||
182 type == Float.class) {
183 retObj = (Object) byteArrayToFloat(paramBytes);
184 } else if ( type == double.class ||
185 type == Double.class) {
186 retObj = (Object) byteArrayToDouble(paramBytes);
187 } else if ( type == boolean.class ||
188 type == Boolean.class) {
189 retObj = (Object) byteArrayToBoolean(paramBytes);
190 } else if ( type == char.class ||
191 type == Character.class) {
192 retObj = (Object) byteArrayToChar(paramBytes);
193 } else if (type == String.class) {
194 retObj = (Object) byteArrayToString(paramBytes);
196 } else if (type.isArray()) {
197 retObj = getParamObjectArray(type, paramBytes);
199 } else if (type == List.class) {
200 retObj = getParamListObject(genTypeVal, paramBytes);
202 throw new Error("IoTRMIUtil: Unrecognizable type: " + type.getName());
209 * getParamObjectArray() converts byte array of certain object type into array of Objects
211 public static Object getParamObjectArray(Class<?> type, byte[] paramBytes) {
213 Object retObj = null;
214 System.out.println("Got here!!!");
215 if ((type == byte[].class) ||
216 (type == byte.class)) {
217 retObj = (Object) paramBytes;
218 } else if ( (type == Byte[].class) ||
219 (type == Byte.class)) {
220 retObj = (Object) byteArrayToByteArray(paramBytes);
221 } else if ( (type == short[].class) ||
222 (type == short.class)) {
223 retObj = (Object) byteArrayToShtArray(paramBytes);
224 } else if ( (type == Short[].class) ||
225 (type == Short.class)) {
226 retObj = (Object) byteArrayToShortArray(paramBytes);
227 } else if ( (type == int[].class) ||
228 (type == int.class)) {
229 retObj = (Object) byteArrayToIntArray(paramBytes);
230 } else if ( (type == Integer[].class) ||
231 (type == Integer.class)) {
232 retObj = (Object) byteArrayToIntegerArray(paramBytes);
233 } else if ( (type == long[].class) ||
234 (type == long.class)) {
235 retObj = (Object) byteArrayToLngArray(paramBytes);
236 } else if ( (type == Long[].class) ||
237 (type == Long.class)) {
238 retObj = (Object) byteArrayToLongArray(paramBytes);
239 } else if ( (type == float[].class) ||
240 (type == float.class)) {
241 retObj = (Object) byteArrayToFltArray(paramBytes);
242 } else if ( (type == Float[].class) ||
243 (type == Float.class)) {
244 retObj = (Object) byteArrayToFloatArray(paramBytes);
245 } else if ( (type == double[].class) ||
246 (type == double.class)) {
247 retObj = (Object) byteArrayToDblArray(paramBytes);
248 } else if ( (type == Double[].class) ||
249 (type == Double.class)) {
250 retObj = (Object) byteArrayToDoubleArray(paramBytes);
251 } else if ( (type == boolean[].class) ||
252 (type == boolean.class)) {
253 retObj = (Object) byteArrayToBolArray(paramBytes);
254 } else if ( (type == Boolean[].class) ||
255 (type == Boolean.class)) {
256 retObj = (Object) byteArrayToBooleanArray(paramBytes);
257 } else if ( (type == char[].class) ||
258 (type == char.class)) {
259 retObj = (Object) byteArrayToChrArray(paramBytes);
260 } else if ( (type == Character[].class) ||
261 (type == Character.class)) {
262 retObj = (Object) byteArrayToCharacterArray(paramBytes);
263 } else if ( (type == String[].class) ||
264 (type == String.class)) {
265 retObj = (Object) byteArrayToStringArray(paramBytes);
266 //} else if (type.isArray()) {
267 // This is an array but it's more than 1 dimension, e.g. 2-dimensional,
268 // 3-dimensional, etc.
269 // for loop to check inner array perhaps using object
270 // then call this function recursively
271 // combine the result altogether
274 throw new Error("IoTRMIUtil: Unrecognizable type: " + type.getName());
281 * getObjectBytes() converts an object into byte array
283 public static byte[] getObjectBytes(Object obj) {
285 byte[] retObjBytes = null;
286 if (obj instanceof Byte) {
287 retObjBytes = (byte[]) obj;
288 } else if (obj instanceof Short) {
289 retObjBytes = shortToByteArray((short) obj);
290 } else if (obj instanceof Integer) {
291 retObjBytes = intToByteArray((int) obj);
292 } else if (obj instanceof Long) {
293 retObjBytes = longToByteArray((long) obj);
294 } else if (obj instanceof Float) {
295 retObjBytes = floatToByteArray((float) obj);
296 } else if (obj instanceof Double) {
297 retObjBytes = doubleToByteArray((double) obj);
298 } else if (obj instanceof Character) {
299 retObjBytes = charToByteArray((char) obj);
300 } else if (obj instanceof Boolean) {
301 retObjBytes = booleanToByteArray((boolean) obj);
302 } else if (obj instanceof String) {
303 retObjBytes = stringToByteArray((String) obj);
305 } else if (obj.getClass().isArray()) {
306 retObjBytes = getArrayObjectBytes(obj);
307 // Set and its implementations
308 /*} else if (obj instanceof Set<?>) {
309 retObjBytes = setToByteArray((Set<?>) obj);*/
310 // List and its implementations
311 } else if (obj instanceof List<?>) {
312 retObjBytes = listToByteArray((List<?>) obj);
313 // Map and its implementations
314 /*} else if (obj instanceof Map<?,?>) {
315 retObjBytes = mapToByteArray((Map<?,?>) obj);*/
317 throw new Error("IoTRMIUtil: Unrecognizable object: " + obj.getClass());
324 * getArrayObjectBytes() converts array of objects into bytes array
326 public static byte[] getArrayObjectBytes(Object obj) {
328 byte[] retObjBytes = null;
329 if (obj instanceof byte[]) {
330 retObjBytes = (byte[]) obj;
331 } else if (obj instanceof Byte[]) {
332 retObjBytes = arrByteToByteArray((Byte[]) obj);
333 } else if (obj instanceof short[]) {
334 retObjBytes = arrShortToByteArray((short[]) obj);
335 } else if (obj instanceof Short[]) {
336 retObjBytes = arrShortToByteArray((Short[]) obj);
337 } else if (obj instanceof int[]) {
338 retObjBytes = arrIntToByteArray((int[]) obj);
339 } else if (obj instanceof Integer[]) {
340 retObjBytes = arrIntToByteArray((Integer[]) obj);
341 } else if (obj instanceof long[]) {
342 retObjBytes = arrLongToByteArray((long[]) obj);
343 } else if (obj instanceof Long[]) {
344 retObjBytes = arrLongToByteArray((Long[]) obj);
345 } else if (obj instanceof float[]) {
346 retObjBytes = arrFloatToByteArray((float[]) obj);
347 } else if (obj instanceof Float[]) {
348 retObjBytes = arrFloatToByteArray((Float[]) obj);
349 } else if (obj instanceof double[]) {
350 retObjBytes = arrDoubleToByteArray((double[]) obj);
351 } else if (obj instanceof Double[]) {
352 retObjBytes = arrDoubleToByteArray((Double[]) obj);
353 } else if (obj instanceof char[]) {
354 retObjBytes = arrCharToByteArray((char[]) obj);
355 } else if (obj instanceof Character[]) {
356 retObjBytes = arrCharToByteArray((Character[]) obj);
357 } else if (obj instanceof boolean[]) {
358 retObjBytes = arrBooleanToByteArray((boolean[]) obj);
359 } else if (obj instanceof Boolean[]) {
360 retObjBytes = arrBooleanToByteArray((Boolean[]) obj);
361 } else if (obj instanceof String[]) {
362 retObjBytes = arrStringToByteArray((String[]) obj);
364 throw new Error("IoTRMIUtil: Unrecognizable object: " + obj.getClass());
370 // Collection data structures
371 /*public static byte[] setToByteArray(Set<?> set) {
373 // Find out the class of the type
374 Iterator<?> it = set.iterator();
375 Object[] arrObj = null;
376 Object obj = it.next();
378 if (obj instanceof Byte) {
379 arrObj = set.toArray(new Byte[set.size()]);
380 } else if (obj instanceof Short) {
381 arrObj = set.toArray(new Short[set.size()]);
382 } else if (obj instanceof Integer) {
383 arrObj = set.toArray(new Integer[set.size()]);
384 } else if (obj instanceof Long) {
385 arrObj = set.toArray(new Long[set.size()]);
386 } else if (obj instanceof Float) {
387 arrObj = set.toArray(new Float[set.size()]);
388 } else if (obj instanceof Double) {
389 arrObj = set.toArray(new Double[set.size()]);
390 } else if (obj instanceof Character) {
391 arrObj = set.toArray(new Character[set.size()]);
392 } else if (obj instanceof Boolean) {
393 arrObj = set.toArray(new Boolean[set.size()]);
394 } else if (obj instanceof String) {
395 arrObj = set.toArray(new String[set.size()]);
397 throw new Error("IoTRMIUtil: Unrecognizable object: " + obj.getClass());
399 byte[] arrObjBytes = getArrayObjectBytes(arrObj);
404 public static byte[] listToByteArray(List<?> list) {
406 // Find out the class of the type
407 Iterator<?> it = list.iterator();
408 Object[] arrObj = null;
409 Object obj = it.next();
411 if (obj instanceof Byte) {
412 arrObj = list.toArray(new Byte[list.size()]);
413 } else if (obj instanceof Short) {
414 arrObj = list.toArray(new Short[list.size()]);
415 } else if (obj instanceof Integer) {
416 arrObj = list.toArray(new Integer[list.size()]);
417 } else if (obj instanceof Long) {
418 arrObj = list.toArray(new Long[list.size()]);
419 } else if (obj instanceof Float) {
420 arrObj = list.toArray(new Float[list.size()]);
421 } else if (obj instanceof Double) {
422 arrObj = list.toArray(new Double[list.size()]);
423 } else if (obj instanceof Character) {
424 arrObj = list.toArray(new Character[list.size()]);
425 } else if (obj instanceof Boolean) {
426 arrObj = list.toArray(new Boolean[list.size()]);
427 } else if (obj instanceof String) {
428 arrObj = list.toArray(new String[list.size()]);
430 throw new Error("IoTRMIUtil: Unrecognizable object: " + obj.getClass());
432 byte[] arrObjBytes = getArrayObjectBytes(arrObj);
437 // Convert keySet of a Map
438 /*public static byte[] mapKeyToByteArray(Map<?,?> map) {
441 // Find out the class of the type for K
442 Iterator<?> it = map.keySet().iterator();
443 Object[] arrObj = null;
444 Object obj = it.next();
446 if (obj instanceof Byte) {
447 arrObj = map.keySet().toArray(new Byte[map.size()]);
448 } else if (obj instanceof Short) {
449 arrObj = map.keySet().toArray(new Short[map.size()]);
450 } else if (obj instanceof Integer) {
451 arrObj = map.keySet().toArray(new Integer[map.size()]);
452 } else if (obj instanceof Long) {
453 arrObj = map.keySet().toArray(new Long[map.size()]);
454 } else if (obj instanceof Float) {
455 arrObj = map.keySet().toArray(new Float[map.size()]);
456 } else if (obj instanceof Double) {
457 arrObj = map.keySet().toArray(new Double[map.size()]);
458 } else if (obj instanceof Character) {
459 arrObj = map.keySet().toArray(new Character[map.size()]);
460 } else if (obj instanceof Boolean) {
461 arrObj = map.keySet().toArray(new Boolean[map.size()]);
462 } else if (obj instanceof String) {
463 arrObj = map.keySet().toArray(new String[map.size()]);
465 throw new Error("IoTRMIUtil: Unrecognizable object: " + obj.getClass());
466 byte[] arrObjBytes = getArrayObjectBytes(arrObj);
472 // Convert entrySet of a Map
473 public static byte[] mapEntryToByteArray(Map<?,?> map) {
476 // Find out the class of the type for V
477 Iterator<?> it = map.values().iterator();
478 Object[] arrObj = null;
479 Object obj = it.next();
481 if (obj instanceof Byte) {
482 arrObj = map.values().toArray(new Byte[map.size()]);
483 } else if (obj instanceof Short) {
484 arrObj = map.values().toArray(new Short[map.size()]);
485 } else if (obj instanceof Integer) {
486 arrObj = map.values().toArray(new Integer[map.size()]);
487 } else if (obj instanceof Long) {
488 arrObj = map.values().toArray(new Long[map.size()]);
489 } else if (obj instanceof Float) {
490 arrObj = map.values().toArray(new Float[map.size()]);
491 } else if (obj instanceof Double) {
492 arrObj = map.values().toArray(new Double[map.size()]);
493 } else if (obj instanceof Character) {
494 arrObj = map.values().toArray(new Character[map.size()]);
495 } else if (obj instanceof Boolean) {
496 arrObj = map.values().toArray(new Boolean[map.size()]);
497 } else if (obj instanceof String) {
498 arrObj = map.values().toArray(new String[map.size()]);
500 throw new Error("IoTRMIUtil: Unrecognizable object: " + obj.getClass());
502 byte[] arrObjBytes = getArrayObjectBytes(arrObj);
507 // Merge keySet and entrySet of a Map into one long byte array
508 public static byte[] mapToByteArray(Map<?,?> map) {
510 // Put map size in the packet
511 byte[] numEntries = intToByteArray(map.size());
512 byte[] keySetBytes = mapKeyToByteArray(map);
513 byte[] entrySetBytes = mapEntryToByteArray(map);
514 byte[] mapBytes = new byte[INT_LEN + keySetBytes.length + entrySetBytes.length];
516 System.arraycopy(numEntries, 0, mapBytes, 0, INT_LEN);
517 System.arraycopy(keySetBytes, 0, mapBytes, INT_LEN, keySetBytes.length);
518 System.arraycopy(entrySetBytes, 0, mapBytes, (INT_LEN + keySetBytes.length), entrySetBytes.length);
524 // Get a Set object from bytes
525 public static Object getParamSetObject(Class<?> genericType, byte[] paramBytes) {
527 Set<Object> retSet = new HashSet<Object>();
528 Object retObj = null;
529 if (genericType == Byte.class) {
530 Byte[] retArr = byteArrayToByteArray(paramBytes);
531 Collections.addAll(retSet, retArr);
532 } else if (genericType == Short.class) {
533 Short[] retArr = byteArrayToShortArray(paramBytes);
534 Collections.addAll(retSet, retArr);
535 } else if (genericType == Integer.class) {
536 Integer[] retArr = byteArrayToIntegerArray(paramBytes);
537 Collections.addAll(retSet, retArr);
538 } else if (genericType == Long.class) {
539 Long[] retArr = byteArrayToLongArray(paramBytes);
540 Collections.addAll(retSet, retArr);
541 } else if (genericType == Float.class) {
542 Float[] retArr = byteArrayToFloatArray(paramBytes);
543 Collections.addAll(retSet, retArr);
544 } else if (genericType == Double.class) {
545 Double[] retArr = byteArrayToDoubleArray(paramBytes);
546 Collections.addAll(retSet, retArr);
547 } else if (genericType == Boolean.class) {
548 Boolean[] retArr = byteArrayToBooleanArray(paramBytes);
549 Collections.addAll(retSet, retArr);
550 } else if (genericType == Character.class) {
551 Character[] retArr = byteArrayToCharacterArray(paramBytes);
552 Collections.addAll(retSet, retArr);
553 } else if (genericType == String.class) {
554 String[] retArr = byteArrayToStringArray(paramBytes);
555 Collections.addAll(retSet, retArr);
557 throw new Error("IoTRMIUtil: Unrecognizable object: " + genericType.getSimpleName());
563 // Get a List object from bytes
564 public static Object getParamListObject(Class<?> genericType, byte[] paramBytes) {
566 List<Object> retList = new ArrayList<Object>();
567 Object retObj = null;
568 if (genericType == Byte.class) {
569 Byte[] retArr = byteArrayToByteArray(paramBytes);
570 Collections.addAll(retList, retArr);
571 } else if (genericType == Short.class) {
572 Short[] retArr = byteArrayToShortArray(paramBytes);
573 Collections.addAll(retList, retArr);
574 } else if (genericType == Integer.class) {
575 Integer[] retArr = byteArrayToIntegerArray(paramBytes);
576 Collections.addAll(retList, retArr);
577 } else if (genericType == Long.class) {
578 Long[] retArr = byteArrayToLongArray(paramBytes);
579 Collections.addAll(retList, retArr);
580 } else if (genericType == Float.class) {
581 Float[] retArr = byteArrayToFloatArray(paramBytes);
582 Collections.addAll(retList, retArr);
583 } else if (genericType == Double.class) {
584 Double[] retArr = byteArrayToDoubleArray(paramBytes);
585 Collections.addAll(retList, retArr);
586 } else if (genericType == Boolean.class) {
587 Boolean[] retArr = byteArrayToBooleanArray(paramBytes);
588 Collections.addAll(retList, retArr);
589 } else if (genericType == Character.class) {
590 Character[] retArr = byteArrayToCharacterArray(paramBytes);
591 Collections.addAll(retList, retArr);
592 } else if (genericType == String.class) {
593 String[] retArr = byteArrayToStringArray(paramBytes);
594 Collections.addAll(retList, retArr);
596 throw new Error("IoTRMIUtil: Unrecognizable object: " + genericType.getSimpleName());
602 // Get a Key array for Map object from bytes
603 /*public static Object getParamMapObject(Class<?> genTypeKey, Class<?> genTypeVal, byte[] paramBytes) {
605 // The complete set of bytes always consists of all keys followed by all values - <K,V> pairs
606 // Calculate number of elements
607 byte[] numElBytes = new byte[INT_LEN];
608 System.arraycopy(paramBytes, 0, numElBytes, 0, INT_LEN);
609 int numEl = byteArrayToInt(numElBytes);
610 int keyLen = numEl * getTypeSize(genTypeKey);
611 int valLen = numEl * getTypeSize(genTypeVal);
612 byte[] prmKeyBytes = new byte[keyLen];
613 byte[] prmValBytes = new byte[valLen];
615 System.arraycopy(paramBytes, INT_LEN, prmKeyBytes, 0, keyLen);
616 System.arraycopy(paramBytes, (INT_LEN + keyLen), prmValBytes, 0, valLen);
618 Object[] retObjKey = (Object[]) getParamObjectArray(genTypeKey, prmKeyBytes);
619 Object[] retObjVal = (Object[]) getParamObjectArray(genTypeVal, prmValBytes);
620 // Put everything back to a Map
621 Map<Object,Object> retMap = new HashMap<Object,Object>();
622 IoTRMITypes.arraysToMap(retMap, retObjKey, retObjVal);
629 * Converters to byte array
632 public static byte[] shortToByteArray(short s) {
634 ByteBuffer bb = ByteBuffer.allocate(SHT_LEN);
641 public static byte[] intToByteArray(int i) {
643 ByteBuffer bb = ByteBuffer.allocate(INT_LEN);
650 public static byte[] longToByteArray(long l) {
652 ByteBuffer bb = ByteBuffer.allocate(LNG_LEN);
659 public static byte[] floatToByteArray(float f) {
661 ByteBuffer bb = ByteBuffer.allocate(FLT_LEN);
668 public static byte[] doubleToByteArray(double d) {
670 ByteBuffer bb = ByteBuffer.allocate(DBL_LEN);
677 public static byte[] charToByteArray(char c) {
679 ByteBuffer bb = ByteBuffer.allocate(CHR_LEN);
686 public static byte[] booleanToByteArray(boolean b) {
688 ByteBuffer bb = ByteBuffer.allocate(BOL_LEN);
698 public static byte[] stringToByteArray(String str) {
700 return str.getBytes();
705 public static byte[] arrByteToByteArray(Byte[] arrByte) {
707 byte[] arrByt = new byte[arrByte.length];
708 for(int i = 0; i < arrByte.length; i++) {
709 arrByt[i] = arrByte[i];
716 public static byte[] arrShortToByteArray(short[] arrShort) {
718 ByteBuffer bb = ByteBuffer.allocate(SHT_LEN * arrShort.length);
719 for(short s : arrShort) {
727 public static byte[] arrShortToByteArray(Short[] arrShort) {
729 ByteBuffer bb = ByteBuffer.allocate(SHT_LEN * arrShort.length);
730 for(Short s : arrShort) {
738 public static byte[] arrIntToByteArray(int[] arrInt) {
740 ByteBuffer bb = ByteBuffer.allocate(INT_LEN * arrInt.length);
741 for(int i : arrInt) {
749 public static byte[] arrIntToByteArray(Integer[] arrInt) {
751 ByteBuffer bb = ByteBuffer.allocate(INT_LEN * arrInt.length);
752 for(Integer i : arrInt) {
760 public static byte[] arrLongToByteArray(long[] arrLong) {
762 ByteBuffer bb = ByteBuffer.allocate(LNG_LEN * arrLong.length);
763 for(long l : arrLong) {
771 public static byte[] arrLongToByteArray(Long[] arrLong) {
773 ByteBuffer bb = ByteBuffer.allocate(LNG_LEN * arrLong.length);
774 for(Long l : arrLong) {
782 public static byte[] arrFloatToByteArray(float[] arrFloat) {
784 ByteBuffer bb = ByteBuffer.allocate(FLT_LEN * arrFloat.length);
785 for(float f : arrFloat) {
793 public static byte[] arrFloatToByteArray(Float[] arrFloat) {
795 ByteBuffer bb = ByteBuffer.allocate(FLT_LEN * arrFloat.length);
796 for(Float f : arrFloat) {
804 public static byte[] arrDoubleToByteArray(double[] arrDouble) {
806 ByteBuffer bb = ByteBuffer.allocate(DBL_LEN * arrDouble.length);
807 for(double d : arrDouble) {
815 public static byte[] arrDoubleToByteArray(Double[] arrDouble) {
817 ByteBuffer bb = ByteBuffer.allocate(DBL_LEN * arrDouble.length);
818 for(Double d : arrDouble) {
826 public static byte[] arrCharToByteArray(char[] arrChar) {
828 ByteBuffer bb = ByteBuffer.allocate(CHR_LEN * arrChar.length);
829 for(char c : arrChar) {
837 public static byte[] arrCharToByteArray(Character[] arrChar) {
839 ByteBuffer bb = ByteBuffer.allocate(CHR_LEN * arrChar.length);
840 for(Character c : arrChar) {
848 public static byte[] arrBooleanToByteArray(boolean[] arrBool) {
850 ByteBuffer bb = ByteBuffer.allocate(BOL_LEN * arrBool.length);
851 for(boolean b : arrBool) {
862 public static byte[] arrBooleanToByteArray(Boolean[] arrBool) {
864 ByteBuffer bb = ByteBuffer.allocate(BOL_LEN * arrBool.length);
865 for(Boolean b : arrBool) {
876 public static byte[] arrStringToByteArray(String[] arrString) {
878 // Format of bytes: | array length | length #1 | string #1 | length #2 | string #2 | ...
879 // Prepare array of bytes
880 int arrLen = INT_LEN; // First allocation for array length
881 for (int i = 0; i < arrString.length; i++) {
882 arrLen = arrLen + INT_LEN + arrString[i].length();
884 byte[] arrStrBytes = new byte[arrLen];
887 byte[] strArrLenBytes = intToByteArray(arrString.length);
888 System.arraycopy(strArrLenBytes, 0, arrStrBytes, pos, INT_LEN);
890 for (String str : arrString) {
892 // Copy string length
893 int strLen = str.length();
894 byte[] strLenBytes = intToByteArray(strLen);
895 System.arraycopy(strLenBytes, 0, arrStrBytes, pos, INT_LEN);
898 byte[] strBytes = stringToByteArray(str);
899 System.arraycopy(strBytes, 0, arrStrBytes, pos, strLen);
908 * Converters from byte array
911 public static short byteArrayToShort(byte[] bytes) {
913 return ByteBuffer.wrap(bytes).getShort();
917 public static int byteArrayToInt(byte[] bytes) {
919 return ByteBuffer.wrap(bytes).getInt();
923 public static long byteArrayToLong(byte[] bytes) {
925 return ByteBuffer.wrap(bytes).getLong();
929 public static float byteArrayToFloat(byte[] bytes) {
931 return ByteBuffer.wrap(bytes).getFloat();
935 public static double byteArrayToDouble(byte[] bytes) {
937 return ByteBuffer.wrap(bytes).getDouble();
941 public static char byteArrayToChar(byte[] bytes) {
943 return ByteBuffer.wrap(bytes).getChar();
947 public static boolean byteArrayToBoolean(byte[] bytes) {
949 Byte boolValByte = ByteBuffer.wrap(bytes).get();
950 short boolVal = boolValByte.shortValue();
958 public static String byteArrayToString(byte[] bytes) {
959 return new String(bytes);
964 public static Byte[] byteArrayToByteArray(byte[] arrByt) {
966 Byte[] arrByte = new Byte[arrByt.length];
967 for(int i = 0; i < arrByt.length; i++) {
968 arrByte[i] = arrByt[i];
975 public static short[] byteArrayToShtArray(byte[] bytes) {
977 // Single element bytes
978 byte[] elmt = new byte[SHT_LEN];
980 int arrLen = bytes.length / SHT_LEN;
981 short[] arr = new short[arrLen];
982 for(int i = 0; i < arrLen; i++) {
983 int offset = i * SHT_LEN;
984 System.arraycopy(bytes, offset, elmt, 0, SHT_LEN);
985 arr[i] = byteArrayToShort(elmt);
992 public static Short[] byteArrayToShortArray(byte[] bytes) {
994 // Single element bytes
995 byte[] elmt = new byte[SHT_LEN];
997 int arrLen = bytes.length / SHT_LEN;
998 Short[] arr = new Short[arrLen];
999 for(int i = 0; i < arrLen; i++) {
1000 int offset = i * SHT_LEN;
1001 System.arraycopy(bytes, offset, elmt, 0, SHT_LEN);
1002 arr[i] = byteArrayToShort(elmt);
1009 public static int[] byteArrayToIntArray(byte[] bytes) {
1011 // Single element bytes
1012 byte[] elmt = new byte[INT_LEN];
1014 int arrLen = bytes.length / INT_LEN;
1015 int[] arr = new int[arrLen];
1016 for(int i = 0; i < arrLen; i++) {
1017 int offset = i * INT_LEN;
1018 System.arraycopy(bytes, offset, elmt, 0, INT_LEN);
1019 arr[i] = byteArrayToInt(elmt);
1026 public static Integer[] byteArrayToIntegerArray(byte[] bytes) {
1028 // Single element bytes
1029 byte[] elmt = new byte[INT_LEN];
1031 int arrLen = bytes.length / INT_LEN;
1032 Integer[] arr = new Integer[arrLen];
1033 for(int i = 0; i < arrLen; i++) {
1034 int offset = i * INT_LEN;
1035 System.arraycopy(bytes, offset, elmt, 0, INT_LEN);
1036 arr[i] = byteArrayToInt(elmt);
1043 public static long[] byteArrayToLngArray(byte[] bytes) {
1045 // Single element bytes
1046 byte[] elmt = new byte[LNG_LEN];
1048 int arrLen = bytes.length / LNG_LEN;
1049 long[] arr = new long[arrLen];
1050 for(int i = 0; i < arrLen; i++) {
1051 int offset = i * LNG_LEN;
1052 System.arraycopy(bytes, offset, elmt, 0, LNG_LEN);
1053 arr[i] = byteArrayToLong(elmt);
1060 public static Long[] byteArrayToLongArray(byte[] bytes) {
1062 // Single element bytes
1063 byte[] elmt = new byte[LNG_LEN];
1065 int arrLen = bytes.length / LNG_LEN;
1066 Long[] arr = new Long[arrLen];
1067 for(int i = 0; i < arrLen; i++) {
1068 int offset = i * LNG_LEN;
1069 System.arraycopy(bytes, offset, elmt, 0, LNG_LEN);
1070 arr[i] = byteArrayToLong(elmt);
1077 public static float[] byteArrayToFltArray(byte[] bytes) {
1079 // Single element bytes
1080 byte[] elmt = new byte[FLT_LEN];
1082 int arrLen = bytes.length / FLT_LEN;
1083 float[] arr = new float[arrLen];
1084 for(int i = 0; i < arrLen; i++) {
1085 int offset = i * FLT_LEN;
1086 System.arraycopy(bytes, offset, elmt, 0, FLT_LEN);
1087 arr[i] = byteArrayToFloat(elmt);
1094 public static Float[] byteArrayToFloatArray(byte[] bytes) {
1096 // Single element bytes
1097 byte[] elmt = new byte[FLT_LEN];
1099 int arrLen = bytes.length / FLT_LEN;
1100 Float[] arr = new Float[arrLen];
1101 for(int i = 0; i < arrLen; i++) {
1102 int offset = i * FLT_LEN;
1103 System.arraycopy(bytes, offset, elmt, 0, FLT_LEN);
1104 arr[i] = byteArrayToFloat(elmt);
1111 public static double[] byteArrayToDblArray(byte[] bytes) {
1113 // Single element bytes
1114 byte[] elmt = new byte[DBL_LEN];
1116 int arrLen = bytes.length / DBL_LEN;
1117 double[] arr = new double[arrLen];
1118 for(int i = 0; i < arrLen; i++) {
1119 int offset = i * DBL_LEN;
1120 System.arraycopy(bytes, offset, elmt, 0, DBL_LEN);
1121 arr[i] = byteArrayToDouble(elmt);
1128 public static Double[] byteArrayToDoubleArray(byte[] bytes) {
1130 // Single element bytes
1131 byte[] elmt = new byte[DBL_LEN];
1133 int arrLen = bytes.length / DBL_LEN;
1134 Double[] arr = new Double[arrLen];
1135 for(int i = 0; i < arrLen; i++) {
1136 int offset = i * DBL_LEN;
1137 System.arraycopy(bytes, offset, elmt, 0, DBL_LEN);
1138 arr[i] = byteArrayToDouble(elmt);
1145 public static char[] byteArrayToChrArray(byte[] bytes) {
1147 // Single element bytes
1148 byte[] elmt = new byte[CHR_LEN];
1150 int arrLen = bytes.length / CHR_LEN;
1151 char[] arr = new char[arrLen];
1152 for(int i = 0; i < arrLen; i++) {
1153 int offset = i * CHR_LEN;
1154 System.arraycopy(bytes, offset, elmt, 0, CHR_LEN);
1155 arr[i] = byteArrayToChar(elmt);
1162 public static Character[] byteArrayToCharacterArray(byte[] bytes) {
1164 // Single element bytes
1165 byte[] elmt = new byte[CHR_LEN];
1167 int arrLen = bytes.length / CHR_LEN;
1168 Character[] arr = new Character[arrLen];
1169 for(int i = 0; i < arrLen; i++) {
1170 int offset = i * CHR_LEN;
1171 System.arraycopy(bytes, offset, elmt, 0, CHR_LEN);
1172 arr[i] = byteArrayToChar(elmt);
1179 public static boolean[] byteArrayToBolArray(byte[] bytes) {
1181 // Single element bytes
1182 byte[] elmt = new byte[BOL_LEN];
1184 int arrLen = bytes.length / BOL_LEN;
1185 boolean[] arr = new boolean[arrLen];
1186 for(int i = 0; i < arrLen; i++) {
1187 int offset = i * BOL_LEN;
1188 System.arraycopy(bytes, offset, elmt, 0, BOL_LEN);
1189 arr[i] = byteArrayToBoolean(elmt);
1196 public static Boolean[] byteArrayToBooleanArray(byte[] bytes) {
1198 // Single element bytes
1199 byte[] elmt = new byte[BOL_LEN];
1201 int arrLen = bytes.length / BOL_LEN;
1202 Boolean[] arr = new Boolean[arrLen];
1203 for(int i = 0; i < arrLen; i++) {
1204 int offset = i * BOL_LEN;
1205 System.arraycopy(bytes, offset, elmt, 0, BOL_LEN);
1206 arr[i] = byteArrayToBoolean(elmt);
1213 public static String[] byteArrayToStringArray(byte[] bytes) {
1215 // Format of bytes: | array length | length #1 | string #1 | length #2 | string #2 | ...
1216 // Get string array length
1218 byte[] strArrLenBytes = new byte[INT_LEN];
1219 System.arraycopy(bytes, pos, strArrLenBytes, 0, INT_LEN);
1220 int strArrLen = byteArrayToInt(strArrLenBytes);
1221 pos = pos + INT_LEN;
1222 // Prepare string array
1223 String[] strArray = new String[strArrLen];
1224 // Extract array of strings
1225 for(int i = 0; i < strArrLen; i++) {
1227 // Extract string length
1228 byte[] strLenBytes = new byte[INT_LEN];
1229 System.arraycopy(bytes, pos, strLenBytes, 0, INT_LEN);
1230 int strLen = byteArrayToInt(strLenBytes);
1231 pos = pos + INT_LEN;
1233 byte[] strBytes = new byte[strLen];
1234 System.arraycopy(bytes, pos, strBytes, 0, strLen);
1236 strArray[i] = byteArrayToString(strBytes);
1244 * toByteArray() gets Object and return its byte array
1246 * Adapted from http://www.java2s.com/
1247 * @see <a href="http://www.java2s.com/Code/Java/File-Input-
1248 * Output/Convertobjecttobytearrayandconvertbytearraytoobject.htm"</a>
1250 // toByteArray and toObject are taken from: http://tinyurl.com/69h8l7x
1251 public static byte[] toByteArray(Object obj) throws IOException {
1253 byte[] bytes = null;
1254 ByteArrayOutputStream bos = null;
1255 ObjectOutputStream oos = null;
1258 bos = new ByteArrayOutputStream();
1259 oos = new ObjectOutputStream(bos);
1260 oos.writeObject(obj);
1262 bytes = bos.toByteArray();
1277 * toObject() gets byte array and return its Object
1279 * Adapted from http://www.java2s.com/
1280 * @see <a href="http://www.java2s.com/Code/Java/File-Input-
1281 * Output/Convertobjecttobytearrayandconvertbytearraytoobject.htm"</a>
1283 public static Object toObject(byte[] bytes) throws IOException, ClassNotFoundException {
1286 ByteArrayInputStream bis = null;
1287 ObjectInputStream ois = null;
1290 bis = new ByteArrayInputStream(bytes);
1291 ois = new ObjectInputStream(bis);
1292 obj = ois.readObject();
1306 public static void main(String[] args) {
1308 //boolean data = false;
1310 // float data = 1234.123f;
1311 //double data = 12.51231234;
1312 //long data = 1234l;
1313 //short data = 1234;
1314 //int data = 12345678;
1315 // byte[] result = floatToByteArray(data);
1316 // System.out.println("Result: " + Arrays.toString(result));
1317 // System.out.println("Converted back: " + byteArrayToFloat(result));
1319 //String str = "methodA(int,string,float,double,double)";
1320 //int hash = str.hashCode();
1321 //System.out.println("Hash value: " + hash);
1323 int[][] multi = new int[][] {
1329 for (int[] inner : multi ) {
1330 System.out.println("New row!");
1331 for (Object i : inner) {
1332 System.out.println("Element i: " + i);
1334 System.out.println("Change row!\n");
1337 int[] int1 = { 1, 2, 3 };
1338 int[] int2 = { 6, 5, 4 };
1339 int[] result = new int[int1.length + int2.length];
1340 System.arraycopy(int1, 0, result, 0, int1.length);
1341 System.arraycopy(int2, 0, result, int1.length, int2.length);
1343 System.out.println("Combined array: " + Arrays.toString(result));