} else if ( (type == String[].class) ||
(type == String.class)) {
retObj = (Object) byteArrayToStringArray(paramBytes);
- //} else if (type.isArray()) {
- // This is an array but it's more than 1 dimension, e.g. 2-dimensional,
- // 3-dimensional, etc.
- // for loop to check inner array perhaps using object
- // then call this function recursively
- // combine the result altogether
-
} else
throw new Error("IoTRMIUtil: Unrecognizable type: " + type.getName());
// Arrays
} else if (obj.getClass().isArray()) {
retObjBytes = getArrayObjectBytes(obj);
- // Set and its implementations
- /*} else if (obj instanceof Set<?>) {
- retObjBytes = setToByteArray((Set<?>) obj);*/
// List and its implementations
} else if (obj instanceof List<?>) {
retObjBytes = listToByteArray((List<?>) obj);
- // Map and its implementations
- /*} else if (obj instanceof Map<?,?>) {
- retObjBytes = mapToByteArray((Map<?,?>) obj);*/
} else
throw new Error("IoTRMIUtil: Unrecognizable object: " + obj.getClass());
}
- // Collection data structures
- /*public static byte[] setToByteArray(Set<?> set) {
-
- // Find out the class of the type
- Iterator<?> it = set.iterator();
- Object[] arrObj = null;
- Object obj = it.next();
-
- if (obj instanceof Byte) {
- arrObj = set.toArray(new Byte[set.size()]);
- } else if (obj instanceof Short) {
- arrObj = set.toArray(new Short[set.size()]);
- } else if (obj instanceof Integer) {
- arrObj = set.toArray(new Integer[set.size()]);
- } else if (obj instanceof Long) {
- arrObj = set.toArray(new Long[set.size()]);
- } else if (obj instanceof Float) {
- arrObj = set.toArray(new Float[set.size()]);
- } else if (obj instanceof Double) {
- arrObj = set.toArray(new Double[set.size()]);
- } else if (obj instanceof Character) {
- arrObj = set.toArray(new Character[set.size()]);
- } else if (obj instanceof Boolean) {
- arrObj = set.toArray(new Boolean[set.size()]);
- } else if (obj instanceof String) {
- arrObj = set.toArray(new String[set.size()]);
- } else
- throw new Error("IoTRMIUtil: Unrecognizable object: " + obj.getClass());
-
- byte[] arrObjBytes = getArrayObjectBytes(arrObj);
- return arrObjBytes;
- }*/
-
-
public static byte[] listToByteArray(List<?> list) {
// Find out the class of the type
}
- // Convert keySet of a Map
- /*public static byte[] mapKeyToByteArray(Map<?,?> map) {
-
- // Map<K,V>
- // Find out the class of the type for K
- Iterator<?> it = map.keySet().iterator();
- Object[] arrObj = null;
- Object obj = it.next();
-
- if (obj instanceof Byte) {
- arrObj = map.keySet().toArray(new Byte[map.size()]);
- } else if (obj instanceof Short) {
- arrObj = map.keySet().toArray(new Short[map.size()]);
- } else if (obj instanceof Integer) {
- arrObj = map.keySet().toArray(new Integer[map.size()]);
- } else if (obj instanceof Long) {
- arrObj = map.keySet().toArray(new Long[map.size()]);
- } else if (obj instanceof Float) {
- arrObj = map.keySet().toArray(new Float[map.size()]);
- } else if (obj instanceof Double) {
- arrObj = map.keySet().toArray(new Double[map.size()]);
- } else if (obj instanceof Character) {
- arrObj = map.keySet().toArray(new Character[map.size()]);
- } else if (obj instanceof Boolean) {
- arrObj = map.keySet().toArray(new Boolean[map.size()]);
- } else if (obj instanceof String) {
- arrObj = map.keySet().toArray(new String[map.size()]);
- } else
- throw new Error("IoTRMIUtil: Unrecognizable object: " + obj.getClass());
- byte[] arrObjBytes = getArrayObjectBytes(arrObj);
-
- return arrObjBytes;
- }
-
-
- // Convert entrySet of a Map
- public static byte[] mapEntryToByteArray(Map<?,?> map) {
-
- // Map<K,V>
- // Find out the class of the type for V
- Iterator<?> it = map.values().iterator();
- Object[] arrObj = null;
- Object obj = it.next();
-
- if (obj instanceof Byte) {
- arrObj = map.values().toArray(new Byte[map.size()]);
- } else if (obj instanceof Short) {
- arrObj = map.values().toArray(new Short[map.size()]);
- } else if (obj instanceof Integer) {
- arrObj = map.values().toArray(new Integer[map.size()]);
- } else if (obj instanceof Long) {
- arrObj = map.values().toArray(new Long[map.size()]);
- } else if (obj instanceof Float) {
- arrObj = map.values().toArray(new Float[map.size()]);
- } else if (obj instanceof Double) {
- arrObj = map.values().toArray(new Double[map.size()]);
- } else if (obj instanceof Character) {
- arrObj = map.values().toArray(new Character[map.size()]);
- } else if (obj instanceof Boolean) {
- arrObj = map.values().toArray(new Boolean[map.size()]);
- } else if (obj instanceof String) {
- arrObj = map.values().toArray(new String[map.size()]);
- } else
- throw new Error("IoTRMIUtil: Unrecognizable object: " + obj.getClass());
-
- byte[] arrObjBytes = getArrayObjectBytes(arrObj);
- return arrObjBytes;
- }
-
-
- // Merge keySet and entrySet of a Map into one long byte array
- public static byte[] mapToByteArray(Map<?,?> map) {
-
- // Put map size in the packet
- byte[] numEntries = intToByteArray(map.size());
- byte[] keySetBytes = mapKeyToByteArray(map);
- byte[] entrySetBytes = mapEntryToByteArray(map);
- byte[] mapBytes = new byte[INT_LEN + keySetBytes.length + entrySetBytes.length];
- // Copy the bytes
- System.arraycopy(numEntries, 0, mapBytes, 0, INT_LEN);
- System.arraycopy(keySetBytes, 0, mapBytes, INT_LEN, keySetBytes.length);
- System.arraycopy(entrySetBytes, 0, mapBytes, (INT_LEN + keySetBytes.length), entrySetBytes.length);
-
- return mapBytes;
- }
-
-
- // Get a Set object from bytes
- public static Object getParamSetObject(Class<?> genericType, byte[] paramBytes) {
-
- Set<Object> retSet = new HashSet<Object>();
- Object retObj = null;
- if (genericType == Byte.class) {
- Byte[] retArr = byteArrayToByteArray(paramBytes);
- Collections.addAll(retSet, retArr);
- } else if (genericType == Short.class) {
- Short[] retArr = byteArrayToShortArray(paramBytes);
- Collections.addAll(retSet, retArr);
- } else if (genericType == Integer.class) {
- Integer[] retArr = byteArrayToIntegerArray(paramBytes);
- Collections.addAll(retSet, retArr);
- } else if (genericType == Long.class) {
- Long[] retArr = byteArrayToLongArray(paramBytes);
- Collections.addAll(retSet, retArr);
- } else if (genericType == Float.class) {
- Float[] retArr = byteArrayToFloatArray(paramBytes);
- Collections.addAll(retSet, retArr);
- } else if (genericType == Double.class) {
- Double[] retArr = byteArrayToDoubleArray(paramBytes);
- Collections.addAll(retSet, retArr);
- } else if (genericType == Boolean.class) {
- Boolean[] retArr = byteArrayToBooleanArray(paramBytes);
- Collections.addAll(retSet, retArr);
- } else if (genericType == Character.class) {
- Character[] retArr = byteArrayToCharacterArray(paramBytes);
- Collections.addAll(retSet, retArr);
- } else if (genericType == String.class) {
- String[] retArr = byteArrayToStringArray(paramBytes);
- Collections.addAll(retSet, retArr);
- } else
- throw new Error("IoTRMIUtil: Unrecognizable object: " + genericType.getSimpleName());
-
- return retSet;
- }*/
-
-
// Get a List object from bytes
public static Object getParamListObject(Class<?> genericType, byte[] paramBytes) {
}
- // Get a Key array for Map object from bytes
- /*public static Object getParamMapObject(Class<?> genTypeKey, Class<?> genTypeVal, byte[] paramBytes) {
-
- // The complete set of bytes always consists of all keys followed by all values - <K,V> pairs
- // Calculate number of elements
- byte[] numElBytes = new byte[INT_LEN];
- System.arraycopy(paramBytes, 0, numElBytes, 0, INT_LEN);
- int numEl = byteArrayToInt(numElBytes);
- int keyLen = numEl * getTypeSize(genTypeKey);
- int valLen = numEl * getTypeSize(genTypeVal);
- byte[] prmKeyBytes = new byte[keyLen];
- byte[] prmValBytes = new byte[valLen];
- // Copy bytes
- System.arraycopy(paramBytes, INT_LEN, prmKeyBytes, 0, keyLen);
- System.arraycopy(paramBytes, (INT_LEN + keyLen), prmValBytes, 0, valLen);
- // Get array of keys
- Object[] retObjKey = (Object[]) getParamObjectArray(genTypeKey, prmKeyBytes);
- Object[] retObjVal = (Object[]) getParamObjectArray(genTypeVal, prmValBytes);
- // Put everything back to a Map
- Map<Object,Object> retMap = new HashMap<Object,Object>();
- IoTRMITypes.arraysToMap(retMap, retObjKey, retObjVal);
-
- return retMap;
- }*/
-
-
/**
* Converters to byte array
*/