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() {
630 int64_t compId = typeId;
631 if (compId == GeodeTypeIds::NullObj) {
633 }
else if (compId == GeodeTypeIds::CacheableNullString) {
637 apache::geode::client::GeodeTypeIds::CacheableASCIIString) {
642 apache::geode::client::GeodeTypeIds::CacheableASCIIStringHuge) {
646 }
else if (compId == apache::geode::client::GeodeTypeIds::CacheableString) {
651 apache::geode::client::GeodeTypeIds::CacheableStringHuge) {
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;
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);
1083 #endif // GEODE_DATAINPUT_H_ void readInt(uint64_t *value)
Read a 64-bit unsigned integer from the DataInput.
Definition: DataInput.hpp:207
~DataInput()
destructor
Definition: DataInput.hpp:902
static Serializable * createDeserializable()
creation function for strings
Each enum represents a predefined RegionAttributes in a Cache.
Definition: Assert.hpp:31
virtual Serializable * fromData(DataInput &input)
deserialize this object Throw IllegalArgumentException if the packed CacheableString is not less than...
void readArrayLen(int32_t *len)
Read a 32-bit signed integer array length value from the DataInput in a manner compatible with java s...
Definition: DataInput.hpp:276
void readObject(SerializablePtr &ptr)
Read a Serializable object from the DataInput.
Definition: DataInput.hpp:672
void readUTF(wchar_t **value, uint16_t *len=NULL)
Allocates a wide-character string buffer, and reads a java modified UTF-8 encoded string having maxim...
Definition: DataInput.hpp:525
static Serializable * createDeserializableHuge()
creation function for strings > 64K length
void readUTF(char **value, uint16_t *len=NULL)
Allocates a c string buffer, and reads a java modified UTF-8 encoded string having maximum encoded le...
Definition: DataInput.hpp:439
void readUTFNoLen(wchar_t **value, uint16_t decodedLen)
Reads a java modified UTF-8 encoded string having maximum encoded length of 64K without reading the l...
Definition: DataInput.hpp:466
#define CPPCACHE_EXPORT
Defines a Geode CPPCACHE export.
Definition: geode_base.hpp:58
int32_t getBytesRemaining() const
get the number of bytes remaining to be read in the buffer
Definition: DataInput.hpp:917
DataInput(const uint8_t *m_buffer, int32_t len)
constructor given a pre-allocated byte array with size
Definition: DataInput.hpp:895
static void freeUTFMemory(char *value)
free the C string allocated by readASCII, readASCIIHuge, readUTF, readUTFHuge methods ...
Definition: DataInput.hpp:358
void readInt(int16_t *value)
Read a 16-bit signed integer from the DataInput.
Definition: DataInput.hpp:241
void read(int8_t *value)
Read a signed byte from the DataInput.
Definition: DataInput.hpp:73
void readFloat(float *value)
Read a float from the DataInput.
Definition: DataInput.hpp:327
void readInt(uint16_t *value)
Read a 16-bit unsigned integer from the DataInput.
Definition: DataInput.hpp:179
void readInt(uint32_t *value)
Read a 32-bit unsigned integer from the DataInput.
Definition: DataInput.hpp:192
void readInt(int32_t *value)
Read a 32-bit signed integer from the DataInput.
Definition: DataInput.hpp:252
void readBytesOnly(int8_t *buffer, uint32_t len)
Read the given number of signed bytes from the DataInput.
Definition: DataInput.hpp:117
void readASCII(char **value, uint16_t *len=NULL)
Allocates a c string buffer, and reads an ASCII string having maximum length of 64K from DataInput in...
Definition: DataInput.hpp:382
Defines a reference counted shared pointer.
Definition: SharedPtr.hpp:52
static int32_t getDecodedLength(const uint8_t *value, int32_t length)
Get the length required to represent a given UTF-8 encoded string (created using DataOutput::writeUTF...
Definition: DataInput.hpp:868
int gf_sprintf(char *buffer, const char *fmt,...)
sprintf implementation.
void readASCIIHuge(char **value, uint32_t *len=NULL)
Allocates a c string buffer, and reads an ASCII string from DataInput into it.
Definition: DataInput.hpp:410
void readInt(int64_t *value)
Read a 64-bit signed integer from the DataInput.
Definition: DataInput.hpp:263
void readBoolean(bool *value)
Read a boolean value from the DataInput.
Definition: DataInput.hpp:83
static void freeUTFMemory(wchar_t *value)
free the wide-characted string allocated by readASCII, readASCIIHuge, readUTF, readUTFHuge methods ...
Definition: DataInput.hpp:365
static Serializable * createUTFDeserializable()
creation function for wide strings
void readObject(SharedPtr< PTR > &ptr, bool throwOnError=false)
Read a Serializable object from the DataInput.
Definition: DataInput.hpp:598
void readUnsignedVL(int64_t *value)
Decode a 64 bit integer as a variable length array.
Definition: DataInput.hpp:306
static Serializable * createUTFDeserializableHuge()
creation function for wide strings > 64K length in UTF8 encoding
Provide operations for reading primitive data values, byte arrays, strings, Serializable objects from...
Definition: DataInput.hpp:56
void rewindCursor(int32_t offset)
rewind the cursor by given offset
Definition: DataInput.hpp:925
void readUTFHuge(char **value, uint32_t *len=NULL)
Allocates a c string buffer, and reads a java modified UTF-8 encoded string from DataInput into it...
Definition: DataInput.hpp:490
int32_t getBytesRead() const
get the number of bytes read in the buffer
Definition: DataInput.hpp:912
void readBytes(int8_t **bytes, int32_t *len)
Read an array of signed bytes from the DataInput expecting to find the length of array in the stream ...
Definition: DataInput.hpp:159
#define GF_NEW(v, stmt)
Allocates x and throws OutOfMemoryException if it fails.
Definition: geode_base.hpp:293
void readUTFHuge(wchar_t **value, uint32_t *len=NULL)
Allocates a wide-character string buffer, and reads a java modified UTF-8 encoded string from DataInp...
Definition: DataInput.hpp:557
void read(uint8_t *value)
Read an unsigned byte from the DataInput.
Definition: DataInput.hpp:63
const uint8_t * currentBufferPosition() const
Get the pointer to current buffer position.
Definition: DataInput.hpp:909
void reset()
reset the cursor to the start of buffer
Definition: DataInput.hpp:928
void advanceCursor(int32_t offset)
advance the cursor by given offset
Definition: DataInput.hpp:922
void readDouble(double *value)
Read a double precision number from the DataInput.
Definition: DataInput.hpp:343
This namespace contains all the Geode C++ API classes, enumerations and globals.
void readBytes(uint8_t **bytes, int32_t *len)
Read an array of unsigned bytes from the DataInput expecting to find the length of array in the strea...
Definition: DataInput.hpp:135
void readBytesOnly(uint8_t *buffer, uint32_t len)
Read the given number of unsigned bytes from the DataInput.
Definition: DataInput.hpp:99