示例#1
0
        public bool Read()
        {
            _TlvRecord = null;
            try
            {
                int type = _Stream.ReadByte();
                if (type == -1)
                    throw new EndOfStreamException();

                TTlvTypeIdentifier typeIdentifier;
                ushort identifier;
                uint length;
                byte[] value = null;

                if ((type & TlvConstant.RESOURCE_WITH_VALUE) == TlvConstant.RESOURCE_WITH_VALUE)
                    typeIdentifier = TTlvTypeIdentifier.ResourceWithValue;
                else if ((type & TlvConstant.MULTIPLE_RESOURCES) == TlvConstant.MULTIPLE_RESOURCES)
                    typeIdentifier = TTlvTypeIdentifier.MultipleResources;
                else if ((type & TlvConstant.RESOURCE_INSTANCE) == TlvConstant.RESOURCE_INSTANCE)
                    typeIdentifier = TTlvTypeIdentifier.ResourceInstance;
                else
                    typeIdentifier = TTlvTypeIdentifier.ObjectInstance;

                bool identifier16Bits = ((type & TlvConstant.IDENTIFIER_16BITS) == TlvConstant.IDENTIFIER_16BITS);
                if (identifier16Bits)
                {
                    identifier = NetworkByteOrderConverter.ToUInt16(_Stream);
                }
                else
                {
                    int readByte = _Stream.ReadByte();
                    if (readByte == -1)
                        throw new EndOfStreamException();
                    identifier = (ushort)readByte;
                }

                if ((type & TlvConstant.LENGTH_24BIT) == TlvConstant.LENGTH_24BIT)
                {
                    length = NetworkByteOrderConverter.ToUInt24(_Stream); ;
                }
                else if ((type & TlvConstant.LENGTH_16BIT) == TlvConstant.LENGTH_16BIT)
                {
                    length = NetworkByteOrderConverter.ToUInt16(_Stream);
                }
                else if ((type & TlvConstant.LENGTH_8BIT) == TlvConstant.LENGTH_8BIT)
                {
                    int readByte = _Stream.ReadByte();
                    if (readByte == -1)
                        throw new EndOfStreamException();
                    length = (uint)readByte;
                }
                else //3Bit length
                {
                    length = (uint)(type & 0x7);
                }

                value = new byte[length];
                int read = _Stream.Read(value, 0, (int)length);
                if (read != (int)length)
                    throw new EndOfStreamException();
                _TlvRecord = new TlvRecord() { TypeIdentifier = typeIdentifier, Identifier = identifier, Length = length, Value = value };

            }
            catch (EndOfStreamException)
            {

            }
            return _TlvRecord != null;
        }
示例#2
0
        public bool Read()
        {
            _TlvRecord = null;
            try
            {
                int type = _Stream.ReadByte();
                if (type == -1)
                {
                    throw new EndOfStreamException();
                }

                TTlvTypeIdentifier typeIdentifier;
                ushort             identifier;
                uint   length;
                byte[] value = null;

                if ((type & TlvConstant.RESOURCE_WITH_VALUE) == TlvConstant.RESOURCE_WITH_VALUE)
                {
                    typeIdentifier = TTlvTypeIdentifier.ResourceWithValue;
                }
                else if ((type & TlvConstant.MULTIPLE_RESOURCES) == TlvConstant.MULTIPLE_RESOURCES)
                {
                    typeIdentifier = TTlvTypeIdentifier.MultipleResources;
                }
                else if ((type & TlvConstant.RESOURCE_INSTANCE) == TlvConstant.RESOURCE_INSTANCE)
                {
                    typeIdentifier = TTlvTypeIdentifier.ResourceInstance;
                }
                else
                {
                    typeIdentifier = TTlvTypeIdentifier.ObjectInstance;
                }

                bool identifier16Bits = ((type & TlvConstant.IDENTIFIER_16BITS) == TlvConstant.IDENTIFIER_16BITS);
                if (identifier16Bits)
                {
                    identifier = NetworkByteOrderConverter.ToUInt16(_Stream);
                }
                else
                {
                    int readByte = _Stream.ReadByte();
                    if (readByte == -1)
                    {
                        throw new EndOfStreamException();
                    }
                    identifier = (ushort)readByte;
                }

                if ((type & TlvConstant.LENGTH_24BIT) == TlvConstant.LENGTH_24BIT)
                {
                    length = NetworkByteOrderConverter.ToUInt24(_Stream);;
                }
                else if ((type & TlvConstant.LENGTH_16BIT) == TlvConstant.LENGTH_16BIT)
                {
                    length = NetworkByteOrderConverter.ToUInt16(_Stream);
                }
                else if ((type & TlvConstant.LENGTH_8BIT) == TlvConstant.LENGTH_8BIT)
                {
                    int readByte = _Stream.ReadByte();
                    if (readByte == -1)
                    {
                        throw new EndOfStreamException();
                    }
                    length = (uint)readByte;
                }
                else                 //3Bit length
                {
                    length = (uint)(type & 0x7);
                }

                value = new byte[length];
                int read = _Stream.Read(value, 0, (int)length);
                if (read != (int)length)
                {
                    throw new EndOfStreamException();
                }
                _TlvRecord = new TlvRecord()
                {
                    TypeIdentifier = typeIdentifier, Identifier = identifier, Length = length, Value = value
                };
            }
            catch (EndOfStreamException)
            {
            }
            return(_TlvRecord != null);
        }