WriteUInt16() public static method

Write a ushort value to a stream and advances the position within the stream by 2.
public static WriteUInt16 ( Stream stream, ushort value ) : int
stream Stream The stream.
value ushort A ushort value.
return int
Exemplo n.º 1
0
        /// <summary>
        /// Serialize this instance to a stream
        /// </summary>
        /// <param name="stream">A data stream contains serialized object.</param>
        /// <returns>The number of bytes written to the stream.</returns>
        public override int Serialize(Stream stream)
        {
            int previousPosition = (int)stream.Position;
            int bytesWritten     = 0;

            bytesWritten += StreamHelper.WriteUInt16(stream, this.version);
            bytesWritten += StreamHelper.WriteUInt16(stream, this.padding1);
            bytesWritten += StreamHelper.WriteUInt32(stream, this.errorCode);
            byte[] buffer = StructureSerializer.Serialize(this.folderGID);
            bytesWritten += StreamHelper.WriteBuffer(stream, buffer);
            bytesWritten += StreamHelper.WriteUInt16(stream, this.padding2);
            buffer        = StructureSerializer.Serialize(this.messageGID);
            bytesWritten += StreamHelper.WriteBuffer(stream, buffer);
            bytesWritten += StreamHelper.WriteUInt16(stream, this.padding3);

            if (this.reserved1 == null || this.reserved1.Length != 24)
            {
                AdapterHelper.Site.Assert.Fail("The Reserved field should not be null and its length MUST be 24, but the actual length is {0}.", this.reserved1.Length);
            }

            for (int i = 0; i < this.reserved1.Length; i++)
            {
                AdapterHelper.Site.Assert.AreEqual(0, this.reserved1[i], "Reserved (24 bytes):  SHOULD be set to zeros and SHOULD be ignored by clients.");
            }

            bytesWritten += StreamHelper.WriteBuffer(stream, this.reserved1);
            bytesWritten += StreamHelper.WriteUInt32(stream, this.auxBytesCount);
            bytesWritten += StreamHelper.WriteUInt32(stream, this.auxBytesOffset);
            if (this.reserved2 != null)
            {
                bytesWritten += StreamHelper.WriteBuffer(stream, this.reserved2);
            }

            AdapterHelper.Site.Assert.AreEqual((int)this.auxBytesOffset, previousPosition + bytesWritten, "The offset and writen length are not equal, the offset is {0} and writen length is {1}.", this.auxBytesOffset, previousPosition + bytesWritten);

            AdapterHelper.Site.Assert.AreEqual((int)this.auxBytesCount, this.auxBytes.Length, "The original and serialized auxBytes length are not equal, the original length is {0} and serialized length is {1}.", this.auxBytesCount, this.auxBytes.Length);

            bytesWritten += StreamHelper.WriteBuffer(stream, this.auxBytes);
            return(bytesWritten);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Serialize current instance to a stream.
        /// </summary>
        /// <param name="stream">The stream.</param>
        /// <returns>The number of bytes written to the stream.</returns>
        public override int Serialize(Stream stream)
        {
            int size = 0;

            StreamHelper.WriteUInt32(stream, this.propertyTagCount);
            size += 4;
            AdapterHelper.Site.Assert.AreEqual((int)this.propertyTagCount, this.propertyTags.Length, "This field MUST contain PropertyTagCount tags. The expected count is {0}, the actual count is {1}.", this.propertyTagCount, this.propertyTags.Length);

            for (int i = 0; i < this.propertyTagCount; i++)
            {
                PropertyTag tag = this.propertyTags[i].Item1;
                StreamHelper.WriteUInt16(stream, tag.PropertyType);
                StreamHelper.WriteUInt16(stream, tag.PropertyId);
                size += 4;
                if (this.IsNamedProperty(tag))
                {
                    GroupPropertyName name = this.propertyTags[i].Item2;
                    AdapterHelper.Site.Assert.IsNotNull(name, "The property name should not be null.");
                    size += name.Serialize(stream);
                }
            }

            return(size);
        }