C # Implementation Stream writer Cbyteswriter

Source: Internet
Author: User
Cbyteswriter
Using System; namespace Common {///<summary>///author:Jave.Lin///date:2018-03-24///</summar 
            Y> public class Cbyteswriter:idisposable {public static bool Islittleendian//return True {
        get {return Bitconverter.islittleendian;}
        } private int _m_ipos;
        Private byte[] _m_pbuffer;
        private int _m_iidx;

        private int _m_ilen;
        public int Irealpos {get {return _m_iidx + _m_ipos;}}
        public int IPos {get {_m_ipos;} set {_m_ipos = value;}}
        public int Ilen {get {return _m_ilen;}}

        public int Ibuffremain {get {return Math.max (_m_ilen-(_m_iidx + _m_ipos), 0);}
        Public Cbyteswriter (byte[] buffer, int idx = 0, int len = 0) {setbuffer (buffer, IDX, Len);
        public void Dispose () {_m_pbuffer = null; } public void SetBuffer (byte[) buffer, inT idx = 0, int len = 0) {_m_pbuffer = buffer;
            _m_iidx = idx; _m_ilen = Len = 0?
            _m_pbuffer.length:len;
            _m_ipos = 0;
        _checkbufflen (_m_ilen);
        }///<summary>///Attention, it'll change Original-buffer data! </summary>///<param name= "idx" >reset the offset index of writting</param> Public V
            OID Trimtwritten () {var remain = Ibuffremain;
            var rpos = Irealpos;
            if (remain > 0) {buffer.blockcopy (_m_pbuffer, RpoS, _m_pbuffer, _m_iidx, remain);
        } _m_ipos = 0;
            public void Writechar (char value) {_checkbuffremain (2);
            Bitconverterext.getbytes (_m_pbuffer, value, _m_iidx + _m_ipos);
        _m_ipos + 2; } public void Writechar (char[] value) {_checkbuffremain (2);
            Bitconverterext.getbytes (_m_pbuffer, value, _m_iidx + _m_ipos); _m_ipos + + 2 * value.
        Length;
            public void Writebool (bool value) {_checkbuffremain (1);
            Bitconverterext.getbytes (_m_pbuffer, value, _m_iidx + _m_ipos);
        _m_ipos + 1;
            public void Writebool (bool[] value) {_checkbuffremain (1);
            Bitconverterext.getbytes (_m_pbuffer, value, _m_iidx + _m_ipos); _m_ipos + = value.
        Length;
            public void Writesbyte (sbyte value) {_checkbuffremain (1);
            Bitconverterext.getbytes (_m_pbuffer, value, _m_iidx + _m_ipos);
        _m_ipos + 1;
            public void WriteByte (byte value) {_checkbuffremain (1);
            Bitconverterext.getbytes (_m_pbuffer, value, _m_iidx + _m_ipos);
        _m_ipos + 1; }///<summary>///buffer.blockcopy (Writebuf,Writeidxd, _m_pbuffer, _m_iidx + _m_ipos, Writelen);  </summary> public void Writebytes (byte[] writebuf, int writeidxd, int writelen) {if
            (Writebuf = null) {_throwerr (this. GetType ().
            FullName + "Writebytes argument:writebuf = null"); } if (Writelen > (_m_pbuffer.length-irealpos)) {_throwerr (this. GetType ().
            FullName + "Writebytes Argument:writelen too Large");
            } _checkbuffremain (Writelen);
            Buffer.blockcopy (Writebuf, Writeidxd, _m_pbuffer, _m_iidx + _m_ipos, Writelen);
        _m_ipos + = Writelen;
            public void WriteInt16 (short value) {_checkbuffremain (2);
            Bitconverterext.getbytes (_m_pbuffer, value, _m_iidx + _m_ipos);
        _m_ipos + 2;
            public void WriteUInt16 (ushort value) {_checkbuffremain (2); BitconvertErext.getbytes (_m_pbuffer, value, _m_iidx + _m_ipos);
        _m_ipos + 2;
            public void WriteUInt16 (ushort[] value) {_checkbuffremain (2);
            Bitconverterext.getbytes (_m_pbuffer, value, _m_iidx + _m_ipos); _m_ipos + + 2 * value.
        Length;
            public void WriteInt32 (int value) {_checkbuffremain (4);
            Bitconverterext.getbytes (_m_pbuffer, value, _m_iidx + _m_ipos);
        _m_ipos + 4;
            public void WriteInt32 (int[] value) {_checkbuffremain (4);
            Bitconverterext.getbytes (_m_pbuffer, value, _m_iidx + _m_ipos); _m_ipos + + 4 * value.
        Length;
            public void WriteUInt32 (uint value) {_checkbuffremain (4);
            Bitconverterext.getbytes (_m_pbuffer, value, _m_iidx + _m_ipos);
        _m_ipos + 4; public void WriteUInt32 (uint[] value) {_checkbuffreMain (4);
            Bitconverterext.getbytes (_m_pbuffer, value, _m_iidx + _m_ipos); _m_ipos + + 4 * value.
        Length;
            public void WriteInt64 (Long value) {_checkbuffremain (8);
            Bitconverterext.getbytes (_m_pbuffer, value, _m_iidx + _m_ipos);
        _m_ipos + 8;
            public void WriteInt64 (long[] value) {_checkbuffremain (8);
            Bitconverterext.getbytes (_m_pbuffer, value, _m_iidx + _m_ipos); _m_ipos + + 8 * value.
        Length;
            public void WriteUInt64 (ulong value) {_checkbuffremain (8);
            Bitconverterext.getbytes (_m_pbuffer, value, _m_iidx + _m_ipos);
        _m_ipos + 8;
            public void WriteUInt64 (ulong[] value) {_checkbuffremain (8);
            Bitconverterext.getbytes (_m_pbuffer, value, _m_iidx + _m_ipos); _m_ipos + + 8 * value.
        Length; } public void Writesingle(float value)
            {_checkbuffremain (4);
            Bitconverterext.getbytes (_m_pbuffer, value, _m_iidx + _m_ipos);
        _m_ipos + 4;
            public void Writesingle (float[] value) {_checkbuffremain (4);
            Bitconverterext.getbytes (_m_pbuffer, value, _m_iidx + _m_ipos); _m_ipos + + 4 * value.
        Length;
            public void writedouble (double value) {_checkbuffremain (8);
            Bitconverterext.getbytes (_m_pbuffer, value, _m_iidx + _m_ipos);
        _m_ipos + 8;
            public void writedouble (double[] value) {_checkbuffremain (8);
            Bitconverterext.getbytes (_m_pbuffer, value, _m_iidx + _m_ipos); _m_ipos + + 8 * value.
        Length;
                private void _checkbuffremain (int value) {if (Ibuffremain < value) { _throwerr (this. GetType ().
   FullName + "EOB (end of byte[])");         } private void _checkbufflen (int value) {if (Value > Ibuffremain) {_throwerr (this. GetType (). FullName + "Set write buffer ' s Len larger than buffer.
            Length ");
        } private void _throwerr (string errmsg) {throw new Exception (errmsg); }
    }
}
Apply
            var one = false;
            var two = true;
            var three = true;
            var four = false;

            var arrbool = new Bool[3] {true, false, true};

            var bytes = new byte[200];
            var pos = 0;
            Bitconverterext.getbytes (bytes, one, 0);
            Bitconverterext.getbytes (bytes, two, pos + sizeof (BOOL));
            Bitconverterext.getbytes (bytes, three, pos + sizeof (BOOL));
            Bitconverterext.getbytes (bytes, four, pos + sizeof (BOOL));
            Bitconverterext.getbytes (Bytes, ' a ', pos + sizeof (BOOL));
            Bitconverterext.getbytes (Bytes, arrbool, pos + sizeof (char));
            Bitconverterext.getbytes (bytes, (ushort) 0xf0ff, pos = sizeof (BOOL) * 3);
            Bitconverterext.getbytes (bytes, new char[] {' A ', ' B '}, pos + sizeof (ushort));
            Bitconverterext.getbytes (bytes, new ushort[] {0xff, 0x8}, pos + = sizeof (char) * 2); Bitconverterext.getbytes (Bytes, 0xff, pos + sizeof (ushort) * 2);
            Bitconverterext.getbytes (Bytes, 0xffffffu, pos + sizeof (int));
            Bitconverterext.getbytes (Bytes, 0.999f, pos + sizeof (UINT));
            Bitconverterext.getbytes (Bytes, 0.999D, pos + sizeof (float));
            Bitconverterext.getbytes (bytes, new int[] {1, 2, 3, 4}, pos + + sizeof (double));
            Bitconverterext.getbytes (bytes, new uint[] {5, 6, 7, 8}, pos = sizeof (int) * 4);
            Bitconverterext.getbytes (bytes, new float[] {0.1f, 0.2f, 0.3f, 0.4f}, pos + + sizeof (UINT) * 4);
            Bitconverterext.getbytes (bytes, new double[] {0.1d, 0.2d, 0.3d, 0.4d}, pos = sizeof (FLOAT) * 4);
            Bitconverterext.getbytes (bytes, new long[] {0xff, 0xFFFF, 0xFFFFFF, 0xFFFFFFFF}, pos + + sizeof (double) * 4);

            Bitconverterext.getbytes (bytes, new ulong[] {0xffff, 0xffffff, 0xFFFFFFFF, 0xffffffffff}, pos = sizeof (LONG) * 4);
            var bytes1 = new byte[200];
    var writer = new Cbyteswriter (bytes1);        Writer.
            Writebool (one); Writer.
            Writebool (two); Writer.
            Writebool (three); Writer.
            Writebool (four); Writer.
            Writechar (' a '); Writer.
            Writebool (Arrbool); Writer.
            WriteUInt16 ((ushort) 0xf0ff); Writer.
            Writechar (new char[] {' A ', ' B '}); Writer.
            WriteUInt16 (new ushort[] {0xff, 0x8}); Writer.
            WriteInt32 (0xFF); Writer.
            WriteUInt32 (0XFFFFFFU); Writer.
            Writesingle (0.999f); Writer.
            Writedouble (0.999D); Writer.
            WriteInt32 (New int[] {1, 2, 3, 4}); Writer.
            WriteUInt32 (New uint[] {5, 6, 7, 8}); Writer.
            Writesingle (new float[] {0.1f, 0.2f, 0.3f, 0.4f}); Writer.
            Writedouble (new double[] {0.1d, 0.2d, 0.3d, 0.4d}); Writer.
            WriteInt64 (new long[] {0xff, 0xFFFF, 0xFFFFFF, 0xFFFFFFFF}); Writer.

            WriteUInt64 (new ulong[] {0xffff, 0xffffff, 0xFFFFFFFF, 0xffffffffff}); var equal = EquaL (bytes, bytes1);
                private static bool Equal (byte[] A, byte[] b) {if (a.length!= b.length) return false;  for (int i = 0, len = a.length i < len; ++i) {if (a[i)!= B[i]) return
                False
            return true; }
Output
+       bytes   {byte[200]} byte[]
+       bytes1  {byte[200]} byte[]
        equal   true    bool
+       writer  {Common.cbyteswriter}   Common.cbyteswriter
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.