コード例 #1
0
ファイル: SingleSerializer.cs プロジェクト: navirius/bitcare
        // Serialization
        public void Serialize(Single valueToSerialize)
        {
            // Is it default value
            if (valueToSerialize == 0)
            {
                SerializerStorage.WriteStorageFormat(new DefaultValue());
                return;
            }

            byte[] singleBytes = BitConverter.GetBytes(valueToSerialize);

            byte configByte = 0;

            byte[] tmpBytes       = new byte[4]; // 4 bytes of Single
            int    storedTmpBytes = 0;

            // Store bytes in buffer
            for (int pos = 0; pos < 4; pos++)
            {
                if (singleBytes[pos] > 0)
                {
                    configByte |= (byte)(1 << pos);              // If byte is different then 0
                    tmpBytes[storedTmpBytes] = singleBytes[pos]; // Copy byte to output list
                    storedTmpBytes++;
                }
            }

            byte[] packedBytes = new byte[storedTmpBytes];
            Array.Copy(tmpBytes, 0, packedBytes, 0, storedTmpBytes);

            SerializerStorage.WriteStorageFormat(new ValueInDataStream(configByte));
            SerializerStorage.WritePackedData(packedBytes);
        }
コード例 #2
0
        // Serialization of simple elements, that doesn't use caching
        private void SerializeInternal(T[] valueToSerialize, Action arraySerializeAction)
        {
            // Case: null value
            if (valueToSerialize == null)
            {
                SerializerStorage.WriteStorageFormat(new NullArray());
                return;
            }

            // Case: empty Array
            if (valueToSerialize.Length == 0)
            {
                SerializerStorage.WriteStorageFormat(new EmptyArray());
                return;
            }

            // Case: normal Array with Id
            SerializerStorage.WriteStorageFormat(new NormalArray());

            // Store array size
            Int32Serializer intSerializer = new Int32Serializer(SerializerStorage);

            intSerializer.Serialize(valueToSerialize.Length);

            // Serialization of array elements
            arraySerializeAction();
        }
コード例 #3
0
ファイル: DecimalSerializer.cs プロジェクト: navirius/bitcare
        // Serialization
        public void Serialize(Decimal valueToSerialize)
        {
            // Is it default value
            if (valueToSerialize == Decimal.Zero)
            {
                SerializerStorage.WriteStorageFormat(new DefaultValue());
                return;
            }

            // Is it negative value
            bool isNegativeValue = valueToSerialize < 0;

            if (isNegativeValue)
            {
                valueToSerialize *= (-1);
            }

            // Value different then default one
            int[] fourInt32Values = Decimal.GetBits(valueToSerialize);

            byte[] tmpDecimalBytes = new byte[16]; // 4 x Int32 bytes

            Array.Copy(BitConverter.GetBytes(fourInt32Values[0]), 0, tmpDecimalBytes, 0, 4);
            Array.Copy(BitConverter.GetBytes(fourInt32Values[1]), 0, tmpDecimalBytes, 4, 4);
            Array.Copy(BitConverter.GetBytes(fourInt32Values[2]), 0, tmpDecimalBytes, 8, 4);
            Array.Copy(BitConverter.GetBytes(fourInt32Values[3]), 0, tmpDecimalBytes, 12, 4);

            int outputBytesCounter = 0;

            // Count valid bytes
            for (int pos = 0; pos < 16; pos++)
            {
                if (tmpDecimalBytes[pos] > 0)
                {
                    outputBytesCounter++;
                }

                if (tmpDecimalBytes[pos] == 0)
                {
                    break;
                }
            }

            // Output buffer
            byte[] packedData = new byte[outputBytesCounter]; // stored decimal bytes
            Array.Copy(tmpDecimalBytes, 0, packedData, 0, outputBytesCounter);

            // Is it PositiveValueInDataStream storage case
            if (isNegativeValue)
            {
                SerializerStorage.WriteStorageFormat(new NegativeValueInDataStream((byte)packedData.Length));
            }
            else
            {
                // Has positive value
                SerializerStorage.WriteStorageFormat(new PositiveValueInDataStream((byte)packedData.Length));
            }

            SerializerStorage.WritePackedData(packedData);
        }
コード例 #4
0
        // Serialization of simple elements, that doesn't use caching
        public void Serialize(T[] valueToSerialize)
        {
            // Case: null value
            if (valueToSerialize == null)
            {
                SerializerStorage.WriteStorageFormat(new NullArray());
                return;
            }

            // Case: empty Array
            if (valueToSerialize.Length == 0)
            {
                SerializerStorage.WriteStorageFormat(new EmptyArray());
                return;
            }

            // Case: normal Array with Id
            SerializerStorage.WriteStorageFormat(new NormalArray());

            // Store array size
            Int32Serializer intSerializer = new Int32Serializer(SerializerStorage);

            intSerializer.Serialize(valueToSerialize.Length);

            // Serialization of array elements
            for (int pos = 0; pos < valueToSerialize.Length; pos++)
            {
                m_ElemSerializationAction(valueToSerialize[pos]);
            }
        }
コード例 #5
0
ファイル: ObjectSerializer.cs プロジェクト: navirius/bitcare
        // Serialization
        public void Serialize(Object valueToSerialize)
        {
            if (valueToSerialize != null)
            {
                SerializerStorage.WriteStorageFormat(new ObjectValue());
                return;
            }

            SerializerStorage.WriteStorageFormat(new NullValue());
        }
コード例 #6
0
        // Serialization
        public void Serialize(Double valueToSerialize)
        {
            // Is it default value
            if (valueToSerialize == 0)
            {
                SerializerStorage.WriteStorageFormat(new DefaultValue());
                return;
            }

            SerializerStorage.WriteStorageFormat(new ValueInDataStream());
            new Int64Serializer(SerializerStorage).Serialize(BitConverter.DoubleToInt64Bits(valueToSerialize));
        }
コード例 #7
0
        // Serialization
        public void Serialize(Boolean valueToSerialize)
        {
            // Is it True value
            if (valueToSerialize)
            {
                SerializerStorage.WriteStorageFormat(new TrueValue());
                return;
            }

            // False value
            SerializerStorage.WriteStorageFormat(new FalseValue());
        }
コード例 #8
0
ファイル: GuidSerializer.cs プロジェクト: navirius/bitcare
        // Serialization
        public void Serialize(Guid valueToSerialize)
        {
            // Is it default value
            if (valueToSerialize == Guid.Empty)
            {
                SerializerStorage.WriteStorageFormat(new DefaultValue());
                return;
            }

            SerializerStorage.WriteStorageFormat(new ValueInDataStream());

            byte[] guidBytes = BitToolkit.ConvertGuidToByteArray(valueToSerialize); // 16 bytes
            SerializerStorage.WritePackedData(guidBytes);
        }
コード例 #9
0
        // Serialization
        public void Serialize(SByte valueToSerialize)
        {
            // Is it default value
            if (valueToSerialize == 0)
            {
                SerializerStorage.WriteStorageFormat(new DefaultValue());
                return;
            }

            // We can store value as separate byte
            SerializerStorage.WriteStorageFormat(new ValueInDataStream());
            SerializerStorage.WritePackedData(new byte[1] {
                (byte)valueToSerialize
            });
        }
コード例 #10
0
ファイル: Int64Serializer.cs プロジェクト: navirius/bitcare
        // Serialization
        public void Serialize(Int64 valueToSerialize)
        {
            // Is it default value
            if (valueToSerialize == 0)
            {
                SerializerStorage.WriteStorageFormat(new DefaultValue());
                return;
            }

            // Should we store value in main data stream?
            if (valueToSerialize > 0 && valueToSerialize <= ValueInConfig.MaxValueToStoreInConfig)
            {
                // We can store value in config
                SerializerStorage.WriteStorageFormat(new ValueInConfig(valueToSerialize));
            }
            else
            {
                byte[] packedData = BitToolkit.ConvertInt64ToByteArray(valueToSerialize);
                SerializerStorage.WriteStorageFormat(new ValueInDataStream((byte)packedData.Length));
                SerializerStorage.WritePackedData(packedData);
            }
        }
コード例 #11
0
        public bool ShouldStoreFullData <T>(T valueToSerialize)
        {
            // Case: null value
            if (valueToSerialize == null)
            {
                SerializerStorage.WriteStorageFormat(new NullObject());
                return(false);
            }

            // If caching has been activated
            if (SerializerStorage.UseRefCaching)
            {
                // Regular string - we should obtain string Id from cache
                bool shouldStoreFullData = this.ObjectCache.GetObjectIdForRefTypeField(valueToSerialize, this);

                // If we should store full data
                if (shouldStoreFullData)
                {
                    SerializerStorage.WriteStorageFormat(new NormalObject()); // Case: normal object with Id
                }
                else
                {
                    SerializerStorage.WriteStorageFormat(new CachedObject()); // Case: cached object
                }
                // Object Id
                WKTSerializers.Int32.Serialize(this.ObjectId);
            }
            else
            {
                // Store value without caching
                SerializerStorage.WriteStorageFormat(new NormalObject());
            }

            // Should store full data...
            return(true);
        }
コード例 #12
0
ファイル: StringSerializer.cs プロジェクト: navirius/bitcare
        // Serialization
        public void Serialize(string valueToSerialize)
        {
            // Case: null value
            if (valueToSerialize == null)
            {
                SerializerStorage.WriteStorageFormat(new NullString());
                return;
            }

            // Case: empty string
            if (valueToSerialize == String.Empty)
            {
                SerializerStorage.WriteStorageFormat(new EmptyString());
                return;
            }

            // Int32 Serializer
            Int32Serializer int32Serializer = new Int32Serializer(SerializerStorage);

            // If caching has been activated
            if (SerializerStorage.UseValCaching)
            {
                // Regular string - we should obtain string Id from cache
                bool shouldStoreFullData = ObjectCache.GetObjectIdForValueTypeField(valueToSerialize, this);

                // If we should store full data
                if (shouldStoreFullData)
                {
                    // Case: normal string with Id
                    SerializerStorage.WriteStorageFormat(new NormalString());

                    // Store Id of string
                    int32Serializer.Serialize(this.ObjectId);

                    // Encode string to utf-8
                    byte[] stringData = Encoding.UTF8.GetBytes(valueToSerialize);

                    // Store string length
                    int32Serializer.Serialize(stringData.Length);

                    // Store string data
                    SerializerStorage.WritePackedData(stringData);
                }
                else
                {
                    // Case: cached string
                    SerializerStorage.WriteStorageFormat(new CachedString());

                    // Store Id of string
                    int32Serializer.Serialize(this.ObjectId);
                }
            }
            else
            {
                // Store value without caching
                SerializerStorage.WriteStorageFormat(new NormalString());

                // Encode string to utf-8
                byte[] stringData = Encoding.UTF8.GetBytes(valueToSerialize);

                // Store string length
                int32Serializer.Serialize(stringData.Length);

                // Store string data
                SerializerStorage.WritePackedData(stringData);
            }
        }