1 /** Class IoTRMIUtil provides methods that the upper
2 * layers can use to transport and invoke methods
3 * when using IoTSocket, IoTSocketClient and IoTSocketServer.
5 * @author Rahmadi Trimananda <rtrimana @ uci.edu>
9 #ifndef _IOTRMIUTIL_HPP__
10 #define _IOTRMIUTIL_HPP__
25 #include "IoTRMITypes.hpp"
36 static void printBytes(char* bytes, const int len, const bool hex);
37 static int hashCode(string str);
38 static char* getHashCodeBytes(string methodSign, char* bytes);
39 int getTypeSize(string type);
40 int getVarTypeSize(string type, void* paramObj);
41 static int getArrStringLength(vector<string> arrString);
42 static int getByteStringLength(vector<string> arrString);
44 // Primitives to byte array
45 static char* shortToByteArray(short i, char* bytes);
46 static char* intToByteArray(int i, char* bytes);
47 static char* longToByteArray(int64_t i, char* bytes);
48 static char* floatToByteArray(float f, char* bytes);
49 static char* doubleToByteArray(double d, char* bytes);
50 static char* charToByteArray(char c, char* bytes);
51 static char* booleanToByteArray(bool c, char* bytes);
52 static char* stringToByteArray(string c, char* bytes);
54 // Byte array to primitives
55 static short* byteArrayToShort(short* result, char* bytes);
56 static int* byteArrayToInt(int* result, char* bytes);
57 static int64_t* byteArrayToLong(int64_t* result, char* bytes);
58 static float* byteArrayToFloat(float* result, char* bytes);
59 static double* byteArrayToDouble(double* result, char* bytes);
60 static char* byteArrayToChar(char* result, char* bytes);
61 static bool* byteArrayToBoolean(bool* result, char* bytes);
62 static string* byteArrayToString(string* result, char* bytes);
63 static string* byteArrayToString(string* result, char* bytes, int len);
65 // Get parameter object from byte array
66 static void* getParamObject(void* retObj, const char* type, char* paramBytes, int len);
67 static char* getObjectBytes(char* retObjBytes, void* obj, const char* type);
70 static char* arrShortToByteArray(vector<short> arrShort, char* bytes);
71 static char* arrIntToByteArray(vector<int> arrInt, char* bytes);
72 static char* arrLongToByteArray(vector<int64_t> arrInt, char* bytes);
73 static char* arrFloatToByteArray(vector<float> arrFloat, char* bytes);
74 static char* arrDoubleToByteArray(vector<double> arrDouble, char* bytes);
75 static char* arrCharToByteArray(vector<char> arrChar, char* bytes);
76 static char* arrBooleanToByteArray(vector<bool> arrBoolean, char* bytes);
77 static char* arrStringToByteArray(vector<string> arrString, char* bytes);
80 static vector<short>* byteArrayToShortArray(vector<short>* result, char* bytes, int len);
81 static vector<int>* byteArrayToIntArray(vector<int>* result, char* bytes, int len);
82 static vector<int64_t>* byteArrayToLongArray(vector<int64_t>* result, char* bytes, int len);
83 static vector<float>* byteArrayToFloatArray(vector<float>* result, char* bytes, int len);
84 static vector<double>* byteArrayToDoubleArray(vector<double>* result, char* bytes, int len);
85 static vector<char>* byteArrayToCharArray(vector<char>* result, char* bytes, int len);
86 static vector<bool>* byteArrayToBooleanArray(vector<bool>* result, char* bytes, int len);
87 static vector<string>* byteArrayToStringArray(vector<string>* result, char* bytes, int len);
89 // Aggregator functions
90 static char* getArrayObjectBytes(char* retObjBytes, void* obj, const char* type);
91 static void* getArrayParamObject(void* retObj, const char* type, char* paramBytes, int len);
94 const static int OBJECT_ID_LEN = 4; // 4 bytes = 32 bits
95 const static int METHOD_ID_LEN = 4; // 4 bytes = 32 bits
96 const static int PARAM_LEN = 4; // 4 bytes = 32 bits (4-byte field that stores the length of the param)
97 const static int CHAR_LEN = 2; // 2 bytes (we follow Java convention)
98 const static int BOOL_LEN = 1; // 1 byte
101 map<string,string> mapPrimitives;
102 map<string,int> mapPrimitiveSizes;
103 map<string,string> mapNonPrimitives;
108 IoTRMIUtil::IoTRMIUtil() {
110 // Prepare vectors for inputs
111 std::vector<string> primJava (IoTRMITypes::primitivesJava,
112 IoTRMITypes::primitivesJava + sizeof(IoTRMITypes::primitivesJava)/sizeof(string));
113 std::vector<string> primCplus (IoTRMITypes::primitivesCplus,
114 IoTRMITypes::primitivesCplus + sizeof(IoTRMITypes::primitivesCplus)/sizeof(string));
115 std::vector<int> primSizes (IoTRMITypes::primitivesSizes,
116 IoTRMITypes::primitivesSizes + sizeof(IoTRMITypes::primitivesSizes)/sizeof(int));
117 std::vector<string> nonPrimJava (IoTRMITypes::nonPrimitivesJava,
118 IoTRMITypes::nonPrimitivesJava + sizeof(IoTRMITypes::nonPrimitivesJava)/sizeof(string));
119 std::vector<string> nonPrimCplus (IoTRMITypes::nonPrimitivesCplus,
120 IoTRMITypes::nonPrimitivesCplus + sizeof(IoTRMITypes::nonPrimitivesCplus)/sizeof(string));
124 IoTRMITypes::arraysToMap(mapPrimitives, primJava, primCplus);
125 IoTRMITypes::arraysToMap(mapPrimitiveSizes, primJava, primSizes);
126 IoTRMITypes::arraysToMap(mapNonPrimitives, nonPrimJava, nonPrimCplus);
132 void IoTRMIUtil::printBytes(char* bytes, const int len, const bool hex) {
135 for (int i = 0; i < len; i++) {
136 if (hex) // print in hexadecimal
137 printf("%x", bytes[i]);
139 printf("%d", bytes[i]);
147 // Return hashCode value
148 // This mimics the method Object.hashCode() in Java
149 int IoTRMIUtil::hashCode(string str)
152 int len = str.length();
157 for (int i = 0; i < len; i++) {
159 hash = (31*hash) + (int) c;
166 char* IoTRMIUtil::getHashCodeBytes(string methodSign, char* bytes) {
168 int hash = hashCode(methodSign);
169 return intToByteArray(hash, bytes);
173 int IoTRMIUtil::getTypeSize(string type) {
175 // Handle the types and find the sizes
176 if (mapPrimitiveSizes.find(type) != mapPrimitiveSizes.end())
177 return mapPrimitiveSizes.find(type)->second;
179 return -1; // Size is unknown
183 // Get variable type size, e.g. strings, arrays, etc.
184 int IoTRMIUtil::getVarTypeSize(string type, void* paramObj) {
187 if (type.compare("string") == 0) {
188 // Get the length of the string through void* casting to string*
189 paramLen = (*(string*)paramObj).length();
190 } else if (type.compare("string[]") == 0) {
191 paramLen = IoTRMIUtil::getByteStringLength(*(vector<string>*) paramObj);
192 } else if (type.compare("byte[]") == 0) {
193 int dataSize = getTypeSize("byte");
194 paramLen = (*(vector<char>*) paramObj).size() * dataSize;
195 } else if (type.compare("short[]") == 0) {
196 int dataSize = getTypeSize("short");
197 paramLen = (*(vector<short>*) paramObj).size() * dataSize;
198 } else if (type.compare("int[]") == 0) {
199 int dataSize = getTypeSize("int");
200 paramLen = (*(vector<int>*) paramObj).size() * dataSize;
201 } else if (type.compare("long[]") == 0) {
202 int dataSize = getTypeSize("long");
203 paramLen = (*(vector<int64_t>*) paramObj).size() * dataSize;
204 } else if (type.compare("float[]") == 0) {
205 int dataSize = getTypeSize("float");
206 paramLen = (*(vector<float>*) paramObj).size() * dataSize;
207 } else if (type.compare("double[]") == 0) {
208 int dataSize = getTypeSize("double");
209 paramLen = (*(vector<double>*) paramObj).size() * dataSize;
210 } else if (type.compare("bool[]") == 0) {
211 int dataSize = getTypeSize("bool");
212 paramLen = (*(vector<bool>*) paramObj).size() * dataSize;
213 } else if (type.compare("char[]") == 0) {
214 int dataSize = getTypeSize("char");
215 paramLen = (*(vector<char>*) paramObj).size() * dataSize;
217 string error = "IoTRMICall: Unrecognizable type: " + type;
225 int IoTRMIUtil::getArrStringLength(vector<string> arrString) {
228 for (string& str : arrString) {
229 len = len + str.length();
235 int IoTRMIUtil::getByteStringLength(vector<string> arrString) {
237 int len = PARAM_LEN + (PARAM_LEN*arrString.size()) + getArrStringLength(arrString);
242 // ****************************
243 // Parameters Translation
244 // ****************************
246 // Getting parameter object based on received byte array
247 void* IoTRMIUtil::getParamObject(void* retObj, const char* type, char* paramBytes, int len) {
249 if (strcmp(type, "b") == 0 ||
250 strcmp(type, "byte") == 0) {
251 retObj = (void*) ¶mBytes[0];
252 } else if ( strcmp(type, "s") == 0 ||
253 strcmp(type, "short") == 0) {
254 retObj = (void*) byteArrayToShort((short*) retObj, paramBytes);
255 } else if ( strcmp(type, "i") == 0 ||
256 strcmp(type, "int") == 0) {
257 retObj = (void*) byteArrayToInt((int*) retObj, paramBytes);
258 } else if ( strcmp(type, "l") == 0 ||
259 strcmp(type, "long") == 0) {
260 retObj = (void*) byteArrayToLong((int64_t*) retObj, paramBytes);
261 } else if ( strcmp(type, "f") == 0 ||
262 strcmp(type, "float") == 0) {
263 retObj = (void*) byteArrayToFloat((float*) retObj, paramBytes);
264 } else if ( strcmp(type, "d") == 0 ||
265 strcmp(type, "double") == 0) {
266 retObj = (void*) byteArrayToDouble((double*) retObj, paramBytes);
267 } else if ( strcmp(type, "b") == 0 ||
268 strcmp(type, "bool") == 0) {
269 retObj = (void*) byteArrayToBoolean((bool*) retObj, paramBytes);
270 } else if ( strcmp(type, "c") == 0 ||
271 strcmp(type, "char") == 0) {
272 retObj = (void*) byteArrayToChar((char*) retObj, paramBytes);
273 } else if ( strcmp(type, "Ss") == 0 ||
274 strcmp(type, "string") == 0) {
275 retObj = (void*) byteArrayToString((string*) retObj, paramBytes, len);
276 } else if ( string(type).find("[]") != string::npos) {
277 // This is an array type, i.e. vector
278 retObj = getArrayParamObject(retObj, type, paramBytes, len);
280 string error = "IoTRMIUtil: Unrecognizable type: " + string(type);
288 // Get array of objects from byte array - overload getParamObject function
289 void* IoTRMIUtil::getArrayParamObject(void* retObj, const char* type, char* paramBytes, int len) {
291 if (strcmp(type, "byte[]") == 0) {
292 retObj = (vector<char>*) paramBytes;
293 } else if (strcmp(type, "short[]") == 0) {
294 retObj = byteArrayToShortArray((vector<short>*) retObj, paramBytes, len);
295 } else if (strcmp(type, "int[]") == 0) {
296 retObj = byteArrayToIntArray((vector<int>*) retObj, paramBytes, len);
297 } else if (strcmp(type, "long[]") == 0) {
298 retObj = byteArrayToLongArray((vector<int64_t>*) retObj, paramBytes, len);
299 } else if (strcmp(type, "float[]") == 0) {
300 retObj = byteArrayToFloatArray((vector<float>*) retObj, paramBytes, len);
301 } else if (strcmp(type, "double[]") == 0) {
302 retObj = byteArrayToDoubleArray((vector<double>*) retObj, paramBytes, len);
303 } else if (strcmp(type, "bool[]") == 0) {
304 retObj = byteArrayToBooleanArray((vector<bool>*) retObj, paramBytes, len);
305 } else if (strcmp(type, "char[]") == 0) {
306 retObj = byteArrayToCharArray((vector<char>*) retObj, paramBytes, len);
307 } else if (strcmp(type, "string[]") == 0) {
308 retObj = byteArrayToStringArray((vector<string>*) retObj, paramBytes, len);
310 string error = "IoTRMIUtil: Unrecognizable type: " + string(type);
318 // Getting byte array based on parameter and its type
319 char* IoTRMIUtil::getObjectBytes(char* retObjBytes, void* obj, const char* type) {
321 if (strcmp(type, "b") == 0 ||
322 strcmp(type, "byte") == 0) {
323 retObjBytes = (char*) obj;
324 } else if ( strcmp(type, "s") == 0 ||
325 strcmp(type, "short") == 0) {
326 retObjBytes = shortToByteArray(*((short*) obj), retObjBytes);
327 } else if ( strcmp(type, "i") == 0 ||
328 strcmp(type, "int") == 0) {
329 retObjBytes = intToByteArray(*((int*) obj), retObjBytes);
330 } else if ( strcmp(type, "l") == 0 ||
331 strcmp(type, "long") == 0) {
332 retObjBytes = longToByteArray(*((int64_t*) obj), retObjBytes);
333 } else if ( strcmp(type, "f") == 0 ||
334 strcmp(type, "float") == 0) {
335 retObjBytes = floatToByteArray(*((float*) obj), retObjBytes);
336 } else if ( strcmp(type, "d") == 0 ||
337 strcmp(type, "double") == 0) {
338 retObjBytes = doubleToByteArray(*((double*) obj), retObjBytes);
339 } else if ( strcmp(type, "b") == 0 ||
340 strcmp(type, "bool") == 0) {
341 retObjBytes = booleanToByteArray(*((bool*) obj), retObjBytes);
342 } else if ( strcmp(type, "c") == 0 ||
343 strcmp(type, "char") == 0) {
344 retObjBytes = charToByteArray(*((char*) obj), retObjBytes);
345 } else if ( strcmp(type, "Ss") == 0 ||
346 strcmp(type, "string") == 0) {
347 retObjBytes = stringToByteArray(*((string*) obj), retObjBytes);
348 } else if ( string(type).find("[]") != string::npos) {
349 // This is an array type, i.e. vector
350 retObjBytes = getArrayObjectBytes(retObjBytes, obj, type);
352 string error = "IoTRMIUtil: Unrecognizable type: " + string(type);
360 // Getting byte array for arrays of primitives
361 char* IoTRMIUtil::getArrayObjectBytes(char* retObjBytes, void* obj, const char* type) {
363 if (strcmp(type, "byte[]") == 0) {
364 retObjBytes = (char*) obj;
365 } else if (strcmp(type, "short[]") == 0) {
366 retObjBytes = arrShortToByteArray(*((vector<short>*) obj), retObjBytes);
367 } else if (strcmp(type, "int[]") == 0) {
368 retObjBytes = arrIntToByteArray(*((vector<int>*) obj), retObjBytes);
369 } else if (strcmp(type, "long[]") == 0) {
370 retObjBytes = arrLongToByteArray(*((vector<int64_t>*) obj), retObjBytes);
371 } else if (strcmp(type, "float[]") == 0) {
372 retObjBytes = arrFloatToByteArray(*((vector<float>*) obj), retObjBytes);
373 } else if (strcmp(type, "double[]") == 0) {
374 retObjBytes = arrDoubleToByteArray(*((vector<double>*) obj), retObjBytes);
375 } else if (strcmp(type, "bool[]") == 0) {
376 retObjBytes = arrBooleanToByteArray(*((vector<bool>*) obj), retObjBytes);
377 } else if (strcmp(type, "char[]") == 0) {
378 retObjBytes = arrCharToByteArray(*((vector<char>*) obj), retObjBytes);
379 } else if (strcmp(type, "string[]") == 0) {
380 retObjBytes = arrStringToByteArray(*((vector<string>*) obj), retObjBytes);
382 string error = "IoTRMIUtil: Unrecognizable type: " + string(type);
391 // Array handlers - we use vector data type and not traditional arrays
393 char* IoTRMIUtil::arrShortToByteArray(vector<short> arrShort, char* bytes) {
396 for (short& sht : arrShort) {
397 char tmpBytes[sizeof(short)];
398 shortToByteArray(sht, tmpBytes);
399 memcpy(bytes + pos, tmpBytes, sizeof(short));
400 pos = pos + sizeof(short);
407 char* IoTRMIUtil::arrIntToByteArray(vector<int> arrInt, char* bytes) {
410 for (int& in : arrInt) {
411 char tmpBytes[sizeof(int)];
412 intToByteArray(in, tmpBytes);
413 memcpy(bytes + pos, tmpBytes, sizeof(int));
414 pos = pos + sizeof(int);
421 char* IoTRMIUtil::arrLongToByteArray(vector<int64_t> arrLong, char* bytes) {
424 for (int64_t& lng : arrLong) {
425 char tmpBytes[sizeof(int64_t)];
426 longToByteArray(lng, tmpBytes);
427 memcpy(bytes + pos, tmpBytes, sizeof(int64_t));
428 pos = pos + sizeof(int64_t);
435 char* IoTRMIUtil::arrFloatToByteArray(vector<float> arrFloat, char* bytes) {
438 for (float& flt : arrFloat) {
439 char tmpBytes[sizeof(float)];
440 floatToByteArray(flt, tmpBytes);
441 memcpy(bytes + pos, tmpBytes, sizeof(float));
442 pos = pos + sizeof(float);
449 char* IoTRMIUtil::arrDoubleToByteArray(vector<double> arrDouble, char* bytes) {
452 for (double& dbl : arrDouble) {
453 char tmpBytes[sizeof(double)];
454 doubleToByteArray(dbl, tmpBytes);
455 memcpy(bytes + pos, tmpBytes, sizeof(double));
456 pos = pos + sizeof(double);
463 char* IoTRMIUtil::arrCharToByteArray(vector<char> arrChar, char* bytes) {
466 for (char& chr : arrChar) {
467 char tmpBytes[CHAR_LEN];
468 charToByteArray(chr, tmpBytes);
469 memcpy(bytes + pos, tmpBytes, CHAR_LEN);
470 pos = pos + CHAR_LEN;
477 char* IoTRMIUtil::arrBooleanToByteArray(vector<bool> arrBoolean, char* bytes) {
480 for (bool bl : arrBoolean) {
481 char tmpBytes[BOOL_LEN];
482 booleanToByteArray(bl, tmpBytes);
483 memcpy(bytes + pos, tmpBytes, BOOL_LEN);
484 pos = pos + BOOL_LEN;
491 char* IoTRMIUtil::arrStringToByteArray(vector<string> arrString, char* bytes) {
494 char strArrLenBytes[PARAM_LEN];
495 intToByteArray(arrString.size(), strArrLenBytes);
496 memcpy(bytes, strArrLenBytes, PARAM_LEN);
497 pos = pos + PARAM_LEN;
498 for (string& str : arrString) {
500 // Copy string length
501 int strLen = str.length();
502 char strLenBytes[PARAM_LEN];
503 intToByteArray(strLen, strLenBytes);
504 memcpy(bytes + pos, strLenBytes, PARAM_LEN);
505 pos = pos + PARAM_LEN;
507 char strBytes[strLen];
508 stringToByteArray(str, strBytes);
509 memcpy(bytes + pos, strBytes, strLen);
518 vector<short>* IoTRMIUtil::byteArrayToShortArray(vector<short>* result, char* bytes, int len) {
520 // Single element bytes
521 char elmt[sizeof(short)];
523 int arrLen = len/sizeof(short);
524 for(int i = 0; i < arrLen; i++) {
525 int offset = i * sizeof(short);
526 memcpy(elmt, bytes + offset, sizeof(short));
528 byteArrayToShort(&res, elmt);
529 result->push_back(res);
536 vector<int>* IoTRMIUtil::byteArrayToIntArray(vector<int>* result, char* bytes, int len) {
538 // Single element bytes
539 char elmt[sizeof(int)];
541 int arrLen = len/sizeof(int);
542 for(int i = 0; i < arrLen; i++) {
543 int offset = i * sizeof(int);
544 memcpy(elmt, bytes + offset, sizeof(int));
546 byteArrayToInt(&res, elmt);
547 result->push_back(res);
554 vector<int64_t>* IoTRMIUtil::byteArrayToLongArray(vector<int64_t>* result, char* bytes, int len) {
556 // Single element bytes
557 char elmt[sizeof(int64_t)];
559 int arrLen = len/sizeof(int64_t);
560 for(int i = 0; i < arrLen; i++) {
561 int offset = i * sizeof(int64_t);
562 memcpy(elmt, bytes + offset, sizeof(int64_t));
564 byteArrayToLong(&res, elmt);
565 result->push_back(res);
572 vector<float>* IoTRMIUtil::byteArrayToFloatArray(vector<float>* result, char* bytes, int len) {
574 // Single element bytes
575 char elmt[sizeof(float)];
577 int arrLen = len/sizeof(float);
578 for(int i = 0; i < arrLen; i++) {
579 int offset = i * sizeof(float);
580 memcpy(elmt, bytes + offset, sizeof(float));
582 byteArrayToFloat(&res, elmt);
583 result->push_back(res);
590 vector<double>* IoTRMIUtil::byteArrayToDoubleArray(vector<double>* result, char* bytes, int len) {
592 // Single element bytes
593 char elmt[sizeof(double)];
595 int arrLen = len/sizeof(double);
596 for(int i = 0; i < arrLen; i++) {
597 int offset = i * sizeof(double);
598 memcpy(elmt, bytes + offset, sizeof(double));
600 byteArrayToDouble(&res, elmt);
601 result->push_back(res);
608 vector<char>* IoTRMIUtil::byteArrayToCharArray(vector<char>* result, char* bytes, int len) {
610 // Single element bytes
613 int arrLen = len/CHAR_LEN;
614 for(int i = 0; i < arrLen; i++) {
615 int offset = i * CHAR_LEN;
616 memcpy(elmt, bytes + offset, CHAR_LEN);
618 byteArrayToChar(&res, elmt);
619 result->push_back(res);
626 vector<bool>* IoTRMIUtil::byteArrayToBooleanArray(vector<bool>* result, char* bytes, int len) {
628 // Single element bytes
631 int arrLen = len/BOOL_LEN;
632 for(int i = 0; i < arrLen; i++) {
633 int offset = i * BOOL_LEN;
634 memcpy(elmt, bytes + offset, BOOL_LEN);
636 byteArrayToBoolean(&res, elmt);
637 result->push_back(res);
644 vector<string>* IoTRMIUtil::byteArrayToStringArray(vector<string>* result, char* bytes, int len) {
646 // Format of bytes: | array length | length #1 | string #1 | length #2 | string #2 | ...
647 // Get string array length
649 char strArrLenBytes[PARAM_LEN];
650 memcpy(strArrLenBytes, bytes, PARAM_LEN);
652 byteArrayToInt(&strArrLen, strArrLenBytes);
653 pos = pos + PARAM_LEN;
654 // Extract array of strings
655 for(int i = 0; i < strArrLen; i++) {
657 // Extract string length
658 char strLenBytes[PARAM_LEN];
659 memcpy(strLenBytes, bytes + pos, PARAM_LEN);
661 byteArrayToInt(&strLen, strLenBytes);
662 pos = pos + PARAM_LEN;
664 char strBytes[strLen];
665 memcpy(strBytes, bytes + pos, strLen);
668 // Note: Somehow we need to instantiate the string
669 // with the length here although we are passing
670 // an array of bytes with an exact length
671 byteArrayToString(&tmpStr, strBytes, strLen);
672 result->push_back(tmpStr);
680 // Primitives to byte array
681 char* IoTRMIUtil::shortToByteArray(short s, char* bytes) {
683 short sInvert = htobe16(s);
684 //short sInvert = htons(s);
685 memcpy(bytes, &sInvert, sizeof(short));
691 char* IoTRMIUtil::intToByteArray(int i, char* bytes) {
693 int iInvert = htobe32(i);
694 //int iInvert = htonl(i);
695 memcpy(bytes, &iInvert, sizeof(int));
701 char* IoTRMIUtil::longToByteArray(int64_t l, char* bytes) {
703 int64_t lInvert = htobe64(l);
704 memcpy(bytes, &lInvert, sizeof(int64_t));
710 char* IoTRMIUtil::floatToByteArray(float f, char* bytes) {
712 // Copy to int to allow the usage of htobeXX() functions
714 memcpy(&i, &f, sizeof(float));
715 int iInvert = htobe32(i);
716 memcpy(bytes, &iInvert, sizeof(int));
722 char* IoTRMIUtil::doubleToByteArray(double d, char* bytes) {
724 // Copy to int to allow the usage of htobeXX() functions
726 memcpy(&i, &d, sizeof(double));
727 int64_t iInvert = htobe64(i);
728 memcpy(bytes, &iInvert, sizeof(int64_t));
734 char* IoTRMIUtil::charToByteArray(char c, char* bytes) {
736 // We need 2 bytes to accommodate Java char type, whose size is 2
744 char* IoTRMIUtil::booleanToByteArray(bool b, char* bytes) {
746 bytes[0] = (b) ? 1 : 0;
751 char* IoTRMIUtil::stringToByteArray(string str, char* bytes) {
753 strcpy(bytes, str.c_str());
759 // Byte array to primitives
760 short* IoTRMIUtil::byteArrayToShort(short* result, char* bytes) {
763 memcpy(&s, bytes, sizeof(short));
764 //short result = be16toh(s);
765 *result = be16toh(s);
771 int* IoTRMIUtil::byteArrayToInt(int* result, char* bytes) {
774 memcpy(&i, bytes, sizeof(int));
775 *result = be32toh(i);
781 int64_t* IoTRMIUtil::byteArrayToLong(int64_t* result, char* bytes) {
784 memcpy(&l, bytes, sizeof(int64_t));
785 *result = be64toh(l);
791 float* IoTRMIUtil::byteArrayToFloat(float* result, char* bytes) {
793 // Copy to int to allow the usage of beXXtoh() functions
795 memcpy(&i, bytes, sizeof(int));
796 int iInvert = be32toh(i);
797 memcpy(result, &iInvert, sizeof(float));
803 double* IoTRMIUtil::byteArrayToDouble(double* result, char* bytes) {
805 // Copy to int to allow the usage of beXXtoh() functions
807 memcpy(&i, bytes, sizeof(int64_t));
808 int64_t iInvert = be64toh(i);
809 memcpy(result, &iInvert, sizeof(double));
815 char* IoTRMIUtil::byteArrayToChar(char* result, char* bytes) {
822 bool* IoTRMIUtil::byteArrayToBoolean(bool* result, char* bytes) {
824 *result = (bytes[0]) ? true : false;
829 string* IoTRMIUtil::byteArrayToString(string* result, char* bytes) {
831 *result = string(bytes);
836 string* IoTRMIUtil::byteArrayToString(string* result, char* bytes, int strLen) {
838 *result = string(bytes, strLen);