public static ulong ReadUInt64(this IBitBuffer buffer) { Span <byte> tmp = stackalloc byte[sizeof(ulong)]; buffer.Read(tmp); return(BinaryPrimitives.ReadUInt64LittleEndian(tmp)); }
/// <summary> /// Reads a <see cref="long"/> stored in 1 to 64 bits, written by <see cref="Write(long, int)"/>. /// </summary> /// <exception cref="EndOfMessageException"></exception> public static long ReadInt64(this IBitBuffer buffer, int bitCount) { Span <byte> tmp = stackalloc byte[sizeof(long)]; if (bitCount == tmp.Length * 8) { buffer.Read(tmp); return(BinaryPrimitives.ReadInt64LittleEndian(tmp)); } tmp.Clear(); buffer.ReadBits(tmp, bitCount, tmp.Length * 8); long value = BinaryPrimitives.ReadInt64LittleEndian(tmp); long signBit = 1 << (bitCount - 1); if ((value & signBit) == 0) { return(value); // positive } // negative unchecked { ulong mask = ((ulong)-1) >> (65 - bitCount); ulong nValue = ((ulong)value & mask) + 1; return(-(long)nValue); } }
/// <summary> /// Reads a <see cref="int"/> stored in 1 to 32 bits, written by <see cref="Write(int, int)"/>. /// </summary> /// <exception cref="EndOfMessageException"></exception> public static int ReadInt32(this IBitBuffer buffer, int bitCount) { Span <byte> tmp = stackalloc byte[sizeof(int)]; if (bitCount == tmp.Length * 8) { buffer.Read(tmp); return(BinaryPrimitives.ReadInt32LittleEndian(tmp)); } tmp.Clear(); buffer.ReadBits(tmp, bitCount, tmp.Length * 8); int value = BinaryPrimitives.ReadInt32LittleEndian(tmp); int signBit = 1 << (bitCount - 1); if ((value & signBit) == 0) { return(value); // positive } // negative unchecked { uint mask = ((uint)-1) >> (33 - bitCount); uint nValue = ((uint)value & mask) + 1; return(-(int)nValue); } }
/// <summary> /// Reads a 32-bit <see cref="int"/> written by <see cref="Write(int)"/>. /// </summary> /// <exception cref="EndOfMessageException"></exception> public static int ReadInt32(this IBitBuffer buffer) { Span <byte> tmp = stackalloc byte[sizeof(int)]; buffer.Read(tmp); return(BinaryPrimitives.ReadInt32LittleEndian(tmp)); }
public static ushort ReadUInt16(this IBitBuffer buffer) { Span <byte> tmp = stackalloc byte[sizeof(ushort)]; buffer.Read(tmp); return(BinaryPrimitives.ReadUInt16LittleEndian(tmp)); }
/// <summary> /// </summary> /// <exception cref="EndOfMessageException"></exception> public static byte[] Read(this IBitBuffer buffer, int count) { var data = new byte[count]; buffer.Read(data); return(data); }
/// <summary> /// Reads bytes into the given span. /// </summary> /// <param name="destination">The destination span.</param> public static int StreamRead(this IBitBuffer buffer, Span <byte> destination) { if (buffer.IsByteAligned()) { int remainingBytes = Math.Min(buffer.ByteLength - buffer.BytePosition, destination.Length); buffer.Read(destination.Slice(0, remainingBytes)); return(remainingBytes); } else { int remainingBits = Math.Min(buffer.BitLength - buffer.BitPosition, destination.Length * 8); buffer.ReadBits(destination, remainingBits); return(NetUtility.DivBy8(remainingBits)); } }