Esempio n. 1
0
        /// <inheritdoc />
        public override object GetData()
        {
            CheckCacheException();
            var binaryData = new BinaryFormatSerializationData(this);

            if (binaryData == null)
            {
                throw new Exception($"{nameof(BinaryFormatSerializationData)} generation failed");
            }
            return(binaryData.GetBytes());
        }
Esempio n. 2
0
        /// <inheritdoc />
        protected override bool ParseBytes(IEnumerable <byte> allBytes)
        {
            bool result = false;
            BinaryFormatSerializationData binaryFormatSerializationData;

            if (BinaryFormatSerializationData.TryParseBytes(allBytes, out binaryFormatSerializationData))
            {
                ClearData();
                foreach (var binaryDataItem in binaryFormatSerializationData.DataItems)
                {
                    AddDataItem(new BinaryFormatDataItem(binaryDataItem), false);
                }
                result = true;
            }
            return(result);
        }
        private static bool TryParseBytesManually(byte[] allBytes, out BinaryFormatSerializationData binaryFormatSerializationData)
        {
            Debug.Assert(allBytes != null, "Internal binary parse bytes pre-check for null failed");
            bool result = false;
            BinaryFormatSerializationData tempData = new BinaryFormatSerializationData();

            if (Header == BitConverter.ToInt16(allBytes, 0))
            {
                tempData.RecordsCount = BitConverter.ToUInt16(allBytes, sizeof(ushort));
                tempData.DataItems    = new List <BinaryFormatSerializationDataItem>();
                int parsedItemsSize = MinSize;
                if (tempData.RecordsCount > 0)
                {
                    for (int x = 0; x < tempData.RecordsCount; x++)
                    {
                        int itemSize;
                        BinaryFormatSerializationDataItem serializationDataItem;
                        if (BinaryFormatSerializationDataItem.TryParseBinarySerializationDataItem(allBytes, parsedItemsSize,
                                                                                                  out serializationDataItem, out itemSize))
                        {
                            tempData.DataItems.Add(serializationDataItem);
                            parsedItemsSize += itemSize;
                            if (x == tempData.RecordsCount - 1)
                            {
                                result = true;
                            }
                        }
                        else
                        {
                            tempData = null;
                            break;
                        }
                    }
                }
                else
                {
                    result = true;
                }
            }
            binaryFormatSerializationData = tempData;
            return(result);
        }
        public static bool TryParseBytes(IEnumerable <byte> allBytes, out BinaryFormatSerializationData binaryFormatSerializationData)
        {
            bool result = false;

            binaryFormatSerializationData = null;
            if (allBytes != null)
            {
                var byteArray = allBytes.ToArray();
                if (byteArray.Length >= MinSize)
                {
                    BinaryFormatSerializationData resultData;
                    result = TryParseBytesManually(byteArray, out resultData);
                    if (result)
                    {
                        binaryFormatSerializationData = resultData;
                    }
                }
            }
            if (!result)
            {
                throw new FormatException("Binary validation failed: format is invalid");
            }
            return(result);
        }