Exemple #1
0
        private static ModelException CreateStreamDescriptorException(IStreamedParameter parameter, Exception ex)
        {
            const string Format =
                "A stream entry for the parameter with the path {0} is incompatible, see inner exception for more information.";

            return(new ModelException(string.Format(CultureInfo.InvariantCulture, Format, parameter.GetPath()), ex));
        }
Exemple #2
0
        private static object BitConvert(IStreamedParameter parameter, byte[] rawArray)
        {
            var descriptor = parameter.StreamDescriptor.GetValueOrDefault();
            int offset;
            var array = GetArray(descriptor, rawArray, out offset);

            try
            {
                switch (descriptor.Format)
                {
                case StreamFormat.Byte:
                    return((long)array[offset]);

                case StreamFormat.UInt16BigEndian:
                case StreamFormat.UInt16LittleEndian:
                    return((long)BitConverter.ToUInt16(array, offset));

                case StreamFormat.UInt32BigEndian:
                case StreamFormat.UInt32LittleEndian:
                    return((long)BitConverter.ToUInt32(array, offset));

                case StreamFormat.UInt64BigEndian:
                case StreamFormat.UInt64LittleEndian:
                    return(unchecked ((long)BitConverter.ToUInt64(array, offset)));

                case StreamFormat.SByte:
                    return((long)unchecked ((sbyte)array[offset]));

                case StreamFormat.Int16BigEndian:
                case StreamFormat.Int16LittleEndian:
                    return((long)BitConverter.ToInt16(array, offset));

                case StreamFormat.Int32BigEndian:
                case StreamFormat.Int32LittleEndian:
                    return((long)BitConverter.ToInt32(array, offset));

                case StreamFormat.Int64BigEndian:
                case StreamFormat.Int64LittleEndian:
                    return(BitConverter.ToInt64(array, offset));

                case StreamFormat.Float32BigEndian:
                case StreamFormat.Float32LittleEndian:
                    return((double)BitConverter.ToSingle(array, offset));

                default:
                    return(BitConverter.ToDouble(array, offset));
                }
            }
            catch (ArgumentException ex)
            {
                throw CreateStreamDescriptorException(parameter, ex);
            }
            catch (IndexOutOfRangeException ex)
            {
                throw CreateStreamDescriptorException(parameter, ex);
            }
        }
Exemple #3
0
        private static object ExtractValue(IStreamedParameter parameter, object rawValue)
        {
            if (parameter.StreamDescriptor.HasValue)
            {
                var rawArray = rawValue as byte[];

                if (rawArray == null)
                {
                    const string Format =
                        "Read stream value {0} while expecting to read an octetstring for the parameter with the path {1}.";
                    throw new ModelException(
                              string.Format(CultureInfo.InvariantCulture, Format, rawValue, parameter.GetPath()));
                }

                return(BitConvert(parameter, rawArray));
            }
            else
            {
                return(rawValue);
            }
        }