Esempio n. 1
0
        /// <summary>
        /// Write the header and the elements of the payload in the stream
        /// specified if requested.
        /// </summary>
        public void ToStream(Stream s, bool headerFlag)
        {
            BinaryWriter w = new BinaryWriter(s, Encoding.GetEncoding("iso-8859-1"));

            if (headerFlag)
            {
                w.Write(Base.hton(Major));
                w.Write(Base.hton(Minor));
                w.Write(Base.hton(Type));
                w.Write(Base.hton(ID));
                w.Write(Base.hton(PayloadSize()));
            }

            foreach (Element e in Elements)
            {
                switch (e.Type)
                {
                case AnpType.UInt32:
                    w.Write((byte)1);
                    w.Write(Base.hton(e.UInt32));
                    break;

                case AnpType.UInt64:
                    w.Write((byte)2);
                    w.Write(Base.hton(e.UInt64));
                    break;

                case AnpType.String:
                    w.Write((byte)3);
                    w.Write(Base.hton((UInt32)e.String.Length));
                    w.Write(e.String.ToCharArray());
                    break;

                case AnpType.Bin:
                    w.Write((byte)4);
                    w.Write(Base.hton((UInt32)e.Bin.Length));
                    w.Write(e.Bin);
                    break;
                }
            }
        }