Code:
/ 4.0 / 4.0 / untmp / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / clr / src / BCL / System / IO / PinnedBufferMemoryStream.cs / 1305376 / PinnedBufferMemoryStream.cs
// ==++== // // Copyright (c) Microsoft Corporation. All rights reserved. // // ==--== /*============================================================ ** ** Class: PinnedBufferMemoryStream ** **[....] ** ** ** Purpose: Pins a byte[], exposing it as an unmanaged memory ** stream. Used in ResourceReader for corner cases. ** ** ===========================================================*/ using System; using System.Runtime.InteropServices; using System.Diagnostics.Contracts; namespace System.IO { internal sealed unsafe class PinnedBufferMemoryStream : UnmanagedMemoryStream { private byte[] _array; private GCHandle _pinningHandle; // The new inheritance model requires a Critical default ctor since base (UnmanagedMemoryStream) has one [System.Security.SecurityCritical] private PinnedBufferMemoryStream():base(){} [System.Security.SecurityCritical] // auto-generated internal PinnedBufferMemoryStream(byte[] array) { Contract.Assert(array != null, "Array can't be null"); int len = array.Length; // Handle 0 length byte arrays specially. if (len == 0) { array = new byte[1]; len = 0; } _array = array; _pinningHandle = new GCHandle(array, GCHandleType.Pinned); // Now the byte[] is pinned for the lifetime of this instance. // But I also need to get a pointer to that block of memory... fixed(byte* ptr = _array) Initialize(ptr, len, len, FileAccess.Read, true); } ~PinnedBufferMemoryStream() { Dispose(false); } [System.Security.SecuritySafeCritical] // auto-generated protected override void Dispose(bool disposing) { if (_isOpen) { _pinningHandle.Free(); _isOpen = false; } #if _DEBUG // To help track down lifetime issues on checked builds, force //a full GC here. if (disposing) { GC.Collect(); GC.WaitForPendingFinalizers(); } #endif base.Dispose(disposing); } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- VersionValidator.cs
- MetadataPropertyAttribute.cs
- SynchronizedDispatch.cs
- IgnoreSectionHandler.cs
- HotSpotCollection.cs
- DesignerActionMethodItem.cs
- DataGridViewColumnStateChangedEventArgs.cs
- SuppressMergeCheckAttribute.cs
- HttpApplicationFactory.cs
- AuthorizationPolicyTypeElement.cs
- ConfigXmlSignificantWhitespace.cs
- XamlHostingConfiguration.cs
- ParameterReplacerVisitor.cs
- CommandTreeTypeHelper.cs
- BitmapEffect.cs
- SmtpClient.cs
- DocumentGrid.cs
- ListBoxItem.cs
- XmlSchemaObjectCollection.cs
- SwitchAttribute.cs
- HtmlDocument.cs
- DataGridPagerStyle.cs
- StringCollectionEditor.cs
- DatagramAdapter.cs
- RolePrincipal.cs
- DisplayInformation.cs
- HintTextMaxWidthConverter.cs
- PrintDialog.cs
- ImageCollectionEditor.cs
- Point3DConverter.cs
- FontFamily.cs
- PerformanceCounter.cs
- GestureRecognizer.cs
- PathFigureCollectionConverter.cs
- SQLMoneyStorage.cs
- EmitterCache.cs
- BamlVersionHeader.cs
- SapiInterop.cs
- ExtendedPropertiesHandler.cs
- CustomTokenProvider.cs
- SiteMapDataSourceDesigner.cs
- CancellationToken.cs
- StringExpressionSet.cs
- DoubleLinkListEnumerator.cs
- ToolZoneDesigner.cs
- WebAdminConfigurationHelper.cs
- TransportationConfigurationTypeInstallComponent.cs
- XPathSelfQuery.cs
- MembershipValidatePasswordEventArgs.cs
- Region.cs
- DependencyPropertyValueSerializer.cs
- FixUp.cs
- WebPartUserCapability.cs
- SchemaHelper.cs
- InternalDispatchObject.cs
- MetadataArtifactLoader.cs
- TimersDescriptionAttribute.cs
- StyleCollection.cs
- Byte.cs
- AstTree.cs
- Pointer.cs
- WindowProviderWrapper.cs
- RestHandler.cs
- IsolatedStorageFilePermission.cs
- DateTimeConverter.cs
- SrgsDocumentParser.cs
- DataBoundControlHelper.cs
- TraceUtility.cs
- X509SubjectKeyIdentifierClause.cs
- PeerToPeerException.cs
- _FtpDataStream.cs
- InfoCardRSAOAEPKeyExchangeFormatter.cs
- ParameterExpression.cs
- GridEntryCollection.cs
- UrlMappingCollection.cs
- StyleHelper.cs
- TriState.cs
- WindowsAuthenticationEventArgs.cs
- CheckableControlBaseAdapter.cs
- XmlDesigner.cs
- InvalidComObjectException.cs
- SmiTypedGetterSetter.cs
- IntSecurity.cs
- RuntimeResourceSet.cs
- CacheOutputQuery.cs
- ButtonChrome.cs
- InternalResources.cs
- WebEventTraceProvider.cs
- PostBackOptions.cs
- HuffCodec.cs
- EventData.cs
- ButtonChrome.cs
- Point.cs
- InvokePattern.cs
- CacheAxisQuery.cs
- DependencyObjectProvider.cs
- WebBrowserContainer.cs
- EntryWrittenEventArgs.cs
- GcHandle.cs
- QuaternionConverter.cs