build-artifacts/win/product/include/gfcpp/DataInput.hpp

Go to the documentation of this file.
00001 #ifndef __GEMFIRE_DATAINPUT_H__
00002 #define __GEMFIRE_DATAINPUT_H__
00003 
00004 /*=========================================================================
00005  * Copyright (c) 2002-2014 Pivotal Software, Inc. All Rights Reserved.
00006  * This product is protected by U.S. and international copyright
00007  * and intellectual property laws. Pivotal products are covered by
00008  * more patents listed at http://www.pivotal.io/patents.
00009  *=========================================================================
00010  */
00011 
00012 #include "gfcpp_globals.hpp"
00013 #include "ExceptionTypes.hpp"
00014 #include <string.h>
00015 #include "gf_types.hpp"
00016 #include "Serializable.hpp"
00017 #include "CacheableString.hpp"
00018 
00024 #if GF_DEBUG_ASSERTS == 1
00025 #define DINP_THROWONERROR_DEFAULT true
00026 #else
00027 #define DINP_THROWONERROR_DEFAULT false
00028 #endif
00029 
00030 #define checkBufferSize(x) _checkBufferSize(x,__LINE__)
00031 
00032 namespace gemfire
00033 {
00034 
00035 extern int gf_sprintf(char* buffer, const char* fmt, ...);
00036 
00044 class CPPCACHE_EXPORT DataInput
00045 {
00046 public:
00047 
00053   inline void read( uint8_t* value )
00054   {
00055     checkBufferSize(1);
00056     *value = *(m_buf++);
00057   }
00058 
00064   inline void read( int8_t* value )
00065   {
00066     checkBufferSize(1);
00067     *value = *(m_buf++);
00068   }
00069 
00075   inline void readBoolean( bool* value )
00076   {
00077     checkBufferSize(1);
00078     *value = (*m_buf == 1 ? true : false);
00079     m_buf++;
00080   }
00081 
00092   inline void readBytesOnly(uint8_t* buffer, uint32_t len)
00093   {
00094     if (len > 0) {
00095       checkBufferSize(len);
00096       memcpy( buffer, m_buf, len );
00097       m_buf += len;
00098     }
00099   }
00100 
00111   inline void readBytesOnly(int8_t* buffer, uint32_t len)
00112   {
00113     if (len > 0) {
00114       checkBufferSize(len);
00115       memcpy( buffer, m_buf, len );
00116       m_buf += len;
00117     }
00118   }
00119 
00130   inline void readBytes( uint8_t** bytes, int32_t* len )
00131   {
00132     int32_t length;
00133     readArrayLen( &length );
00134     *len = length;
00135     uint8_t* buffer = NULL;
00136     if ( length > 0 ) {
00137       checkBufferSize(length);
00138       GF_NEW( buffer, uint8_t[ length ] );
00139       memcpy( buffer, m_buf, length );
00140       m_buf += length;
00141     }
00142     *bytes = buffer;
00143   }
00144 
00155   inline void readBytes( int8_t** bytes, int32_t* len )
00156   {
00157     int32_t length;
00158     readArrayLen( &length );
00159     *len = length;
00160     int8_t* buffer = NULL;
00161     if ( length > 0 ) {
00162       checkBufferSize(length);
00163       GF_NEW( buffer, int8_t[ length ] );
00164       memcpy( buffer, m_buf, length );
00165       m_buf += length;
00166     }
00167     *bytes = buffer;
00168   }
00169 
00176   inline void readInt( uint16_t* value )
00177   {
00178     checkBufferSize(2);
00179     uint16_t tmp = *(m_buf++);
00180     tmp = (uint16_t)((tmp << 8) | *(m_buf++));
00181     *value = tmp;
00182   }
00183 
00190   inline void readInt( uint32_t* value )
00191   {
00192     checkBufferSize(4);
00193     uint32_t tmp = *(m_buf++);
00194     tmp = (tmp << 8) | *(m_buf++);
00195     tmp = (tmp << 8) | *(m_buf++);
00196     tmp = (tmp << 8) | *(m_buf++);
00197     *value = tmp;
00198   }
00199 
00206   inline void readInt( uint64_t* value )
00207   {
00208     checkBufferSize(8);
00209     uint64_t tmp;
00210     if ( sizeof( long ) == 8 ) {
00211       tmp = *(m_buf++);
00212       tmp = (tmp << 8) | *(m_buf++);
00213       tmp = (tmp << 8) | *(m_buf++);
00214       tmp = (tmp << 8) | *(m_buf++);
00215       tmp = (tmp << 8) | *(m_buf++);
00216       tmp = (tmp << 8) | *(m_buf++);
00217       tmp = (tmp << 8) | *(m_buf++);
00218       tmp = (tmp << 8) | *(m_buf++);
00219     } else {
00220       uint32_t hword = *(m_buf++);
00221       hword = (hword << 8) | *(m_buf++);
00222       hword = (hword << 8) | *(m_buf++);
00223       hword = (hword << 8) | *(m_buf++);
00224 
00225       tmp = hword;
00226       hword = *(m_buf++);
00227       hword = (hword << 8) | *(m_buf++);
00228       hword = (hword << 8) | *(m_buf++);
00229       hword = (hword << 8) | *(m_buf++);
00230       tmp = (tmp << 32) | hword;
00231     }
00232     *value = tmp;
00233   }
00234 
00241   inline void readInt( int16_t* value )
00242   {
00243     checkBufferSize(2);
00244     readInt( (uint16_t*)value );
00245   }
00246 
00253   inline void readInt( int32_t* value )
00254   {
00255     checkBufferSize(4);
00256     readInt( (uint32_t*)value );
00257   }
00258 
00265   inline void readInt( int64_t* value )
00266   {
00267     checkBufferSize(8);
00268     readInt( (uint64_t*)value );
00269   }
00270 
00279   inline void readArrayLen( int32_t* len )
00280   {
00281     uint8_t code;
00282     read(&code);
00283     if (code == 0xFF) {
00284       *len = -1;
00285     } else {
00286       int32_t result = code;
00287       if (result > 252) {  // 252 is java's ((byte)-4 && 0xFF)
00288         if (code == 0xFE) {
00289           uint16_t val;
00290           readInt(&val);
00291           result = val;
00292         } else if (code == 0xFD) {
00293           uint32_t val;
00294           readInt(&val);
00295           result = val;
00296         } else {
00297           throw IllegalStateException("unexpected array length code");
00298         }
00299       }
00300       *len = result;
00301     }
00302   }
00303 
00310   inline void readUnsignedVL(int64_t * value)
00311   {
00312     int32_t shift = 0;
00313     int64_t result = 0;
00314     while (shift < 64) {
00315       int8_t b;
00316       read(&b);
00317       result |= (int64_t)(b & 0x7F) << shift;
00318       if ((b & 0x80) == 0) {
00319         *value = result;
00320         return;
00321       }
00322       shift += 7;
00323     }
00324     throw IllegalStateException("Malformed variable length integer");
00325   }
00326 
00332   inline void readFloat( float* value )
00333   {
00334     checkBufferSize(4);
00335     union float_uint32_t
00336     {
00337         float f;
00338         uint32_t u;
00339     }v;
00340     readInt( (uint32_t*)&v.u);
00341     *value = v.f;
00342   }
00343 
00350   inline void readDouble( double* value )
00351   {
00352     checkBufferSize(8);
00353     union double_uint64_t
00354     {
00355         double d;
00356         uint64_t        ll;
00357     }v;
00358     readInt( (uint64_t*)&v.ll);
00359     *value = v.d;
00360   }
00361 
00367   static inline void freeUTFMemory( char* value )
00368   {
00369     delete [] value;
00370   }
00371 
00377   static inline void freeUTFMemory( wchar_t* value )
00378   {
00379     delete [] value;
00380   }
00381 
00397   inline void readASCII( char** value, uint16_t* len = NULL )
00398   {
00399     uint16_t length;
00400     readInt( &length );
00401     checkBufferSize(length);
00402     if ( len != NULL ) {
00403       *len = length;
00404     }
00405     char* str;
00406     GF_NEW( str, char[ length + 1 ] );
00407     *value = str;
00408     readBytesOnly((int8_t*)str, length);
00409     str[ length ] = '\0';
00410   }
00411 
00426   inline void readASCIIHuge( char** value, uint32_t* len = NULL )
00427   {
00428     uint32_t length;
00429     readInt( &length );
00430     if ( len != NULL ) {
00431       *len = length;
00432     }
00433     char* str;
00434     GF_NEW( str, char[ length + 1 ] );
00435     *value = str;
00436     readBytesOnly( (int8_t*)str, length );
00437     str[ length ] = '\0';
00438   }
00439 
00456   inline void readUTF( char** value, uint16_t* len = NULL )
00457   {
00458     uint16_t length;
00459     readInt( &length );
00460     checkBufferSize(length);
00461     uint16_t decodedLen = (uint16_t)getDecodedLength( m_buf, length );
00462     if ( len != NULL ) {
00463       *len = decodedLen;
00464     }
00465     char* str;
00466     GF_NEW( str, char[ decodedLen + 1 ] );
00467     *value = str;
00468     for( uint16_t i = 0; i < decodedLen; i++ ) {
00469       decodeChar( str++ );
00470     }
00471     *str = '\0'; // null terminate for c-string.
00472   }
00473 
00483   inline void readUTFNoLen(wchar_t** value, uint16_t decodedLen)
00484   {
00485     wchar_t* str;
00486     GF_NEW(str, wchar_t[decodedLen + 1]);
00487     *value = str;
00488     for (uint16_t i = 0; i < decodedLen; i++) {
00489       decodeChar(str++);
00490     }
00491     *str = L'\0'; // null terminate for c-string.
00492   }
00493 
00508   inline void readUTFHuge( char** value, uint32_t* len = NULL )
00509   {
00510     uint32_t length;
00511     readInt( &length );
00512     if ( len != NULL ) {
00513       *len = length;
00514     }
00515     char* str;
00516     GF_NEW( str, char[ length + 1 ] );
00517     *value = str;
00518     for( uint32_t i = 0; i < length; i++ ) {
00519       int8_t item;
00520       read(&item); // ignore this - should be higher order zero byte
00521       read(&item);
00522       *str = item;
00523       str++;
00524     }
00525     *str = '\0'; // null terminate for c-string.
00526   }
00527 
00544   inline void readUTF( wchar_t** value, uint16_t* len = NULL )
00545   {
00546     uint16_t length;
00547     readInt( &length );
00548     checkBufferSize(length);
00549     uint16_t decodedLen = (uint16_t)getDecodedLength( m_buf, length );
00550     if ( len != NULL ) {
00551       *len = decodedLen;
00552     }
00553     wchar_t* str;
00554     GF_NEW( str, wchar_t[ decodedLen + 1 ] );
00555     *value = str;
00556     for( uint16_t i = 0; i < decodedLen; i++ ) {
00557       decodeChar( str++ );
00558     }
00559     *str = L'\0'; // null terminate for c-string.
00560   }
00561 
00576   inline void readUTFHuge( wchar_t** value, uint32_t* len = NULL )
00577   {
00578     uint32_t length;
00579     readInt( &length );
00580     if ( len != NULL ) {
00581       *len = length;
00582     }
00583     wchar_t* str;
00584     GF_NEW( str, wchar_t[ length + 1 ] );
00585     *value = str;
00586     for( uint32_t i = 0; i < length; i++ ) {
00587       uint8_t hibyte;
00588       read(&hibyte);
00589       uint8_t lobyte;
00590       read(&lobyte);
00591       *str = (((uint16_t)hibyte) << 8) | (uint16_t)lobyte;
00592       str++;
00593     }
00594     *str = L'\0'; // null terminate for c-string.
00595   }
00596 
00616   template< class PTR >
00617   inline void readObject( SharedPtr<PTR>& ptr,
00618       bool throwOnError = DINP_THROWONERROR_DEFAULT )
00619   {
00620     SerializablePtr sPtr;
00621     readObjectInternal( sPtr );
00622     if ( throwOnError ) {
00623       ptr = dynCast< SharedPtr<PTR> >( sPtr );
00624     } else {
00625       ptr = staticCast< SharedPtr<PTR> >( sPtr );
00626     }
00627   }
00628 
00629   inline bool readNativeBool( )
00630   {
00631     int8_t typeId = 0;
00632     read( &typeId );
00633     
00634     bool val;
00635     readBoolean(&val);
00636     return val;
00637   }
00638 
00639   inline int32_t readNativeInt32( )
00640   {
00641     int8_t typeId = 0;
00642     read( &typeId );
00643     
00644     int32_t val;
00645     readInt(&val);
00646     return val;
00647   }
00648 
00649   inline bool readNativeString(CacheableStringPtr& csPtr)
00650   {
00651     int8_t typeId = 0;
00652     read( &typeId );
00653     int64_t compId = typeId;
00654     if (compId == GemfireTypeIds::NullObj) {
00655      csPtr = NULLPTR;
00656     }
00657     else if (compId == GemfireTypeIds::CacheableNullString) {
00658       csPtr = CacheableStringPtr(dynamic_cast<CacheableString *>(CacheableString::createDeserializable()));
00659     }
00660     else if ( compId == gemfire::GemfireTypeIds::CacheableASCIIString) {
00661       csPtr = CacheableStringPtr(dynamic_cast<CacheableString *>(CacheableString::createDeserializable()));
00662       csPtr.ptr()->fromData(*this);
00663     }
00664     else if ( compId == gemfire::GemfireTypeIds::CacheableASCIIStringHuge) {
00665       csPtr = CacheableStringPtr(dynamic_cast<CacheableString *>(CacheableString::createDeserializableHuge()));
00666       csPtr.ptr()->fromData(*this);
00667     }
00668     else if ( compId == gemfire::GemfireTypeIds::CacheableString) {
00669       csPtr = CacheableStringPtr(dynamic_cast<CacheableString *>(CacheableString::createUTFDeserializable()));
00670       csPtr.ptr()->fromData(*this);
00671     }
00672     else if ( compId == gemfire::GemfireTypeIds::CacheableStringHuge) {
00673       csPtr = CacheableStringPtr(dynamic_cast<CacheableString *>(CacheableString::createUTFDeserializableHuge()));
00674       csPtr.ptr()->fromData(*this);
00675     }
00676     else {
00677       LOGDEBUG("In readNativeString something is wrong while expecting string");
00678       rewindCursor(1);
00679       csPtr = NULLPTR;
00680       return false;
00681     }
00682     return true;
00683   }
00684 
00685   inline void readDirectObject( SerializablePtr& ptr, int8_t typeId = -1 )
00686   {
00687     readObjectInternal( ptr, typeId );
00688   }
00689 
00694   inline void readObject( SerializablePtr& ptr )
00695   {
00696     readObjectInternal( ptr );
00697   }
00698 
00699   inline void readObject(wchar_t* value){
00700     uint16_t temp = 0;
00701     readInt(&temp);
00702     *value = (wchar_t)temp;
00703   }
00704 
00705   inline void readObject(bool* value){
00706     readBoolean(value);
00707   }
00708 
00709   inline void readObject(int8_t* value){
00710     read(value);
00711   }
00712 
00713   inline void readObject(int16_t* value){
00714     readInt(value);
00715   }
00716 
00717   inline void readObject(int32_t* value){
00718     readInt(value);
00719   }
00720 
00721   inline void readObject(int64_t* value){
00722     readInt(value);
00723   }
00724 
00725   inline void readObject(float* value){
00726       readFloat(value);
00727   }
00728 
00729   inline void readObject(double* value){
00730     readDouble(value);
00731   }
00732 
00733   inline void readCharArray(char** value, int32_t& length){
00734     int arrayLen = 0;
00735     readArrayLen(&arrayLen);
00736     length = arrayLen;
00737     char* objArray = NULL;
00738     if(arrayLen > 0) {
00739     objArray = new char[arrayLen];
00740       int i = 0;
00741       for( i = 0; i < arrayLen; i++ ){
00742         char tmp = 0;
00743         readPdxChar(&tmp);
00744         objArray[i] =  tmp;
00745       }
00746       *value = objArray;
00747     }
00748   }
00749 
00750   inline void readWideCharArray(wchar_t** value, int32_t& length){
00751     readObject(value, length);
00752   }
00753 
00754   inline void readBooleanArray(bool** value, int32_t& length){
00755     readObject(value, length);
00756   }
00757 
00758   inline void readByteArray(int8_t** value, int32_t& length){
00759     readObject(value, length);
00760   }
00761 
00762   inline void readShortArray(int16_t** value, int32_t& length){
00763     readObject(value, length);
00764   }
00765 
00766   inline void readIntArray(int32_t** value, int32_t& length){
00767     readObject(value, length);
00768   }
00769 
00770   inline void readLongArray(int64_t** value, int32_t& length){
00771     readObject(value, length);
00772   }
00773 
00774   inline void readFloatArray(float** value, int32_t& length){
00775     readObject(value, length);
00776   }
00777 
00778   inline void readDoubleArray(double** value, int32_t& length){
00779     readObject(value, length);
00780   }
00781 
00782   inline void readString(char** value) {
00783     int8_t typeId;
00784     read(&typeId);
00785 
00786     //Check for NULL String
00787     if (typeId == GemfireTypeIds::CacheableNullString){
00788         *value = NULL;
00789         return;
00790     }
00791     /*
00792     if (typeId == GemfireTypeIds::CacheableString) {
00793       readUTF(value);
00794     } else {
00795       readUTFHuge(value);
00796     }
00797     */
00798     if (typeId == (int8_t)GemfireTypeIds::CacheableASCIIString || typeId == (int8_t)GemfireTypeIds::CacheableString) {
00799       //readUTF( value);
00800       readASCII(value);
00801         //m_len = shortLen;
00802     }else if (typeId == (int8_t)GemfireTypeIds::CacheableASCIIStringHuge || typeId == (int8_t)GemfireTypeIds::CacheableStringHuge) {
00803       //readUTFHuge( value);
00804       readASCIIHuge(value);
00805     }
00806     else {
00807         throw IllegalArgumentException("DI readString error:: String type not supported ");
00808     }
00809 
00810   }
00811 
00812   inline void readWideString(wchar_t** value) {
00813     int8_t typeId;
00814     read(&typeId);
00815 
00816     //Check for NULL String
00817     if (typeId == GemfireTypeIds::CacheableNullString){
00818       *value = NULL;
00819       return;
00820     }
00821 
00822     if (typeId == (int8_t)GemfireTypeIds::CacheableASCIIString || typeId == (int8_t)GemfireTypeIds::CacheableString) {
00823       readUTF( value);
00824     }else if (typeId == (int8_t)GemfireTypeIds::CacheableASCIIStringHuge || typeId == (int8_t)GemfireTypeIds::CacheableStringHuge) {
00825       readUTFHuge( value);
00826     }
00827     else {
00828       throw IllegalArgumentException("DI readWideString error:: WideString type provided is not supported ");
00829     }
00830   }
00831 
00832   inline void readStringArray(char*** strArray, int32_t& length) {
00833     int32_t arrLen;
00834     readArrayLen(&arrLen);
00835     length = arrLen;
00836     if (arrLen == -1) {
00837       *strArray = NULL;
00838       return;
00839     } else {
00840       char** tmpArray;
00841       GF_NEW( tmpArray, char*[arrLen]);
00842       for (int i = 0; i < arrLen; i++) {
00843         readString(&tmpArray[i]);
00844       }
00845       *strArray = tmpArray;
00846     }
00847   }
00848 
00849   inline void readWideStringArray(wchar_t*** strArray, int32_t& length) {
00850     int32_t arrLen;
00851     readArrayLen(&arrLen);
00852     length = arrLen;
00853     if (arrLen == -1) {
00854       *strArray = NULL;
00855       return;
00856     } else {
00857       wchar_t** tmpArray;
00858       GF_NEW( tmpArray, wchar_t*[arrLen]);
00859       for (int i = 0; i < arrLen; i++) {
00860         readWideString(&tmpArray[i]);
00861       }
00862       *strArray = tmpArray;
00863     }
00864   }
00865 
00866   inline void readArrayOfByteArrays(int8_t*** arrayofBytearr,
00867     int32_t& arrayLength, int32_t** elementLength) {
00868     int32_t arrLen;
00869     readArrayLen(&arrLen);
00870     arrayLength = arrLen;
00871 
00872     if (arrLen == -1) {
00873       *arrayofBytearr = NULL;
00874       return;
00875     } else {
00876       int8_t** tmpArray;
00877       int32_t* tmpLengtharr;
00878       GF_NEW( tmpArray, int8_t*[arrLen]);
00879       GF_NEW( tmpLengtharr, int32_t[arrLen]);
00880       for (int i = 0; i < arrLen; i++) {
00881         readBytes(&tmpArray[i], &tmpLengtharr[i]);
00882       }
00883       *arrayofBytearr = tmpArray;
00884       *elementLength = tmpLengtharr;
00885     }
00886   }
00887 
00898   static int32_t getDecodedLength( const uint8_t* value, int32_t length )
00899   {
00900     const uint8_t* end = value + length;
00901     int32_t decodedLen = 0;
00902     while ( value < end ) {
00903        // get next byte unsigned
00904     int32_t b = *value++ & 0xff;
00905     int32_t k = b >> 5;
00906     // classify based on the high order 3 bits
00907       switch (  k )
00908         {       
00909         case 6:
00910           {
00911             value++;
00912             break;
00913           }
00914         case 7:
00915           {
00916             value += 2;
00917             break;
00918           }
00919         default:
00920           break;
00921         }
00922       decodedLen += 1;
00923     }
00924     if ( value > end ) decodedLen--;
00925     return decodedLen;
00926   }
00927 
00929   DataInput(const uint8_t* m_buffer, int32_t len)
00930     : m_buf(m_buffer), m_bufHead(m_buffer), m_bufLength(len), m_poolName(NULL)
00931   {
00932   }
00933 
00935   ~DataInput( ) { }
00936 
00942   inline const uint8_t* currentBufferPosition() const
00943   {
00944     return m_buf;
00945   }
00946 
00948   inline int32_t getBytesRead() const
00949   {
00950     return static_cast<int32_t>(m_buf - m_bufHead);
00951   }
00952 
00954   inline int32_t getBytesRemaining() const
00955   {
00956     return (m_bufLength - getBytesRead());
00957   }
00958 
00960   inline void advanceCursor(int32_t offset)
00961   {
00962     m_buf += offset;
00963   }
00964 
00966   inline void rewindCursor(int32_t offset)
00967   {
00968     m_buf -= offset;
00969   }
00970 
00972   inline void reset()
00973   {
00974     m_buf = m_bufHead;
00975   }
00976 
00977   inline void setBuffer()
00978   {
00979     m_buf = currentBufferPosition();    
00980     m_bufLength = getBytesRemaining();   
00981   }
00982 
00983   inline void resetPdx(int32_t offset )
00984   {
00985     m_buf = m_bufHead + offset;
00986     //setBuffer();
00987   }
00988 
00989   inline int32_t getPdxBytes() const
00990   {
00991     return m_bufLength;
00992   }
00993 
00994   static uint8_t * getBufferCopy(const uint8_t *from, uint32_t length)
00995   {
00996     uint8_t * result;
00997     GF_NEW( result, uint8_t[ length ] );
00998     memcpy( result, from, length);
00999     
01000     return result;
01001   }
01002 
01003   inline void reset(int32_t offset )
01004   {
01005     m_buf = m_bufHead + offset;
01006   }
01007 
01008   uint8_t * getBufferCopyFrom(const uint8_t *from, uint32_t length)
01009   {
01010     uint8_t * result;
01011     GF_NEW( result, uint8_t[ length ] );
01012     memcpy( result, from, length);
01013     
01014     return result;
01015   }
01016 
01017   /*
01018    * This is for internal use
01019    */
01020   const char* getPoolName()
01021   {
01022     return m_poolName;
01023   }
01024 
01025   /*
01026    * This is for internal use
01027    */
01028   void setPoolName(const char* poolName)
01029   {
01030     m_poolName = poolName;
01031   }
01032 
01033 private:
01034 
01035   const uint8_t* m_buf;
01036   const uint8_t* m_bufHead;
01037   int32_t m_bufLength;
01038   const char* m_poolName;
01039 
01040   void readObjectInternal( SerializablePtr& ptr, int8_t typeId = -1 );
01041 
01042   template <typename mType>
01043   void readObject(mType** value, int32_t& length)
01044   {
01045     int arrayLen;
01046     readArrayLen(&arrayLen);
01047     length = arrayLen;
01048     mType* objArray;
01049     if(arrayLen > 0) {
01050       objArray = new mType[arrayLen];
01051       int i = 0;
01052       for( i = 0; i < arrayLen; i++ ){
01053         mType tmp = 0;
01054         readObject(&tmp);
01055         objArray[i] =  tmp; //*value[i] = tmp;
01056       }
01057       *value = objArray;
01058     }
01059   }
01060 
01061   inline void readPdxChar(char* value){
01062     int16_t val = 0;
01063     readInt(&val);
01064     *value = (char)val;
01065   }
01066 
01067   inline void _checkBufferSize(int32_t size, int32_t line)
01068   {
01069     if ((m_bufLength - (m_buf - m_bufHead)) < size) {
01070       char exMsg[128];
01071       gf_sprintf(exMsg, "DataInput: attempt to read beyond buffer at line %d: "
01072           "available buffer size %d, attempted read of size %d ", line,
01073           m_bufLength - (m_buf - m_bufHead), size);
01074       throw OutOfRangeException(exMsg);
01075     }
01076   }
01077 
01078   inline void decodeChar( char* str )
01079   {
01080     uint8_t bt = *(m_buf++);
01081     if ( bt & 0x80 ) {
01082       if ( bt & 0x20 ) {
01083         // three bytes.
01084         *str = (char)(((bt & 0x0f) << 12) | (((*m_buf++) & 0x3f) << 6));
01085         *str |= (char)((*m_buf++) & 0x3f);
01086       } else {
01087         // two bytes.
01088         *str = (char)(((bt & 0x1f) << 6) | ((*m_buf++) & 0x3f));
01089       }
01090     } else {
01091       // single byte...
01092       *str = bt;
01093     }
01094   }
01095 
01096   inline void decodeChar( wchar_t* str )
01097   {
01098     // get next byte unsigned
01099     int32_t b = *m_buf++ & 0xff;
01100     int32_t k = b >> 5;
01101     // classify based on the high order 3 bits
01102     switch (  k )
01103       {     
01104       case 6:
01105         {
01106           // two byte encoding
01107           // 110yyyyy 10xxxxxx
01108           // use low order 6 bits
01109           int32_t y = b & 0x1f;
01110           // use low order 6 bits of the next byte
01111           // It should have high order bits 10, which we don't check.
01112           int32_t x = *m_buf++ & 0x3f;
01113           // 00000yyy yyxxxxxx
01114           *str = ( y << 6 | x );
01115           break;
01116         }
01117     case 7:
01118       {
01119         // three byte encoding
01120         // 1110zzzz 10yyyyyy 10xxxxxx
01121         //assert ( b & 0x10 )
01122           //     == 0 : "UTF8Decoder does not handle 32-bit characters";
01123         // use low order 4 bits
01124         int32_t z = b & 0x0f;
01125         // use low order 6 bits of the next byte
01126         // It should have high order bits 10, which we don't check.
01127         int32_t y = *m_buf++ & 0x3f;
01128         // use low order 6 bits of the next byte
01129         // It should have high order bits 10, which we don't check.
01130         int32_t x = *m_buf++ & 0x3f;
01131         // zzzzyyyy yyxxxxxx
01132         int32_t asint = ( z << 12 | y << 6 | x );
01133         *str =  asint;
01134         break;
01135       }
01136        default:
01137         // one byte encoding
01138         // 0xxxxxxx
01139         // use just low order 7 bits
01140         // 00000000 0xxxxxxx
01141         *str = ( b & 0x7f );
01142         break;
01143     }    
01144   }
01145 
01146   // disable other constructors and assignment
01147   DataInput();
01148   DataInput(const DataInput&);
01149   DataInput& operator =(const DataInput&);
01150 };
01151 
01152 }
01153 
01154 #endif // __GEMFIRE_DATAINPUT_H__

GemFire C++ Cache API Documentation