/// <summary> /// Build segment record with specified item in memory stream. /// (writes length + item bytes) /// </summary> /// <param name="item">Item</param> /// <param name="writer">Writer that wraps memory stream</param> private void SerializeItemToStream(T item, RegionBinaryWriter writer) { Debug.Assert(writer != null); Debug.Assert(Monitor.IsEntered(_writeLock)); checked { var stream = writer.BaseStream; int origin = stream.InnerStreamPosition; stream.SetOriginLength(origin + 4, -1); // Offset 4 to store length later Debug.Assert(stream.Length == 0); _serializer.Serialize(writer, item); writer.Flush(); Debug.Assert(stream.Length >= 0); int length = (int)stream.Length; stream.SetOrigin(origin); // Offset back to the beggining writer.Write((int)length); // Write length stream.Seek(0, SeekOrigin.End); // Seek to the end of the stream } }
/// <summary> /// Build segment record with specified item in memory stream. /// (writes length + item bytes) /// </summary> /// <param name="item">Item</param> /// <param name="writer">Writer that wraps memory stream</param> private void SerializeItemToStream(T item, RegionBinaryWriter writer) { TurboContract.Requires(writer != null, conditionString: "writer != null"); TurboContract.Assert(Monitor.IsEntered(_writeLock), conditionString: "Monitor.IsEntered(_writeLock)"); checked { var stream = writer.BaseStream; int origin = stream.InnerStreamPosition; stream.SetOriginLength(origin + 4, -1); // Offset 4 to store length later TurboContract.Assert(stream.Length == 0, conditionString: "stream.Length == 0"); _serializer.Serialize(writer, item); writer.Flush(); TurboContract.Assert(stream.Length >= 0, conditionString: "stream.Length >= 0"); int length = (int)stream.Length; stream.SetOrigin(origin); // Offset back to the beggining writer.Write((int)length); // Write length stream.Seek(0, SeekOrigin.End); // Seek to the end of the stream } }