internal override void DeserializeOverride(BoundedStream stream, EventShuttle eventShuttle) { var parent = (TypeNode)TypeNode.Parent; // resolve value type for deserialization if (TypeNode.SubtypeBinding != null) { SetValueType(TypeNode.SubtypeBinding, this, TypeNode.SubtypeAttributes, TypeNode.SubtypeDefaultAttribute); } else if (parent.ItemSubtypeBinding != null) { SetValueType(parent.ItemSubtypeBinding, (ValueNode)Parent, parent.ItemSubtypeAttributes, parent.ItemSubtypeDefaultAttribute); } else { // trivial case with no subtypes _valueType = TypeNode.Type; if (_valueType.GetTypeInfo().IsAbstract) { throw new InvalidOperationException( "Abstract types must have at least one subtype binding to be deserialized."); } } // skip over if null (this may happen if subtypes are unknown during deserialization) if (_valueType != null) { var typeNode = (ObjectTypeNode)TypeNode; // generate correct children for this subtype var subType = typeNode.GetSubTypeNode(_valueType); Children = new List <ValueNode>(subType.Children.Select(child => child.CreateSerializer(this))); try { ObjectDeserializeOverride(stream, eventShuttle); } catch (EndOfStreamException) { // this is ok but we can't consider this object fully formed. _valueType = null; } } var length = GetFieldLength(); // Check if we need to read past padding if (length != null) { if (length > stream.RelativePosition) { var padLength = length - stream.RelativePosition; var pad = new byte[(int)padLength]; stream.Read(pad, 0, pad.Length); } } }
private void SkipPadding(BoundedStream stream) { var length = GetFieldLength(); if (length != null) { if (length > stream.RelativePosition) { var padLength = length - stream.RelativePosition; var pad = new byte[(int)padLength.TotalByteCount]; stream.Read(pad, padLength); } } }
public void TestRead() { using (MemoryStream baseStream = new MemoryStream(_buff)) { using (BoundedStream boundedStream = new BoundedStream(baseStream, 10, 100)) { Assert.AreEqual((boundedStream.Position + boundedStream.LowerBound) % 256, boundedStream.ReadByte() % 256); Assert.AreEqual((boundedStream.Position + boundedStream.LowerBound) % 256, boundedStream.ReadByte() % 256); // Partial reads to upper bound boundedStream.Seek(1, SeekOrigin.End); byte[] buff = new byte[2] { 0, 200 }; byte[] expectedBuff = new byte[2] { 99, 200 }; boundedStream.Read(buff, 0, buff.Length); CollectionAssert.AreEqual(expectedBuff, buff); } } }