Пример #1
0
        // Generic deserialization
        private T[] DeserializeInternal(Action <int, T[]> arrayDeserializationAction)
        {
            // Read info about storage format
            ArrayStorageFormats format = (ArrayStorageFormats)SerializerStorage.ReadStorageFormatId(ArrayStorageBase.FormatIdSizeInBits);

            // Case: null Array
            if (format == ArrayStorageFormats.NullArray)
            {
                return(null);
            }

            // Case: empty Array
            if (format == ArrayStorageFormats.EmptyArray)
            {
                return(new T[0]);
            }

            // Restore array size
            Int32Serializer intSerializer = new Int32Serializer(SerializerStorage);
            Int32           arrayLength   = intSerializer.Deserialize();

            // Case: normal Array
            T[] outputArray = new T[arrayLength];

            // Deserialize array elements
            arrayDeserializationAction(arrayLength, outputArray);

            // Return result
            return(outputArray);
        }
Пример #2
0
        // Deserialization
        public Single Deserialize()
        {
            // Read info about storage format
            SingleStorageFormats format = (SingleStorageFormats)SerializerStorage.ReadStorageFormatId(SingleStorageBase.FormatIdSizeInBits);

            // Is it default value
            if (format == SingleStorageFormats.DefaultValue)
            {
                return(0);
            }

            // Deserialize full data

            // Read config byte - 4 bits for 4 bytes
            byte config = (byte)SerializerStorage.ReadStorageFormatData(4);

            // Single bytes
            byte[] singleBytes = new byte[4];

            // Conversion - we check out bits for all the bytes (4 bytes)
            for (int bitPos = 0; bitPos < 4; ++bitPos)
            {
                // If bit is set (has value 1) then we put this byte on proper position
                if ((config & (1 << bitPos)) > 0)
                {
                    singleBytes[bitPos] = SerializerStorage.ReadPackedDataByte();
                }
            }

            // Return result
            return(BitConverter.ToSingle(singleBytes, 0));
        }
Пример #3
0
        // Generic deserialization
        public T[] Deserialize()
        {
            // Read info about storage format
            ArrayStorageFormats format = (ArrayStorageFormats)SerializerStorage.ReadStorageFormatId(ArrayStorageBase.FormatIdSizeInBits);

            // Case: null Array
            if (format == ArrayStorageFormats.NullArray)
            {
                return(null);
            }

            // Case: empty Array
            if (format == ArrayStorageFormats.EmptyArray)
            {
                return(new T[0]);
            }

            // Restore array size
            Int32Serializer intSerializer = new Int32Serializer(SerializerStorage);
            Int32           arrayLength   = intSerializer.Deserialize();

            // Case: normal Array
            T[] outputArray = new T[arrayLength];

            // Deserialize all the elems
            for (int pos = 0; pos < arrayLength; pos++)
            {
                T value = m_ElemDeserializationFunc();
                outputArray[pos] = value;
            }

            // Return result
            return(outputArray);
        }
Пример #4
0
        // 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);
        }
Пример #5
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]);
            }
        }
Пример #6
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();
        }
Пример #7
0
        public bool ShouldLoadFullData <T>(ref T objectValue) where T : class
        {
            // Read info about storage format
            ComplexTypeStorageFormats format = (ComplexTypeStorageFormats)SerializerStorage.ReadStorageFormatId(ComplexTypeStorageBase.FormatIdSizeInBits);

            // Case: null object
            if (format == ComplexTypeStorageFormats.NullObject)
            {
                objectValue = null;
                return(false);
            }

            // If caching has been activated
            if (SerializerStorage.UseRefCaching)
            {
                // Deserialize object ID
                this.ObjectId = WKTSerializers.Int32.Deserialize();

                // Case: cached object
                if (format == ComplexTypeStorageFormats.CachedObject)
                {
                    objectValue = ObjectCache.GetObjectValueForRefTypeField <T>(this);
                    return(false);
                }
            }

            // Case: normal object - load full data
            return(true);
        }
Пример #8
0
        // 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);
        }
Пример #9
0
        // Deserialization
        public Int64 Deserialize()
        {
            // Read info about storage format
            Int64StorageFormats format = (Int64StorageFormats)SerializerStorage.ReadStorageFormatId(Int64StorageBase.FormatIdSizeInBits);

            // Is it default value
            if (format == Int64StorageFormats.DefaultValue)
            {
                return(0);
            }

            if (format == Int64StorageFormats.ValueInConfig)
            {
                ValueInConfig valInConfig = new ValueInConfig();
                valInConfig.FormatConfig.Bits = SerializerStorage.ReadStorageFormatData(ValueInConfig.UsedConfigBitsForValue);
                return((Int64)valInConfig.Value);
            }

            // Value stored in PackedData
            ValueInDataStream valInDataStream = new ValueInDataStream();

            valInDataStream.FormatConfig.Bits = SerializerStorage.ReadStorageFormatData(ValueInDataStream.UsedConfigBitsForCase);
            byte[] encodedValue = SerializerStorage.ReadPackedData(valInDataStream.PackedDataSize);

            // Return decoded value
            return(BitToolkit.ConvertByteArrayToInt64(encodedValue));
        }
Пример #10
0
        // Serialization
        public void Serialize(Object valueToSerialize)
        {
            if (valueToSerialize != null)
            {
                SerializerStorage.WriteStorageFormat(new ObjectValue());
                return;
            }

            SerializerStorage.WriteStorageFormat(new NullValue());
        }
Пример #11
0
        // Serialization
        public void Serialize(Boolean valueToSerialize)
        {
            // Is it True value
            if (valueToSerialize)
            {
                SerializerStorage.WriteStorageFormat(new TrueValue());
                return;
            }

            // False value
            SerializerStorage.WriteStorageFormat(new FalseValue());
        }
Пример #12
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));
        }
Пример #13
0
        // Deserialization
        public Object Deserialize()
        {
            // Read info about storage format
            ObjectStorageFormats format = (ObjectStorageFormats)SerializerStorage.ReadStorageFormatId(ObjectStorageBase.FormatIdSizeInBits);

            if (format == ObjectStorageFormats.ObjectValue)
            {
                return(new Object());
            }

            return(null);
        }
Пример #14
0
        // Deserialization
        public Boolean Deserialize()
        {
            // Read info about storage format
            BooleanStorageFormats format = (BooleanStorageFormats)SerializerStorage.ReadStorageFormatId(BooleanStorageBase.FormatIdSizeInBits);

            // Is it True value
            if (format == BooleanStorageFormats.TrueValue)
            {
                return(true);
            }

            // It's False value
            return(false);
        }
Пример #15
0
        // Deserialization
        public Decimal Deserialize()
        {
            // Read info about storage format
            DecimalStorageFormats format = (DecimalStorageFormats)SerializerStorage.ReadStorageFormatId(DecimalStorageBase.FormatIdSizeInBits);

            // Is it default value
            if (format == DecimalStorageFormats.DefaultValue)
            {
                return(Decimal.Zero);
            }

            // Size of data in buffer
            PositiveValueInDataStream positiveValueInDataStream = new PositiveValueInDataStream();

            positiveValueInDataStream.FormatConfig.Bits = SerializerStorage.ReadStorageFormatData(PositiveValueInDataStream.UsedConfigBitsForCase);
            byte packedDataSize = (byte)positiveValueInDataStream.PackedDataSize;

            // Data
            byte[] packedData = SerializerStorage.ReadPackedData(packedDataSize);

            // Buffer
            int[] decimalBuffer = new int[4];

            int intPos    = 0;
            int byteShift = 0;

            // Restore value
            for (int pos = 0; pos < packedDataSize; pos++)
            {
                decimalBuffer[intPos] |= packedData[pos] << byteShift;

                byteShift += 8;
                byteShift %= 8;

                if (byteShift == 0)
                {
                    intPos++;
                }
            }

            // Is it NegativeValueInDataStream) storage case
            if (format == DecimalStorageFormats.NegativeValueInDataStream)
            {
                return(new Decimal(decimalBuffer) * (-1));
            }

            // Is it PositiveValueInDataStream storage case
            // DecimalStorageFormats.PositiveValueInDataStream
            return(new Decimal(decimalBuffer));
        }
Пример #16
0
        // 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);
        }
Пример #17
0
        // Deserialization
        public Double Deserialize()
        {
            // Read info about storage format
            DoubleStorageFormats format = (DoubleStorageFormats)SerializerStorage.ReadStorageFormatId(DoubleStorageBase.FormatIdSizeInBits);

            // Is it default value
            if (format == DoubleStorageFormats.DefaultValue)
            {
                return(0);
            }

            // Deserialize full data
            return(BitConverter.Int64BitsToDouble(new Int64Serializer(SerializerStorage).Deserialize()));
        }
Пример #18
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
            });
        }
Пример #19
0
        // Deserialization
        public Guid Deserialize()
        {
            // Read info about storage format
            GuidStorageFormats format = (GuidStorageFormats)SerializerStorage.ReadStorageFormatId(GuidStorageBase.FormatIdSizeInBits);

            // Is it default value
            if (format == GuidStorageFormats.DefaultValue)
            {
                return(Guid.Empty);
            }

            // Deserialize full data
            byte[] guidBytes = SerializerStorage.ReadPackedData(16); // 16 bytes
            return(BitToolkit.ConvertByteArrayToGuid(guidBytes));
        }
Пример #20
0
        // Deserialization
        public SByte Deserialize()
        {
            // Read info about storage format
            ByteStorageFormats format = (ByteStorageFormats)SerializerStorage.ReadStorageFormatId(ByteStorageBase.FormatIdSizeInBits);

            // Is it default value
            if (format == ByteStorageFormats.DefaultValue)
            {
                return(0);
            }

            // Value stored in PackedData
            byte[] encodedValue = SerializerStorage.ReadPackedData(1);

            // Return decoded value
            return((SByte)encodedValue[0]);
        }
Пример #21
0
        // 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);
            }
        }
Пример #22
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);
        }
Пример #23
0
 public static bool TryDeserialize <T>(ref ReadOnlySequence <byte> input, T value, out long byteRead) =>
 SerializerStorage <T> .TryDeserialize(ref input, value, out byteRead);
Пример #24
0
        // Deserialization
        public string Deserialize()
        {
            // Read info about storage format
            StringStorageFormats format = (StringStorageFormats)SerializerStorage.ReadStorageFormatId(StringStorageBase.FormatIdSizeInBits);

            // Case: null string
            if (format == StringStorageFormats.NullString)
            {
                return(null);
            }

            // Case: empty string
            if (format == StringStorageFormats.EmptyString)
            {
                return(string.Empty);
            }

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

            // If caching has been activated
            if (SerializerStorage.UseValCaching)
            {
                // Case: cached string
                if (format == StringStorageFormats.CachedString)
                {
                    // Read object Id
                    this.ObjectId = int32Serializer.Deserialize();

                    // Take value from cache (objects dictionary)
                    return(ObjectCache.GetObjectValueForValueTypeField <String>(this));
                }

                // Case: new string
                if (format == StringStorageFormats.NormalString)
                {
                    this.ObjectId = int32Serializer.Deserialize();                                      // Read object Id
                    int    encodedStringLength = int32Serializer.Deserialize();                         // Read encoded string length
                    byte[] encodedString       = SerializerStorage.ReadPackedData(encodedStringLength); // Read encoded data

                    // Decode string
                    string result = Encoding.UTF8.GetString(encodedString, 0, encodedString.Length);

                    // Register new string in cache
                    ObjectCache.RegisterValue(result, this);

                    // Return result
                    return(result);
                }
            }
            else
            {
                // Value without caching (so without Id)
                int    encodedStringLength = int32Serializer.Deserialize();                         // Read encoded string length
                byte[] encodedString       = SerializerStorage.ReadPackedData(encodedStringLength); // Read encoded data

                // Decode string
                return(Encoding.UTF8.GetString(encodedString, 0, encodedString.Length));
            }

            // Default result
            return(null);
        }
Пример #25
0
        // 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);
            }
        }