/// <seealso cref="BitArrayOutputStream.write(int)">
        /// </seealso>
        public virtual void  testWrite()
        {
            BitArrayOutputStream stream = new BitArrayOutputStream();

            stream.WriteByte(0xFF);
            stream.writeBit(true);
            stream.writeBit(false);
            stream.writeBit(true);
            stream.writeBit(false);
            stream.WriteByte(0xFF);
            stream.WriteByte(0x0F);
            stream.writeBit(true);
            stream.writeBit(true);
            stream.writeBit(true);
            stream.writeBit(true);
            System.Console.Out.WriteLine("Write " + ByteTools.byteArrayToHexString(stream.ToArray()));
            ByteTools.checkBuffers(stream.ToArray(), new byte[] { 0xFF, 0xAF, 0xF0, 0xFF });

            stream.writeBit(true);
            stream.writeBit(false);
            stream.writeBit(true);
            stream.writeBit(false);
            byte[] temp_byteArray;
            temp_byteArray = new byte[] { (0xCC), (0xFF), (0xFF), (0xBB) };
            stream.Write(temp_byteArray, 0, temp_byteArray.Length);
            System.Console.Out.WriteLine("After buf write " + ByteTools.byteArrayToHexString(stream.ToArray()));
            ByteTools.checkBuffers(stream.ToArray(), new byte[] { (0xFF), (0xAF), (0xF0), (0xFF), (0xAC), (0xCF), (0xFF), (0xFB), (0xB0) });
            stream.align();
            stream.writeBit(true);
            stream.writeBit(true);
            stream.align();
            stream.WriteByte(0xFF);

            System.Console.Out.WriteLine("After align " + ByteTools.byteArrayToHexString(stream.ToArray()));
            ByteTools.checkBuffers(stream.ToArray(), new byte[] { (0xFF), (0xAF), (0xF0), (0xFF), (0xAC), (0xCF), (0xFF), (0xFB), (0xB0), (0xC0), (0xFF) });
        }
Пример #2
0
        public override int encodeBitString(Object obj, Stream stream, ElementInfo elementInfo)
        {
            /*NOTE – (Tutorial) Bitstrings constrained to a fixed length less than or equal to 16 bits do not cause octet alignment. Larger
             * bitstrings are octet-aligned in the ALIGNED variant. If the length is fixed by constraints and the upper bound is less than 64K,
             * there is no explicit length encoding, otherwise a length encoding is included which can take any of the forms specified earlier for
             * length encodings, including fragmentation for large bit strings.*/

            int       resultSize = 0, sizeOfString = 0;
            BitString str = (BitString)obj;

            byte[] buffer = str.Value;
            sizeOfString = str.getLengthInBits(); // buffer.Length*8 - str.TrailBitsCnt;
            resultSize  += encodeLength(sizeOfString, elementInfo, stream);
            doAlign(stream);

            BitArrayOutputStream bitStream = (BitArrayOutputStream)stream;

            if (sizeOfString > 0)
            {
                //doAlign(stream);
                if (str.TrailBitsCnt == 0)
                {
                    stream.Write(buffer, 0, buffer.Length);
                }
                else
                {
                    bitStream.Write(buffer, 0, buffer.Length - 1);
                    for (int i = 0; i < str.TrailBitsCnt; i++)
                    {
                        int bitValue = (buffer[buffer.Length - 1] >> (7 - i)) & 0x1;
                        bitStream.writeBit(bitValue);
                    }
                }
            }
            return(resultSize);
        }