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;
55 * Public static data structure to keep track of multiple skeletons and stubs
57 //public static IoTRMIObject rmiObj;
58 //public static IoTRMICall rmiCall;
59 //public static int objIdCounter = Integer.MAX_VALUE;
60 public static Integer objIdCounter = new Integer(Integer.MAX_VALUE);
61 public static Map<Integer,Object> mapStub = new HashMap<Integer,Object>(); // Map object to its stub ID
62 public static Map<Object,Object> mapSkel = new HashMap<Object,Object>(); // Map object to its skeleton
63 public static Map<Object,Integer> mapSkelId = new HashMap<Object,Integer>(); // Map object to its skeleton ID
71 mapPrimitives = new HashMap<String,String>();
72 IoTRMITypes.arraysToMap(mapPrimitives,
73 IoTRMITypes.primitivesJava, IoTRMITypes.primitivesCplus);
74 mapPrimitiveSizes = new HashMap<String,Integer>();
75 IoTRMITypes.arraysToMap(mapPrimitiveSizes,
76 IoTRMITypes.primitivesJava, IoTRMITypes.primitivesSizes);
77 mapNonPrimitives = new HashMap<String,String>();
78 IoTRMITypes.arraysToMap(mapNonPrimitives,
79 IoTRMITypes.nonPrimitivesJava, IoTRMITypes.nonPrimitivesCplus);
83 /*public static void initRMICall(int port, String address, int rev) throws IOException {
84 rmiCall = new IoTRMICall(port, address, rev);
87 public static void initRMICall(int localPort, int port, String address, int rev) throws IOException {
88 rmiCall = new IoTRMICall(localPort, port, address, rev);
91 public static void initRMIObject(int port) throws IOException, ClassNotFoundException,
92 InstantiationException, IllegalAccessException {
93 rmiObj = new IoTRMIObject(port);
98 * getHashCodeBytes() gets hash value (in bytes) from method name
100 public static byte[] getHashCodeBytes(String string) {
102 int hash = string.hashCode();
103 byte[] hashBytes = ByteBuffer.allocate(4).putInt(hash).array();
113 * translateType() try to translate a type
115 * It returns the original type when fails.
117 public String translateType(String type) {
119 if (mapPrimitives.containsKey(type))
120 return mapPrimitives.get(type);
121 else if (mapNonPrimitives.containsKey(type))
122 return mapNonPrimitives.get(type);
129 * getTypeSize() gets the size of a type
132 public int getTypeSize(String type) {
134 if (mapPrimitiveSizes.containsKey(type))
135 return mapPrimitiveSizes.get(type);
137 return -1; // Size is unknown (variable length)
142 * getTypeSize() gets the size of a type
145 public static int getTypeSize(Class<?> type) {
148 if (type == byte.class) {
150 } else if (type == Byte.class) {
152 } else if (type == short.class) {
154 } else if (type == Short.class) {
156 } else if ( type == int.class) {
158 } else if ( type == Integer.class) {
160 } else if ( type == long.class) {
162 } else if ( type == Long.class) {
164 } else if ( type == float.class) {
166 } else if ( type == Float.class) {
168 } else if ( type == double.class) {
170 } else if ( type == Double.class) {
172 } else if ( type == boolean.class) {
174 } else if ( type == Boolean.class) {
176 } else if ( type == char.class) {
178 } else if ( type == Character[].class) {
180 } else if (type == String[].class) {
183 throw new Error("IoTRMIUtil: Unrecognizable type: " + type.getName());
190 * getParamObject() converts byte array of certain object type into Object
192 public static Object getParamObject(Class<?> type, Class<?> genTypeVal, byte[] paramBytes) {
194 Object retObj = null;
195 if (type == byte.class ||
196 type == Byte.class) {
197 retObj = (Object) paramBytes[0];
198 } else if ( type == short.class ||
199 type == Short.class) {
200 retObj = (Object) byteArrayToShort(paramBytes);
201 } else if ( type == int.class ||
202 type == Integer.class) {
203 retObj = (Object) byteArrayToInt(paramBytes);
204 } else if ( type == long.class ||
205 type == Long.class) {
206 retObj = (Object) byteArrayToLong(paramBytes);
207 } else if ( type == float.class ||
208 type == Float.class) {
209 retObj = (Object) byteArrayToFloat(paramBytes);
210 } else if ( type == double.class ||
211 type == Double.class) {
212 retObj = (Object) byteArrayToDouble(paramBytes);
213 } else if ( type == boolean.class ||
214 type == Boolean.class) {
215 retObj = (Object) byteArrayToBoolean(paramBytes);
216 } else if ( type == char.class ||
217 type == Character.class) {
218 retObj = (Object) byteArrayToChar(paramBytes);
219 } else if (type == String.class) {
220 retObj = (Object) byteArrayToString(paramBytes);
222 } else if (type.isArray()) {
223 retObj = getParamObjectArray(type, paramBytes);
225 } else if (type == List.class) {
226 retObj = getParamListObject(genTypeVal, paramBytes);
228 throw new Error("IoTRMIUtil: Unrecognizable type: " + type.getName());
235 * getParamObjectArray() converts byte array of certain object type into array of Objects
237 public static Object getParamObjectArray(Class<?> type, byte[] paramBytes) {
239 Object retObj = null;
240 if ((type == byte[].class) ||
241 (type == byte.class)) {
242 retObj = (Object) paramBytes;
243 } else if ( (type == Byte[].class) ||
244 (type == Byte.class)) {
245 retObj = (Object) byteArrayToByteArray(paramBytes);
246 } else if ( (type == short[].class) ||
247 (type == short.class)) {
248 retObj = (Object) byteArrayToShtArray(paramBytes);
249 } else if ( (type == Short[].class) ||
250 (type == Short.class)) {
251 retObj = (Object) byteArrayToShortArray(paramBytes);
252 } else if ( (type == int[].class) ||
253 (type == int.class)) {
254 retObj = (Object) byteArrayToIntArray(paramBytes);
255 } else if ( (type == Integer[].class) ||
256 (type == Integer.class)) {
257 retObj = (Object) byteArrayToIntegerArray(paramBytes);
258 } else if ( (type == long[].class) ||
259 (type == long.class)) {
260 retObj = (Object) byteArrayToLngArray(paramBytes);
261 } else if ( (type == Long[].class) ||
262 (type == Long.class)) {
263 retObj = (Object) byteArrayToLongArray(paramBytes);
264 } else if ( (type == float[].class) ||
265 (type == float.class)) {
266 retObj = (Object) byteArrayToFltArray(paramBytes);
267 } else if ( (type == Float[].class) ||
268 (type == Float.class)) {
269 retObj = (Object) byteArrayToFloatArray(paramBytes);
270 } else if ( (type == double[].class) ||
271 (type == double.class)) {
272 retObj = (Object) byteArrayToDblArray(paramBytes);
273 } else if ( (type == Double[].class) ||
274 (type == Double.class)) {
275 retObj = (Object) byteArrayToDoubleArray(paramBytes);
276 } else if ( (type == boolean[].class) ||
277 (type == boolean.class)) {
278 retObj = (Object) byteArrayToBolArray(paramBytes);
279 } else if ( (type == Boolean[].class) ||
280 (type == Boolean.class)) {
281 retObj = (Object) byteArrayToBooleanArray(paramBytes);
282 } else if ( (type == char[].class) ||
283 (type == char.class)) {
284 retObj = (Object) byteArrayToChrArray(paramBytes);
285 } else if ( (type == Character[].class) ||
286 (type == Character.class)) {
287 retObj = (Object) byteArrayToCharacterArray(paramBytes);
288 } else if ( (type == String[].class) ||
289 (type == String.class)) {
290 retObj = (Object) byteArrayToStringArray(paramBytes);
292 throw new Error("IoTRMIUtil: Unrecognizable type: " + type.getName());
299 * getObjectBytes() converts an object into byte array
301 public static byte[] getObjectBytes(Object obj) {
303 byte[] retObjBytes = null;
304 if (obj instanceof Byte) {
305 retObjBytes = new byte[] { (byte) obj };
306 } else if (obj instanceof Short) {
307 retObjBytes = shortToByteArray((short) obj);
308 } else if (obj instanceof Integer) {
309 retObjBytes = intToByteArray((int) obj);
310 } else if (obj instanceof Long) {
311 retObjBytes = longToByteArray((long) obj);
312 } else if (obj instanceof Float) {
313 retObjBytes = floatToByteArray((float) obj);
314 } else if (obj instanceof Double) {
315 retObjBytes = doubleToByteArray((double) obj);
316 } else if (obj instanceof Character) {
317 retObjBytes = charToByteArray((char) obj);
318 } else if (obj instanceof Boolean) {
319 retObjBytes = booleanToByteArray((boolean) obj);
320 } else if (obj instanceof String) {
321 retObjBytes = stringToByteArray((String) obj);
323 } else if (obj.getClass().isArray()) {
324 retObjBytes = getArrayObjectBytes(obj);
325 // List and its implementations
326 } else if (obj instanceof List<?>) {
327 retObjBytes = listToByteArray((List<?>) obj);
329 throw new Error("IoTRMIUtil: Unrecognizable object: " + obj.getClass());
336 * getArrayObjectBytes() converts array of objects into bytes array
338 public static byte[] getArrayObjectBytes(Object obj) {
340 byte[] retObjBytes = null;
341 if (obj instanceof byte[]) {
342 retObjBytes = (byte[]) obj;
343 } else if (obj instanceof Byte[]) {
344 retObjBytes = arrByteToByteArray((Byte[]) obj);
345 } else if (obj instanceof short[]) {
346 retObjBytes = arrShortToByteArray((short[]) obj);
347 } else if (obj instanceof Short[]) {
348 retObjBytes = arrShortToByteArray((Short[]) obj);
349 } else if (obj instanceof int[]) {
350 retObjBytes = arrIntToByteArray((int[]) obj);
351 } else if (obj instanceof Integer[]) {
352 retObjBytes = arrIntToByteArray((Integer[]) obj);
353 } else if (obj instanceof long[]) {
354 retObjBytes = arrLongToByteArray((long[]) obj);
355 } else if (obj instanceof Long[]) {
356 retObjBytes = arrLongToByteArray((Long[]) obj);
357 } else if (obj instanceof float[]) {
358 retObjBytes = arrFloatToByteArray((float[]) obj);
359 } else if (obj instanceof Float[]) {
360 retObjBytes = arrFloatToByteArray((Float[]) obj);
361 } else if (obj instanceof double[]) {
362 retObjBytes = arrDoubleToByteArray((double[]) obj);
363 } else if (obj instanceof Double[]) {
364 retObjBytes = arrDoubleToByteArray((Double[]) obj);
365 } else if (obj instanceof char[]) {
366 retObjBytes = arrCharToByteArray((char[]) obj);
367 } else if (obj instanceof Character[]) {
368 retObjBytes = arrCharToByteArray((Character[]) obj);
369 } else if (obj instanceof boolean[]) {
370 retObjBytes = arrBooleanToByteArray((boolean[]) obj);
371 } else if (obj instanceof Boolean[]) {
372 retObjBytes = arrBooleanToByteArray((Boolean[]) obj);
373 } else if (obj instanceof String[]) {
374 retObjBytes = arrStringToByteArray((String[]) obj);
376 throw new Error("IoTRMIUtil: Unrecognizable object: " + obj.getClass());
382 public static byte[] listToByteArray(List<?> list) {
384 // Find out the class of the type
385 Iterator<?> it = list.iterator();
386 Object[] arrObj = null;
387 Object obj = it.next();
389 if (obj instanceof Byte) {
390 arrObj = list.toArray(new Byte[list.size()]);
391 } else if (obj instanceof Short) {
392 arrObj = list.toArray(new Short[list.size()]);
393 } else if (obj instanceof Integer) {
394 arrObj = list.toArray(new Integer[list.size()]);
395 } else if (obj instanceof Long) {
396 arrObj = list.toArray(new Long[list.size()]);
397 } else if (obj instanceof Float) {
398 arrObj = list.toArray(new Float[list.size()]);
399 } else if (obj instanceof Double) {
400 arrObj = list.toArray(new Double[list.size()]);
401 } else if (obj instanceof Character) {
402 arrObj = list.toArray(new Character[list.size()]);
403 } else if (obj instanceof Boolean) {
404 arrObj = list.toArray(new Boolean[list.size()]);
405 } else if (obj instanceof String) {
406 arrObj = list.toArray(new String[list.size()]);
408 throw new Error("IoTRMIUtil: Unrecognizable object: " + obj.getClass());
410 byte[] arrObjBytes = getArrayObjectBytes(arrObj);
415 // Get a List object from bytes
416 public static Object getParamListObject(Class<?> genericType, byte[] paramBytes) {
418 List<Object> retList = new ArrayList<Object>();
419 Object retObj = null;
420 if (genericType == Byte.class) {
421 Byte[] retArr = byteArrayToByteArray(paramBytes);
422 Collections.addAll(retList, retArr);
423 } else if (genericType == Short.class) {
424 Short[] retArr = byteArrayToShortArray(paramBytes);
425 Collections.addAll(retList, retArr);
426 } else if (genericType == Integer.class) {
427 Integer[] retArr = byteArrayToIntegerArray(paramBytes);
428 Collections.addAll(retList, retArr);
429 } else if (genericType == Long.class) {
430 Long[] retArr = byteArrayToLongArray(paramBytes);
431 Collections.addAll(retList, retArr);
432 } else if (genericType == Float.class) {
433 Float[] retArr = byteArrayToFloatArray(paramBytes);
434 Collections.addAll(retList, retArr);
435 } else if (genericType == Double.class) {
436 Double[] retArr = byteArrayToDoubleArray(paramBytes);
437 Collections.addAll(retList, retArr);
438 } else if (genericType == Boolean.class) {
439 Boolean[] retArr = byteArrayToBooleanArray(paramBytes);
440 Collections.addAll(retList, retArr);
441 } else if (genericType == Character.class) {
442 Character[] retArr = byteArrayToCharacterArray(paramBytes);
443 Collections.addAll(retList, retArr);
444 } else if (genericType == String.class) {
445 String[] retArr = byteArrayToStringArray(paramBytes);
446 Collections.addAll(retList, retArr);
448 throw new Error("IoTRMIUtil: Unrecognizable object: " + genericType.getSimpleName());
455 * Converters to byte array
458 public static byte[] shortToByteArray(short s) {
460 ByteBuffer bb = ByteBuffer.allocate(SHT_LEN);
467 public static byte[] intToByteArray(int i) {
469 ByteBuffer bb = ByteBuffer.allocate(INT_LEN);
476 public static byte[] longToByteArray(long l) {
478 ByteBuffer bb = ByteBuffer.allocate(LNG_LEN);
485 public static byte[] floatToByteArray(float f) {
487 ByteBuffer bb = ByteBuffer.allocate(FLT_LEN);
494 public static byte[] doubleToByteArray(double d) {
496 ByteBuffer bb = ByteBuffer.allocate(DBL_LEN);
503 public static byte[] charToByteArray(char c) {
505 ByteBuffer bb = ByteBuffer.allocate(CHR_LEN);
512 public static byte[] booleanToByteArray(boolean b) {
514 ByteBuffer bb = ByteBuffer.allocate(BOL_LEN);
524 public static byte[] stringToByteArray(String str) {
526 return str.getBytes();
531 public static byte[] arrByteToByteArray(Byte[] arrByte) {
533 byte[] arrByt = new byte[arrByte.length];
534 for(int i = 0; i < arrByte.length; i++) {
535 arrByt[i] = arrByte[i];
542 public static byte[] arrShortToByteArray(short[] arrShort) {
544 ByteBuffer bb = ByteBuffer.allocate(SHT_LEN * arrShort.length);
545 for(short s : arrShort) {
553 public static byte[] arrShortToByteArray(Short[] arrShort) {
555 ByteBuffer bb = ByteBuffer.allocate(SHT_LEN * arrShort.length);
556 for(Short s : arrShort) {
564 public static byte[] arrIntToByteArray(int[] arrInt) {
566 ByteBuffer bb = ByteBuffer.allocate(INT_LEN * arrInt.length);
567 for(int i : arrInt) {
575 public static byte[] arrIntToByteArray(Integer[] arrInt) {
577 ByteBuffer bb = ByteBuffer.allocate(INT_LEN * arrInt.length);
578 for(Integer i : arrInt) {
586 public static byte[] arrLongToByteArray(long[] arrLong) {
588 ByteBuffer bb = ByteBuffer.allocate(LNG_LEN * arrLong.length);
589 for(long l : arrLong) {
597 public static byte[] arrLongToByteArray(Long[] arrLong) {
599 ByteBuffer bb = ByteBuffer.allocate(LNG_LEN * arrLong.length);
600 for(Long l : arrLong) {
608 public static byte[] arrFloatToByteArray(float[] arrFloat) {
610 ByteBuffer bb = ByteBuffer.allocate(FLT_LEN * arrFloat.length);
611 for(float f : arrFloat) {
619 public static byte[] arrFloatToByteArray(Float[] arrFloat) {
621 ByteBuffer bb = ByteBuffer.allocate(FLT_LEN * arrFloat.length);
622 for(Float f : arrFloat) {
630 public static byte[] arrDoubleToByteArray(double[] arrDouble) {
632 ByteBuffer bb = ByteBuffer.allocate(DBL_LEN * arrDouble.length);
633 for(double d : arrDouble) {
641 public static byte[] arrDoubleToByteArray(Double[] arrDouble) {
643 ByteBuffer bb = ByteBuffer.allocate(DBL_LEN * arrDouble.length);
644 for(Double d : arrDouble) {
652 public static byte[] arrCharToByteArray(char[] arrChar) {
654 ByteBuffer bb = ByteBuffer.allocate(CHR_LEN * arrChar.length);
655 for(char c : arrChar) {
663 public static byte[] arrCharToByteArray(Character[] arrChar) {
665 ByteBuffer bb = ByteBuffer.allocate(CHR_LEN * arrChar.length);
666 for(Character c : arrChar) {
674 public static byte[] arrBooleanToByteArray(boolean[] arrBool) {
676 ByteBuffer bb = ByteBuffer.allocate(BOL_LEN * arrBool.length);
677 for(boolean b : arrBool) {
688 public static byte[] arrBooleanToByteArray(Boolean[] arrBool) {
690 ByteBuffer bb = ByteBuffer.allocate(BOL_LEN * arrBool.length);
691 for(Boolean b : arrBool) {
702 public static byte[] arrStringToByteArray(String[] arrString) {
704 // Format of bytes: | array length | length #1 | string #1 | length #2 | string #2 | ...
705 // Prepare array of bytes
706 int arrLen = INT_LEN; // First allocation for array length
707 for (int i = 0; i < arrString.length; i++) {
708 arrLen = arrLen + INT_LEN + arrString[i].length();
710 byte[] arrStrBytes = new byte[arrLen];
713 byte[] strArrLenBytes = intToByteArray(arrString.length);
714 System.arraycopy(strArrLenBytes, 0, arrStrBytes, pos, INT_LEN);
716 for (String str : arrString) {
718 // Copy string length
719 int strLen = str.length();
720 byte[] strLenBytes = intToByteArray(strLen);
721 System.arraycopy(strLenBytes, 0, arrStrBytes, pos, INT_LEN);
724 byte[] strBytes = stringToByteArray(str);
725 System.arraycopy(strBytes, 0, arrStrBytes, pos, strLen);
734 * Converters from byte array
737 public static short byteArrayToShort(byte[] bytes) {
739 return ByteBuffer.wrap(bytes).getShort();
743 public static int byteArrayToInt(byte[] bytes) {
745 return ByteBuffer.wrap(bytes).getInt();
749 public static long byteArrayToLong(byte[] bytes) {
751 return ByteBuffer.wrap(bytes).getLong();
755 public static float byteArrayToFloat(byte[] bytes) {
757 return ByteBuffer.wrap(bytes).getFloat();
761 public static double byteArrayToDouble(byte[] bytes) {
763 return ByteBuffer.wrap(bytes).getDouble();
767 public static char byteArrayToChar(byte[] bytes) {
769 return ByteBuffer.wrap(bytes).getChar();
773 public static boolean byteArrayToBoolean(byte[] bytes) {
775 Byte boolValByte = ByteBuffer.wrap(bytes).get();
776 short boolVal = boolValByte.shortValue();
784 public static String byteArrayToString(byte[] bytes) {
785 return new String(bytes);
790 public static Byte[] byteArrayToByteArray(byte[] arrByt) {
792 Byte[] arrByte = new Byte[arrByt.length];
793 for(int i = 0; i < arrByt.length; i++) {
794 arrByte[i] = arrByt[i];
801 public static short[] byteArrayToShtArray(byte[] bytes) {
803 // Single element bytes
804 byte[] elmt = new byte[SHT_LEN];
806 int arrLen = bytes.length / SHT_LEN;
807 short[] arr = new short[arrLen];
808 for(int i = 0; i < arrLen; i++) {
809 int offset = i * SHT_LEN;
810 System.arraycopy(bytes, offset, elmt, 0, SHT_LEN);
811 arr[i] = byteArrayToShort(elmt);
818 public static Short[] byteArrayToShortArray(byte[] bytes) {
820 // Single element bytes
821 byte[] elmt = new byte[SHT_LEN];
823 int arrLen = bytes.length / SHT_LEN;
824 Short[] arr = new Short[arrLen];
825 for(int i = 0; i < arrLen; i++) {
826 int offset = i * SHT_LEN;
827 System.arraycopy(bytes, offset, elmt, 0, SHT_LEN);
828 arr[i] = byteArrayToShort(elmt);
835 public static int[] byteArrayToIntArray(byte[] bytes) {
837 // Single element bytes
838 byte[] elmt = new byte[INT_LEN];
840 int arrLen = bytes.length / INT_LEN;
841 int[] arr = new int[arrLen];
842 for(int i = 0; i < arrLen; i++) {
843 int offset = i * INT_LEN;
844 System.arraycopy(bytes, offset, elmt, 0, INT_LEN);
845 arr[i] = byteArrayToInt(elmt);
852 public static Integer[] byteArrayToIntegerArray(byte[] bytes) {
854 // Single element bytes
855 byte[] elmt = new byte[INT_LEN];
857 int arrLen = bytes.length / INT_LEN;
858 Integer[] arr = new Integer[arrLen];
859 for(int i = 0; i < arrLen; i++) {
860 int offset = i * INT_LEN;
861 System.arraycopy(bytes, offset, elmt, 0, INT_LEN);
862 arr[i] = byteArrayToInt(elmt);
869 public static long[] byteArrayToLngArray(byte[] bytes) {
871 // Single element bytes
872 byte[] elmt = new byte[LNG_LEN];
874 int arrLen = bytes.length / LNG_LEN;
875 long[] arr = new long[arrLen];
876 for(int i = 0; i < arrLen; i++) {
877 int offset = i * LNG_LEN;
878 System.arraycopy(bytes, offset, elmt, 0, LNG_LEN);
879 arr[i] = byteArrayToLong(elmt);
886 public static Long[] byteArrayToLongArray(byte[] bytes) {
888 // Single element bytes
889 byte[] elmt = new byte[LNG_LEN];
891 int arrLen = bytes.length / LNG_LEN;
892 Long[] arr = new Long[arrLen];
893 for(int i = 0; i < arrLen; i++) {
894 int offset = i * LNG_LEN;
895 System.arraycopy(bytes, offset, elmt, 0, LNG_LEN);
896 arr[i] = byteArrayToLong(elmt);
903 public static float[] byteArrayToFltArray(byte[] bytes) {
905 // Single element bytes
906 byte[] elmt = new byte[FLT_LEN];
908 int arrLen = bytes.length / FLT_LEN;
909 float[] arr = new float[arrLen];
910 for(int i = 0; i < arrLen; i++) {
911 int offset = i * FLT_LEN;
912 System.arraycopy(bytes, offset, elmt, 0, FLT_LEN);
913 arr[i] = byteArrayToFloat(elmt);
920 public static Float[] byteArrayToFloatArray(byte[] bytes) {
922 // Single element bytes
923 byte[] elmt = new byte[FLT_LEN];
925 int arrLen = bytes.length / FLT_LEN;
926 Float[] arr = new Float[arrLen];
927 for(int i = 0; i < arrLen; i++) {
928 int offset = i * FLT_LEN;
929 System.arraycopy(bytes, offset, elmt, 0, FLT_LEN);
930 arr[i] = byteArrayToFloat(elmt);
937 public static double[] byteArrayToDblArray(byte[] bytes) {
939 // Single element bytes
940 byte[] elmt = new byte[DBL_LEN];
942 int arrLen = bytes.length / DBL_LEN;
943 double[] arr = new double[arrLen];
944 for(int i = 0; i < arrLen; i++) {
945 int offset = i * DBL_LEN;
946 System.arraycopy(bytes, offset, elmt, 0, DBL_LEN);
947 arr[i] = byteArrayToDouble(elmt);
954 public static Double[] byteArrayToDoubleArray(byte[] bytes) {
956 // Single element bytes
957 byte[] elmt = new byte[DBL_LEN];
959 int arrLen = bytes.length / DBL_LEN;
960 Double[] arr = new Double[arrLen];
961 for(int i = 0; i < arrLen; i++) {
962 int offset = i * DBL_LEN;
963 System.arraycopy(bytes, offset, elmt, 0, DBL_LEN);
964 arr[i] = byteArrayToDouble(elmt);
971 public static char[] byteArrayToChrArray(byte[] bytes) {
973 // Single element bytes
974 byte[] elmt = new byte[CHR_LEN];
976 int arrLen = bytes.length / CHR_LEN;
977 char[] arr = new char[arrLen];
978 for(int i = 0; i < arrLen; i++) {
979 int offset = i * CHR_LEN;
980 System.arraycopy(bytes, offset, elmt, 0, CHR_LEN);
981 arr[i] = byteArrayToChar(elmt);
988 public static Character[] byteArrayToCharacterArray(byte[] bytes) {
990 // Single element bytes
991 byte[] elmt = new byte[CHR_LEN];
993 int arrLen = bytes.length / CHR_LEN;
994 Character[] arr = new Character[arrLen];
995 for(int i = 0; i < arrLen; i++) {
996 int offset = i * CHR_LEN;
997 System.arraycopy(bytes, offset, elmt, 0, CHR_LEN);
998 arr[i] = byteArrayToChar(elmt);
1005 public static boolean[] byteArrayToBolArray(byte[] bytes) {
1007 // Single element bytes
1008 byte[] elmt = new byte[BOL_LEN];
1010 int arrLen = bytes.length / BOL_LEN;
1011 boolean[] arr = new boolean[arrLen];
1012 for(int i = 0; i < arrLen; i++) {
1013 int offset = i * BOL_LEN;
1014 System.arraycopy(bytes, offset, elmt, 0, BOL_LEN);
1015 arr[i] = byteArrayToBoolean(elmt);
1022 public static Boolean[] byteArrayToBooleanArray(byte[] bytes) {
1024 // Single element bytes
1025 byte[] elmt = new byte[BOL_LEN];
1027 int arrLen = bytes.length / BOL_LEN;
1028 Boolean[] arr = new Boolean[arrLen];
1029 for(int i = 0; i < arrLen; i++) {
1030 int offset = i * BOL_LEN;
1031 System.arraycopy(bytes, offset, elmt, 0, BOL_LEN);
1032 arr[i] = byteArrayToBoolean(elmt);
1039 public static String[] byteArrayToStringArray(byte[] bytes) {
1041 // Format of bytes: | array length | length #1 | string #1 | length #2 | string #2 | ...
1042 // Get string array length
1044 byte[] strArrLenBytes = new byte[INT_LEN];
1045 System.arraycopy(bytes, pos, strArrLenBytes, 0, INT_LEN);
1046 int strArrLen = byteArrayToInt(strArrLenBytes);
1047 pos = pos + INT_LEN;
1048 // Prepare string array
1049 String[] strArray = new String[strArrLen];
1050 // Extract array of strings
1051 for(int i = 0; i < strArrLen; i++) {
1053 // Extract string length
1054 byte[] strLenBytes = new byte[INT_LEN];
1055 System.arraycopy(bytes, pos, strLenBytes, 0, INT_LEN);
1056 int strLen = byteArrayToInt(strLenBytes);
1057 pos = pos + INT_LEN;
1059 byte[] strBytes = new byte[strLen];
1060 System.arraycopy(bytes, pos, strBytes, 0, strLen);
1062 strArray[i] = byteArrayToString(strBytes);
1070 * toByteArray() gets Object and return its byte array
1072 * Adapted from http://www.java2s.com/
1073 * @see <a href="http://www.java2s.com/Code/Java/File-Input-
1074 * Output/Convertobjecttobytearrayandconvertbytearraytoobject.htm"</a>
1076 // toByteArray and toObject are taken from: http://tinyurl.com/69h8l7x
1077 public static byte[] toByteArray(Object obj) throws IOException {
1079 byte[] bytes = null;
1080 ByteArrayOutputStream bos = null;
1081 ObjectOutputStream oos = null;
1084 bos = new ByteArrayOutputStream();
1085 oos = new ObjectOutputStream(bos);
1086 oos.writeObject(obj);
1088 bytes = bos.toByteArray();
1103 * toObject() gets byte array and return its Object
1105 * Adapted from http://www.java2s.com/
1106 * @see <a href="http://www.java2s.com/Code/Java/File-Input-
1107 * Output/Convertobjecttobytearrayandconvertbytearraytoobject.htm"</a>
1109 public static Object toObject(byte[] bytes) throws IOException, ClassNotFoundException {
1112 ByteArrayInputStream bis = null;
1113 ObjectInputStream ois = null;
1116 bis = new ByteArrayInputStream(bytes);
1117 ois = new ObjectInputStream(bis);
1118 obj = ois.readObject();