1 #ifndef __GEMFIRE_DATAOUTPUT_H__ 2 #define __GEMFIRE_DATAOUTPUT_H__ 12 #include "gfcpp_globals.hpp" 33 #define GF_ALLOC(v,t,s) \ 35 v = (t*)malloc((s) * sizeof(t)); \ 37 throw OutOfMemoryException( \ 38 "Out of Memory while allocating buffer for "#t" of size "#s); \ 46 #define GF_RESIZE(v,t,s) \ 48 v = (t*)realloc(v, (s) * sizeof(t)); \ 50 throw OutOfMemoryException( \ 51 "Out of Memory while resizing buffer for "#t); \ 55 #define GF_FREE(v) free(v) 77 inline void write(uint8_t value)
90 write((uint8_t)value);
100 write( (uint8_t)value );
112 ensureCapacity( len + 5 );
113 writeArrayLen( bytes==NULL ? 0 : len );
114 if ( len > 0 && bytes != NULL) {
115 memcpy( m_buf, bytes, len );
131 writeBytes( (
const uint8_t*)bytes, len );
147 ensureCapacity( len );
148 memcpy( m_buf, bytes, len );
165 writeBytesOnly( (
const uint8_t*)bytes, len );
176 *(m_buf++) = (uint8_t)(value >> 8);
177 *(m_buf++) = (uint8_t)value;
188 *(m_buf++) = (uint8_t)(value >> 8);
189 *(m_buf++) = (uint8_t)value;
200 *(m_buf++) = (uint8_t)(value >> 24);
201 *(m_buf++) = (uint8_t)(value >> 16);
202 *(m_buf++) = (uint8_t)(value >> 8);
203 *(m_buf++) = (uint8_t)value;
218 if (
sizeof(
long ) == 8 ) {
219 *(m_buf++) = (uint8_t)(value >> 56);
220 *(m_buf++) = (uint8_t)(value >> 48);
221 *(m_buf++) = (uint8_t)(value >> 40);
222 *(m_buf++) = (uint8_t)(value >> 32);
223 *(m_buf++) = (uint8_t)(value >> 24);
224 *(m_buf++) = (uint8_t)(value >> 16);
225 *(m_buf++) = (uint8_t)(value >> 8);
226 *(m_buf++) = (uint8_t)value;
228 uint32_t hword = (uint32_t)(value >> 32);
229 *(m_buf++) = (uint8_t)(hword >> 24);
230 *(m_buf++) = (uint8_t)(hword >> 16);
231 *(m_buf++) = (uint8_t)(hword >> 8);
232 *(m_buf++) = (uint8_t)hword;
234 hword = (uint32_t)value;
235 *(m_buf++) = (uint8_t)(hword >> 24);
236 *(m_buf++) = (uint8_t)(hword >> 16);
237 *(m_buf++) = (uint8_t)(hword >> 8);
238 *(m_buf++) = (uint8_t)hword;
249 writeInt( (uint16_t)value );
259 writeInt( (uint32_t)value );
269 writeInt( (uint64_t)value );
283 }
else if (len <= 252) {
285 }
else if (len <= 0xFFFF) {
287 writeInt((uint16_t)len);
317 union double_uint64_t
338 inline void writeASCII(
const char* value, uint32_t length = 0)
340 if ( value != NULL ) {
342 length = (uint32_t)strlen( value );
344 uint16_t len = (uint16_t)( length > 0xFFFF ? 0xFFFF : length );
346 writeBytesOnly( (int8_t*)value, len );
348 writeInt( (uint16_t)0 );
352 inline void writeNativeString(
const char* value)
375 if ( value != NULL ) {
377 length = (uint32_t)strlen( value );
379 writeInt( (uint32_t) length );
380 writeBytesOnly( (int8_t*)value, (uint32_t) length );
382 writeInt( (uint32_t)0 );
399 if ( value != NULL ) {
400 int32_t len = getEncodedLength( value, length );
401 uint16_t encodedLen = (uint16_t)( len > 0xFFFF ? 0xFFFF : len );
402 writeInt( (int32_t)encodedLen );
403 ensureCapacity( encodedLen );
405 uint8_t* end = m_buf + encodedLen;
406 while( m_buf < end ) {
407 encodeChar( *value++ );
409 if ( m_buf > end ) m_buf = end;
411 writeInt( (uint16_t)0 );
426 inline void writeUTF(
const char* value, uint32_t length = 0)
428 if ( value != NULL ) {
429 int32_t len = getEncodedLength( value, length );
430 uint16_t encodedLen = (uint16_t)( len > 0xFFFF ? 0xFFFF : len );
431 writeInt( encodedLen );
432 ensureCapacity( encodedLen );
433 uint8_t* end = m_buf + encodedLen;
434 while( m_buf < end ) {
435 encodeChar( *value++ );
437 if ( m_buf > end ) m_buf = end;
439 writeInt( (uint16_t)0 );
459 length = (uint32_t)strlen(value);
462 ensureCapacity(length * 2);
463 for (uint32_t pos = 0; pos < length; pos++) {
464 writeNoCheck((int8_t)0);
465 writeNoCheck((int8_t)value[pos]);
469 writeInt((uint32_t)0);
484 inline void writeUTF(
const wchar_t* value, uint32_t length = 0)
486 if ( value != NULL ) {
487 int32_t len = getEncodedLength( value, length );
488 uint16_t encodedLen = (uint16_t)( len > 0xFFFF ? 0xFFFF : len );
489 writeInt( encodedLen );
490 ensureCapacity( encodedLen );
491 uint8_t* end = m_buf + encodedLen;
492 while( m_buf < end ) {
493 encodeChar( *value++ );
495 if ( m_buf > end ) m_buf = end;
497 writeInt( (uint16_t)0 );
515 length = (uint32_t)wcslen(value);
518 ensureCapacity(length * 2);
519 for (uint32_t pos = 0; pos < length; pos++) {
520 uint16_t item = (uint16_t)value[pos];
521 writeNoCheck((uint8_t)((item & 0xFF00) >> 8));
522 writeNoCheck((uint8_t)(item & 0xFF));
526 writeInt((uint32_t)0);
541 int32_t length = 0, uint32_t* valLength = NULL )
543 if ( value == NULL )
return 0;
545 int32_t encodedLen = 0;
546 const char* start = value;
548 while ( (currentChar = *value) !=
'\0' ) {
549 getEncodedLength( currentChar, encodedLen );
553 const char* end = value + length;
554 while ( value < end ) {
555 currentChar = *value;
556 getEncodedLength( currentChar, encodedLen );
560 if ( valLength != NULL ) {
561 *valLength =
static_cast<uint32_t
>(value - start);
577 int32_t length = 0, uint32_t* valLength = NULL )
579 if ( value == NULL )
return 0;
581 int32_t encodedLen = 0;
582 const wchar_t* start = value;
584 while ( (currentChar = *value) != 0 ) {
585 getEncodedLength( currentChar, encodedLen );
589 const wchar_t* end = value + length;
590 while ( value < end ) {
591 currentChar = *value;
592 getEncodedLength( currentChar, encodedLen );
596 if ( valLength != NULL ) {
597 *valLength =
static_cast<uint32_t
>(value - start);
608 template<
class PTR >
611 writeObjectInternal( objptr.ptr( ), isDelta );
622 writeObjectInternal( objptr );
641 ensureCapacity(offset);
655 void updateValueAtPos(uint32_t offset, uint8_t value)
657 m_bytes[offset] = value;
660 uint8_t getValueAtPos(uint32_t offset)
662 return m_bytes[offset];
668 DataOutput::checkinBuffer(m_bytes, m_size);
686 return m_size - getBufferLength();
695 inline const uint8_t*
getBuffer( uint32_t* rsize )
const 697 *rsize = (uint32_t)( m_buf - m_bytes );
702 inline uint8_t* getBufferCopy( )
704 uint32_t size = (uint32_t)( m_buf - m_bytes );
707 memcpy( result, m_bytes, size );
716 return (uint32_t)( m_buf - m_bytes );
724 if (m_haveBigBuffer) {
728 GF_ALLOC(m_bytes, uint8_t, m_lowWaterMark);
729 m_size = m_lowWaterMark;
731 m_haveBigBuffer =
false;
739 inline void ensureCapacity( uint32_t size )
741 uint32_t offset = (uint32_t)( m_buf - m_bytes );
742 if ( (m_size - offset) < size ) {
743 uint32_t newSize = m_size * 2 + (8192 * (size / 8192));
744 if (newSize >= m_highWaterMark && !m_haveBigBuffer) {
748 m_haveBigBuffer =
true;
752 m_buf = m_bytes + offset;
759 const char* getPoolName()
767 void setPoolName(
const char* poolName)
769 m_poolName = poolName;
772 uint8_t * getBufferCopyFrom(
const uint8_t *from, uint32_t length)
775 GF_NEW( result, uint8_t[ length ] );
776 memcpy( result, from, length);
781 static void safeDelete(uint8_t* src)
790 static void releaseDataOutput(
DataOutput* dataOutput)
796 void writeObjectInternal(
const Serializable* ptr,
bool isDelta =
false );
798 static void acquireLock();
799 static void releaseLock();
803 const char* m_poolName;
811 static uint32_t m_lowWaterMark;
812 static uint32_t m_highWaterMark;
814 volatile bool m_haveBigBuffer;
816 inline static void getEncodedLength(
const char val, int32_t& encodedLen )
818 if ( (val == 0) || (val & 0x80) ) {
827 inline static void getEncodedLength(
const wchar_t val, int32_t& encodedLen )
833 else if ( val < 0x80 )
837 else if ( val < 0x800 )
847 inline void encodeChar(
const char value )
849 uint8_t tmp = (uint8_t)value;
850 if ( (tmp == 0) || (tmp & 0x80) ) {
852 *(m_buf++) = (uint8_t)(0xc0 | ((tmp & 0xc0 ) >> 6));
853 *(m_buf++) = (uint8_t)(0x80 | (tmp & 0x3f ));
861 inline void encodeChar(
const wchar_t value )
863 uint16_t c = (uint16_t)value;
868 else if ( c < 0x80 ) {
869 *(m_buf++) = (uint8_t)c;
871 else if ( c < 0x800 ) {
872 *(m_buf++) = (uint8_t)( 0xC0 | c >> 6 );
873 *(m_buf++) = (uint8_t)( 0x80 | (c & 0x3F) );
876 *(m_buf++) = (uint8_t)( 0xE0 | c >> 12 );
877 *(m_buf++) = (uint8_t)( 0x80 | ((c >> 6) & 0x3F) );
878 *(m_buf++) = (uint8_t)( 0x80 | (c & 0x3F) );
882 inline void writeNoCheck(uint8_t value)
887 inline void writeNoCheck(int8_t value)
889 writeNoCheck((uint8_t)value);
893 static uint8_t* checkoutBuffer( uint32_t* size );
894 static void checkinBuffer( uint8_t* buffer, uint32_t size );
903 #endif // __GEMFIRE_DATAOUTPUT_H__ void writeInt(int16_t value)
Write a 16-bit signed integer value to the DataOutput.
Definition: DataOutput.hpp:247
#define GF_ALLOC(v, t, s)
C style memory allocation that throws OutOfMemoryException if it fails.
Definition: DataOutput.hpp:33
virtual int8_t typeId() const
Return the typeId byte of the instance being serialized.
void writeInt(int64_t value)
Write a 64-bit signed integer value to the DataOutput.
Definition: DataOutput.hpp:267
Provide operations for writing primitive data values, byte arrays, strings, Serializable objects to a...
Definition: DataOutput.hpp:62
void writeASCIIHuge(const char *value, uint32_t length=0)
Writes the given ASCII string supporting upto maximum 32-bit integer value.
Definition: DataOutput.hpp:373
const uint8_t * getBuffer(uint32_t *rsize) const
Get a pointer to the internal buffer of DataOutput.
Definition: DataOutput.hpp:695
void rewindCursor(uint32_t offset)
Rewind the buffer cursor by the given offset.
Definition: DataOutput.hpp:650
void writeBytes(const uint8_t *bytes, int32_t len)
Write an array of unsigned bytes to the DataOutput.
Definition: DataOutput.hpp:109
virtual void toData(DataOutput &output) const
serialize this object
void writeDouble(double value)
Write a double precision real number to the DataOutput.
Definition: DataOutput.hpp:315
void advanceCursor(uint32_t offset)
Advance the buffer cursor by the given offset.
Definition: DataOutput.hpp:639
void writeBytesOnly(const uint8_t *bytes, uint32_t len)
Write an array of unsigned bytes without its length to the DataOutput.
Definition: DataOutput.hpp:145
void writeChar(uint16_t value)
Write a 16-bit Char (wchar_t) value to the DataOutput.
Definition: DataOutput.hpp:185
void writeUTFHuge(const char *value, uint32_t length=0)
Writes the given string using java modified UTF-8 encoding.
Definition: DataOutput.hpp:455
const uint8_t * getBuffer() const
Get a pointer to the internal buffer of DataOutput.
Definition: DataOutput.hpp:674
void write(uint8_t value)
Write an unsigned byte to the DataOutput.
Definition: DataOutput.hpp:77
uint32_t getBufferLength() const
Get the length of current data in the internal buffer of DataOutput.
Definition: DataOutput.hpp:715
const uint8_t * getCursor()
Get an internal pointer to the current location in the DataOutput byte array.
Definition: DataOutput.hpp:629
void writeInt(uint64_t value)
Write a 64-bit unsigned integer value to the DataOutput.
Definition: DataOutput.hpp:211
static CacheableStringPtr create(const char *value, int32_t len=0)
Factory method for creating an instance of CacheableString from a null terminated C string optionally...
Definition: CacheableString.hpp:102
void writeFullUTF(const char *value, uint32_t length=0)
Writes the given given string using java modified UTF-8 encoding supporting maximum encoded length of...
Definition: DataOutput.hpp:397
void writeFloat(float value)
Write a float value to the DataOutput.
Definition: DataOutput.hpp:299
void writeArrayLen(int32_t len)
Write a 32-bit signed integer array length value to the DataOutput in a manner compatible with java s...
Definition: DataOutput.hpp:279
void writeObject(const SharedPtr< PTR > &objptr, bool isDelta=false)
Write a Serializable object to the DataOutput.
Definition: DataOutput.hpp:609
void writeInt(uint16_t value)
Write a 16-bit unsigned integer value to the DataOutput.
Definition: DataOutput.hpp:173
#define GF_SAFE_DELETE(x)
Deletes x only if it exists.
Definition: gf_base.hpp:327
This namespace contains all the GemFire C++ API classes, enumerations and globals.
Definition: Assert.hpp:19
static int32_t getEncodedLength(const char *value, int32_t length=0, uint32_t *valLength=NULL)
Get the length required to represent a given wide-character string in java modified UTF-8 format...
Definition: DataOutput.hpp:540
void write(int8_t value)
Write a signed byte to the DataOutput.
Definition: DataOutput.hpp:88
#define GF_RESIZE(v, t, s)
C style memory re-allocation that throws OutOfMemoryException if it fails.
Definition: DataOutput.hpp:46
void writeObject(const Serializable *objptr)
Write a Serializable object to the DataOutput.
Definition: DataOutput.hpp:620
void writeBytesOnly(const int8_t *bytes, uint32_t len)
Write an array of signed bytes without its length to the DataOutput.
Definition: DataOutput.hpp:163
void writeBoolean(bool value)
Write a boolean value to the DataOutput.
Definition: DataOutput.hpp:98
This abstract base class is the superclass of all user objects in the cache that can be serialized...
Definition: Serializable.hpp:39
void writeUTFHuge(const wchar_t *value, uint32_t length=0)
Writes the given string using java modified UTF-8 encoding.
Definition: DataOutput.hpp:511
void writeInt(int32_t value)
Write a 32-bit signed integer value to the DataOutput.
Definition: DataOutput.hpp:257
#define CPPCACHE_EXPORT
Defines a GemFire CPPCACHE export.
Definition: gf_base.hpp:51
~DataOutput()
destructor
Definition: DataOutput.hpp:665
void reset()
Reset the internal cursor to the start of the buffer.
Definition: DataOutput.hpp:722
static int32_t getEncodedLength(const wchar_t *value, int32_t length=0, uint32_t *valLength=NULL)
Get the length required to represent a given wide-character string in java modified UTF-8 format...
Definition: DataOutput.hpp:576
#define GF_NEW(v, stmt)
Allocates x and throws OutOfMemoryException if it fails.
Definition: gf_base.hpp:315
void writeBytes(const int8_t *bytes, int32_t len)
Write an array of signed bytes to the DataOutput.
Definition: DataOutput.hpp:129
void writeUTF(const char *value, uint32_t length=0)
Writes the given given string using java modified UTF-8 encoding supporting maximum encoded length of...
Definition: DataOutput.hpp:426
uint32_t getRemainingBufferLength() const
Get a pointer to the internal buffer of DataOutput.
Definition: DataOutput.hpp:683
void writeASCII(const char *value, uint32_t length=0)
Writes the given ASCII string supporting maximum length of 64K (i.e.
Definition: DataOutput.hpp:338
void writeInt(uint32_t value)
Write a 32-bit unsigned integer value to the DataOutput.
Definition: DataOutput.hpp:197
void writeUTF(const wchar_t *value, uint32_t length=0)
Writes the given given string using java modified UTF-8 encoding supporting maximum encoded length of...
Definition: DataOutput.hpp:484