Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 8EEC9200C1F for ; Fri, 13 Jan 2017 23:45:54 +0100 (CET) Received: by cust-asf.ponee.io (Postfix) id 8D7B2160B59; Fri, 13 Jan 2017 22:45:54 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id B7B8E160B58 for ; Fri, 13 Jan 2017 23:45:47 +0100 (CET) Received: (qmail 65939 invoked by uid 500); 13 Jan 2017 22:45:46 -0000 Mailing-List: contact commits-help@geode.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@geode.apache.org Delivered-To: mailing list commits@geode.apache.org Received: (qmail 65176 invoked by uid 99); 13 Jan 2017 22:45:45 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 13 Jan 2017 22:45:45 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 81183DFC16; Fri, 13 Jan 2017 22:45:45 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: rvs@apache.org To: commits@geode.apache.org Date: Fri, 13 Jan 2017 22:45:52 -0000 Message-Id: In-Reply-To: References: X-Mailer: ASF-Git Admin Mailer Subject: [08/51] [partial] geode git commit: Software Grant Agreement (SGA) from Pivotal for Apache Geode archived-at: Fri, 13 Jan 2017 22:45:54 -0000 http://git-wip-us.apache.org/repos/asf/geode/blob/2d4a7ecd/src/clicache/src/DataInput.hpp ---------------------------------------------------------------------- diff --git a/src/clicache/src/DataInput.hpp b/src/clicache/src/DataInput.hpp new file mode 100644 index 0000000..546d2b6 --- /dev/null +++ b/src/clicache/src/DataInput.hpp @@ -0,0 +1,685 @@ +/*========================================================================= + * Copyright (c) 2002-2014 Pivotal Software, Inc. All Rights Reserved. + * This product is protected by U.S. and international copyright + * and intellectual property laws. Pivotal products are covered by + * more patents listed at http://www.pivotal.io/patents. + *========================================================================= + */ + +#pragma once + +#include "gf_defs.hpp" +#include +#include "impl/NativeWrapper.hpp" +#include "Log.hpp" +#include "ExceptionTypes.hpp" +//#include "../../CacheableDate.hpp" + + +using namespace System; +using namespace System::Collections::Generic; + +namespace GemStone +{ + namespace GemFire + { + namespace Cache + { + namespace Generic + { + + interface class IGFSerializable; + + /// + /// Provides operations for reading primitive data values, byte arrays, + /// strings, IGFSerializable objects from a byte stream. + /// + public ref class DataInput sealed + : public Generic::Internal::UMWrap + { + public: + + /// + /// Construct DataInput using an given array of bytes. + /// + /// + /// The buffer to use for reading data values. + /// + /// + /// if the buffer is null + /// + DataInput( array^ buffer ); + + /// + /// Construct DataInput using a given length of an array of + /// bytes. + /// + /// + /// The buffer to use for reading data values. + /// + /// + /// The number of bytes from the start of the buffer to use. + /// + /// + /// if the buffer is null + /// + DataInput( array^ buffer, int32_t len ); + + /// + /// Dispose: frees the internal buffer. + /// + ~DataInput( ) { Cleanup( ); } + + /// + /// Finalizer: frees the internal buffer. + /// + !DataInput( ) { Cleanup( ); } + + /// + /// Read a signed byte from the stream. + /// + SByte ReadSByte( ); + + /// + /// Read a boolean value from the stream. + /// + bool ReadBoolean( ); + + /// + /// Read a char value from the stream. + /// + Char ReadChar( ); + + /// + /// Read an array of bytes from the stream reading the length + /// from the stream first. + /// + array^ ReadBytes( ); + + /// + /// Read an array of signed bytes from the stream reading the length + /// from the stream first. + /// + array^ ReadSBytes( ); + + /// + /// Read the given number of bytes from the stream. + /// + /// Number of bytes to read. + array^ ReadBytesOnly( uint32_t len ); + + void ReadBytesOnly( array ^ buffer, int offset, int count ); + + /// + /// Read the given number of signed bytes from the stream. + /// + /// Number of signed bytes to read. + array^ ReadSBytesOnly( uint32_t len ); + + /// + /// Read a array len based on array size. + /// + int ReadArrayLen( ); + + /// + /// Read a 16-bit integer from the stream. + /// + int16_t ReadInt16( ); + + /// + /// Read a 32-bit integer from the stream. + /// + int32_t ReadInt32( ); + + /// + /// Read a 64-bit integer from the stream. + /// + int64_t ReadInt64( ); + + /// + /// Read a floating point number from the stream. + /// + float ReadFloat( ); + + /// + /// Read a double precision number from the stream. + /// + double ReadDouble( ); + + /// + /// Read a string after java-modified UTF-8 decoding from the stream. + /// The maximum length supported is 2^16-1 beyond which the string + /// shall be truncated. + /// + String^ ReadUTF( ); + + /// + /// Read a string after java-modified UTF-8 decoding from the stream. + /// + String^ ReadUTFHuge( ); + + /// + /// Read a ASCII string from the stream. Where size is more than 2^16-1 + /// + String^ ReadASCIIHuge( ); + + /// + /// Read a serializable object from the data. Null objects are handled. + /// + Object^ ReadObject( ); + + /// + /// Get the count of bytes that have been read from the stream. + /// + property uint32_t BytesRead + { + uint32_t get( ); + } + + /// + /// Get the count of bytes that are remaining in the buffer. + /// + property uint32_t BytesRemaining + { + uint32_t get(); + } + + /// + /// Advance the cursor of the buffer by the given offset. + /// + /// + /// The offset(number of bytes) by which to advance the cursor. + /// + void AdvanceCursor( int32_t offset ); + + /// + /// Rewind the cursor of the buffer by the given offset. + /// + /// + /// The offset(number of bytes) by which to rewind the cursor. + /// + void RewindCursor( int32_t offset ); + + /// + /// Reset the cursor to the start of buffer. + /// + void Reset(); + + /// + /// Get the underlying native unmanaged pointer. + /// + property IntPtr NativeIntPtr + { + inline IntPtr get() + { + return IntPtr(_NativePtr); + } + } + + /// + /// Read a dictionary from the stream in a given dictionary instance. + /// + /// Object which implements System::Collections::IDictionary interface. + void ReadDictionary(System::Collections::IDictionary^ dictionary); + + /// + /// Read a date from the stream. + /// + System::DateTime ReadDate( ); + + /// + /// Read a collection from the stream in a given collection instance. + /// + /// Object which implements System::Collections::IList interface. + void ReadCollection(System::Collections::IList^ list); + + /// + /// Read a char array from the stream. + /// + array^ ReadCharArray( ); + + /// + /// Read a bool array from the stream. + /// + array^ ReadBooleanArray( ); + + /// + /// Read a short int array from the stream. + /// + array^ ReadShortArray( ); + + /// + /// Read a int array from the stream. + /// + array^ ReadIntArray(); + + /// + /// Read a long array from the stream. + /// + array^ ReadLongArray(); + + /// + /// Read a float array from the stream. + /// + array^ ReadFloatArray(); + + /// + /// Read a double array from the stream. + /// + array^ ReadDoubleArray(); + + /// + /// Read a object array from the stream from the stream. + /// + List^ ReadObjectArray(); + + /// + /// Read a array of signed byte array from the stream. + /// + array^>^ ReadArrayOfByteArrays( ); + + internal: + + void setPdxdeserialization(bool val) + { + m_ispdxDesrialization = true; + } + bool isRootObjectPdx() + { + return m_isRootObjectPdx; + } + void setRootObjectPdx(bool val) + { + m_isRootObjectPdx = val; + } + + Object^ readDotNetObjectArray(); + System::Collections::Generic::IDictionary^ ReadDictionary(); + + String^ ReadString(); + + const char * GetPoolName() + { + return _NativePtr->getPoolName(); + } + + Object^ ReadDotNetTypes(int8_t typeId); + + /// + /// Get the count of bytes that have been read from the stream, for internal use only. + /// + property uint32_t BytesReadInternally + { + uint32_t get( ); + } + + void ReadObject(bool% obj) + { + obj = ReadBoolean(); + } + + void ReadObject(Byte% obj) + { + obj = ReadByte(); + } + + void ReadObject(Char% obj) + { + obj = (Char)ReadUInt16(); + } + + inline Char decodeChar( ) + { + Char retChar; + int b = m_buffer[ m_cursor++ ] & 0xff; + int k = b >> 5; + switch ( k ) + { + default: + retChar = ( Char ) ( b & 0x7f ); + break; + case 6: + { + // two byte encoding + // 110yyyyy 10xxxxxx + // use low order 6 bits + int y = b & 0x1f; + // use low order 6 bits of the next byte + // It should have high order bits 10, which we don't check. + int x = m_buffer[ m_cursor++ ] & 0x3f; + // 00000yyy yyxxxxxx + retChar = ( Char ) ( y << 6 | x ); + break; + } + case 7: + { + // three byte encoding + // 1110zzzz 10yyyyyy 10xxxxxx + //assert ( b & 0x10 ) + // == 0 : "UTF8Decoder does not handle 32-bit characters"; + // use low order 4 bits + int z = b & 0x0f; + // use low order 6 bits of the next byte + // It should have high order bits 10, which we don't check. + int y = m_buffer[ m_cursor++ ] & 0x3f; + // use low order 6 bits of the next byte + // It should have high order bits 10, which we don't check. + int x = m_buffer[ m_cursor++ ] & 0x3f; + // zzzzyyyy yyxxxxxx + int asint = ( z << 12 | y << 6 | x ); + retChar = ( Char ) asint; + break; + } + }// end switch + + return retChar; + } + + System::Collections::Hashtable^ ReadHashtable() + { + int len = this->ReadArrayLen(); + + if(len == -1) + return nullptr; + else + { + System::Collections::Hashtable^ dict = gcnew System::Collections::Hashtable(); + for(int i =0; i< len; i++) + { + Object^ key = this->ReadObject(); + Object^ val = this->ReadObject(); + + dict->Add(key, val); + } + return dict; + } + } + + /// + /// Read a byte from the stream. + /// + Byte ReadByte( ); + + /// + /// Read a 16-bit unsigned integer from the stream. + /// + uint16_t ReadUInt16( ); + + /// + /// Read a 32-bit unsigned integer from the stream. + /// + uint32_t ReadUInt32( ); + + /// + /// Read a 64-bit unsigned integer from the stream. + /// + uint64_t ReadUInt64( ); + + void ReadObject(Double% obj) + { + obj = ReadDouble(); + } + + void ReadObject(Single% obj) + { + obj = ReadFloat(); + } + + void ReadObject(int16_t% obj) + { + obj = ReadInt16(); + } + + void ReadObject(int32_t% obj) + { + obj = ReadInt32(); + } + + void ReadObject(int64_t% obj) + { + obj = ReadInt64(); + } + + void ReadObject(array^% obj) + { + obj = ReadSBytes(); + } + + void DataInput::ReadObject(array^% obj); + void DataInput::ReadObject(array^% obj); + void DataInput::ReadObject(array^% obj); + + template + void ReadObject(array^ %objArray) + { + int arrayLen = ReadArrayLen(); + if(arrayLen >= 0) { + objArray = gcnew array(arrayLen); + + int i = 0; + for( i = 0; i < arrayLen; i++ ){ + mType tmp; + ReadObject(tmp); + objArray[i] = tmp; + } + } + } + + array^ ReadStringArray() + { + int len = this->ReadArrayLen(); + if ( len == -1) + { + return nullptr; + } + else + { + array^ ret = gcnew array(len); + if (len > 0) + { + for( int i = 0; i < len; i++) + { + Object^ obj = this->ReadObject(); + if(obj != nullptr) + ret[i] = static_cast(obj); + else + ret[i] = nullptr; + } + } + return ret; + } + } + + uint8_t* GetCursor() + { + return m_buffer + m_cursor; + } + + uint8_t* GetBytes(uint8_t* src, uint32_t size) + { + return NativePtr->getBufferCopyFrom(src, size); + } + + + void AdvanceUMCursor() + { + NativePtr->advanceCursor(m_cursor); + m_cursor = 0; + m_bufferLength = 0; + } + + void AdvanceCursorPdx(int offset) + { + m_cursor += offset; + } + + void RewindCursorPdx(int rewind) + { + m_cursor = 0; + } + + void ResetAndAdvanceCursorPdx(int offset) + { + m_cursor = offset; + } + + void ResetPdx(int offset) + { + NativePtr->reset(offset); + SetBuffer(); + } + + inline array^ ReadReverseBytesOnly(int len); + + void SetBuffer() + { + m_buffer = const_cast (NativePtr->currentBufferPosition()); + m_cursor = 0; + m_bufferLength = NativePtr->getBytesRemaining(); + } + + String^ DecodeBytes(int length) + { + //array^ output = gcnew array(length); + + if(m_forStringDecode->Length < length) + m_forStringDecode = gcnew array(length); + // index input[] + int i = 0; + // index output[] + int j = 0; + while ( i < length ) + { + // get next byte unsigned + //Byte b = m_buffer[ m_cursor++ ] & 0xff; + Byte b = ReadByte(); + i++; + Byte k = b >> 5; + // classify based on the high order 3 bits + switch ( k ) + { + default: + // one byte encoding + // 0xxxxxxx + // use just low order 7 bits + // 00000000 0xxxxxxx + m_forStringDecode[ j++ ] = ( Char ) ( b & 0x7f ); + break; + case 6: + { + // two byte encoding + // 110yyyyy 10xxxxxx + // use low order 6 bits + int y = b & 0x1f; + // use low order 6 bits of the next byte + // It should have high order bits 10, which we don't check. + int x = m_buffer[ m_cursor++ ] & 0x3f; + i++; + // 00000yyy yyxxxxxx + m_forStringDecode[ j++ ] = ( Char ) ( y << 6 | x ); + break; + } + case 7: + { + // three byte encoding + // 1110zzzz 10yyyyyy 10xxxxxx + //assert ( b & 0x10 ) + // == 0 : "UTF8Decoder does not handle 32-bit characters"; + // use low order 4 bits + int z = b & 0x0f; + // use low order 6 bits of the next byte + // It should have high order bits 10, which we don't check. + int y = m_buffer[ m_cursor++ ] & 0x3f; + i++; + // use low order 6 bits of the next byte + // It should have high order bits 10, which we don't check. + int x = m_buffer[ m_cursor++ ] & 0x3f; + i++; + // zzzzyyyy yyxxxxxx + int asint = ( z << 12 | y << 6 | x ); + m_forStringDecode[ j++ ] = ( Char ) asint; + break; + } + }// end switch + }// end while + + String^ str = gcnew String(m_forStringDecode, 0, j); + return str; + } + + void CheckBufferSize(int size); + + + Object^ ReadInternalGenericObject(); + + Object^ ReadInternalObject(); + + DataInput^ GetClone(); + + /// + /// Internal constructor to wrap a native object pointer + /// + /// The native object pointer + inline DataInput( gemfire::DataInput* nativeptr, bool managedObject ) + : UMWrap(nativeptr, false) + { + m_ispdxDesrialization = false; + m_isRootObjectPdx = false; + m_cursor = 0; + m_isManagedObject = managedObject; + m_forStringDecode = gcnew array(100); + m_buffer = const_cast(nativeptr->currentBufferPosition()); + if ( m_buffer != NULL) { + m_bufferLength = nativeptr->getBytesRemaining(); + } + else { + m_bufferLength = 0; + } + } + + DataInput( uint8_t* buffer, int size ); + + /* inline DataInput( gemfire::DataInput* nativeptr ) + : UMWrap(nativeptr, false) + { + m_cursor = 0; + m_isManagedObject = false; + m_buffer = const_cast(nativeptr->currentBufferPosition()); + if ( m_buffer != NULL) { + m_bufferLength = nativeptr->getBytesRemaining(); + } + else { + m_bufferLength = 0; + } + }*/ + + bool IsManagedObject() + { + return m_isManagedObject; + } + + int GetPdxBytes() + { + return m_bufferLength; + } + + private: + + /// + /// Internal buffer managed by the class. + /// This is freed in the disposer/destructor. + /// + bool m_ispdxDesrialization; + bool m_isRootObjectPdx; + uint8_t* m_buffer; + unsigned int m_bufferLength; + int m_cursor; + bool m_isManagedObject; + array^ m_forStringDecode; + + void Cleanup( ); + }; + } // end namespace generic + } + } +} http://git-wip-us.apache.org/repos/asf/geode/blob/2d4a7ecd/src/clicache/src/DataOutput.cpp ---------------------------------------------------------------------- diff --git a/src/clicache/src/DataOutput.cpp b/src/clicache/src/DataOutput.cpp new file mode 100644 index 0000000..8c00200 --- /dev/null +++ b/src/clicache/src/DataOutput.cpp @@ -0,0 +1,870 @@ +/*========================================================================= + * Copyright (c) 2002-2014 Pivotal Software, Inc. All Rights Reserved. + * This product is protected by U.S. and international copyright + * and intellectual property laws. Pivotal products are covered by + * more patents listed at http://www.pivotal.io/patents. + *========================================================================= + */ + +//#include "gf_includes.hpp" +#include "DataOutput.hpp" +#include +#include + +#include "IGFSerializable.hpp" +#include "CacheableObjectArray.hpp" +#include "impl/PdxHelper.hpp" +#include "impl/PdxWrapper.hpp" +using namespace System; +using namespace System::Runtime::InteropServices; +using namespace gemfire; + +namespace GemStone +{ + namespace GemFire + { + namespace Cache + { + namespace Generic + { + + void DataOutput::WriteByte( Byte value ) + { + EnsureCapacity(1); + m_bytes[m_cursor++] = value; + } + + void DataOutput::WriteSByte( SByte value ) + { + EnsureCapacity(1); + m_bytes[m_cursor++] = value; + } + + void DataOutput::WriteBoolean( bool value ) + { + EnsureCapacity(1); + if (value) + m_bytes[m_cursor++] = 0x01; + else + m_bytes[m_cursor++] = 0x00; + } + + void DataOutput::WriteChar( Char value ) + { + EnsureCapacity(2); + m_bytes[m_cursor++] = (uint8_t)(value >> 8); + m_bytes[m_cursor++] = (uint8_t)value; + } + + void DataOutput::WriteBytes( array^ bytes, int32_t len ) + { + if (bytes != nullptr && bytes->Length >= 0) + { + if ( len >= 0 && len <= bytes->Length ) + { + WriteArrayLen(len); + EnsureCapacity(len); + for( int i = 0; i < len; i++ ) + m_bytes[m_cursor++] = bytes[i]; + } + else + { + throw gcnew IllegalArgumentException("DataOutput::WriteBytes argument len is not in byte array range." ); + } + } + else + { + WriteByte(0xFF); + } + } + + void DataOutput::WriteArrayLen( int32_t len ) + { + if (len == -1) {//0xff + WriteByte(0xFF); + } else if (len <= 252) { // 252 is java's ((byte)-4 && 0xFF) or 0xfc + WriteByte((Byte)(len)); + } else if (len <= 0xFFFF) { + WriteByte(0xFE);//0xfe + WriteUInt16((len)); + } else { + WriteByte((0xFD));//0xfd + WriteUInt32(len); + } + } + + void DataOutput::WriteSBytes( array^ bytes, int32_t len ) + { + if (bytes != nullptr && bytes->Length >= 0) + { + if ( len >= 0 && len <= bytes->Length ) + { + WriteArrayLen(len); + EnsureCapacity(len); + for( int i = 0; i < len; i++ ) + m_bytes[m_cursor++] = bytes[i]; + } + else + { + throw gcnew IllegalArgumentException("DataOutput::WriteSBytes argument len is not in SByte array range." ); + } + } + else + { + WriteByte(0xFF); + } + } + + void DataOutput::WriteBytesOnly( array^ bytes, uint32_t len ) + { + WriteBytesOnly(bytes, len, 0); + } + + void DataOutput::WriteBytesOnly( array^ bytes, uint32_t len, uint32_t offset ) + { + if (bytes != nullptr) + { + if ( len >= 0 && len <= ((uint32_t)bytes->Length - offset) ) + { + EnsureCapacity(len); + for( uint32_t i = 0; i < len; i++ ) + m_bytes[m_cursor++] = bytes[offset + i]; + } + else + { + throw gcnew IllegalArgumentException("DataOutput::WriteBytesOnly argument len is not in Byte array range." ); + } + } + } + + void DataOutput::WriteSBytesOnly( array^ bytes, uint32_t len ) + { + if (bytes != nullptr) + { + if ( len >= 0 && len <= (uint32_t)bytes->Length ) + { + EnsureCapacity(len); + for( uint32_t i = 0; i < len; i++ ) + m_bytes[m_cursor++] = bytes[i]; + } + else + { + throw gcnew IllegalArgumentException("DataOutput::WriteSBytesOnly argument len is not in SByte array range." ); + } + } + } + + void DataOutput::WriteUInt16( uint16_t value ) + { + EnsureCapacity(2); + m_bytes[m_cursor++] = (uint8_t)(value >> 8); + m_bytes[m_cursor++] = (uint8_t)value; + } + + void DataOutput::WriteUInt32( uint32_t value ) + { + EnsureCapacity(4); + m_bytes[m_cursor++] = (uint8_t)(value >> 24); + m_bytes[m_cursor++] = (uint8_t)(value >> 16); + m_bytes[m_cursor++] = (uint8_t)(value >> 8); + m_bytes[m_cursor++] = (uint8_t)value; + } + + void DataOutput::WriteUInt64( uint64_t value ) + { + EnsureCapacity(8); + m_bytes[m_cursor++] = (uint8_t)(value >> 56); + m_bytes[m_cursor++] = (uint8_t)(value >> 48); + m_bytes[m_cursor++] = (uint8_t)(value >> 40); + m_bytes[m_cursor++] = (uint8_t)(value >> 32); + m_bytes[m_cursor++] = (uint8_t)(value >> 24); + m_bytes[m_cursor++] = (uint8_t)(value >> 16); + m_bytes[m_cursor++] = (uint8_t)(value >> 8); + m_bytes[m_cursor++] = (uint8_t)value; + } + + void DataOutput::WriteInt16( int16_t value ) + { + WriteUInt16(value); + } + + void DataOutput::WriteInt32( int32_t value ) + { + WriteUInt32(value); + } + + void DataOutput::WriteInt64( int64_t value ) + { + WriteUInt64(value); + } + + void DataOutput::WriteFloat( float value ) + { + array^ bytes = BitConverter::GetBytes(value); + EnsureCapacity(4); + for(int i = 4 - 1; i >=0 ; i--) + m_bytes[m_cursor++] = bytes[i]; + } + + void DataOutput::WriteDouble( double value ) + { + array^ bytes = BitConverter::GetBytes(value); + EnsureCapacity(8); + for(int i = 8 - 1; i >=0 ; i--) + m_bytes[m_cursor++] = bytes[i]; + } + + void DataOutput::WriteDictionary(System::Collections::IDictionary^ dict) + { + if(dict != nullptr) + { + this->WriteArrayLen(dict->Count); + for each( System::Collections::DictionaryEntry^ entry in dict) + { + this->WriteObject(entry->Key); + this->WriteObject(entry->Value); + } + } + else + { + WriteByte( (int8_t) -1); + } + } + + void DataOutput::WriteCollection(System::Collections::IList^ collection) + { + if(collection != nullptr) + { + this->WriteArrayLen(collection->Count); + for each (Object^ obj in collection) { + this->WriteObject(obj); + } + } + else + this->WriteByte((int8_t) -1); + } + + void DataOutput::WriteDate(System::DateTime date) + { + if(date.Ticks != 0L) + { + CacheableDate^ cd = gcnew CacheableDate(date); + cd->ToData(this); + } + else + this->WriteInt64(-1L); + } + + void DataOutput::WriteCharArray(array^ charArray) + { + if(charArray != nullptr) + { + this->WriteArrayLen(charArray->Length); + for(int i = 0; i < charArray->Length; i++) { + this->WriteObject(charArray[i]); + } + } + else + this->WriteByte((int8_t) -1); + } + + void DataOutput::WriteObjectArray(List^ objectArray) + { + if(objectArray != nullptr) + { + CacheableObjectArray^ coa = CacheableObjectArray::Create(objectArray); + coa->ToData(this); + } + else + this->WriteByte((int8_t) -1); + } + + void DataOutput::WriteDotNetObjectArray(Object^ objectArray) + { + System::Collections::IList^ list = (System::Collections::IList^)objectArray; + this->WriteArrayLen(list->Count); + WriteByte((int8_t)gemfire::GemfireTypeIdsImpl::Class); + String^ pdxDomainClassname = Serializable::GetPdxTypeName(objectArray->GetType()->GetElementType()->FullName); + WriteByte((int8_t)gemfire::GemfireTypeIds::CacheableASCIIString); + WriteUTF(pdxDomainClassname); + for each(Object^ o in list) + WriteObject(o); + } + + void DataOutput::WriteArrayOfByteArrays(array^>^ byteArrays) + { + if(byteArrays != nullptr) + { + int fdLen = byteArrays->Length; + this->WriteArrayLen(byteArrays->Length); + for(int i = 0; i < fdLen; i++) { + this->WriteBytes(byteArrays[i]); + } + } + else + this->WriteByte((int8_t) -1); + } + + void DataOutput::WriteUTF( String^ value ) + { + if (value != nullptr) { + int len = getEncodedLength(value); + + if (len > 0xffff) + len = 0xffff; + + WriteUInt16(len); + EnsureCapacity(len); + EncodeUTF8String(value, len); + } + else { + WriteUInt16(0); + } + } + + void DataOutput::WriteStringWithType( String^ value ) + { + //value will not be null + int len = getEncodedLength(value); + + if (len > 0xffff) + { + if(len == value->Length)//huge ascii + { + WriteByte(GemfireTypeIds::CacheableASCIIStringHuge); + WriteASCIIHuge(value); + } + else//huge utf + { + WriteByte(GemfireTypeIds::CacheableStringHuge); + WriteUTFHuge(value); + } + return; + } + + if(len == value->Length) + { + WriteByte(GemfireTypeIds::CacheableASCIIString);//ascii string + } + else + { + WriteByte(GemfireTypeIds::CacheableString);//utf string + } + WriteUInt16(len); + EnsureCapacity(len); + EncodeUTF8String(value, len); + + } + + void DataOutput::WriteASCIIHuge( String^ value ) + { + if (value != nullptr) { + const int strLength = value->Length; + WriteUInt32(strLength); + EnsureCapacity(strLength); + for ( int i = 0; i < strLength; i++ ) { + m_bytes[m_cursor++] = (uint8_t)value[i]; + } + } + else { + WriteUInt32(0); + } + } + + /* Write UTF-16 */ + void DataOutput::WriteUTFHuge( String^ value ) + { + if (value != nullptr) { + WriteUInt32(value->Length); + EnsureCapacity(value->Length * 2); + for( int i = 0; i < value->Length; i++) + { + Char ch = value[i]; + m_bytes[m_cursor++] = (Byte)((ch & 0xff00) >> 8); + m_bytes[m_cursor++] = (Byte)(ch & 0xff); + } + } + else { + WriteUInt32(0); + } + } + + /*void DataOutput::WriteObject( Object^ obj ) + { + WriteObjectInternal((IGFSerializable^)obj); + }*/ + + /*void DataOutput::WriteObject( Object^ obj ) + { + WriteObject( (IGFSerializable^)obj ); + }*/ + + int8_t DataOutput::GetTypeId(uint32_t classId ) + { + if (classId >= 0x80000000) { + return (int8_t)((classId - 0x80000000) % 0x20000000); + } + else if (classId <= 0x7F) { + return (int8_t)GemfireTypeIdsImpl::CacheableUserData; + } + else if (classId <= 0x7FFF) { + return (int8_t)GemfireTypeIdsImpl::CacheableUserData2; + } + else { + return (int8_t)GemfireTypeIdsImpl::CacheableUserData4; + } + } + + int8_t DataOutput::DSFID(uint32_t classId) + { + // convention that [0x8000000, 0xa0000000) is for FixedIDDefault, + // [0xa000000, 0xc0000000) is for FixedIDByte, + // [0xc0000000, 0xe0000000) is for FixedIDShort + // and [0xe0000000, 0xffffffff] is for FixedIDInt + // Note: depends on fact that FixedIDByte is 1, FixedIDShort is 2 + // and FixedIDInt is 3; if this changes then correct this accordingly + if (classId >= 0x80000000) { + return (int8_t)((classId - 0x80000000) / 0x20000000); + } + return 0; + } + + void DataOutput::WriteObject(Object^ obj) + { + + if ( obj == nullptr ) + { + WriteByte( (int8_t) GemfireTypeIds::NullObj ); + return; + } + + if(m_ispdxSerialization && obj->GetType()->IsEnum) + { + //need to set + int enumVal = Internal::PdxHelper::GetEnumValue(obj->GetType()->FullName, Enum::GetName(obj->GetType(), obj), obj->GetHashCode()); + WriteByte(GemFireClassIds::PDX_ENUM); + WriteByte(enumVal >> 24); + WriteArrayLen(enumVal & 0xFFFFFF); + return; + } + + //GemStone::GemFire::Cache::Generic::Log::Debug("DataOutput::WriteObject " + obj); + + Byte typeId = GemStone::GemFire::Cache::Generic::Serializable::GetManagedTypeMappingGeneric(obj->GetType()); + + switch(typeId) + { + case gemfire::GemfireTypeIds::CacheableByte: + { + WriteByte(typeId); + WriteSByte((SByte)obj); + return; + } + case gemfire::GemfireTypeIds::CacheableBoolean: + { + WriteByte(typeId); + WriteBoolean((bool)obj); + return; + } + case gemfire::GemfireTypeIds::CacheableWideChar: + { + WriteByte(typeId); + WriteObject((Char)obj); + return; + } + case gemfire::GemfireTypeIds::CacheableDouble: + { + WriteByte(typeId); + WriteDouble((Double)obj); + return; + } + case gemfire::GemfireTypeIds::CacheableASCIIString: + { + //CacheableString^ cStr = CacheableString::Create((String^)obj); + //// TODO: igfser mapping between generic and non generic + //WriteObjectInternal(cStr); + WriteStringWithType((String^)obj); + return; + } + case gemfire::GemfireTypeIds::CacheableFloat: + { + WriteByte(typeId); + WriteFloat((float)obj); + return; + } + case gemfire::GemfireTypeIds::CacheableInt16: + { + WriteByte(typeId); + WriteInt16((Int16)obj); + return; + } + case gemfire::GemfireTypeIds::CacheableInt32: + { + WriteByte(typeId); + WriteInt32((Int32)obj); + return; + } + case gemfire::GemfireTypeIds::CacheableInt64: + { + WriteByte(typeId); + WriteInt64((Int64)obj); + return; + } + case gemfire::GemfireTypeIds::CacheableDate: + { + //CacheableDate^ cd = gcnew CacheableDate((DateTime)obj); + // TODO: igfser mapping between generic and non generic + //WriteObjectInternal(cd); + WriteByte(typeId); + WriteDate((DateTime)obj); + return; + } + case gemfire::GemfireTypeIds::CacheableBytes: + { + WriteByte(typeId); + WriteBytes((array^)obj); + return; + } + case gemfire::GemfireTypeIds::CacheableDoubleArray: + { + WriteByte(typeId); + WriteObject((array^)obj); + return; + } + case gemfire::GemfireTypeIds::CacheableFloatArray: + { + WriteByte(typeId); + WriteObject((array^)obj); + return; + } + case gemfire::GemfireTypeIds::CacheableInt16Array: + { + WriteByte(typeId); + WriteObject((array^)obj); + return; + } + case gemfire::GemfireTypeIds::CacheableInt32Array: + { + WriteByte(typeId); + WriteObject((array^)obj); + return; + } + case gemfire::GemfireTypeIds::CacheableInt64Array: + { + WriteByte(typeId); + WriteObject((array^)obj); + return; + } + case gemfire::GemfireTypeIds::BooleanArray: + { + WriteByte(typeId); + WriteObject((array^)obj); + return; + } + case gemfire::GemfireTypeIds::CharArray: + { + WriteByte(typeId); + WriteObject((array^)obj); + return; + } + case gemfire::GemfireTypeIds::CacheableStringArray: + { + WriteByte(typeId); + WriteObject((array^)obj); + return; + } + case gemfire::GemfireTypeIds::CacheableHashTable: + case gemfire::GemfireTypeIds::CacheableHashMap: + case gemfire::GemfireTypeIds::CacheableIdentityHashMap: + { + WriteByte(typeId); + WriteDictionary((System::Collections::IDictionary^)obj); + return; + } + case gemfire::GemfireTypeIds::CacheableVector: + { + //CacheableVector^ cv = gcnew CacheableVector((System::Collections::IList^)obj); + //// TODO: igfser mapping between generic and non generic + //WriteObjectInternal(cv); + WriteByte(gemfire::GemfireTypeIds::CacheableVector); + WriteList((System::Collections::IList^)obj); + return; + } + case gemfire::GemfireTypeIds::CacheableLinkedList: + { + //CacheableArrayList^ cal = gcnew CacheableArrayList((System::Collections::IList^)obj); + //// TODO: igfser mapping between generic and non generic + //WriteObjectInternal(cal); + WriteByte(gemfire::GemfireTypeIds::CacheableLinkedList); + System::Collections::ICollection^ linkedList = (System::Collections::ICollection^)obj; + this->WriteArrayLen(linkedList->Count); + for each (Object^ o in linkedList) + this->WriteObject(o); + return; + } + case gemfire::GemfireTypeIds::CacheableArrayList: + { + //CacheableArrayList^ cal = gcnew CacheableArrayList((System::Collections::IList^)obj); + //// TODO: igfser mapping between generic and non generic + //WriteObjectInternal(cal); + WriteByte(gemfire::GemfireTypeIds::CacheableArrayList); + WriteList((System::Collections::IList^)obj); + return; + } + case gemfire::GemfireTypeIds::CacheableStack: + { + CacheableStack^ cs = gcnew CacheableStack((System::Collections::ICollection^)obj); + // TODO: igfser mapping between generic and non generic + WriteObjectInternal(cs); + return; + } + default: + { + IPdxSerializable^ pdxObj = dynamic_cast(obj); + if(pdxObj != nullptr) + { + WriteByte(GemFireClassIds::PDX); + Internal::PdxHelper::SerializePdx(this, pdxObj); + return; + } + else + { + //pdx serialization and is array of object + if(m_ispdxSerialization && obj->GetType()->IsArray) + { + WriteByte(gemfire::GemfireTypeIds::CacheableObjectArray); + WriteDotNetObjectArray(obj); + return; + } + + IGFSerializable^ ct = dynamic_cast(obj); + if(ct != nullptr) { + WriteObjectInternal(ct); + return ; + } + + if(Serializable::IsObjectAndPdxSerializerRegistered(nullptr)) + { + pdxObj = gcnew PdxWrapper(obj); + WriteByte(GemFireClassIds::PDX); + Internal::PdxHelper::SerializePdx(this, pdxObj); + return; + } + } + + throw gcnew System::Exception("DataOutput not found appropriate type to write it for object: " + obj->GetType()); + } + } + } + + void DataOutput::WriteStringArray(array^ strArray) + { + if(strArray != nullptr) + { + this->WriteArrayLen(strArray->Length); + for(int i = 0; i < strArray->Length; i++) + { + // this->WriteUTF(strArray[i]); + WriteObject(strArray[i]); + } + } + else + WriteByte(-1); + } + + void DataOutput::WriteObjectInternal( IGFSerializable^ obj ) + { + //CacheableKey^ key = gcnew CacheableKey(); + if ( obj == nullptr ) { + WriteByte( (int8_t) GemfireTypeIds::NullObj ); + } else { + int8_t typeId = DataOutput::GetTypeId( obj->ClassId); + switch (DataOutput::DSFID(obj->ClassId)) { + case GemfireTypeIdsImpl::FixedIDByte: + WriteByte((int8_t)GemfireTypeIdsImpl::FixedIDByte); + WriteByte( typeId ); // write the type ID. + break; + case GemfireTypeIdsImpl::FixedIDShort: + WriteByte((int8_t)GemfireTypeIdsImpl::FixedIDShort); + WriteInt16( (int16_t)typeId ); // write the type ID. + break; + case GemfireTypeIdsImpl::FixedIDInt: + WriteByte((int8_t)GemfireTypeIdsImpl::FixedIDInt); + WriteInt32( (int32_t)typeId ); // write the type ID. + break; + default: + WriteByte( typeId ); // write the type ID. + break; + } + + if ( (int32_t)typeId == GemfireTypeIdsImpl::CacheableUserData ) { + WriteByte( (int8_t) obj->ClassId ); + } else if ( (int32_t)typeId == GemfireTypeIdsImpl::CacheableUserData2 ) { + WriteInt16( (int16_t) obj->ClassId ); + } else if ( (int32_t)typeId == GemfireTypeIdsImpl::CacheableUserData4 ) { + WriteInt32( (int32_t) obj->ClassId ); + } + obj->ToData( this ); // let the obj serialize itself. + } + } + + void DataOutput::AdvanceCursor( uint32_t offset ) + { + EnsureCapacity(offset); + m_cursor += offset; + } + + void DataOutput::RewindCursor( uint32_t offset ) + { + //first set native one + WriteBytesToUMDataOutput(); + NativePtr->rewindCursor(offset); + SetBuffer(); + } + + array^ DataOutput::GetBuffer( ) + { + WriteBytesToUMDataOutput(); + SetBuffer(); + + int buffLen = NativePtr->getBufferLength(); + array^ buffer = gcnew array( buffLen ); + + if ( buffLen > 0 ) { + pin_ptr pin_buffer = &buffer[ 0 ]; + memcpy( (void*)pin_buffer, NativePtr->getBuffer( ), buffLen ); + } + return buffer; + } + + uint32_t DataOutput::BufferLength::get( ) + { + //first set native one + WriteBytesToUMDataOutput(); + SetBuffer(); + + return NativePtr->getBufferLength(); + } + + void DataOutput::Reset( ) + { + WriteBytesToUMDataOutput(); + NativePtr->reset(); + SetBuffer(); + } + + void DataOutput::WriteString(String^ value) + { + if(value == nullptr) + { + this->WriteByte(GemfireTypeIds::CacheableNullString); + } + else + { + WriteObject(value); + /*CacheableString^ cs = gcnew CacheableString(value); + + this->WriteByte( (Byte)(cs->ClassId - 0x80000000)); + cs->ToData(this);*/ + } + } + + void DataOutput::WriteBytesToUMDataOutput() + { + NativePtr->advanceCursor(m_cursor); + m_cursor = 0; + m_remainingBufferLength = 0; + m_bytes = nullptr; + } + + void DataOutput::WriteObject(bool% obj) + { + WriteBoolean(obj); + } + + void DataOutput::WriteObject(Byte% obj) + { + WriteByte(obj); + } + + void DataOutput::WriteObject(Char% obj) + { + unsigned short us = (unsigned short)obj; + EnsureCapacity(2); + m_bytes[m_cursor++] = us >> 8; + m_bytes[m_cursor++] = (Byte)us; + } + + void DataOutput::WriteObject(Double% obj) + { + WriteDouble(obj); + } + + void DataOutput::WriteObject(Single% obj) + { + WriteFloat(obj); + } + + void DataOutput::WriteObject(int16_t% obj) + { + WriteInt16(obj); + } + + void DataOutput::WriteObject(int32_t% obj) + { + WriteInt32(obj); + } + + void DataOutput::WriteObject(int64_t% obj) + { + WriteInt64(obj); + } + + void DataOutput::WriteObject(UInt16% obj) + { + WriteUInt16(obj); + } + + void DataOutput::WriteObject(UInt32% obj) + { + WriteUInt32(obj); + } + + void DataOutput::WriteObject(UInt64% obj) + { + WriteUInt64(obj); + } + + void DataOutput::WriteBooleanArray(array^ boolArray) + { + WriteObject(boolArray); + } + + void DataOutput::WriteShortArray(array^ shortArray) + { + WriteObject(shortArray); + } + + void DataOutput::WriteIntArray(array^ intArray) + { + WriteObject(intArray); + } + + void DataOutput::WriteLongArray(array^ longArray) + { + WriteObject(longArray); + } + + void DataOutput::WriteFloatArray(array^ floatArray) + { + WriteObject(floatArray); + } + + void DataOutput::WriteDoubleArray(array^ doubleArray) + { + WriteObject(doubleArray); + } + } // end namespace generic + } + } +} http://git-wip-us.apache.org/repos/asf/geode/blob/2d4a7ecd/src/clicache/src/DataOutput.hpp ---------------------------------------------------------------------- diff --git a/src/clicache/src/DataOutput.hpp b/src/clicache/src/DataOutput.hpp new file mode 100644 index 0000000..c3ef94c --- /dev/null +++ b/src/clicache/src/DataOutput.hpp @@ -0,0 +1,600 @@ +/*========================================================================= + * Copyright (c) 2002-2014 Pivotal Software, Inc. All Rights Reserved. + * This product is protected by U.S. and international copyright + * and intellectual property laws. Pivotal products are covered by + * more patents listed at http://www.pivotal.io/patents. + *========================================================================= + */ + +#pragma once + +#include "gf_defs.hpp" +#include +//#include "impl/NativeWrapper.hpp" +#include "Log.hpp" +#include "ExceptionTypes.hpp" +#include "Serializable.hpp" + +#include "CacheableString.hpp" +#include "CacheableDate.hpp" +#include "CacheableVector.hpp" +#include "CacheableArrayList.hpp" +#include "CacheableStack.hpp" + +using namespace System; +using namespace System::Runtime::CompilerServices; + +namespace GemStone +{ + namespace GemFire + { + namespace Cache + { + namespace Generic + { + + interface class IGFSerializable; + + /// + /// Provides operations for writing primitive data values, and + /// user-defined objects implementing IGFSerializable, to a byte stream. + /// This class is intentionally not thread safe. + /// + public ref class DataOutput sealed + : public Generic::Internal::UMWrap + { + private: + int32_t m_cursor; + bool m_isManagedObject; + uint8_t * m_bytes; + int32_t m_remainingBufferLength; + bool m_ispdxSerialization; + public: + + /// + /// Default constructor. + /// + inline DataOutput( ) + : UMWrap( new gemfire::DataOutput( ), true ) + { + m_isManagedObject = true; + m_cursor = 0; + m_bytes = const_cast(NativePtr->getCursor()); + m_remainingBufferLength = (int32_t)NativePtr->getRemainingBufferLength(); + m_ispdxSerialization = false; + } + + /// + /// Write length of the array to the DataOutput. + /// + /// Array len to write. + void WriteArrayLen( int32_t len ); + + /// + /// Write a signed byte to the DataOutput. + /// + /// The signed byte to write. + void WriteSByte( SByte value ); + + /// + /// Write a boolean value to the DataOutput. + /// + /// The boolean value to write. + void WriteBoolean( bool value ); + + /// + /// Write a char value to the DataOutput. + /// + /// The char value to write. + void WriteChar( Char value ); + + /// + /// Write a given length of bytes to the DataOutput. + /// + /// The array of bytes to write. + /// + /// The number of bytes from the start of array to write. + /// + void WriteBytes( array^ bytes, int32_t len ); + + /// + /// Write an array of bytes to the DataOutput. + /// + /// The array of bytes to write. + inline void WriteBytes( array^ bytes ) + { + WriteBytes( bytes, ( bytes == nullptr ? -1 : bytes->Length ) ); + } + + /// + /// Write a given length of signed bytes to the DataOutput. + /// + /// The array of signed bytes to write. + /// + /// The number of bytes from the start of array to write. + /// + void WriteSBytes( array^ bytes, int32_t len ); + + /// + /// Write an array of signed bytes to the DataOutput. + /// + /// The array of signed bytes to write. + inline void WriteSBytes( array^ bytes ) + { + WriteSBytes( bytes, ( bytes == nullptr ? -1 : bytes->Length ) ); + } + + /// + /// Write a given length of bytes without its length to the + /// DataOutput. + /// + /// The array of bytes to write. + /// + /// The number of bytes from the start of array to write. + /// + void WriteBytesOnly( array^ bytes, uint32_t len ); + + void WriteBytesOnly( array^ bytes, uint32_t len, uint32_t offset ); + + /// + /// Write an array of bytes without its length to the + /// DataOutput. + /// + /// The array of bytes to write. + inline void WriteBytesOnly( array^ bytes ) + { + WriteBytesOnly( bytes, ( bytes == nullptr ? 0 : bytes->Length ) ); + } + + /// + /// Write a given length of signed bytes without its length + /// to the DataOutput. + /// + /// The array of signed bytes to write. + /// + /// The number of bytes from the start of array to write. + /// + void WriteSBytesOnly( array^ bytes, uint32_t len ); + + /// + /// Write an array of signed bytes without its length + /// to the DataOutput. + /// + /// The array of signed bytes to write. + inline void WriteSBytesOnly( array^ bytes ) + { + WriteSBytesOnly( bytes, ( bytes == nullptr ? 0 : bytes->Length ) ); + } + + /// + /// Write a 16-bit integer to the DataOutput. + /// + /// The 16-bit integer to write. + void WriteInt16( int16_t value ); + + /// + /// Write a 32-bit integer to the DataOutput. + /// + /// The 32-bit integer to write. + void WriteInt32( int32_t value ); + + /// + /// Write a 64-bit integer to the DataOutput. + /// + /// The 64-bit integer to write. + void WriteInt64( int64_t value ); + + /// + /// Write a float to the DataOutput. + /// + /// The float value to write. + void WriteFloat( float value ); + + /// + /// Write a double precision real number to the DataOutput. + /// + /// + /// The double precision real number to write. + /// + void WriteDouble( double value ); + + /// + /// Write a string using java-modified UTF-8 encoding to + /// DataOutput. + /// The maximum length supported is 2^16-1 beyond which the string + /// shall be truncated. + /// + /// The UTF encoded string to write. + void WriteUTF( String^ value ); + + /// + /// Write a string using java-modified UTF-8 encoding to + /// DataOutput. + /// Length should be more than 2^16 -1. + /// + /// The UTF encoded string to write. + void WriteUTFHuge( String^ value ); + + /// + /// Write a string(only ASCII char) to + /// DataOutput. + /// Length should be more than 2^16 -1. + /// + /// The UTF encoded string to write. + void WriteASCIIHuge( String^ value ); + + /// + /// Write an IGFSerializable object to the DataOutput. + /// + /// The object to write. + // void WriteObject( IGFSerializable^ obj ); + + /// + /// Write a Serializable object to the DataOutput. + /// This is provided to conveniently pass primitive types (like string) + /// that shall be implicitly converted to corresponding + /// IGFSerializable wrapper types. + /// + /// The object to write. + void WriteObject( Object^ obj ); + + /// + /// Advance the buffer cursor by the given offset. + /// + /// + /// The offset by which to advance the cursor. + /// + void AdvanceCursor( uint32_t offset ); + + /// + /// Rewind the buffer cursor by the given offset. + /// + /// + /// The offset by which to rewind the cursor. + /// + void RewindCursor( uint32_t offset ); + + /// + /// Get a copy of the current buffer. + /// + array^ GetBuffer( ); + + /// + /// Get the length of current data in the buffer. + /// + property uint32_t BufferLength + { + uint32_t get( ); + } + + /// + /// Reset the cursor to the start of the buffer. + /// + void Reset( ); + + /// + /// Get the underlying native unmanaged pointer. + /// + property IntPtr NativeIntPtr + { + inline IntPtr get() + { + return IntPtr(_NativePtr); + } + } + + /// + /// Write a Dictionary to the DataOutput. + /// + /// The object which implements IDictionary to write. + void WriteDictionary(System::Collections::IDictionary^ value); + + /// + /// Write a date to the DataOutput. + /// + /// The date value to write. + void WriteDate(System::DateTime value); + + /// + /// Write a collection to the DataOutput. + /// + /// The object which implements IList to write. + void WriteCollection(System::Collections::IList^ value); + + /// + /// Write a char array to the DataOutput. + /// + /// The char array to write. + void WriteCharArray(array^ value); + + /// + /// Write a bool array to the DataOutput. + /// + /// The bool array to write. + void WriteBooleanArray(array^ value); + + /// + /// Write a short array to the DataOutput. + /// + /// The short array to write. + void WriteShortArray(array^ value); + + /// + /// Write a int array to the DataOutput. + /// + /// The int array to write. + void WriteIntArray(array^ value); + + /// + /// Write a long array to the DataOutput. + /// + /// The long array to write. + void WriteLongArray(array^ value); + + /// + /// Write a float array to the DataOutput. + /// + /// The float array to write. + void WriteFloatArray(array^ value); + + /// + /// Write a double array to the DataOutput. + /// + /// The double array to write. + void WriteDoubleArray(array^ value); + + /// + /// Write a object array to the DataOutput. + /// + /// The object array to write. + void WriteObjectArray(List^ value); + + /// + /// Write a array of sign byte array to the DataOutput. + /// + /// The array of sign byte array to write. + void WriteArrayOfByteArrays(array^>^ value); + + internal: + + void WriteDotNetObjectArray(Object^ objectArray); + + /// + /// Write a byte to the DataOutput. + /// + /// The byte to write. + void WriteByte( Byte value ); + + /// + /// Write an unsigned short integer (int16_t) to the DataOutput. + /// + /// The unsigned 16-bit integer to write. + void WriteUInt16( uint16_t value ); + + /// + /// Write an unsigned 32-bit integer to the DataOutput. + /// + /// The unsigned 32-bit integer to write. + void WriteUInt32( uint32_t value ); + + /// + /// Write an unsigned 64-bit integer to the DataOutput. + /// + /// The unsigned 64-bit integer to write. + void WriteUInt64( uint64_t value ); + + + int32_t GetBufferLengthPdx() + { + return (int32_t)NativePtr->getBufferLength(); + } + + void WriteString(String^ value); + + int32_t GetCursorPdx() + { + return m_cursor; + } + + const char * GetPoolName() + { + return _NativePtr->getPoolName(); + } + + void WriteStringArray(array^ strArray); + + void EncodeUTF8String( String^ input, int encLength ) + { + const int strLength = input->Length; + const int end = m_cursor + encLength; + for ( int i = 0; i < strLength && m_cursor < end; i++ ) + { + unsigned short c = (unsigned short)input[i]; + + if( c == 0 ) + { + m_bytes[m_cursor++] = 0xc0; + m_bytes[m_cursor++] = 0x80; + } + else if ( c < 0x80 )//ASCII character + { + // 7-bits done in one byte. + m_bytes[m_cursor++] = (uint8_t)c; + } + else if ( c < 0x800 ) + { + // 8-11 bits done in 2 bytes + m_bytes[m_cursor++] = ( 0xC0 | c >> 6 ); + m_bytes[m_cursor++] = ( 0x80 | c & 0x3F ); + } + else + { + // 12-16 bits done in 3 bytes + m_bytes[m_cursor++] = ( 0xE0 | c >> 12 ); + m_bytes[m_cursor++] = ( 0x80 | c >> 6 & 0x3F ); + m_bytes[m_cursor++] = ( 0x80 | c & 0x3F ); + } + } + + // TODO ASSERT end = m_cursor + } + + static int getEncodedLength(String^ input) + { + int count = 0; + for ( int i = 0; i < input->Length; i++ ) + { + unsigned short c = (unsigned short)input[i]; + + if( c == 0) + { + count += 2; + } + else if ( c < 0x80 )//ASCII character + { + count++; + } + else if ( c < 0x800 ) + { + count += 2; + } + else + { + count += 3; + } + }// end for + + return count; + } + + void setPdxSerialization(bool val) + { + m_ispdxSerialization = val; + } + + void WriteStringWithType( String^ value ); + + static int8_t GetTypeId(uint32_t classId ); + + static int8_t DSFID(uint32_t classId); + + void WriteObjectInternal( IGFSerializable^ obj ); + + void WriteBytesToUMDataOutput(); + + void WriteObject(bool% obj); + + void WriteObject(Byte% obj); + + void WriteObject(Char% obj); + + void WriteObject(Double% obj); + + void WriteObject(Single% obj); + + void WriteObject(int16_t% obj); + + void WriteObject(int32_t% obj); + + void WriteObject(int64_t% obj); + + void WriteObject(UInt16% obj); + + void WriteObject(UInt32% obj); + + void WriteObject(UInt64% obj); + + // void WriteObject(array^ objArray); + //void WriteObject(array^ objArray); + //void WriteObject(array^ objArray); + + + template + void WriteObject(array^ %objArray) + { + if(objArray != nullptr) { + int arrayLen = objArray->Length; + WriteArrayLen(arrayLen); + if(arrayLen > 0) { + int i = 0; + for( i = 0; i < arrayLen; i++ ){ + WriteObject(objArray[i]); + } + } + } + else { + WriteByte(0xff); + } + } + + bool IsManagedObject() + { + //TODO:: + return m_isManagedObject; + } + + void SetBuffer() + { + m_cursor = 0; + m_bytes = const_cast(NativePtr->getCursor()); + m_remainingBufferLength = (int32_t)NativePtr->getRemainingBufferLength(); + } + + uint8_t* GetStartBufferPosition() + { + return const_cast( NativePtr->getBuffer());; + } + + inline void EnsureCapacity( int32_t size ) + { + int32_t bytesLeft = m_remainingBufferLength - m_cursor; + if ( bytesLeft < size ) { + try + { + NativePtr->ensureCapacity(m_cursor + size); + m_bytes = const_cast( NativePtr->getCursor()); + m_remainingBufferLength = (int32_t)NativePtr->getRemainingBufferLength(); + } + catch(gemfire::OutOfMemoryException ex ) + { + throw gcnew OutOfMemoryException(ex); + } + } + } + + //it expects list is not null + inline void WriteList(System::Collections::IList^ list) + { + this->WriteArrayLen(list->Count); + for each (Object^ obj in list) + this->WriteObject(obj); + } + + uint8_t* GetBytes(uint8_t* src, uint32_t size) + { + return NativePtr->getBufferCopyFrom(src, size); + } + + int32_t GetRemainingBufferLength() + { + return (int32_t) NativePtr->getRemainingBufferLength(); + } + + /// + /// Internal constructor to wrap a native object pointer + /// + /// The native object pointer + inline DataOutput( gemfire::DataOutput* nativeptr, bool managedObject ) + : UMWrap( nativeptr, false ) + { + m_isManagedObject = managedObject; + m_cursor = 0; + m_bytes = const_cast(nativeptr->getCursor()); + m_remainingBufferLength = (int32_t)nativeptr->getRemainingBufferLength(); + m_ispdxSerialization = false; + } + }; + } // end namespace generic + } + } +} http://git-wip-us.apache.org/repos/asf/geode/blob/2d4a7ecd/src/clicache/src/DiskPolicyType.hpp ---------------------------------------------------------------------- diff --git a/src/clicache/src/DiskPolicyType.hpp b/src/clicache/src/DiskPolicyType.hpp new file mode 100644 index 0000000..5484b6d --- /dev/null +++ b/src/clicache/src/DiskPolicyType.hpp @@ -0,0 +1,78 @@ +/*========================================================================= + * Copyright (c) 2002-2014 Pivotal Software, Inc. All Rights Reserved. + * This product is protected by U.S. and international copyright + * and intellectual property laws. Pivotal products are covered by + * more patents listed at http://www.pivotal.io/patents. + *========================================================================= + */ + +#pragma once + + + +#include "gf_defs.hpp" +#include + + +using namespace System; + +namespace GemStone +{ + namespace GemFire + { + namespace Cache { namespace Generic + { + + /// + /// Enumerated type for disk policy. + /// Contains values for setting the disk policy type. + /// + public enum class DiskPolicyType + { + /// No policy. + None = 0, + + /// Overflow to disk. + Overflows + }; + + + /// + /// Static class containing convenience methods for DiskPolicyType. + /// + /// + /// + public ref class DiskPolicy STATICCLASS + { + public: + + /// + /// True if the current policy is Overflows. + /// + inline static bool IsOverflow( DiskPolicyType type ) + { + return (type == DiskPolicyType::Overflows); + } + + /// + /// True if the current policy is None. + /// + inline static bool IsNone( DiskPolicyType type ) + { + return (type == DiskPolicyType::None); + } + + ///// + ///// True if the current policy is Persist. + ///// -- Persist is NOT YET IMPLEMENTED IN C++ + //inline static bool IsPersist( DiskPolicyType type ) + //{ + // return (type == DiskPolicyType::Persist); + //} + }; + + } + } +} + } //namespace +