Пример #1
0
        //
        // 16 bit
        //
        /// <summary>
        /// Reads an Int16 without advancing the read pointer
        /// </summary>
        public Int16 PeekInt16()
        {
            ReadOverflowException.Assert(_lengthBits - _readPosition >= 16);
            uint retval = BitReaderWriter.ReadUInt16(_data, 16, _readPosition);

            return((short)retval);
        }
Пример #2
0
        //
        // 1 bit
        //
        /// <summary>
        /// Reads a 1-bit Boolean without advancing the read pointer
        /// </summary>
        public bool PeekBoolean()
        {
            ReadOverflowException.Assert(_lengthBits - _readPosition >= 1);
            byte retval = BitReaderWriter.ReadByte(_data, 1, _readPosition);

            return(retval > 0 ? true : false);
        }
Пример #3
0
        /// <summary>
        /// Reads an SByte without advancing the read pointer
        /// </summary>
        public sbyte PeekSByte()
        {
            ReadOverflowException.Assert(_lengthBits - _readPosition >= 8);
            byte retval = BitReaderWriter.ReadByte(_data, 8, _readPosition);

            return((sbyte)retval);
        }
Пример #4
0
        /// <summary>
        /// Reads a UInt32 without advancing the read pointer
        /// </summary>
        public UInt32 PeekUInt32()
        {
            ReadOverflowException.Assert(_lengthBits - _readPosition >= 32);
            uint retval = BitReaderWriter.ReadUInt32(_data, 32, _readPosition);

            return(retval);
        }
Пример #5
0
        /// <summary>
        /// Reads the specified number of bits into an Int32 without advancing the read pointer
        /// </summary>
        public Int32 PeekInt32(int numberOfBits)
        {
            BitBufferException.Assert((numberOfBits > 0 && numberOfBits <= 32), "ReadInt() can only read between 1 and 32 bits");
            ReadOverflowException.Assert(_lengthBits - _readPosition >= numberOfBits);

            uint retval = BitReaderWriter.ReadUInt32(_data, numberOfBits, _readPosition);

            if (numberOfBits == 32)
            {
                return((int)retval);
            }

            int signBit = 1 << (numberOfBits - 1);

            if ((retval & signBit) == 0)
            {
                return((int)retval);                // positive
            }
            // negative
            unchecked
            {
                uint mask = ((uint)-1) >> (33 - numberOfBits);
                uint tmp  = (retval & mask) + 1;
                return(-((int)tmp));
            }
        }
Пример #6
0
        /// <summary>
        /// Reads the specified number of bytes without advancing the read pointer
        /// </summary>
        public void PeekBytes(byte[] into, int offset, int numberOfBytes)
        {
            ReadOverflowException.Assert(_lengthBits - _readPosition >= (numberOfBytes * 8));
            BitBufferException.Assert(offset + numberOfBytes <= into.Length);

            BitReaderWriter.ReadBytes(_data, numberOfBytes, _readPosition, into, offset);
            return;
        }
Пример #7
0
        /// <summary>
        /// Reads the specified number of bytes without advancing the read pointer
        /// </summary>
        public byte[] PeekBytes(int numberOfBytes)
        {
            ReadOverflowException.Assert(_lengthBits - _readPosition >= (numberOfBytes * 8));

            byte[] retval = new byte[numberOfBytes];
            BitReaderWriter.ReadBytes(_data, numberOfBytes, _readPosition, retval, 0);
            return(retval);
        }
Пример #8
0
        /// <summary>
        /// Reads a byte
        /// </summary>
        public byte ReadByte()
        {
            ReadOverflowException.Assert(_lengthBits - _readPosition >= 8);
            byte retval = BitReaderWriter.ReadByte(_data, 8, _readPosition);

            _readPosition += 8;
            return(retval);
        }
Пример #9
0
 /// <summary>
 /// Reads an Int64 without advancing the read pointer
 /// </summary>
 public Int64 PeekInt64()
 {
     ReadOverflowException.Assert(_lengthBits - _readPosition >= 64);
     unchecked
     {
         ulong retval     = PeekUInt64();
         long  longRetval = (long)retval;
         return(longRetval);
     }
 }
Пример #10
0
        //
        // 64 bit
        //
        /// <summary>
        /// Reads a UInt64 without advancing the read pointer
        /// </summary>
        public UInt64 PeekUInt64()
        {
            ReadOverflowException.Assert(_lengthBits - _readPosition >= 64);

            ulong low  = BitReaderWriter.ReadUInt32(_data, 32, _readPosition);
            ulong high = BitReaderWriter.ReadUInt32(_data, 32, _readPosition + 32);

            ulong retval = low + (high << 32);

            return(retval);
        }
Пример #11
0
        /// <summary>
        /// Reads a 32-bit Single without advancing the read pointer
        /// </summary>
        public float PeekSingle()
        {
            ReadOverflowException.Assert(_lengthBits - _readPosition >= 32);

            if ((_readPosition & 7) == 0)             // read directly
            {
                float retval = BitConverter.ToSingle(_data, _readPosition >> 3);
                return(retval);
            }

            byte[] bytes = PeekBytes(4);
            return(BitConverter.ToSingle(bytes, 0));
        }
Пример #12
0
        /// <summary>
        /// Reads a 64-bit Double without advancing the read pointer
        /// </summary>
        public double PeekDouble()
        {
            ReadOverflowException.Assert(_lengthBits - _readPosition >= 64);

            if ((_readPosition & 7) == 0)             // read directly
            {
                // read directly
                double retval = BitConverter.ToDouble(_data, _readPosition >> 3);
                return(retval);
            }

            byte[] bytes = PeekBytes(8);
            return(BitConverter.ToDouble(bytes, 0));
        }
Пример #13
0
        /// <summary>
        /// Reads the specified number of bits into an UInt64 without advancing the read pointer
        /// </summary>
        public UInt64 PeekUInt64(int numberOfBits)
        {
            BitBufferException.Assert((numberOfBits > 0 && numberOfBits <= 64), "ReadUInt() can only read between 1 and 64 bits");
            ReadOverflowException.Assert(_lengthBits - _readPosition >= numberOfBits);

            ulong retval;

            if (numberOfBits <= 32)
            {
                retval = (ulong)BitReaderWriter.ReadUInt32(_data, numberOfBits, _readPosition);
            }
            else
            {
                retval  = BitReaderWriter.ReadUInt32(_data, 32, _readPosition);
                retval |= (UInt64)BitReaderWriter.ReadUInt32(_data, numberOfBits - 32, _readPosition + 32) << 32;
            }
            return(retval);
        }
Пример #14
0
        /// <summary>
        /// Reads a 32 bit floating point value written using Write(Single)
        /// </summary>
        public float ReadSingle()
        {
            ReadOverflowException.Assert(_lengthBits - _readPosition >= 32);

            if ((_readPosition & 7) == 0)             // read directly
            {
                float retval = BitConverter.ToSingle(_data, _readPosition >> 3);
                _readPosition += 32;
                return(retval);
            }

            byte[] bytes = (byte[])Interlocked.Exchange(ref _buffer, null) ?? new byte[BufferSize];
            ReadBytes(bytes, 0, 4);
            float res = BitConverter.ToSingle(bytes, 0);

            _buffer = bytes;
            return(res);
        }
Пример #15
0
        /// <summary>
        /// Reads the specified number of bits into a preallocated array
        /// </summary>
        /// <param name="into">The destination array</param>
        /// <param name="offset">The offset where to start writing in the destination array</param>
        /// <param name="numberOfBits">The number of bits to read</param>
        public BitReader ReadBits(byte[] into, int offset, int numberOfBits)
        {
            ReadOverflowException.Assert(_lengthBits - _readPosition >= numberOfBits);
            BitBufferException.Assert(offset + BitUtility.BytesToHoldBits(numberOfBits) <= into.Length);

            int numberOfWholeBytes = numberOfBits / 8;
            int extraBits          = numberOfBits - (numberOfWholeBytes * 8);

            BitReaderWriter.ReadBytes(_data, numberOfWholeBytes, _readPosition, into, offset);
            _readPosition += (8 * numberOfWholeBytes);

            if (extraBits > 0)
            {
                into[offset + numberOfWholeBytes] = ReadByte(extraBits);
            }

            return(this);
        }
Пример #16
0
        /// <summary>
        /// Reads a 64 bit floating point value written using Write(Double)
        /// </summary>
        public double ReadDouble()
        {
            ReadOverflowException.Assert(_lengthBits - _readPosition >= 64);

            if ((_readPosition & 7) == 0)             // read directly
            {
                // read directly
                double retval = BitConverter.ToDouble(_data, _readPosition >> 3);
                _readPosition += 64;
                return(retval);
            }

            byte[] bytes = (byte[])Interlocked.Exchange(ref _buffer, null) ?? new byte[BufferSize];
            ReadBytes(bytes, 0, 8);
            double res = BitConverter.ToDouble(bytes, 0);

            _buffer = bytes;
            return(res);
        }