public void WriteAndReadInts() { var bitStream = new BitStream(intsToAdd.Length * intBitCount); bitStream.Clear(); foreach (int i in intsToAdd) { bitStream.Write(i, intBitCount); } Debug.Log(bitStream); for (int i = 0; i < intsToAdd.Length; i++) { Debug.Log(bitStream.ReadInt(intBitCount)); } }
public void WriteAndReadIntsSimulatingNetwork() { var bitStream = new BitStream(intsToAdd.Length * intBitCount); bitStream.Clear(); foreach (int i in intsToAdd) { bitStream.Write(i, intBitCount); } byte[] buffer = bitStream.GetBuffer(); byte[] bufferCopy = new byte[buffer.Length]; System.Array.Copy(buffer, bufferCopy, buffer.Length); // Simulate copying bytes from network bitStream.LoadBuffer(bufferCopy); Debug.Log(bitStream); for (int i = 0; i < intsToAdd.Length; i++) { Debug.Log(bitStream.ReadInt(intBitCount)); } }
public void ReadWriteNumbersOddOffset() { BitStream bs = new BitStream(); bs.WriteBit(1); bs.WriteBit(1); bs.WriteBit(1); //bs.LittleEndian(); Console.WriteLine("After 3"); foreach (byte b in bs.Value) { Console.Write(Byte2String(b)); } Console.WriteLine(""); //Max bs.WriteInt8(sbyte.MaxValue); bs.SeekBits(0, System.IO.SeekOrigin.Begin); Console.WriteLine(Byte2String((byte)sbyte.MaxValue)); foreach (byte b in bs.Value) { Console.Write(Byte2String(b)); } bs.SeekBits(3, System.IO.SeekOrigin.Begin); Console.WriteLine("\n" + Byte2String((byte)bs.ReadInt8())); bs.SeekBits(3, System.IO.SeekOrigin.Begin); Assert.AreEqual(0, bs.ReadBit()); Assert.AreEqual(1, bs.ReadBit()); Assert.AreEqual(1, bs.ReadBit()); Assert.AreEqual(1, bs.ReadBit()); Assert.AreEqual(1, bs.ReadBit()); Assert.AreEqual(1, bs.ReadBit()); Assert.AreEqual(1, bs.ReadBit()); bs.SeekBits(3, System.IO.SeekOrigin.Begin); Assert.AreEqual(sbyte.MaxValue, bs.ReadInt8()); bs.Clear(); bs.WriteBit(1); bs.WriteBit(1); bs.WriteBit(1); bs.WriteInt16(short.MaxValue); bs.SeekBits(3, System.IO.SeekOrigin.Begin); Assert.AreEqual(short.MaxValue, bs.ReadInt16()); bs.Clear(); bs.WriteBit(1); bs.WriteBit(1); bs.WriteBit(1); bs.WriteInt32(67305985); bs.SeekBits(3, System.IO.SeekOrigin.Begin); Assert.AreEqual(67305985, bs.ReadInt32()); bs.Clear(); bs.WriteBit(1); bs.WriteBit(1); bs.WriteBit(1); bs.WriteInt32(Int32.MaxValue); bs.SeekBits(3, System.IO.SeekOrigin.Begin); Assert.AreEqual(Int32.MaxValue, bs.ReadInt32()); bs.Clear(); bs.WriteBit(1); bs.WriteBit(1); bs.WriteBit(1); bs.WriteInt64(Int64.MaxValue); bs.SeekBits(3, System.IO.SeekOrigin.Begin); Assert.AreEqual(Int64.MaxValue, bs.ReadInt64()); bs.Clear(); bs.WriteBit(1); bs.WriteBit(1); bs.WriteBit(1); bs.WriteUInt8(byte.MaxValue); bs.SeekBits(3, System.IO.SeekOrigin.Begin); Assert.AreEqual(byte.MaxValue, bs.ReadUInt8()); bs.Clear(); bs.WriteBit(1); bs.WriteBit(1); bs.WriteBit(1); bs.WriteUInt16(ushort.MaxValue); bs.SeekBits(3, System.IO.SeekOrigin.Begin); Assert.AreEqual(ushort.MaxValue, bs.ReadUInt16()); bs.Clear(); bs.WriteBit(1); bs.WriteBit(1); bs.WriteBit(1); bs.WriteUInt32(UInt32.MaxValue); bs.SeekBits(3, System.IO.SeekOrigin.Begin); Assert.AreEqual(UInt32.MaxValue, bs.ReadUInt32()); bs.Clear(); bs.WriteBit(1); bs.WriteBit(1); bs.WriteBit(1); bs.WriteUInt64(UInt64.MaxValue); bs.SeekBits(3, System.IO.SeekOrigin.Begin); Assert.AreEqual(UInt64.MaxValue, bs.ReadUInt64()); //Min bs.Clear(); bs.WriteBit(1); bs.WriteBit(1); bs.WriteBit(1); bs.WriteInt8(sbyte.MinValue); bs.SeekBits(3, System.IO.SeekOrigin.Begin); Assert.AreEqual(sbyte.MinValue, bs.ReadInt8()); bs.Clear(); bs.WriteBit(1); bs.WriteBit(1); bs.WriteBit(1); bs.WriteInt16(short.MinValue); bs.SeekBits(3, System.IO.SeekOrigin.Begin); Assert.AreEqual(short.MinValue, bs.ReadInt16()); bs.Clear(); bs.WriteBit(1); bs.WriteBit(1); bs.WriteBit(1); bs.WriteInt32(Int32.MinValue); bs.SeekBits(3, System.IO.SeekOrigin.Begin); Assert.AreEqual(Int32.MinValue, bs.ReadInt32()); bs.Clear(); bs.WriteBit(1); bs.WriteBit(1); bs.WriteBit(1); bs.WriteInt64(Int64.MinValue); bs.SeekBits(3, System.IO.SeekOrigin.Begin); Assert.AreEqual(Int64.MinValue, bs.ReadInt64()); // BIG ENDIAN ////////////////////////////////////////// bs = new BitStream(); bs.WriteBit(1); bs.WriteBit(1); bs.WriteBit(1); bs.LittleEndian(); //Max bs.WriteInt8(sbyte.MaxValue); bs.SeekBits(3, System.IO.SeekOrigin.Begin); Assert.AreEqual(sbyte.MaxValue, bs.ReadInt8()); bs.Clear(); bs.WriteBit(1); bs.WriteBit(1); bs.WriteBit(1); bs.WriteInt16(short.MaxValue); bs.SeekBits(3, System.IO.SeekOrigin.Begin); Assert.AreEqual(short.MaxValue, bs.ReadInt16()); bs.Clear(); bs.WriteBit(1); bs.WriteBit(1); bs.WriteBit(1); bs.WriteInt32(67305985); bs.SeekBits(3, System.IO.SeekOrigin.Begin); Assert.AreEqual(67305985, bs.ReadInt32()); bs.Clear(); bs.WriteBit(1); bs.WriteBit(1); bs.WriteBit(1); bs.WriteInt32(Int32.MaxValue); bs.SeekBits(3, System.IO.SeekOrigin.Begin); Assert.AreEqual(Int32.MaxValue, bs.ReadInt32()); bs.Clear(); bs.WriteBit(1); bs.WriteBit(1); bs.WriteBit(1); bs.WriteInt64(Int64.MaxValue); bs.SeekBits(3, System.IO.SeekOrigin.Begin); Assert.AreEqual(Int64.MaxValue, bs.ReadInt64()); bs.Clear(); bs.WriteBit(1); bs.WriteBit(1); bs.WriteBit(1); bs.WriteUInt8(byte.MaxValue); bs.SeekBits(3, System.IO.SeekOrigin.Begin); Assert.AreEqual(byte.MaxValue, bs.ReadUInt8()); bs.Clear(); bs.WriteBit(1); bs.WriteBit(1); bs.WriteBit(1); bs.WriteUInt16(ushort.MaxValue); bs.SeekBits(3, System.IO.SeekOrigin.Begin); Assert.AreEqual(ushort.MaxValue, bs.ReadUInt16()); bs.Clear(); bs.WriteBit(1); bs.WriteBit(1); bs.WriteBit(1); bs.WriteUInt32(UInt32.MaxValue); bs.SeekBits(3, System.IO.SeekOrigin.Begin); Assert.AreEqual(UInt32.MaxValue, bs.ReadUInt32()); bs.Clear(); bs.WriteBit(1); bs.WriteBit(1); bs.WriteBit(1); bs.WriteUInt64(UInt64.MaxValue); bs.SeekBits(3, System.IO.SeekOrigin.Begin); Assert.AreEqual(UInt64.MaxValue, bs.ReadUInt64()); //Min bs.Clear(); bs.WriteBit(1); bs.WriteBit(1); bs.WriteBit(1); bs.WriteInt8(sbyte.MinValue); bs.SeekBits(3, System.IO.SeekOrigin.Begin); Assert.AreEqual(sbyte.MinValue, bs.ReadInt8()); bs.Clear(); bs.WriteBit(1); bs.WriteBit(1); bs.WriteBit(1); bs.WriteInt16(short.MinValue); bs.SeekBits(3, System.IO.SeekOrigin.Begin); Assert.AreEqual(short.MinValue, bs.ReadInt16()); bs.Clear(); bs.WriteBit(1); bs.WriteBit(1); bs.WriteBit(1); bs.WriteInt32(Int32.MinValue); bs.SeekBits(3, System.IO.SeekOrigin.Begin); Assert.AreEqual(Int32.MinValue, bs.ReadInt32()); bs.Clear(); bs.WriteBit(1); bs.WriteBit(1); bs.WriteBit(1); bs.WriteInt64(Int64.MinValue); bs.SeekBits(3, System.IO.SeekOrigin.Begin); Assert.AreEqual(Int64.MinValue, bs.ReadInt64()); }