public void stream_can_seek_from_begin() { var testStream = GetTestStream(); var origin = testStream.Position = testStream.Length/2; Action action; const long length = 500L; using (var s = new RegionStream(testStream, length)) { s.Position.Should().Be(0, "because a region stream starts at 0"); action = () => s.Seek(-10, SeekOrigin.Begin); action.ShouldThrow<IOException>("because offset is less then 0"); s.Position.Should().Be(0, "because position should not have changed"); s.BaseStream.Position.Should().Be(origin); s.Seek(50, SeekOrigin.Begin); s.Position.Should().Be(50); s.BaseStream.Position.Should().Be(origin + 50); s.Seek(100, SeekOrigin.Begin); s.Position.Should().Be(100); s.BaseStream.Position.Should().Be(origin + 100); action = () => s.Seek(length + 50, SeekOrigin.Begin); action.ShouldThrow<IOException>("because offset exceeds end of stream"); s.Position.Should().Be(100, "because position should not have changed"); s.BaseStream.Position.Should().Be(origin + 100); } }
public void stream_can_seek_from_begin() { var testStream = GetTestStream(); var origin = testStream.Position = testStream.Length / 2; Action action; const long length = 500L; using (var s = new RegionStream(testStream, length)) { s.Position.Should().Be(0, "because a region stream starts at 0"); action = () => s.Seek(-10, SeekOrigin.Begin); action.ShouldThrow <IOException>("because offset is less then 0"); s.Position.Should().Be(0, "because position should not have changed"); s.BaseStream.Position.Should().Be(origin); s.Seek(50, SeekOrigin.Begin); s.Position.Should().Be(50); s.BaseStream.Position.Should().Be(origin + 50); s.Seek(100, SeekOrigin.Begin); s.Position.Should().Be(100); s.BaseStream.Position.Should().Be(origin + 100); action = () => s.Seek(length + 50, SeekOrigin.Begin); action.ShouldThrow <IOException>("because offset exceeds end of stream"); s.Position.Should().Be(100, "because position should not have changed"); s.BaseStream.Position.Should().Be(origin + 100); } }
public void stream_starts_at_start() { const long length = 200L; using (var s = new RegionStream(GetTestStream(), length)) { s.Position.Should().Be(0, "because a region stream starts at 0"); s.BaseStream.Position.Should().Be(0, "because the base stream started at 0"); s.Length.Should().Be(length, "because a region stream has a fixed length of {0}", length); } }
public void stream_starts_at_start() { const long length = 200L; using (var s = new RegionStream(GetTestStream(), length)) { s.Position.Should().Be(0, "because a region stream starts at 0"); s.BaseStream.Position.Should().Be(0, "because the base stream started at 0"); s.Length.Should().Be(length, "because a region stream has a fixed length of {0}", length); } }
public void stream_starts_in_middle() { const long position = 100L; const long length = 200L; using (var s = new RegionStream(GetTestStream(position), length)) { s.Position.Should().Be(0, "because a region stream starts at 0"); s.BaseStream.Position.Should().Be(position, "because a region stream starts at {0}", position); s.Length.Should().Be(length, "because a region stream has a fixed length of {0}", length); } }
public void stream_starts_in_middle() { const long position = 100L; const long length = 200L; using (var s = new RegionStream(GetTestStream(position), length)) { s.Position.Should().Be(0, "because a region stream starts at 0"); s.BaseStream.Position.Should().Be(position, "because a region stream starts at {0}", position); s.Length.Should().Be(length, "because a region stream has a fixed length of {0}", length); } }
void AddRegion(Boct head, Guid?guid = null, int?regionId = null) { var region = new BoctRegion(); region.GUID = guid ?? Guid.NewGuid(); region.LUID = regionId ?? RegionIndex; BoctTools.FillRegionId(head, region.LUID); region.Head = head; Regions.Add(region.LUID, region); if (NewRegionIdList != null) { NewRegionIdList.Add(RegionIndex); } if (regionId == null) { RegionIndex++; } region.CurrentState.Subscribe(state => { switch (state) { case BoctRegion.State.Ready: break; case BoctRegion.State.Disposed: Regions.Remove(region.LUID); break; case BoctRegion.State.Dirty: region.MaterialCounts = BoctMaterialTools.CountMaterials(region.Head, MaterialList.Materials); break; } }); // Call last time. RegionStream.OnNext(region); }
/// <inheritdoc /> protected override object ReadField(BinaryReader reader, ControllerSerializationContext serializationContext) { string name = null; var field = serializationContext.Member as FieldInfo; if (field != null) { if (field.HasAttribute <OptionalAttribute>() && reader.BaseStream.Position == reader.BaseStream.Length) { // Exit because field is optional, and we are at end of stream. return(null); } // The expected field or controller name to find on the stream. name = field.GetCustomAttribute <ParseNameAttribute>()?.Name ?? field.Name; } // Read the size of the data. int size = reader.ReadInt32(); // Save current position of stream. At the end, we compare the size with the number of bytes read for validation purposes. long startPos = reader.BaseStream.Position; if (field != null && !string.IsNullOrEmpty(name) && reader.SkipMember(field, name)) { // The property must be skipped, so revert stream back to the start position. reader.BaseStream.Position = startPos - 4; return(null); } long expectedPosition = startPos + size; long dataSize = expectedPosition - reader.BaseStream.Position; using (var regionStream = new RegionStream(reader.BaseStream, dataSize)) { using (var regionReader = new BinaryReader(regionStream, FileEncoding.Default, true)) { object retVal = base.ReadField(regionReader, serializationContext); reader.BaseStream.EnsureStreamPosition(expectedPosition, name ?? serializationContext.Name); return(retVal); } } }