-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMapViewStream.cs
More file actions
229 lines (187 loc) · 6.55 KB
/
Copy pathMapViewStream.cs
File metadata and controls
229 lines (187 loc) · 6.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
//
// MapViewStream.cs
//
// Implementation of a library to use Win32 Memory Mapped
// Files from within .NET applications
//
// COPYRIGHT (C) 2001, Tomas Restrepo (tomasr@mvps.org)
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
using System;
using System.IO;
using System.Runtime.InteropServices;
namespace Winterdom.IO.FileMap
{
/// <summary>
/// Specifies page protection for the mapped file
/// These correspond to the PAGE_XXX set of flags
/// passed to CreateFileMapping()
/// </summary>
[ Flags ]
public enum MapProtection
{
PageNone = 0x00000000,
// protection
PageReadOnly = 0x00000002,
PageReadWrite = 0x00000004,
PageWriteCopy = 0x00000008,
// attributes
SecImage = 0x01000000,
SecReserve = 0x04000000,
SecCommit = 0x08000000,
SecNoCache = 0x10000000,
}
/// <summary>
/// Allows you to read/write from/to
/// a view of a memory mapped file.
/// </summary>
public class MapViewStream : Stream, IDisposable
{
#region variables
//! What's our access?
MapProtection _protection = MapProtection.PageNone;
//! base address of our buffer
IntPtr _base = IntPtr.Zero;
//! our current buffer length
long _length = 0;
//! our current position in the stream buffer
long _position = 0;
//! are we open?
bool _isOpen = false;
#endregion // variables
/// <summary>
/// Constructor used internally by MemoryMappedFile.
/// </summary>
/// <param name="baseAddress">base address where the view starts</param>
/// <param name="length">Length of view, in bytes</param>
/// <param name="protection"></param>
internal MapViewStream ( IntPtr baseAddress, long length,
MapProtection protection )
{
_base = baseAddress;
_length = length;
_protection = protection;
_position = 0;
_isOpen = (baseAddress != IntPtr.Zero);
}
~MapViewStream()
{
Dispose(false);
}
#region Properties
public override bool CanRead
{
get { return true; }
}
public override bool CanSeek {
get { return true; }
}
public override bool CanWrite {
get { return (((int)_protection) & 0x000000C) != 0; }
}
public override long Length {
get { return _length; }
}
public override long Position {
get { return _position; }
set { Seek(value, SeekOrigin.Begin); }
}
private bool IsOpen {
get { return _isOpen; }
set { _isOpen = value; }
}
#endregion // Properties
#region Stream Methods
public override void Flush()
{
if ( !IsOpen )
throw new ObjectDisposedException ( "Stream is closed" );
// flush the view but leave the buffer intact
// FIX: get rid of cast
Win32MapApis.FlushViewOfFile ( _base, (int)_length );
}
public override int Read ( byte[] buffer, int offset, int count )
{
if ( !IsOpen )
throw new ObjectDisposedException ( "Stream is closed" );
if ( buffer.Length - offset < count )
throw new ArgumentException ( "Invalid Offset" );
int bytesToRead = (int)Math.Min(Length-_position, count);
Marshal.Copy((IntPtr)(_base.ToInt64()+_position), buffer, offset, bytesToRead);
_position += bytesToRead;
return bytesToRead;
}
public override void Write ( byte[] buffer, int offset, int count )
{
if ( !this.IsOpen )
throw new ObjectDisposedException("Stream is closed");
if ( !this.CanWrite )
throw new FileMapIOException ( "Stream cannot be written to" );
if ( buffer.Length - offset < count )
throw new ArgumentException ( "Invalid Offset" );
int bytesToWrite = (int)Math.Min(Length-_position, count);
if ( bytesToWrite==0 )
return;
Marshal.Copy(buffer, offset, (IntPtr)(_base.ToInt64()+_position), bytesToWrite);
_position += bytesToWrite;
}
public override long Seek ( long offset, SeekOrigin origin )
{
if ( !IsOpen )
throw new ObjectDisposedException ( "Stream is closed" );
long newpos = 0;
switch ( origin )
{
case SeekOrigin.Begin: newpos = offset; break;
case SeekOrigin.Current: newpos = Position + offset; break;
case SeekOrigin.End: newpos = Length + offset; break;
}
// sanity check
if ( newpos < 0 || newpos > Length )
throw new FileMapIOException ( "Invalid Seek Offset" );
_position = newpos;
return newpos;
}
public override void SetLength ( long value )
{
// not supported!
throw new NotSupportedException ( "Can't change View Size" );
}
public override void Close()
{
Dispose(true);
}
#endregion // Stream methods
#region IDisposable Implementation
public void Dispose()
{
Dispose(true);
}
protected virtual void Dispose(bool disposing)
{
if ( IsOpen )
{
Flush();
// FIX: eliminate cast
Win32MapApis.UnmapViewOfFile ( _base );
IsOpen = false;
}
if ( disposing )
GC.SuppressFinalize(this);
}
#endregion // IDisposable Implementation
} // class MapViewStream
} // namespace Winterdom.IO.FileMap