public static BasicHeader Upgrade(RecyclableMemoryStream target) { var type = (PacketType)target.ReadByte(); target.Seek(-1, SeekOrigin.Current); BasicHeader upgraded; switch (type) { case PacketType.ContentPacket: upgraded = new ContentHeader(); break; case PacketType.RawData: upgraded = new RawDataHeader(); break; case PacketType.Trackable: upgraded = new TrackableHeader(); break; default: upgraded = new BasicHeader(); break; } return(upgraded.Deserialize(target)); }
public void UpdatedLongBufferIsClean() { var stream = new RecyclableMemoryStream(); stream.WriteByte(0x42); stream.SetLength(11); Assert.That(stream.Length, Is.EqualTo(11)); stream.Position = 0; Assert.That(stream.ReadByte(), Is.EqualTo(0x42)); for (int i = 0; i < 10; i++) { Assert.That(stream.ReadByte(), Is.EqualTo(0)); } stream.SetLength(80 * 1024); stream.Position = 1; for (int i = 1; i < stream.Length; i++) { Assert.That(stream.ReadByte(), Is.EqualTo(0)); } stream.Dispose(); }
public void PublicMethodThrowAfterDispose() { var stream = new RecyclableMemoryStream(); stream.Dispose(); byte[] buffer = new byte[1]; Assert.That( () => stream.SetLength(10), Throws.InstanceOf <ObjectDisposedException>()); Assert.That( () => stream.WriteByte(0x00), Throws.InstanceOf <ObjectDisposedException>()); Assert.That( () => stream.Write(buffer, 0, 1), Throws.InstanceOf <ObjectDisposedException>()); Assert.That( () => stream.ReadByte(), Throws.InstanceOf <ObjectDisposedException>()); Assert.That( () => stream.Read(buffer, 0, 1), Throws.InstanceOf <ObjectDisposedException>()); }
public virtual BasicHeader Deserialize(RecyclableMemoryStream target) { PacketType = (PacketType)target.ReadByte(); PacketFlag = (PacketFlag)target.ReadByte(); return(this); }