/// <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); }
// // 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); }
/// <summary> /// Writes a signed integer using 1 to 32 bits /// </summary> public BitWriter Write(Int32 source, int numberOfBits) { BitBufferException.Assert((numberOfBits > 0 && numberOfBits <= 32), "Write(int, numberOfBits) can only write between 1 and 32 bits"); EnsureBufferSize(_lengthBits + numberOfBits); if (numberOfBits != 32) { // make first bit sign int signBit = 1 << (numberOfBits - 1); if (source < 0) { source = (-source - 1) | signBit; } else { source &= (~signBit); } } BitReaderWriter.WriteUInt32((uint)source, numberOfBits, _data, _lengthBits); _lengthBits += numberOfBits; return(this); }
/// <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)); } }
/// <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); }
//public static ulong ReadUInt64(byte[] fromBuffer, int numberOfBits, int readBitOffset) /// <summary> /// Writes an unsigned 16 bit integer /// </summary> public static void WriteUInt16(ushort source, int numberOfBits, byte[] destination, int destinationBitOffset) { if (numberOfBits == 0) { return; } BitBufferException.Assert((numberOfBits >= 0 && numberOfBits <= 16), "numberOfBits must be between 0 and 16"); #if BIGENDIAN // reorder bytes uint intSource = source; intSource = ((intSource & 0x0000ff00) >> 8) | ((intSource & 0x000000ff) << 8); source = (ushort)intSource; #endif if (numberOfBits <= 8) { BitReaderWriter.WriteByte((byte)source, numberOfBits, destination, destinationBitOffset); return; } BitReaderWriter.WriteByte((byte)source, 8, destination, destinationBitOffset); numberOfBits -= 8; if (numberOfBits > 0) { BitReaderWriter.WriteByte((byte)(source >> 8), numberOfBits, destination, destinationBitOffset + 8); } }
// // 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); }
/// <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; }
/// <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); }
/// <summary> /// Writes a 64 bit unsigned integer /// </summary> public BitWriter Write(UInt64 source) { EnsureBufferSize(_lengthBits + 64); BitReaderWriter.WriteUInt64(source, 64, _data, _lengthBits); _lengthBits += 64; return(this); }
/// <summary> /// Writes a 32 bit unsigned integer /// </summary> public BitWriter Write(UInt32 source) { EnsureBufferSize(_lengthBits + 32); BitReaderWriter.WriteUInt32(source, 32, _data, _lengthBits); _lengthBits += 32; return(this); }
/// <summary> /// Writes a boolean value using 1 bit /// </summary> public BitWriter Write(bool value) { EnsureBufferSize(_lengthBits + 1); BitReaderWriter.WriteByte((value ? (byte)1 : (byte)0), 1, _data, _lengthBits); _lengthBits += 1; return(this); }
/// <summary> /// Write a byte /// </summary> public BitWriter Write(byte source) { EnsureBufferSize(_lengthBits + 8); BitReaderWriter.WriteByte(source, 8, _data, _lengthBits); _lengthBits += 8; return(this); }
/// <summary> /// Reads 1 to 8 bits into a byte /// </summary> public byte ReadByte(int numberOfBits) { BitBufferException.Assert(numberOfBits > 0 && numberOfBits <= 8, "ReadByte(bits) can only read between 1 and 8 bits"); byte retval = BitReaderWriter.ReadByte(_data, numberOfBits, _readPosition); _readPosition += numberOfBits; return(retval); }
/// <summary> /// Reads a byte /// </summary> public byte ReadByte() { ReadOverflowException.Assert(_lengthBits - _readPosition >= 8); byte retval = BitReaderWriter.ReadByte(_data, 8, _readPosition); _readPosition += 8; return(retval); }
/// <summary> /// Writes an unsigned integer using 1 to 64 bits /// </summary> public BitWriter Write(UInt64 source, int numberOfBits) { EnsureBufferSize(_lengthBits + numberOfBits); BitReaderWriter.WriteUInt64(source, numberOfBits, _data, _lengthBits); _lengthBits += numberOfBits; return(this); }
/// <summary> /// Writes a signed 16 bit integer /// </summary> public BitWriter Write(Int16 source) { EnsureBufferSize(_lengthBits + 16); BitReaderWriter.WriteUInt16((ushort)source, 16, _data, _lengthBits); _lengthBits += 16; return(this); }
/// <summary> /// Writes a 32 bit signed integer /// </summary> public BitWriter Write(UInt32 source, int numberOfBits) { BitBufferException.Assert((numberOfBits > 0 && numberOfBits <= 32), "Write(uint, numberOfBits) can only write between 1 and 32 bits"); EnsureBufferSize(_lengthBits + numberOfBits); BitReaderWriter.WriteUInt32(source, numberOfBits, _data, _lengthBits); _lengthBits += numberOfBits; return(this); }
/// <summary> /// Reads the specified number of bits into a UInt32 without advancing the read pointer /// </summary> public UInt32 PeekUInt32(int numberOfBits) { BitBufferException.Assert((numberOfBits > 0 && numberOfBits <= 32), "ReadUInt() can only read between 1 and 32 bits"); //NetException.Assert(m_bitLength - m_readBitPtr >= numberOfBits, "tried to read past buffer size"); UInt32 retval = BitReaderWriter.ReadUInt32(_data, numberOfBits, _readPosition); return(retval); }
/// <summary> /// Writes a 32 bit signed integer at a given offset in the buffer /// </summary> public BitWriter WriteAt(Int32 offset, Int32 source) { int newBitLength = Math.Max(_lengthBits, offset + 32); EnsureBufferSize(newBitLength); BitReaderWriter.WriteUInt32((UInt32)source, 32, _data, offset); _lengthBits = newBitLength; return(this); }
/// <summary> /// Writes a 16 bit signed integer at a given offset in the buffer /// </summary> public BitWriter WriteAt(Int32 offset, Int16 source) { int newBitLength = Math.Max(_lengthBits, offset + 16); EnsureBufferSize(newBitLength); BitReaderWriter.WriteUInt16((ushort)source, 16, _data, offset); _lengthBits = newBitLength; return(this); }
/// <summary> /// Writes a byte at a given offset in the buffer /// </summary> public BitWriter WriteAt(Int32 offset, byte source) { int newBitLength = Math.Max(_lengthBits, offset + 8); EnsureBufferSize(newBitLength); BitReaderWriter.WriteByte((byte)source, 8, _data, offset); _lengthBits = newBitLength; return(this); }
/// <summary> /// Writes a 64 bit unsigned integer at a given offset in the buffer /// </summary> public BitWriter WriteAt(Int32 offset, UInt64 source) { int newBitLength = Math.Max(_lengthBits, offset + 64); EnsureBufferSize(newBitLength); BitReaderWriter.WriteUInt64(source, 64, _data, offset); _lengthBits = newBitLength; return(this); }
// // 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); }
/// <summary> /// Writes all bytes in an array /// </summary> public BitWriter Write(byte[] source) { if (source == null) { throw new ArgumentNullException("source"); } int bits = source.Length * 8; EnsureBufferSize(_lengthBits + bits); BitReaderWriter.WriteBytes(source, 0, source.Length, _data, _lengthBits); _lengthBits += bits; return(this); }
/// <summary> /// Writes the specified number of bytes from an array /// </summary> public BitWriter Write(byte[] source, int offsetInBytes, int numberOfBytes) { if (source == null) { throw new ArgumentNullException("source"); } int bits = numberOfBytes * 8; EnsureBufferSize(_lengthBits + bits); BitReaderWriter.WriteBytes(source, offsetInBytes, numberOfBytes, _data, _lengthBits); _lengthBits += bits; return(this); }
/// <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); }
/// <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); }
/// <summary> /// Writes the specified number of bits into a byte array /// </summary> public static int WriteUInt32(uint source, int numberOfBits, byte[] destination, int destinationBitOffset) { #if BIGENDIAN // reorder bytes source = ((source & 0xff000000) >> 24) | ((source & 0x00ff0000) >> 8) | ((source & 0x0000ff00) << 8) | ((source & 0x000000ff) << 24); #endif int returnValue = destinationBitOffset + numberOfBits; if (numberOfBits <= 8) { BitReaderWriter.WriteByte((byte)source, numberOfBits, destination, destinationBitOffset); return(returnValue); } BitReaderWriter.WriteByte((byte)source, 8, destination, destinationBitOffset); destinationBitOffset += 8; numberOfBits -= 8; if (numberOfBits <= 8) { BitReaderWriter.WriteByte((byte)(source >> 8), numberOfBits, destination, destinationBitOffset); return(returnValue); } BitReaderWriter.WriteByte((byte)(source >> 8), 8, destination, destinationBitOffset); destinationBitOffset += 8; numberOfBits -= 8; if (numberOfBits <= 8) { BitReaderWriter.WriteByte((byte)(source >> 16), numberOfBits, destination, destinationBitOffset); return(returnValue); } BitReaderWriter.WriteByte((byte)(source >> 16), 8, destination, destinationBitOffset); destinationBitOffset += 8; numberOfBits -= 8; BitReaderWriter.WriteByte((byte)(source >> 24), numberOfBits, destination, destinationBitOffset); return(returnValue); }
/// <summary> /// Reads the specified number of bits into a Byte without advancing the read pointer /// </summary> public byte PeekByte(int numberOfBits) { byte retval = BitReaderWriter.ReadByte(_data, numberOfBits, _readPosition); return(retval); }