public override object Decode(byte[] encoded, Type type)
        {
            if (!IsSupportedType(type))
            {
                throw new NotSupportedException(type + " is not supported");
            }
            var decoded = intTypeDecoder.DecodeInt(encoded);

            return(Convert.ToBoolean(decoded));
        }
Exemple #2
0
        protected virtual object DecodeDynamicElementType(byte[] encoded, Type type, int size)
        {
            var decodedListOutput = (IList)Activator.CreateInstance(type);

            if (decodedListOutput == null)
            {
                throw new Exception("Only types that implement IList<T> are supported to decode Array Types");
            }

            var elementType = GetIListElementType(type);

            if (elementType == null)
            {
                throw new Exception("Only types that implement IList<T> are supported to decode Array Types");
            }

            var intDecoder  = new IntTypeDecoder();
            var dataIndexes = new List <int>();

            var currentIndex = 0;

            while (currentIndex < size)
            {
                dataIndexes.Add(intDecoder.DecodeInt(encoded.Skip(currentIndex * 32).Take(32).ToArray()));
                currentIndex++;
            }

            currentIndex = 0;

            while (currentIndex < size)
            {
                var currentDataIndex = dataIndexes[currentIndex];
                var nextDataIndex    = encoded.Length;
                if (currentIndex + 1 < dataIndexes.Count)
                {
                    nextDataIndex = dataIndexes[currentIndex + 1];
                }
                var encodedElement =
                    encoded.Skip(currentDataIndex).Take(nextDataIndex - currentDataIndex).ToArray();

                DecodeAndAddElement(elementType, decodedListOutput, encodedElement);

                currentIndex++;
            }

            return(decodedListOutput);
        }
 public static int GetNumberOfBytes(byte[] encoded)
 {
     var intDecoder = new IntTypeDecoder();
     var numberOfBytesEncoded = encoded.Take(32);
     return intDecoder.DecodeInt(numberOfBytesEncoded.ToArray());
 }