3 #ifndef GEODE_DATAINPUT_H_
4 #define GEODE_DATAINPUT_H_
23 #include "geode_globals.hpp"
35 #if GF_DEBUG_ASSERTS == 1
36 #define DINP_THROWONERROR_DEFAULT true
38 #define DINP_THROWONERROR_DEFAULT false
41 #define checkBufferSize(x) _checkBufferSize(x, __LINE__)
47 extern int gf_sprintf(
char* buffer,
const char* fmt, ...);
63 inline void read(uint8_t* value) {
73 inline void read(int8_t* value) {
85 *value = (*m_buf == 1 ? true :
false);
101 checkBufferSize(len);
102 std::memcpy(buffer, m_buf, len);
119 checkBufferSize(len);
120 std::memcpy(buffer, m_buf, len);
137 readArrayLen(&length);
139 uint8_t* buffer = NULL;
141 checkBufferSize(length);
142 GF_NEW(buffer, uint8_t[length]);
143 std::memcpy(buffer, m_buf, length);
161 readArrayLen(&length);
163 int8_t* buffer = NULL;
165 checkBufferSize(length);
166 GF_NEW(buffer, int8_t[length]);
167 std::memcpy(buffer, m_buf, length);
181 uint16_t tmp = *(m_buf++);
182 tmp = static_cast<uint16_t>((tmp << 8) | *(m_buf++));
194 uint32_t tmp = *(m_buf++);
195 tmp = (tmp << 8) | *(m_buf++);
196 tmp = (tmp << 8) | *(m_buf++);
197 tmp = (tmp << 8) | *(m_buf++);
210 if (
sizeof(
long) == 8) {
212 tmp = (tmp << 8) | *(m_buf++);
213 tmp = (tmp << 8) | *(m_buf++);
214 tmp = (tmp << 8) | *(m_buf++);
215 tmp = (tmp << 8) | *(m_buf++);
216 tmp = (tmp << 8) | *(m_buf++);
217 tmp = (tmp << 8) | *(m_buf++);
218 tmp = (tmp << 8) | *(m_buf++);
220 uint32_t hword = *(m_buf++);
221 hword = (hword << 8) | *(m_buf++);
222 hword = (hword << 8) | *(m_buf++);
223 hword = (hword << 8) | *(m_buf++);
227 hword = (hword << 8) | *(m_buf++);
228 hword = (hword << 8) | *(m_buf++);
229 hword = (hword << 8) | *(m_buf++);
230 tmp = (tmp << 32) | hword;
243 readInt(reinterpret_cast<uint16_t*>(value));
254 readInt(reinterpret_cast<uint32_t*>(value));
265 readInt(reinterpret_cast<uint64_t*>(value));
282 int32_t result = code;
288 }
else if (code == 0xFD) {
293 throw IllegalStateException(
"unexpected array length code");
312 result |= static_cast<int64_t>(b & 0x7F) << shift;
313 if ((b & 0x80) == 0) {
319 throw IllegalStateException(
"Malformed variable length integer");
329 union float_uint32_t {
345 union double_uint64_t {
382 inline void readASCII(
char** value, uint16_t* len = NULL) {
385 checkBufferSize(length);
390 GF_NEW(str,
char[length + 1]);
392 readBytesOnly(reinterpret_cast<int8_t*>(str), length);
417 GF_NEW(str,
char[length + 1]);
419 readBytesOnly(reinterpret_cast<int8_t*>(str), length);
439 inline void readUTF(
char** value, uint16_t* len = NULL) {
442 checkBufferSize(length);
443 uint16_t decodedLen =
444 static_cast<uint16_t>(getDecodedLength(m_buf, length));
449 GF_NEW(str,
char[decodedLen + 1]);
451 for (uint16_t i = 0; i < decodedLen; i++) {
468 GF_NEW(str,
wchar_t[decodedLen + 1]);
470 for (uint16_t i = 0; i < decodedLen; i++) {
497 GF_NEW(str,
char[length + 1]);
499 for (uint32_t i = 0; i < length; i++) {
525 inline void readUTF(
wchar_t** value, uint16_t* len = NULL) {
528 checkBufferSize(length);
529 uint16_t decodedLen =
530 static_cast<uint16_t>(getDecodedLength(m_buf, length));
535 GF_NEW(str,
wchar_t[decodedLen + 1]);
537 for (uint16_t i = 0; i < decodedLen; i++) {
564 GF_NEW(str,
wchar_t[length + 1]);
566 for (uint32_t i = 0; i < length; i++) {
571 *str = ((static_cast<uint16_t>(hibyte)) << 8) |
572 static_cast<uint16_t>(lobyte);
599 bool throwOnError = DINP_THROWONERROR_DEFAULT) {
601 readObjectInternal(sPtr);
603 ptr = dynCast<SharedPtr<PTR> >(sPtr);
605 ptr = staticCast<SharedPtr<PTR> >(sPtr);
609 inline bool readNativeBool() {
618 inline int32_t readNativeInt32() {
627 inline bool readNativeString(CacheableStringPtr& csPtr) {
630 int64_t compId = typeId;
631 if (compId == GeodeTypeIds::NullObj) {
633 }
else if (compId == GeodeTypeIds::CacheableNullString) {
634 csPtr = CacheableStringPtr(dynamic_cast<CacheableString*>(
637 apache::geode::client::GeodeTypeIds::CacheableASCIIString) {
638 csPtr = CacheableStringPtr(dynamic_cast<CacheableString*>(
640 csPtr.ptr()->fromData(*
this);
642 apache::geode::client::GeodeTypeIds::CacheableASCIIStringHuge) {
643 csPtr = CacheableStringPtr(dynamic_cast<CacheableString*>(
645 csPtr.ptr()->fromData(*
this);
646 }
else if (compId == apache::geode::client::GeodeTypeIds::CacheableString) {
647 csPtr = CacheableStringPtr(dynamic_cast<CacheableString*>(
649 csPtr.ptr()->fromData(*
this);
651 apache::geode::client::GeodeTypeIds::CacheableStringHuge) {
652 csPtr = CacheableStringPtr(dynamic_cast<CacheableString*>(
654 csPtr.ptr()->fromData(*
this);
656 LOGDEBUG(
"In readNativeString something is wrong while expecting string");
664 inline void readDirectObject(SerializablePtr& ptr, int8_t typeId = -1) {
665 readObjectInternal(ptr, typeId);
674 inline void readObject(
wchar_t* value) {
677 *value = static_cast<wchar_t>(temp);
680 inline void readObject(
bool* value) { readBoolean(value); }
682 inline void readObject(int8_t* value) { read(value); }
684 inline void readObject(int16_t* value) { readInt(value); }
686 inline void readObject(int32_t* value) { readInt(value); }
688 inline void readObject(int64_t* value) { readInt(value); }
690 inline void readObject(
float* value) { readFloat(value); }
692 inline void readObject(
double* value) { readDouble(value); }
694 inline void readCharArray(
char** value, int32_t& length) {
696 readArrayLen(&arrayLen);
698 char* objArray = NULL;
700 objArray =
new char[arrayLen];
702 for (i = 0; i < arrayLen; i++) {
711 inline void readWideCharArray(
wchar_t** value, int32_t& length) {
712 readObject(value, length);
715 inline void readBooleanArray(
bool** value, int32_t& length) {
716 readObject(value, length);
719 inline void readByteArray(int8_t** value, int32_t& length) {
720 readObject(value, length);
723 inline void readShortArray(int16_t** value, int32_t& length) {
724 readObject(value, length);
727 inline void readIntArray(int32_t** value, int32_t& length) {
728 readObject(value, length);
731 inline void readLongArray(int64_t** value, int32_t& length) {
732 readObject(value, length);
735 inline void readFloatArray(
float** value, int32_t& length) {
736 readObject(value, length);
739 inline void readDoubleArray(
double** value, int32_t& length) {
740 readObject(value, length);
743 inline void readString(
char** value) {
748 if (typeId == GeodeTypeIds::CacheableNullString) {
759 if (typeId == static_cast<int8_t>(GeodeTypeIds::CacheableASCIIString) ||
760 typeId == static_cast<int8_t>(GeodeTypeIds::CacheableString)) {
764 }
else if (typeId == static_cast<int8_t>(
765 GeodeTypeIds::CacheableASCIIStringHuge) ||
767 static_cast<int8_t>(GeodeTypeIds::CacheableStringHuge)) {
769 readASCIIHuge(value);
771 throw IllegalArgumentException(
772 "DI readString error:: String type not supported ");
776 inline void readWideString(
wchar_t** value) {
781 if (typeId == GeodeTypeIds::CacheableNullString) {
786 if (typeId == static_cast<int8_t>(GeodeTypeIds::CacheableASCIIString) ||
787 typeId == static_cast<int8_t>(GeodeTypeIds::CacheableString)) {
789 }
else if (typeId == static_cast<int8_t>(
790 GeodeTypeIds::CacheableASCIIStringHuge) ||
792 static_cast<int8_t>(GeodeTypeIds::CacheableStringHuge)) {
795 throw IllegalArgumentException(
796 "DI readWideString error:: WideString type provided is not "
801 inline void readStringArray(
char*** strArray, int32_t& length) {
803 readArrayLen(&arrLen);
810 GF_NEW(tmpArray,
char * [arrLen]);
811 for (
int i = 0; i < arrLen; i++) {
812 readString(&tmpArray[i]);
814 *strArray = tmpArray;
818 inline void readWideStringArray(
wchar_t*** strArray, int32_t& length) {
820 readArrayLen(&arrLen);
827 GF_NEW(tmpArray,
wchar_t * [arrLen]);
828 for (
int i = 0; i < arrLen; i++) {
829 readWideString(&tmpArray[i]);
831 *strArray = tmpArray;
835 inline void readArrayOfByteArrays(int8_t*** arrayofBytearr,
836 int32_t& arrayLength,
837 int32_t** elementLength) {
839 readArrayLen(&arrLen);
840 arrayLength = arrLen;
843 *arrayofBytearr = NULL;
847 int32_t* tmpLengtharr;
848 GF_NEW(tmpArray, int8_t * [arrLen]);
849 GF_NEW(tmpLengtharr, int32_t[arrLen]);
850 for (
int i = 0; i < arrLen; i++) {
851 readBytes(&tmpArray[i], &tmpLengtharr[i]);
853 *arrayofBytearr = tmpArray;
854 *elementLength = tmpLengtharr;
869 const uint8_t* end = value + length;
870 int32_t decodedLen = 0;
871 while (value < end) {
873 int32_t b = *value++ & 0xff;
890 if (value > end) decodedLen--;
913 return static_cast<int32_t>(m_buf - m_bufHead);
918 return (m_bufLength - getBytesRead());
928 inline void reset() { m_buf = m_bufHead; }
930 inline void setBuffer() {
931 m_buf = currentBufferPosition();
932 m_bufLength = getBytesRemaining();
935 inline void resetPdx(int32_t offset) { m_buf = m_bufHead + offset; }
937 inline int32_t getPdxBytes()
const {
return m_bufLength; }
939 static uint8_t* getBufferCopy(
const uint8_t* from, uint32_t length) {
941 GF_NEW(result, uint8_t[length]);
942 std::memcpy(result, from, length);
947 inline void reset(int32_t offset) { m_buf = m_bufHead + offset; }
949 uint8_t* getBufferCopyFrom(
const uint8_t* from, uint32_t length) {
951 GF_NEW(result, uint8_t[length]);
952 std::memcpy(result, from, length);
960 const char* getPoolName() {
return m_poolName; }
965 void setPoolName(
const char* poolName) { m_poolName = poolName; }
968 const uint8_t* m_buf;
969 const uint8_t* m_bufHead;
971 const char* m_poolName;
973 void readObjectInternal(SerializablePtr& ptr, int8_t typeId = -1);
975 template <
typename mType>
976 void readObject(mType** value, int32_t& length) {
978 readArrayLen(&arrayLen);
982 objArray =
new mType[arrayLen];
984 for (i = 0; i < arrayLen; i++) {
993 inline void readPdxChar(
char* value) {
996 *value = static_cast<char>(val);
999 inline void _checkBufferSize(int32_t size, int32_t line) {
1000 if ((m_bufLength - (m_buf - m_bufHead)) < size) {
1003 "DataInput: attempt to read beyond buffer at line %d: "
1004 "available buffer size %d, attempted read of size %d ",
1005 line, m_bufLength - (m_buf - m_bufHead), size);
1006 throw OutOfRangeException(exMsg);
1010 inline void decodeChar(
char* str) {
1011 uint8_t bt = *(m_buf++);
1016 static_cast<char>(((bt & 0x0f) << 12) | (((*m_buf++) & 0x3f) << 6));
1017 *str |= static_cast<char>((*m_buf++) & 0x3f);
1020 *str = static_cast<char>(((bt & 0x1f) << 6) | ((*m_buf++) & 0x3f));
1028 inline void decodeChar(
wchar_t* str) {
1030 int32_t b = *m_buf++ & 0xff;
1038 int32_t y = b & 0x1f;
1041 int32_t x = *m_buf++ & 0x3f;
1043 *str = (y << 6 | x);
1052 int32_t z = b & 0x0f;
1055 int32_t y = *m_buf++ & 0x3f;
1058 int32_t x = *m_buf++ & 0x3f;
1060 int32_t asint = (z << 12 | y << 6 | x);
1076 DataInput(
const DataInput&);
1077 DataInput& operator=(
const DataInput&);
1083 #endif // GEODE_DATAINPUT_H_