Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / wpf / src / Base / MS / Internal / IO / Packaging / TrackingMemoryStream.cs / 1305600 / TrackingMemoryStream.cs
//------------------------------------------------------------------------------ // //// Copyright (C) Microsoft Corporation. All rights reserved. // // // Description: // This is a stream that is capable of reporting data usage up to the registered // owner // // History: // 05/24/2005: IgorBel: Initial creation. // 11/08/2005: BruceMac: Change namespace // //----------------------------------------------------------------------------- using System; using System.Diagnostics; using System.IO; namespace MS.Internal.IO.Packaging { // making this class sealed as it is taking advantage of some Virtual methods // in MemoryStream(Capacity); therefore, there is a danger of subclass overriding those and unexpected // behavior changes. Consider calls from Constructor->ReportIfNecessary->Capacity // prior to unsealing this class (they would be marked as FxCop violations) internal sealed class TrackingMemoryStream : MemoryStream { // other constructors can be added later, as we need them, for now we only use the following 2 internal TrackingMemoryStream(ITrackingMemoryStreamFactory memoryStreamFactory): base() { // although we could have implemented this constructor in terms of the other constructor; we shouldn't. // It seems safer to always call the equivalent base class constructor, as we might be ignorant about // some minor differences between various MemoryStream constructors Debug.Assert(memoryStreamFactory != null); _memoryStreamFactory = memoryStreamFactory; ReportIfNeccessary(); } internal TrackingMemoryStream (ITrackingMemoryStreamFactory memoryStreamFactory, Int32 capacity) : base(capacity) { Debug.Assert(memoryStreamFactory != null); _memoryStreamFactory = memoryStreamFactory; ReportIfNeccessary(); } // Here are the overrides for members that could possible result in changes in the allocated memory public override int Read(byte[] buffer, int offset, int count) { int result = base.Read(buffer, offset, count); ReportIfNeccessary(); return result; } public override void Write(byte[] buffer, int offset, int count) { base.Write(buffer, offset, count); ReportIfNeccessary(); } public override void SetLength(long value) { base.SetLength(value); ReportIfNeccessary(); } protected override void Dispose(bool disposing) { try { if (disposing) { if (_memoryStreamFactory != null) { // release all the memory, and report it to the TrackingMemoryStreamFactory SetLength(0); Capacity = 0; ReportIfNeccessary(); _memoryStreamFactory = null; } } } finally { base.Dispose(disposing); } } private void ReportIfNeccessary () { if (this.Capacity !=_lastReportedHighWaterMark) { // we need to report the new memory being allocated as a part of the constructor _memoryStreamFactory.ReportMemoryUsageDelta(checked(this.Capacity - _lastReportedHighWaterMark)); _lastReportedHighWaterMark = this.Capacity; } } private ITrackingMemoryStreamFactory _memoryStreamFactory; private int _lastReportedHighWaterMark; } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // Copyright (c) Microsoft Corporation. All rights reserved. //------------------------------------------------------------------------------ // //// Copyright (C) Microsoft Corporation. All rights reserved. // // // Description: // This is a stream that is capable of reporting data usage up to the registered // owner // // History: // 05/24/2005: IgorBel: Initial creation. // 11/08/2005: BruceMac: Change namespace // //----------------------------------------------------------------------------- using System; using System.Diagnostics; using System.IO; namespace MS.Internal.IO.Packaging { // making this class sealed as it is taking advantage of some Virtual methods // in MemoryStream(Capacity); therefore, there is a danger of subclass overriding those and unexpected // behavior changes. Consider calls from Constructor->ReportIfNecessary->Capacity // prior to unsealing this class (they would be marked as FxCop violations) internal sealed class TrackingMemoryStream : MemoryStream { // other constructors can be added later, as we need them, for now we only use the following 2 internal TrackingMemoryStream(ITrackingMemoryStreamFactory memoryStreamFactory): base() { // although we could have implemented this constructor in terms of the other constructor; we shouldn't. // It seems safer to always call the equivalent base class constructor, as we might be ignorant about // some minor differences between various MemoryStream constructors Debug.Assert(memoryStreamFactory != null); _memoryStreamFactory = memoryStreamFactory; ReportIfNeccessary(); } internal TrackingMemoryStream (ITrackingMemoryStreamFactory memoryStreamFactory, Int32 capacity) : base(capacity) { Debug.Assert(memoryStreamFactory != null); _memoryStreamFactory = memoryStreamFactory; ReportIfNeccessary(); } // Here are the overrides for members that could possible result in changes in the allocated memory public override int Read(byte[] buffer, int offset, int count) { int result = base.Read(buffer, offset, count); ReportIfNeccessary(); return result; } public override void Write(byte[] buffer, int offset, int count) { base.Write(buffer, offset, count); ReportIfNeccessary(); } public override void SetLength(long value) { base.SetLength(value); ReportIfNeccessary(); } protected override void Dispose(bool disposing) { try { if (disposing) { if (_memoryStreamFactory != null) { // release all the memory, and report it to the TrackingMemoryStreamFactory SetLength(0); Capacity = 0; ReportIfNeccessary(); _memoryStreamFactory = null; } } } finally { base.Dispose(disposing); } } private void ReportIfNeccessary () { if (this.Capacity !=_lastReportedHighWaterMark) { // we need to report the new memory being allocated as a part of the constructor _memoryStreamFactory.ReportMemoryUsageDelta(checked(this.Capacity - _lastReportedHighWaterMark)); _lastReportedHighWaterMark = this.Capacity; } } private ITrackingMemoryStreamFactory _memoryStreamFactory; private int _lastReportedHighWaterMark; } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // Copyright (c) Microsoft Corporation. All rights reserved.
Link Menu
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- VirtualDirectoryMappingCollection.cs
- SimplePropertyEntry.cs
- DataServiceResponse.cs
- TimeStampChecker.cs
- Brush.cs
- XmlProcessingInstruction.cs
- OutputCacheProfile.cs
- LookupNode.cs
- InternalBase.cs
- ConstructorNeedsTagAttribute.cs
- Encoding.cs
- WizardPanel.cs
- HttpPostedFile.cs
- EventMappingSettingsCollection.cs
- Size3DValueSerializer.cs
- HotCommands.cs
- ButtonFieldBase.cs
- PageParserFilter.cs
- PageFunction.cs
- SymmetricKeyWrap.cs
- ConditionalExpression.cs
- HtmlHead.cs
- AsymmetricSignatureDeformatter.cs
- SoapEnumAttribute.cs
- AttributeXamlType.cs
- PropertyToken.cs
- ConfigXmlSignificantWhitespace.cs
- RemotingException.cs
- CompilerResults.cs
- ConstrainedDataObject.cs
- BCLDebug.cs
- DataGridViewComboBoxCell.cs
- ThreadExceptionEvent.cs
- ExtendedPropertyDescriptor.cs
- XNodeNavigator.cs
- ExpandedProjectionNode.cs
- IMembershipProvider.cs
- DefaultPrintController.cs
- SessionStateUtil.cs
- ConstraintCollection.cs
- CodeGeneratorOptions.cs
- RuntimeVariablesExpression.cs
- BinaryConverter.cs
- InputProcessorProfiles.cs
- DispatchChannelSink.cs
- _AutoWebProxyScriptHelper.cs
- ColumnMapTranslator.cs
- XmlDownloadManager.cs
- VariableAction.cs
- ConvertEvent.cs
- ExpressionList.cs
- DataPagerFieldCollection.cs
- ChildrenQuery.cs
- XmlSequenceWriter.cs
- ErrorRuntimeConfig.cs
- securitycriticaldata.cs
- IsolatedStorage.cs
- TextEditorCopyPaste.cs
- WindowsBrush.cs
- FormViewActionList.cs
- SHA1Managed.cs
- EntityCollectionChangedParams.cs
- Message.cs
- NullExtension.cs
- FloaterBaseParaClient.cs
- FilterElement.cs
- ResourceReferenceExpression.cs
- BindingValueChangedEventArgs.cs
- GPPOINT.cs
- SoapSchemaExporter.cs
- CurrentChangingEventManager.cs
- ClientSideProviderDescription.cs
- DelayedRegex.cs
- MaskedTextBox.cs
- JpegBitmapDecoder.cs
- Profiler.cs
- NativeActivityContext.cs
- ImageEditor.cs
- TextInfo.cs
- SimpleApplicationHost.cs
- Transform.cs
- SessionIDManager.cs
- EnvelopeVersion.cs
- FileAuthorizationModule.cs
- ErrorInfoXmlDocument.cs
- ReadOnlyDictionary.cs
- NoResizeSelectionBorderGlyph.cs
- DefaultProxySection.cs
- translator.cs
- ImportCatalogPart.cs
- MsmqIntegrationAppDomainProtocolHandler.cs
- GridViewCommandEventArgs.cs
- SubqueryTrackingVisitor.cs
- MenuBindingsEditorForm.cs
- Hyperlink.cs
- HandledMouseEvent.cs
- MimeTypeMapper.cs
- Group.cs
- RadioButton.cs
- EntityTransaction.cs