Home
Discussion Forums
Tutorials & Source Code
Developer Blogs
Developer Events
Developer Jobs
Labs
We need you!
We're working hard on the next version of Developer Fusion -
Let us know
what you think we should be up to!
The Labs
\
Source Viewer
\
SSCLI
\
System.IO
\ PinnedBufferMemoryStream
Object Browser
Solution Explorer
SSCLI
Expand
Microsoft.Win32
Expand
Microsoft.Win32.SafeHandles
Expand
System
Expand
System.Runtime.Hosting
Expand
System.Runtime.InteropServices
Expand
System.Runtime.CompilerServices
Expand
System.Collections
Expand
System.Collections.Generic
Expand
System.Collections.ObjectModel
Expand
System.Configuration.Assemblies
Expand
System.Diagnostics
Expand
System.Diagnostics.CodeAnalysis
Expand
System.Diagnostics.SymbolStore
Expand
System.Globalization
Expand
System.IO
Expand
__ConsoleStream
Expand
__DebugOutputTextWriter
Expand
__Error
Expand
__HResults
Expand
BinaryReader
Expand
BinaryWriter
Expand
BufferedStream
Expand
ByteBufferAllocator
Expand
ByteBufferPool
Expand
Directory
Expand
DirectoryInfo
Expand
DirectoryNotFoundException
Expand
DriveInfo
Expand
DriveNotFoundException
Expand
DriveType
Expand
EndOfStreamException
Expand
File
Expand
FileAccess
Expand
FileAttributes
Expand
FileInfo
Expand
FileLoadException
Expand
FileMode
Expand
FileNotFoundException
Expand
FileOptions
Expand
FileShare
Expand
FileStream
Expand
FileStreamAsyncResult
Expand
FileSystemInfo
Expand
IByteBufferPool
Expand
InvalidDataException
Expand
IOException
Expand
MemoryStream
Expand
NullStream
Expand
NullStreamReader
Expand
NullTextReader
Expand
NullTextWriter
Expand
Path
Expand
PathTooLongException
Expand
PinnedBufferMemoryStream
Dispose(bool)
PinnedBufferMemoryStream(byte)
Expand
SearchData
Expand
SearchOption
Expand
SeekOrigin
Expand
Stream
Expand
StreamReader
Expand
StreamWriter
Expand
StringReader
Expand
StringWriter
Expand
SyncStream
Expand
SyncTextReader
Expand
SyncTextWriter
Expand
TextReader
Expand
TextWriter
Expand
UnmanagedMemoryStream
Expand
UnmanagedMemoryStreamWrapper
Expand
System.IO.IsolatedStorage
Expand
System.Reflection
Expand
System.Reflection.Cache
Expand
System.Reflection.Emit
Expand
System.Resources
Expand
System.Runtime
Expand
System.Runtime.InteropServices.Expando
Expand
System.Runtime.ConstrainedExecution
Expand
System.Runtime.Remoting.Activation
Expand
System.Runtime.Remoting.Messaging
Expand
System.Runtime.Remoting.Channels
Expand
System.Runtime.Remoting.Lifetime
Expand
System.Runtime.Remoting
Expand
System.Runtime.Remoting.Contexts
Expand
System.Runtime.Remoting.Proxies
Expand
System.Runtime.Remoting.Metadata
Expand
System.Runtime.Remoting.Metadata.W3cXsd2001
Expand
System.Runtime.Remoting.Services
Expand
System.Runtime.Serialization
Expand
System.Runtime.Serialization.Formatters
Expand
System.Runtime.Serialization.Formatters.Binary
Expand
System.Runtime.Versioning
Expand
System.Security
Expand
System.Security.Cryptography
Expand
System.Security.Cryptography.X509Certificates
Expand
System.Security.Permissions
Expand
System.Security.Policy
Expand
System.Security.Principal
Expand
System.Security.Util
Expand
System.Text
Expand
System.Threading
Expand
System.Runtime.Remoting.Channels.Http
Expand
System.Runtime.Remoting.Channels.Tcp
Expand
System.Runtime.Remoting.MetadataServices
Expand
System.Runtime.Serialization.Formatters.Soap.Xml
Expand
System.Runtime.Serialization.Formatters.Soap
Expand
Microsoft.Tools.Caspol
Expand
Microsoft.Tools.Build
Expand
Microsoft.CSharp
Expand
System.CodeDom
Expand
System.CodeDom.Compiler
Expand
System.Collections.Specialized
Expand
System.ComponentModel
Expand
System.ComponentModel.Design
Expand
System.ComponentModel.Design.Serialization
Expand
System.Configuration
Expand
System.Web
Expand
System.Configuration.Provider
Expand
System.Configuration.Internal
Expand
System.Internal
Expand
System.Windows.Forms
Expand
System.Net
Expand
System.Net.Sockets
Expand
System.Net.NetworkInformation
Expand
System.Net.ComImports
Expand
System.Net.Security
Expand
System.Net.Cache
Expand
System.Net.Configuration
Expand
System.Text.RegularExpressions
Expand
System.Tools
Expand
System.IO.Compression
Expand
System.IO.Ports
Expand
System.Security.AccessControl
Expand
System.Xml
Expand
System.Xml.XPath.DataBinding
Expand
System.Xml.Schema
Expand
System.Xml.Serialization
Expand
System.Xml.Serialization.Advanced
Expand
System.Xml.Serialization.Configuration
Expand
System.Xml.XPath
Expand
System.Xml.Xsl
Expand
System.Xml.Xsl.IlGen
Expand
System.Xml.Xsl.Qil
Expand
System.Xml.Xsl.Runtime
Expand
System.Xml.Xsl.XPath
Expand
System.Xml.Xsl.Xslt
Expand
System.Xml.Xsl.XsltOld
Expand
System.Xml.Xsl.XsltOld.Debugger
Expand
Microsoft.JScript
Expand
Microsoft.Vsa
Expand
Microsoft.JScript.Vsa
C#
VB.NET
// ==++==
//
//
// Copyright (c) 2006 Microsoft Corporation. All rights reserved.
//
// The use and distribution terms for this software are contained in the file
// named license.txt, which can be found in the root of this distribution.
// By using this software in any fashion, you are agreeing to be bound by the
// terms of this license.
//
// You must not remove this notice, or any other, from this software.
//
//
// ==--==
/*============================================================
**
** 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
;
namespace
System.IO
{
unsafe
internal
sealed
class
PinnedBufferMemoryStream
:
UnmanagedMemoryStream
{
private
byte
[] _array;
private
GCHandle
_pinningHandle;
internal
PinnedBufferMemoryStream(
byte
[] array)
{
BCLDebug.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
);
}
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);
}
}
}
Developer Fusion