Exemplo n.º 1
0
        /// <summary>
        /// Writes a signed integer using 1 to 32 bits
        /// </summary>
        public void Write(Int32 source, int numberOfBits)
        {
            NetException.Assert((numberOfBits > 0 && numberOfBits <= 32), "Write(int, numberOfBits) can only write between 1 and 32 bits");
            EnsureBufferSize(m_bitLength + numberOfBits);

            if (numberOfBits != 32)
            {
                // make first bit sign
                int signBit = 1 << (numberOfBits - 1);
                if (source < 0)
                {
                    source = (-source - 1) | signBit;
                }
                else
                {
                    source &= (~signBit);
                }
            }

            NetBitWriter.WriteUInt32((uint)source, numberOfBits, m_data, m_bitLength);

            m_bitLength += numberOfBits;
        }
Exemplo n.º 2
0
 internal void Write(sbyte source)
 {
     InternalEnsureBufferSize(m_bitLength + 8);
     NetBitWriter.WriteByte((byte)source, 8, m_data, m_bitLength);
     m_bitLength += 8;
 }
Exemplo n.º 3
0
 //
 // 1 bit
 //
 internal void Write(bool value)
 {
     InternalEnsureBufferSize(m_bitLength + 1);
     NetBitWriter.WriteByte((value ? (byte)1 : (byte)0), 1, m_data, m_bitLength);
     m_bitLength += 1;
 }
Exemplo n.º 4
0
 internal void Write(UInt64 source, int numberOfBits)
 {
     InternalEnsureBufferSize(m_bitLength + numberOfBits);
     NetBitWriter.WriteUInt64(source, numberOfBits, m_data, m_bitLength);
     m_bitLength += numberOfBits;
 }
Exemplo n.º 5
0
 //
 // 64 bit
 //
 internal void Write(UInt64 source)
 {
     InternalEnsureBufferSize(m_bitLength + 64);
     NetBitWriter.WriteUInt64(source, 64, m_data, m_bitLength);
     m_bitLength += 64;
 }
Exemplo n.º 6
0
 internal void Write(UInt32 source)
 {
     InternalEnsureBufferSize(m_bitLength + 32);
     NetBitWriter.WriteUInt32(source, 32, m_data, m_bitLength);
     m_bitLength += 32;
 }
Exemplo n.º 7
0
 internal void Write(Int16 source)
 {
     InternalEnsureBufferSize(m_bitLength + 16);
     NetBitWriter.WriteUInt32((uint)source, 16, m_data, m_bitLength);
     m_bitLength += 16;
 }
Exemplo n.º 8
0
        public static int WriteUInt64(ulong source, int numberOfBits, byte[] destination, int destinationBitOffset)
        {
#if BIGENDIAN
            source = ((source & 0xff00000000000000L) >> 56) |
                     ((source & 0x00ff000000000000L) >> 40) |
                     ((source & 0x0000ff0000000000L) >> 24) |
                     ((source & 0x000000ff00000000L) >> 8) |
                     ((source & 0x00000000ff000000L) << 8) |
                     ((source & 0x0000000000ff0000L) << 24) |
                     ((source & 0x000000000000ff00L) << 40) |
                     ((source & 0x00000000000000ffL) << 56);
#endif

            int returnValue = destinationBitOffset + numberOfBits;
            if (numberOfBits <= 8)
            {
                NetBitWriter.WriteByte((byte)source, numberOfBits, destination, destinationBitOffset);
                return(returnValue);
            }
            NetBitWriter.WriteByte((byte)source, 8, destination, destinationBitOffset);
            destinationBitOffset += 8;
            numberOfBits         -= 8;

            if (numberOfBits <= 8)
            {
                NetBitWriter.WriteByte((byte)(source >> 8), numberOfBits, destination, destinationBitOffset);
                return(returnValue);
            }
            NetBitWriter.WriteByte((byte)(source >> 8), 8, destination, destinationBitOffset);
            destinationBitOffset += 8;
            numberOfBits         -= 8;

            if (numberOfBits <= 8)
            {
                NetBitWriter.WriteByte((byte)(source >> 16), numberOfBits, destination, destinationBitOffset);
                return(returnValue);
            }
            NetBitWriter.WriteByte((byte)(source >> 16), 8, destination, destinationBitOffset);
            destinationBitOffset += 8;
            numberOfBits         -= 8;

            if (numberOfBits <= 8)
            {
                NetBitWriter.WriteByte((byte)(source >> 24), numberOfBits, destination, destinationBitOffset);
                return(returnValue);
            }
            NetBitWriter.WriteByte((byte)(source >> 24), 8, destination, destinationBitOffset);
            destinationBitOffset += 8;
            numberOfBits         -= 8;

            if (numberOfBits <= 8)
            {
                NetBitWriter.WriteByte((byte)(source >> 32), numberOfBits, destination, destinationBitOffset);
                return(returnValue);
            }
            NetBitWriter.WriteByte((byte)(source >> 32), 8, destination, destinationBitOffset);
            destinationBitOffset += 8;
            numberOfBits         -= 8;

            if (numberOfBits <= 8)
            {
                NetBitWriter.WriteByte((byte)(source >> 40), numberOfBits, destination, destinationBitOffset);
                return(returnValue);
            }
            NetBitWriter.WriteByte((byte)(source >> 40), 8, destination, destinationBitOffset);
            destinationBitOffset += 8;
            numberOfBits         -= 8;

            if (numberOfBits <= 8)
            {
                NetBitWriter.WriteByte((byte)(source >> 48), numberOfBits, destination, destinationBitOffset);
                return(returnValue);
            }
            NetBitWriter.WriteByte((byte)(source >> 48), 8, destination, destinationBitOffset);
            destinationBitOffset += 8;
            numberOfBits         -= 8;

            if (numberOfBits <= 8)
            {
                NetBitWriter.WriteByte((byte)(source >> 56), numberOfBits, destination, destinationBitOffset);
                return(returnValue);
            }
            NetBitWriter.WriteByte((byte)(source >> 56), 8, destination, destinationBitOffset);
            destinationBitOffset += 8;
            numberOfBits         -= 8;

            return(returnValue);
        }
Exemplo n.º 9
0
 /// <summary>
 /// Writes a 32 bit signed integer
 /// </summary>
 public void Write(Int32 source)
 {
     EnsureBufferSize(m_bitLength + 32);
     NetBitWriter.WriteUInt32((UInt32)source, 32, m_data, m_bitLength);
     m_bitLength += 32;
 }
Exemplo n.º 10
0
 /// <summary>
 /// Write a byte
 /// </summary>
 public void Write(byte source)
 {
     EnsureBufferSize(m_bitLength + 8);
     NetBitWriter.WriteByte(source, 8, m_data, m_bitLength);
     m_bitLength += 8;
 }
        /// <summary>
        /// Reads the specified number of bits into a Byte without advancing the read pointer
        /// </summary>
        public byte PeekByte(int numberOfBits)
        {
            byte retval = NetBitWriter.ReadByte(m_data, numberOfBits, m_readPosition);

            return(retval);
        }