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.HashMap;
12 import java.lang.reflect.Method;
14 import iotrmi.IoTRMITypes;
16 /** Class IoTRMI provides utility services.
18 * It provides miscellaneous (data type/value) translations.
20 * @author Rahmadi Trimananda <rtrimana @ uci.edu>
24 public class IoTRMIUtil {
29 private Map<String,String> mapPrimitives;
30 private Map<String,Integer> mapPrimitiveSizesJava;
31 private Map<String,Integer> mapPrimitiveSizesCplus;
32 private Map<String,String> mapNonPrimitives;
37 public final static int METHOD_ID_LEN = 4; // 4 bytes = 32 bits
38 public final static int PARAM_LEN = 4; // 4 bytes = 32 bits (4-byte field that stores the length of the param)
45 mapPrimitives = new HashMap<String,String>();
46 IoTRMITypes.arraysToMap(mapPrimitives,
47 IoTRMITypes.primitivesJava, IoTRMITypes.primitivesCplus);
48 mapPrimitiveSizesJava = new HashMap<String,Integer>();
49 IoTRMITypes.arraysToMap(mapPrimitiveSizesJava,
50 IoTRMITypes.primitivesJava, IoTRMITypes.primitivesJavaSizes);
51 mapPrimitiveSizesCplus = new HashMap<String,Integer>();
52 IoTRMITypes.arraysToMap(mapPrimitiveSizesCplus,
53 IoTRMITypes.primitivesCplus, IoTRMITypes.primitivesCplusSizes);
54 mapNonPrimitives = new HashMap<String,String>();
55 IoTRMITypes.arraysToMap(mapNonPrimitives,
56 IoTRMITypes.nonPrimitivesJava, IoTRMITypes.nonPrimitivesCplus);
61 * getSignature() gets method signature, i.e. type, identifier, parameters
63 public String getSignature(Method m) {
65 String retType = translateType(m.getReturnType().getSimpleName());
66 String signature = retType +
68 Class<?>[] clsParam = m.getParameterTypes();
69 for (int i = 0; i < clsParam.length; i++) {
70 String paramType = translateType(clsParam[i].getSimpleName());
71 signature = signature + paramType;
72 if (i < clsParam.length - 1) {
73 signature = signature + ",";
76 signature = signature + ")";
82 * getHashCodeBytes() gets hash value (in bytes) from method name
84 public static byte[] getHashCodeBytes(String string) {
86 int hash = string.hashCode();
87 byte[] hashBytes = ByteBuffer.allocate(4).putInt(hash).array();
97 * translateType() try to translate a type
99 * It returns the original type when fails.
101 public String translateType(String type) {
103 if (mapPrimitives.containsKey(type))
104 return mapPrimitives.get(type);
105 else if (mapNonPrimitives.containsKey(type))
106 return mapNonPrimitives.get(type);
113 * getTypeSize() gets the size of a type
116 public int getTypeSize(String type) {
118 if (mapPrimitiveSizesJava.containsKey(type))
119 return mapPrimitiveSizesJava.get(type);
120 else if (mapPrimitiveSizesCplus.containsKey(type))
121 return mapPrimitiveSizesCplus.get(type);
123 return -1; // Size is unknown
128 * getParamObject() converts byte array of certain object type into Object
130 public static Object getParamObject(String type, byte[] paramBytes) {
132 Object retObj = null;
133 if (type.equals("byte") ||
134 type.equals("Byte")) {
135 retObj = (Object) paramBytes[0];
136 } else if ( type.equals("short") ||
137 type.equals("Short")) {
138 retObj = (Object) byteArrayToShort(paramBytes);
139 } else if ( type.equals("int") ||
140 type.equals("Integer")) {
141 retObj = (Object) byteArrayToInt(paramBytes);
142 } else if ( type.equals("long") ||
143 type.equals("Long")) {
144 retObj = (Object) byteArrayToLong(paramBytes);
145 } else if ( type.equals("float") ||
146 type.equals("Float")) {
147 retObj = (Object) byteArrayToFloat(paramBytes);
148 } else if ( type.equals("double") ||
149 type.equals("Double")) {
150 retObj = (Object) byteArrayToDouble(paramBytes);
151 } else if ( type.equals("boolean") ||
152 type.equals("Boolean")) {
153 retObj = (Object) byteArrayToBoolean(paramBytes);
154 } else if ( type.equals("char") ||
155 type.equals("Character")) {
156 retObj = (Object) byteArrayToChar(paramBytes);
157 } else if (type.equals("String")) {
158 retObj = (Object) toString(paramBytes);
160 throw new Error("IoTRMIUtil: Unrecognizable type: " + type);
167 * getObjectBytes() converts an object into byte array
169 public static byte[] getObjectBytes(Object obj) {
171 byte[] retObjBytes = null;
172 if (obj instanceof Byte) {
173 retObjBytes = (byte[]) obj;
174 } else if (obj instanceof Short) {
175 retObjBytes = shortToByteArray((short) obj);
176 } else if (obj instanceof Integer) {
177 retObjBytes = intToByteArray((int) obj);
178 } else if (obj instanceof Long) {
179 retObjBytes = longToByteArray((long) obj);
180 } else if (obj instanceof Float) {
181 retObjBytes = floatToByteArray((float) obj);
182 } else if (obj instanceof Double) {
183 retObjBytes = doubleToByteArray((double) obj);
184 } else if (obj instanceof Character) {
185 retObjBytes = charToByteArray((char) obj);
186 } else if (obj instanceof Boolean) {
187 retObjBytes = booleanToByteArray((boolean) obj);
188 } else if (obj instanceof String) {
189 retObjBytes = ((String) obj).getBytes();
191 throw new Error("IoTRMIUtil: Unrecognizable object: " + obj);
198 * Converters to byte array
201 public static byte[] shortToByteArray(short s) {
203 ByteBuffer bb = ByteBuffer.allocate(2);
210 public static byte[] intToByteArray(int i) {
212 ByteBuffer bb = ByteBuffer.allocate(4);
219 public static byte[] longToByteArray(long l) {
221 ByteBuffer bb = ByteBuffer.allocate(8);
228 public static byte[] floatToByteArray(float f) {
230 ByteBuffer bb = ByteBuffer.allocate(4);
237 public static byte[] doubleToByteArray(double d) {
239 ByteBuffer bb = ByteBuffer.allocate(8);
246 public static byte[] charToByteArray(char c) {
248 ByteBuffer bb = ByteBuffer.allocate(2);
255 public static byte[] booleanToByteArray(boolean b) {
257 ByteBuffer bb = ByteBuffer.allocate(1);
268 public static byte[] arrShortToByteArray(short[] arrShort) {
270 ByteBuffer bb = ByteBuffer.allocate(2 * arrShort.length);
271 for(short s : arrShort) {
279 public static byte[] arrIntToByteArray(int[] arrInt) {
281 ByteBuffer bb = ByteBuffer.allocate(4 * arrInt.length);
282 for(int i : arrInt) {
290 public static byte[] arrLongToByteArray(long[] arrLong) {
292 ByteBuffer bb = ByteBuffer.allocate(8 * arrLong.length);
293 for(long l : arrLong) {
301 public static byte[] arrFloatToByteArray(float[] arrFloat) {
303 ByteBuffer bb = ByteBuffer.allocate(4 * arrFloat.length);
304 for(float f : arrFloat) {
312 public static byte[] arrDoubleToByteArray(double[] arrDouble) {
314 ByteBuffer bb = ByteBuffer.allocate(8 * arrDouble.length);
315 for(double d : arrDouble) {
323 public static byte[] arrCharToByteArray(char[] arrChar) {
325 ByteBuffer bb = ByteBuffer.allocate(2 * arrChar.length);
326 for(char c : arrChar) {
334 public static byte[] arrBooleanToByteArray(boolean[] arrBool) {
336 ByteBuffer bb = ByteBuffer.allocate(1 * arrBool.length);
337 for(boolean b : arrBool) {
349 * Converters from byte array
352 public static short byteArrayToShort(byte[] bytes) {
354 return ByteBuffer.wrap(bytes).getShort();
358 public static int byteArrayToInt(byte[] bytes) {
360 return ByteBuffer.wrap(bytes).getInt();
364 public static long byteArrayToLong(byte[] bytes) {
366 return ByteBuffer.wrap(bytes).getLong();
370 public static float byteArrayToFloat(byte[] bytes) {
372 return ByteBuffer.wrap(bytes).getFloat();
376 public static double byteArrayToDouble(byte[] bytes) {
378 return ByteBuffer.wrap(bytes).getDouble();
382 public static char byteArrayToChar(byte[] bytes) {
384 return ByteBuffer.wrap(bytes).getChar();
388 public static boolean byteArrayToBoolean(byte[] bytes) {
390 Byte boolValByte = ByteBuffer.wrap(bytes).get();
391 short boolVal = boolValByte.shortValue();
399 public static String toString(byte[] bytes) {
400 return new String(bytes);
405 * toByteArray() gets Object and return its byte array
407 * Adapted from http://www.java2s.com/
408 * @see <a href="http://www.java2s.com/Code/Java/File-Input-
409 * Output/Convertobjecttobytearrayandconvertbytearraytoobject.htm"</a>
411 // toByteArray and toObject are taken from: http://tinyurl.com/69h8l7x
412 public static byte[] toByteArray(Object obj) throws IOException {
415 ByteArrayOutputStream bos = null;
416 ObjectOutputStream oos = null;
419 bos = new ByteArrayOutputStream();
420 oos = new ObjectOutputStream(bos);
421 oos.writeObject(obj);
423 bytes = bos.toByteArray();
438 * toObject() gets byte array and return its Object
440 * Adapted from http://www.java2s.com/
441 * @see <a href="http://www.java2s.com/Code/Java/File-Input-
442 * Output/Convertobjecttobytearrayandconvertbytearraytoobject.htm"</a>
444 public static Object toObject(byte[] bytes) throws IOException, ClassNotFoundException {
447 ByteArrayInputStream bis = null;
448 ObjectInputStream ois = null;
451 bis = new ByteArrayInputStream(bytes);
452 ois = new ObjectInputStream(bis);
453 obj = ois.readObject();