3 // Copyright (c) Microsoft Corporation. All rights reserved.
6 /*============================================================
8 ** Class: MemoryMappedViewStream
10 ** Purpose: View stream for managed MemoryMappedFiles.
12 ** Date: February 7, 2007
14 ===========================================================*/
17 using System.Diagnostics;
18 using System.Security;
19 using System.Security.Permissions;
20 using Microsoft.Win32.SafeHandles;
22 namespace System.IO.MemoryMappedFiles {
24 public sealed class MemoryMappedViewStream : UnmanagedMemoryStream {
26 private MemoryMappedView m_view;
28 [System.Security.SecurityCritical]
29 internal unsafe MemoryMappedViewStream(MemoryMappedView view) {
30 Debug.Assert(view != null, "view is null");
33 Initialize(m_view.ViewHandle, m_view.PointerOffset, m_view.Size, MemoryMappedFile.GetFileAccess(m_view.Access));
36 public SafeMemoryMappedViewHandle SafeMemoryMappedViewHandle {
38 [System.Security.SecurityCritical]
39 [SecurityPermissionAttribute(SecurityAction.Demand, Flags = SecurityPermissionFlag.UnmanagedCode)]
41 return m_view != null ? m_view.ViewHandle : null;
45 public override void SetLength(long value) {
46 throw new NotSupportedException(SR.GetString(SR.NotSupported_MMViewStreamsFixedLength));
49 public long PointerOffset
55 throw new InvalidOperationException(SR.GetString(SR.InvalidOperation_ViewIsNull));
58 return m_view.PointerOffset;
62 [SecuritySafeCritical]
63 protected override void Dispose(bool disposing) {
65 if (disposing && m_view != null && !m_view.IsClosed) {
76 base.Dispose(disposing);
81 // Flushes the changes such that they are in [....] with the FileStream bits (ones obtained
82 // with the win32 ReadFile and WriteFile functions). Need to call FileStream's Flush to
84 // NOTE: This will flush all bytes before and after the view up until an offset that is a
85 // multiple of SystemPageSize.
86 [System.Security.SecurityCritical]
87 public override void Flush() {
89 __Error.StreamIsClosed();
94 m_view.Flush((IntPtr)Capacity);