Update Reference Sources to .NET Framework 4.6.1
[mono.git] / mcs / class / referencesource / System.Core / System / IO / MemoryMappedFiles / MemoryMappedViewStream.cs
1 // ==++==
2 // 
3 //   Copyright (c) Microsoft Corporation.  All rights reserved.
4 // 
5 // ==--==
6 /*============================================================
7 **
8 ** Class:    MemoryMappedViewStream
9 **
10 ** Purpose:  View stream for managed MemoryMappedFiles.
11 **
12 ** Date:  February 7, 2007
13 **
14 ===========================================================*/
15
16 using System;
17 using System.Diagnostics;
18 using System.Security;
19 using System.Security.Permissions;
20 using Microsoft.Win32.SafeHandles;
21
22 namespace System.IO.MemoryMappedFiles {
23
24     public sealed class MemoryMappedViewStream : UnmanagedMemoryStream {
25
26         private MemoryMappedView m_view;
27
28         [System.Security.SecurityCritical]
29         internal unsafe MemoryMappedViewStream(MemoryMappedView view) {
30             Debug.Assert(view != null, "view is null");
31
32             m_view = view;
33             Initialize(m_view.ViewHandle, m_view.PointerOffset, m_view.Size, MemoryMappedFile.GetFileAccess(m_view.Access));
34         }
35
36         public SafeMemoryMappedViewHandle SafeMemoryMappedViewHandle {
37
38             [System.Security.SecurityCritical]
39             [SecurityPermissionAttribute(SecurityAction.Demand, Flags = SecurityPermissionFlag.UnmanagedCode)]
40             get {
41                 return m_view != null ? m_view.ViewHandle : null; 
42             }
43         }
44
45         public override void SetLength(long value) {
46             throw new NotSupportedException(SR.GetString(SR.NotSupported_MMViewStreamsFixedLength));
47         }
48
49         public long PointerOffset
50         {
51             get
52             {
53                 if (m_view == null)
54                 {
55                     throw new InvalidOperationException(SR.GetString(SR.InvalidOperation_ViewIsNull));
56                 }
57
58                 return m_view.PointerOffset;
59             }
60         }
61
62         [SecuritySafeCritical]
63         protected override void Dispose(bool disposing) {
64             try {
65                 if (disposing && m_view != null && !m_view.IsClosed) {
66                     Flush();
67                 }
68             }
69             finally {
70                 try {
71                     if (m_view != null) {
72                         m_view.Dispose();
73                     }
74                 }
75                 finally {
76                     base.Dispose(disposing);
77                 }
78             }
79         }
80
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 
83         // flush to the disk.
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() {
88             if (!CanSeek) {
89                 __Error.StreamIsClosed();
90             }
91
92             unsafe {
93                 if (m_view != null) {
94                     m_view.Flush((IntPtr)Capacity);
95                 }
96             }
97         }
98
99     }
100 }