internal static NativeHeapMemoryBlock ReadMemoryBlock(Stream stream, int start, int size)
        {
            var fileStream = stream as FileStream;

            if (fileStream != null)
            {
                var  block = new NativeHeapMemoryBlock(size);
                bool fault = true;
                try
                {
                    stream.Seek(start, SeekOrigin.Begin);
                    int bytesRead;
                    if (!ReadFile(fileStream.SafeFileHandle, block.Pointer, size, out bytesRead, IntPtr.Zero) || bytesRead != size)
                    {
                        throw new IOException(CodeAnalysisResources.UnableToReadMetadataFile, Marshal.GetLastWin32Error());
                    }

                    fault = false;
                }
                finally
                {
                    if (fault)
                    {
                        block.Dispose();
                    }
                }

                return(block);
            }
            else
            {
                var block = new NativeHeapMemoryBlock(size);

                bool fault = true;
                try
                {
                    stream.Seek(start, SeekOrigin.Begin);
                    stream.CopyTo(block.Pointer, size);
                    fault = false;
                }
                finally
                {
                    if (fault)
                    {
                        block.Dispose();
                    }
                }

                return(block);
            }
        }
Пример #2
0
        internal static NativeHeapMemoryBlock ReadMemoryBlock(Stream stream, int start, int size)
        {
            var fileStream = stream as FileStream;
            if (fileStream != null)
            {
                var block = new NativeHeapMemoryBlock(size);
                bool fault = true;
                try
                {
                    stream.Seek(start, SeekOrigin.Begin);
                    int bytesRead;
                    if (!ReadFile(fileStream.SafeFileHandle, block.Pointer, size, out bytesRead, IntPtr.Zero) || bytesRead != size)
                    {
                        throw new IOException(CodeAnalysisResources.UnableToReadMetadataFile, Marshal.GetLastWin32Error());
                    }

                    fault = false;
                }
                finally
                {
                    if (fault)
                    {
                        block.Dispose();
                    }
                }

                return block;
            }
            else
            {
                var block = new NativeHeapMemoryBlock(size);

                bool fault = true;
                try
                {
                    stream.Seek(start, SeekOrigin.Begin);
                    stream.CopyTo(block.Pointer, size);
                    fault = false;
                }
                finally
                {
                    if (fault)
                    {
                        block.Dispose();
                    }
                }

                return block;
            }
        }