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 if ((type == byte[].class) ||
215 (type == byte.class)) {
216 retObj = (Object) paramBytes;
217 } else if ( (type == Byte[].class) ||
218 (type == Byte.class)) {
219 retObj = (Object) byteArrayToByteArray(paramBytes);
220 } else if ( (type == short[].class) ||
221 (type == short.class)) {
222 retObj = (Object) byteArrayToShtArray(paramBytes);
223 } else if ( (type == Short[].class) ||
224 (type == Short.class)) {
225 retObj = (Object) byteArrayToShortArray(paramBytes);
226 } else if ( (type == int[].class) ||
227 (type == int.class)) {
228 retObj = (Object) byteArrayToIntArray(paramBytes);
229 } else if ( (type == Integer[].class) ||
230 (type == Integer.class)) {
231 retObj = (Object) byteArrayToIntegerArray(paramBytes);
232 } else if ( (type == long[].class) ||
233 (type == long.class)) {
234 retObj = (Object) byteArrayToLngArray(paramBytes);
235 } else if ( (type == Long[].class) ||
236 (type == Long.class)) {
237 retObj = (Object) byteArrayToLongArray(paramBytes);
238 } else if ( (type == float[].class) ||
239 (type == float.class)) {
240 retObj = (Object) byteArrayToFltArray(paramBytes);
241 } else if ( (type == Float[].class) ||
242 (type == Float.class)) {
243 retObj = (Object) byteArrayToFloatArray(paramBytes);
244 } else if ( (type == double[].class) ||
245 (type == double.class)) {
246 retObj = (Object) byteArrayToDblArray(paramBytes);
247 } else if ( (type == Double[].class) ||
248 (type == Double.class)) {
249 retObj = (Object) byteArrayToDoubleArray(paramBytes);
250 } else if ( (type == boolean[].class) ||
251 (type == boolean.class)) {
252 retObj = (Object) byteArrayToBolArray(paramBytes);
253 } else if ( (type == Boolean[].class) ||
254 (type == Boolean.class)) {
255 retObj = (Object) byteArrayToBooleanArray(paramBytes);
256 } else if ( (type == char[].class) ||
257 (type == char.class)) {
258 retObj = (Object) byteArrayToChrArray(paramBytes);
259 } else if ( (type == Character[].class) ||
260 (type == Character.class)) {
261 retObj = (Object) byteArrayToCharacterArray(paramBytes);
262 } else if ( (type == String[].class) ||
263 (type == String.class)) {
264 retObj = (Object) byteArrayToStringArray(paramBytes);
266 throw new Error("IoTRMIUtil: Unrecognizable type: " + type.getName());
273 * getObjectBytes() converts an object into byte array
275 public static byte[] getObjectBytes(Object obj) {
277 byte[] retObjBytes = null;
278 if (obj instanceof Byte) {
279 retObjBytes = new byte[] { (byte) obj };
280 } else if (obj instanceof Short) {
281 retObjBytes = shortToByteArray((short) obj);
282 } else if (obj instanceof Integer) {
283 retObjBytes = intToByteArray((int) obj);
284 } else if (obj instanceof Long) {
285 retObjBytes = longToByteArray((long) obj);
286 } else if (obj instanceof Float) {
287 retObjBytes = floatToByteArray((float) obj);
288 } else if (obj instanceof Double) {
289 retObjBytes = doubleToByteArray((double) obj);
290 } else if (obj instanceof Character) {
291 retObjBytes = charToByteArray((char) obj);
292 } else if (obj instanceof Boolean) {
293 retObjBytes = booleanToByteArray((boolean) obj);
294 } else if (obj instanceof String) {
295 retObjBytes = stringToByteArray((String) obj);
297 } else if (obj.getClass().isArray()) {
298 retObjBytes = getArrayObjectBytes(obj);
299 // List and its implementations
300 } else if (obj instanceof List<?>) {
301 retObjBytes = listToByteArray((List<?>) obj);
303 throw new Error("IoTRMIUtil: Unrecognizable object: " + obj.getClass());
310 * getArrayObjectBytes() converts array of objects into bytes array
312 public static byte[] getArrayObjectBytes(Object obj) {
314 byte[] retObjBytes = null;
315 if (obj instanceof byte[]) {
316 retObjBytes = (byte[]) obj;
317 } else if (obj instanceof Byte[]) {
318 retObjBytes = arrByteToByteArray((Byte[]) obj);
319 } else if (obj instanceof short[]) {
320 retObjBytes = arrShortToByteArray((short[]) obj);
321 } else if (obj instanceof Short[]) {
322 retObjBytes = arrShortToByteArray((Short[]) obj);
323 } else if (obj instanceof int[]) {
324 retObjBytes = arrIntToByteArray((int[]) obj);
325 } else if (obj instanceof Integer[]) {
326 retObjBytes = arrIntToByteArray((Integer[]) obj);
327 } else if (obj instanceof long[]) {
328 retObjBytes = arrLongToByteArray((long[]) obj);
329 } else if (obj instanceof Long[]) {
330 retObjBytes = arrLongToByteArray((Long[]) obj);
331 } else if (obj instanceof float[]) {
332 retObjBytes = arrFloatToByteArray((float[]) obj);
333 } else if (obj instanceof Float[]) {
334 retObjBytes = arrFloatToByteArray((Float[]) obj);
335 } else if (obj instanceof double[]) {
336 retObjBytes = arrDoubleToByteArray((double[]) obj);
337 } else if (obj instanceof Double[]) {
338 retObjBytes = arrDoubleToByteArray((Double[]) obj);
339 } else if (obj instanceof char[]) {
340 retObjBytes = arrCharToByteArray((char[]) obj);
341 } else if (obj instanceof Character[]) {
342 retObjBytes = arrCharToByteArray((Character[]) obj);
343 } else if (obj instanceof boolean[]) {
344 retObjBytes = arrBooleanToByteArray((boolean[]) obj);
345 } else if (obj instanceof Boolean[]) {
346 retObjBytes = arrBooleanToByteArray((Boolean[]) obj);
347 } else if (obj instanceof String[]) {
348 retObjBytes = arrStringToByteArray((String[]) obj);
350 throw new Error("IoTRMIUtil: Unrecognizable object: " + obj.getClass());
356 public static byte[] listToByteArray(List<?> list) {
358 // Find out the class of the type
359 Iterator<?> it = list.iterator();
360 Object[] arrObj = null;
361 Object obj = it.next();
363 if (obj instanceof Byte) {
364 arrObj = list.toArray(new Byte[list.size()]);
365 } else if (obj instanceof Short) {
366 arrObj = list.toArray(new Short[list.size()]);
367 } else if (obj instanceof Integer) {
368 arrObj = list.toArray(new Integer[list.size()]);
369 } else if (obj instanceof Long) {
370 arrObj = list.toArray(new Long[list.size()]);
371 } else if (obj instanceof Float) {
372 arrObj = list.toArray(new Float[list.size()]);
373 } else if (obj instanceof Double) {
374 arrObj = list.toArray(new Double[list.size()]);
375 } else if (obj instanceof Character) {
376 arrObj = list.toArray(new Character[list.size()]);
377 } else if (obj instanceof Boolean) {
378 arrObj = list.toArray(new Boolean[list.size()]);
379 } else if (obj instanceof String) {
380 arrObj = list.toArray(new String[list.size()]);
382 throw new Error("IoTRMIUtil: Unrecognizable object: " + obj.getClass());
384 byte[] arrObjBytes = getArrayObjectBytes(arrObj);
389 // Get a List object from bytes
390 public static Object getParamListObject(Class<?> genericType, byte[] paramBytes) {
392 List<Object> retList = new ArrayList<Object>();
393 Object retObj = null;
394 if (genericType == Byte.class) {
395 Byte[] retArr = byteArrayToByteArray(paramBytes);
396 Collections.addAll(retList, retArr);
397 } else if (genericType == Short.class) {
398 Short[] retArr = byteArrayToShortArray(paramBytes);
399 Collections.addAll(retList, retArr);
400 } else if (genericType == Integer.class) {
401 Integer[] retArr = byteArrayToIntegerArray(paramBytes);
402 Collections.addAll(retList, retArr);
403 } else if (genericType == Long.class) {
404 Long[] retArr = byteArrayToLongArray(paramBytes);
405 Collections.addAll(retList, retArr);
406 } else if (genericType == Float.class) {
407 Float[] retArr = byteArrayToFloatArray(paramBytes);
408 Collections.addAll(retList, retArr);
409 } else if (genericType == Double.class) {
410 Double[] retArr = byteArrayToDoubleArray(paramBytes);
411 Collections.addAll(retList, retArr);
412 } else if (genericType == Boolean.class) {
413 Boolean[] retArr = byteArrayToBooleanArray(paramBytes);
414 Collections.addAll(retList, retArr);
415 } else if (genericType == Character.class) {
416 Character[] retArr = byteArrayToCharacterArray(paramBytes);
417 Collections.addAll(retList, retArr);
418 } else if (genericType == String.class) {
419 String[] retArr = byteArrayToStringArray(paramBytes);
420 Collections.addAll(retList, retArr);
422 throw new Error("IoTRMIUtil: Unrecognizable object: " + genericType.getSimpleName());
429 * Converters to byte array
432 public static byte[] shortToByteArray(short s) {
434 ByteBuffer bb = ByteBuffer.allocate(SHT_LEN);
441 public static byte[] intToByteArray(int i) {
443 ByteBuffer bb = ByteBuffer.allocate(INT_LEN);
450 public static byte[] longToByteArray(long l) {
452 ByteBuffer bb = ByteBuffer.allocate(LNG_LEN);
459 public static byte[] floatToByteArray(float f) {
461 ByteBuffer bb = ByteBuffer.allocate(FLT_LEN);
468 public static byte[] doubleToByteArray(double d) {
470 ByteBuffer bb = ByteBuffer.allocate(DBL_LEN);
477 public static byte[] charToByteArray(char c) {
479 ByteBuffer bb = ByteBuffer.allocate(CHR_LEN);
486 public static byte[] booleanToByteArray(boolean b) {
488 ByteBuffer bb = ByteBuffer.allocate(BOL_LEN);
498 public static byte[] stringToByteArray(String str) {
500 return str.getBytes();
505 public static byte[] arrByteToByteArray(Byte[] arrByte) {
507 byte[] arrByt = new byte[arrByte.length];
508 for(int i = 0; i < arrByte.length; i++) {
509 arrByt[i] = arrByte[i];
516 public static byte[] arrShortToByteArray(short[] arrShort) {
518 ByteBuffer bb = ByteBuffer.allocate(SHT_LEN * arrShort.length);
519 for(short s : arrShort) {
527 public static byte[] arrShortToByteArray(Short[] arrShort) {
529 ByteBuffer bb = ByteBuffer.allocate(SHT_LEN * arrShort.length);
530 for(Short s : arrShort) {
538 public static byte[] arrIntToByteArray(int[] arrInt) {
540 ByteBuffer bb = ByteBuffer.allocate(INT_LEN * arrInt.length);
541 for(int i : arrInt) {
549 public static byte[] arrIntToByteArray(Integer[] arrInt) {
551 ByteBuffer bb = ByteBuffer.allocate(INT_LEN * arrInt.length);
552 for(Integer i : arrInt) {
560 public static byte[] arrLongToByteArray(long[] arrLong) {
562 ByteBuffer bb = ByteBuffer.allocate(LNG_LEN * arrLong.length);
563 for(long l : arrLong) {
571 public static byte[] arrLongToByteArray(Long[] arrLong) {
573 ByteBuffer bb = ByteBuffer.allocate(LNG_LEN * arrLong.length);
574 for(Long l : arrLong) {
582 public static byte[] arrFloatToByteArray(float[] arrFloat) {
584 ByteBuffer bb = ByteBuffer.allocate(FLT_LEN * arrFloat.length);
585 for(float f : arrFloat) {
593 public static byte[] arrFloatToByteArray(Float[] arrFloat) {
595 ByteBuffer bb = ByteBuffer.allocate(FLT_LEN * arrFloat.length);
596 for(Float f : arrFloat) {
604 public static byte[] arrDoubleToByteArray(double[] arrDouble) {
606 ByteBuffer bb = ByteBuffer.allocate(DBL_LEN * arrDouble.length);
607 for(double d : arrDouble) {
615 public static byte[] arrDoubleToByteArray(Double[] arrDouble) {
617 ByteBuffer bb = ByteBuffer.allocate(DBL_LEN * arrDouble.length);
618 for(Double d : arrDouble) {
626 public static byte[] arrCharToByteArray(char[] arrChar) {
628 ByteBuffer bb = ByteBuffer.allocate(CHR_LEN * arrChar.length);
629 for(char c : arrChar) {
637 public static byte[] arrCharToByteArray(Character[] arrChar) {
639 ByteBuffer bb = ByteBuffer.allocate(CHR_LEN * arrChar.length);
640 for(Character c : arrChar) {
648 public static byte[] arrBooleanToByteArray(boolean[] arrBool) {
650 ByteBuffer bb = ByteBuffer.allocate(BOL_LEN * arrBool.length);
651 for(boolean b : arrBool) {
662 public static byte[] arrBooleanToByteArray(Boolean[] arrBool) {
664 ByteBuffer bb = ByteBuffer.allocate(BOL_LEN * arrBool.length);
665 for(Boolean b : arrBool) {
676 public static byte[] arrStringToByteArray(String[] arrString) {
678 // Format of bytes: | array length | length #1 | string #1 | length #2 | string #2 | ...
679 // Prepare array of bytes
680 int arrLen = INT_LEN; // First allocation for array length
681 for (int i = 0; i < arrString.length; i++) {
682 arrLen = arrLen + INT_LEN + arrString[i].length();
684 byte[] arrStrBytes = new byte[arrLen];
687 byte[] strArrLenBytes = intToByteArray(arrString.length);
688 System.arraycopy(strArrLenBytes, 0, arrStrBytes, pos, INT_LEN);
690 for (String str : arrString) {
692 // Copy string length
693 int strLen = str.length();
694 byte[] strLenBytes = intToByteArray(strLen);
695 System.arraycopy(strLenBytes, 0, arrStrBytes, pos, INT_LEN);
698 byte[] strBytes = stringToByteArray(str);
699 System.arraycopy(strBytes, 0, arrStrBytes, pos, strLen);
708 * Converters from byte array
711 public static short byteArrayToShort(byte[] bytes) {
713 return ByteBuffer.wrap(bytes).getShort();
717 public static int byteArrayToInt(byte[] bytes) {
719 return ByteBuffer.wrap(bytes).getInt();
723 public static long byteArrayToLong(byte[] bytes) {
725 return ByteBuffer.wrap(bytes).getLong();
729 public static float byteArrayToFloat(byte[] bytes) {
731 return ByteBuffer.wrap(bytes).getFloat();
735 public static double byteArrayToDouble(byte[] bytes) {
737 return ByteBuffer.wrap(bytes).getDouble();
741 public static char byteArrayToChar(byte[] bytes) {
743 return ByteBuffer.wrap(bytes).getChar();
747 public static boolean byteArrayToBoolean(byte[] bytes) {
749 Byte boolValByte = ByteBuffer.wrap(bytes).get();
750 short boolVal = boolValByte.shortValue();
758 public static String byteArrayToString(byte[] bytes) {
759 return new String(bytes);
764 public static Byte[] byteArrayToByteArray(byte[] arrByt) {
766 Byte[] arrByte = new Byte[arrByt.length];
767 for(int i = 0; i < arrByt.length; i++) {
768 arrByte[i] = arrByt[i];
775 public static short[] byteArrayToShtArray(byte[] bytes) {
777 // Single element bytes
778 byte[] elmt = new byte[SHT_LEN];
780 int arrLen = bytes.length / SHT_LEN;
781 short[] arr = new short[arrLen];
782 for(int i = 0; i < arrLen; i++) {
783 int offset = i * SHT_LEN;
784 System.arraycopy(bytes, offset, elmt, 0, SHT_LEN);
785 arr[i] = byteArrayToShort(elmt);
792 public static Short[] byteArrayToShortArray(byte[] bytes) {
794 // Single element bytes
795 byte[] elmt = new byte[SHT_LEN];
797 int arrLen = bytes.length / SHT_LEN;
798 Short[] arr = new Short[arrLen];
799 for(int i = 0; i < arrLen; i++) {
800 int offset = i * SHT_LEN;
801 System.arraycopy(bytes, offset, elmt, 0, SHT_LEN);
802 arr[i] = byteArrayToShort(elmt);
809 public static int[] byteArrayToIntArray(byte[] bytes) {
811 // Single element bytes
812 byte[] elmt = new byte[INT_LEN];
814 int arrLen = bytes.length / INT_LEN;
815 int[] arr = new int[arrLen];
816 for(int i = 0; i < arrLen; i++) {
817 int offset = i * INT_LEN;
818 System.arraycopy(bytes, offset, elmt, 0, INT_LEN);
819 arr[i] = byteArrayToInt(elmt);
826 public static Integer[] byteArrayToIntegerArray(byte[] bytes) {
828 // Single element bytes
829 byte[] elmt = new byte[INT_LEN];
831 int arrLen = bytes.length / INT_LEN;
832 Integer[] arr = new Integer[arrLen];
833 for(int i = 0; i < arrLen; i++) {
834 int offset = i * INT_LEN;
835 System.arraycopy(bytes, offset, elmt, 0, INT_LEN);
836 arr[i] = byteArrayToInt(elmt);
843 public static long[] byteArrayToLngArray(byte[] bytes) {
845 // Single element bytes
846 byte[] elmt = new byte[LNG_LEN];
848 int arrLen = bytes.length / LNG_LEN;
849 long[] arr = new long[arrLen];
850 for(int i = 0; i < arrLen; i++) {
851 int offset = i * LNG_LEN;
852 System.arraycopy(bytes, offset, elmt, 0, LNG_LEN);
853 arr[i] = byteArrayToLong(elmt);
860 public static Long[] byteArrayToLongArray(byte[] bytes) {
862 // Single element bytes
863 byte[] elmt = new byte[LNG_LEN];
865 int arrLen = bytes.length / LNG_LEN;
866 Long[] arr = new Long[arrLen];
867 for(int i = 0; i < arrLen; i++) {
868 int offset = i * LNG_LEN;
869 System.arraycopy(bytes, offset, elmt, 0, LNG_LEN);
870 arr[i] = byteArrayToLong(elmt);
877 public static float[] byteArrayToFltArray(byte[] bytes) {
879 // Single element bytes
880 byte[] elmt = new byte[FLT_LEN];
882 int arrLen = bytes.length / FLT_LEN;
883 float[] arr = new float[arrLen];
884 for(int i = 0; i < arrLen; i++) {
885 int offset = i * FLT_LEN;
886 System.arraycopy(bytes, offset, elmt, 0, FLT_LEN);
887 arr[i] = byteArrayToFloat(elmt);
894 public static Float[] byteArrayToFloatArray(byte[] bytes) {
896 // Single element bytes
897 byte[] elmt = new byte[FLT_LEN];
899 int arrLen = bytes.length / FLT_LEN;
900 Float[] arr = new Float[arrLen];
901 for(int i = 0; i < arrLen; i++) {
902 int offset = i * FLT_LEN;
903 System.arraycopy(bytes, offset, elmt, 0, FLT_LEN);
904 arr[i] = byteArrayToFloat(elmt);
911 public static double[] byteArrayToDblArray(byte[] bytes) {
913 // Single element bytes
914 byte[] elmt = new byte[DBL_LEN];
916 int arrLen = bytes.length / DBL_LEN;
917 double[] arr = new double[arrLen];
918 for(int i = 0; i < arrLen; i++) {
919 int offset = i * DBL_LEN;
920 System.arraycopy(bytes, offset, elmt, 0, DBL_LEN);
921 arr[i] = byteArrayToDouble(elmt);
928 public static Double[] byteArrayToDoubleArray(byte[] bytes) {
930 // Single element bytes
931 byte[] elmt = new byte[DBL_LEN];
933 int arrLen = bytes.length / DBL_LEN;
934 Double[] arr = new Double[arrLen];
935 for(int i = 0; i < arrLen; i++) {
936 int offset = i * DBL_LEN;
937 System.arraycopy(bytes, offset, elmt, 0, DBL_LEN);
938 arr[i] = byteArrayToDouble(elmt);
945 public static char[] byteArrayToChrArray(byte[] bytes) {
947 // Single element bytes
948 byte[] elmt = new byte[CHR_LEN];
950 int arrLen = bytes.length / CHR_LEN;
951 char[] arr = new char[arrLen];
952 for(int i = 0; i < arrLen; i++) {
953 int offset = i * CHR_LEN;
954 System.arraycopy(bytes, offset, elmt, 0, CHR_LEN);
955 arr[i] = byteArrayToChar(elmt);
962 public static Character[] byteArrayToCharacterArray(byte[] bytes) {
964 // Single element bytes
965 byte[] elmt = new byte[CHR_LEN];
967 int arrLen = bytes.length / CHR_LEN;
968 Character[] arr = new Character[arrLen];
969 for(int i = 0; i < arrLen; i++) {
970 int offset = i * CHR_LEN;
971 System.arraycopy(bytes, offset, elmt, 0, CHR_LEN);
972 arr[i] = byteArrayToChar(elmt);
979 public static boolean[] byteArrayToBolArray(byte[] bytes) {
981 // Single element bytes
982 byte[] elmt = new byte[BOL_LEN];
984 int arrLen = bytes.length / BOL_LEN;
985 boolean[] arr = new boolean[arrLen];
986 for(int i = 0; i < arrLen; i++) {
987 int offset = i * BOL_LEN;
988 System.arraycopy(bytes, offset, elmt, 0, BOL_LEN);
989 arr[i] = byteArrayToBoolean(elmt);
996 public static Boolean[] byteArrayToBooleanArray(byte[] bytes) {
998 // Single element bytes
999 byte[] elmt = new byte[BOL_LEN];
1001 int arrLen = bytes.length / BOL_LEN;
1002 Boolean[] arr = new Boolean[arrLen];
1003 for(int i = 0; i < arrLen; i++) {
1004 int offset = i * BOL_LEN;
1005 System.arraycopy(bytes, offset, elmt, 0, BOL_LEN);
1006 arr[i] = byteArrayToBoolean(elmt);
1013 public static String[] byteArrayToStringArray(byte[] bytes) {
1015 // Format of bytes: | array length | length #1 | string #1 | length #2 | string #2 | ...
1016 // Get string array length
1018 byte[] strArrLenBytes = new byte[INT_LEN];
1019 System.arraycopy(bytes, pos, strArrLenBytes, 0, INT_LEN);
1020 int strArrLen = byteArrayToInt(strArrLenBytes);
1021 pos = pos + INT_LEN;
1022 // Prepare string array
1023 String[] strArray = new String[strArrLen];
1024 // Extract array of strings
1025 for(int i = 0; i < strArrLen; i++) {
1027 // Extract string length
1028 byte[] strLenBytes = new byte[INT_LEN];
1029 System.arraycopy(bytes, pos, strLenBytes, 0, INT_LEN);
1030 int strLen = byteArrayToInt(strLenBytes);
1031 pos = pos + INT_LEN;
1033 byte[] strBytes = new byte[strLen];
1034 System.arraycopy(bytes, pos, strBytes, 0, strLen);
1036 strArray[i] = byteArrayToString(strBytes);
1044 * toByteArray() gets Object and return its byte array
1046 * Adapted from http://www.java2s.com/
1047 * @see <a href="http://www.java2s.com/Code/Java/File-Input-
1048 * Output/Convertobjecttobytearrayandconvertbytearraytoobject.htm"</a>
1050 // toByteArray and toObject are taken from: http://tinyurl.com/69h8l7x
1051 public static byte[] toByteArray(Object obj) throws IOException {
1053 byte[] bytes = null;
1054 ByteArrayOutputStream bos = null;
1055 ObjectOutputStream oos = null;
1058 bos = new ByteArrayOutputStream();
1059 oos = new ObjectOutputStream(bos);
1060 oos.writeObject(obj);
1062 bytes = bos.toByteArray();
1077 * toObject() gets byte array and return its Object
1079 * Adapted from http://www.java2s.com/
1080 * @see <a href="http://www.java2s.com/Code/Java/File-Input-
1081 * Output/Convertobjecttobytearrayandconvertbytearraytoobject.htm"</a>
1083 public static Object toObject(byte[] bytes) throws IOException, ClassNotFoundException {
1086 ByteArrayInputStream bis = null;
1087 ObjectInputStream ois = null;
1090 bis = new ByteArrayInputStream(bytes);
1091 ois = new ObjectInputStream(bis);
1092 obj = ois.readObject();