2 // System.IO.UnmanagedMemoryAccessor.cs
5 // Zoltan Varga (vargaz@gmail.com)
6 // Marek Safar (marek.safar@gmail.com)
8 // Copyright (C) 2009 Novell, Inc (http://www.novell.com)
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 using System.Runtime.InteropServices;
34 using System.Security.Permissions;
38 [MonoTODO ("Offset is ignored")]
39 public class UnmanagedMemoryAccessor : IDisposable {
41 #pragma warning disable 414
43 #pragma warning restore
45 bool canwrite, canread;
47 protected UnmanagedMemoryAccessor ()
51 public UnmanagedMemoryAccessor (SafeBuffer buffer, long offset, long capacity)
53 Initialize (buffer, offset, capacity, FileAccess.ReadWrite);
56 public UnmanagedMemoryAccessor (SafeBuffer buffer, long offset, long capacity, FileAccess access)
58 Initialize (buffer, offset, capacity, access);
61 [SecurityPermissionAttribute(SecurityAction.Demand, Flags = SecurityPermissionFlag.UnmanagedCode)]
62 protected void Initialize(SafeBuffer buffer, long offset, long capacity, FileAccess access)
65 throw new ArgumentNullException ("buffer");
67 throw new ArgumentOutOfRangeException ("offset");
69 throw new ArgumentOutOfRangeException ("capacity");
71 if (access == FileAccess.Read || access == FileAccess.ReadWrite)
73 if (access == FileAccess.Write || access == FileAccess.ReadWrite)
76 if (this.buffer != null)
81 this.capacity = capacity;
84 public void Dispose ()
87 GC.SuppressFinalize (this);
90 protected virtual void Dispose (bool disposing)
100 public byte ReadByte (long position)
103 throw new NotSupportedException ();
105 throw new ObjectDisposedException ("buffer");
107 throw new ArgumentOutOfRangeException ();
109 return buffer.Read<byte> ((ulong) position);
112 public bool ReadBoolean (long position)
115 throw new NotSupportedException ();
117 throw new ObjectDisposedException ("buffer");
119 throw new ArgumentOutOfRangeException ();
121 return buffer.Read<bool> ((ulong) position);
124 public char ReadChar (long position)
127 throw new NotSupportedException ();
129 throw new ObjectDisposedException ("buffer");
131 throw new ArgumentOutOfRangeException ();
133 return buffer.Read<char> ((ulong) position);
136 public decimal ReadDecimal (long position)
139 throw new NotSupportedException ();
141 throw new ObjectDisposedException ("buffer");
143 throw new ArgumentOutOfRangeException ();
145 return buffer.Read<decimal> ((ulong) position);
148 public double ReadDouble (long position)
151 throw new NotSupportedException ();
153 throw new ObjectDisposedException ("buffer");
155 throw new ArgumentOutOfRangeException ();
157 return buffer.Read<double> ((ulong) position);
160 public short ReadInt16 (long position)
163 throw new NotSupportedException ();
165 throw new ObjectDisposedException ("buffer");
167 throw new ArgumentOutOfRangeException ();
169 return buffer.Read<short> ((ulong) position);
172 public int ReadInt32 (long position)
175 throw new NotSupportedException ();
177 throw new ObjectDisposedException ("buffer");
179 throw new ArgumentOutOfRangeException ();
181 return buffer.Read<int> ((ulong) position);
184 public long ReadInt64 (long position)
187 throw new NotSupportedException ();
189 throw new ObjectDisposedException ("buffer");
191 throw new ArgumentOutOfRangeException ();
193 return buffer.Read<long> ((ulong) position);
196 [CLSCompliant (false)]
197 public sbyte ReadSByte (long position)
200 throw new NotSupportedException ();
202 throw new ObjectDisposedException ("buffer");
204 throw new ArgumentOutOfRangeException ();
206 return buffer.Read<sbyte> ((ulong) position);
209 public float ReadSingle (long position)
212 throw new NotSupportedException ();
214 throw new ObjectDisposedException ("buffer");
216 throw new ArgumentOutOfRangeException ();
218 return buffer.Read<float> ((ulong) position);
221 [CLSCompliant (false)]
222 public ushort ReadUInt16 (long position)
225 throw new NotSupportedException ();
227 throw new ObjectDisposedException ("buffer");
229 throw new ArgumentOutOfRangeException ();
231 return buffer.Read<ushort> ((ulong) position);
234 [CLSCompliant (false)]
235 public uint ReadUInt32 (long position)
238 throw new NotSupportedException ();
240 throw new ObjectDisposedException ("buffer");
242 throw new ArgumentOutOfRangeException ();
244 return buffer.Read<uint> ((ulong) position);
247 [CLSCompliant (false)]
248 public ulong ReadUInt64 (long position)
251 throw new NotSupportedException ();
253 throw new ObjectDisposedException ("buffer");
255 throw new ArgumentOutOfRangeException ();
257 return buffer.Read<ulong> ((ulong) position);
260 public void Read<T> (long position, out T structure) where T : struct
263 throw new NotSupportedException ();
265 throw new ObjectDisposedException ("buffer");
267 throw new ArgumentOutOfRangeException ();
269 structure = buffer.Read<T> ((ulong) position);
272 public int ReadArray<T> (long position, T [] array, int offset, int count) where T : struct
275 throw new ArgumentOutOfRangeException ();
277 long left = capacity - position;
278 var slots = Math.Min (count, (int)(left / Marshal.SizeOf (typeof (T))));
280 buffer.ReadArray ((ulong) position, array, offset, slots);
284 public void Write (long position, bool value)
287 throw new NotSupportedException ();
289 throw new ObjectDisposedException ("buffer");
291 throw new ArgumentOutOfRangeException ();
293 buffer.Write ((ulong)position, value);
296 public void Write (long position, byte value)
299 throw new NotSupportedException ();
301 throw new ObjectDisposedException ("buffer");
303 throw new ArgumentOutOfRangeException ();
305 buffer.Write ((ulong)position, value);
308 public void Write (long position, char value)
311 throw new NotSupportedException ();
313 throw new ObjectDisposedException ("buffer");
315 throw new ArgumentOutOfRangeException ();
317 buffer.Write ((ulong)position, value);
320 public void Write (long position, decimal value)
323 throw new NotSupportedException ();
325 throw new ObjectDisposedException ("buffer");
327 throw new ArgumentOutOfRangeException ();
329 buffer.Write ((ulong)position, value);
332 public void Write (long position, double value)
335 throw new NotSupportedException ();
337 throw new ObjectDisposedException ("buffer");
339 throw new ArgumentOutOfRangeException ();
341 buffer.Write ((ulong)position, value);
344 public void Write (long position, short value)
347 throw new NotSupportedException ();
349 throw new ObjectDisposedException ("buffer");
351 throw new ArgumentOutOfRangeException ();
353 buffer.Write ((ulong)position, value);
356 public void Write (long position, int value)
359 throw new NotSupportedException ();
361 throw new ObjectDisposedException ("buffer");
363 throw new ArgumentOutOfRangeException ();
365 buffer.Write ((ulong)position, value);
368 public void Write (long position, long value)
371 throw new NotSupportedException ();
373 throw new ObjectDisposedException ("buffer");
375 throw new ArgumentOutOfRangeException ();
377 buffer.Write ((ulong)position, value);
380 [CLSCompliant (false)]
381 public void Write (long position, sbyte value)
384 throw new NotSupportedException ();
386 throw new ObjectDisposedException ("buffer");
388 throw new ArgumentOutOfRangeException ();
390 buffer.Write ((ulong)position, value);
393 public void Write (long position, float value)
396 throw new NotSupportedException ();
398 throw new ObjectDisposedException ("buffer");
400 throw new ArgumentOutOfRangeException ();
402 buffer.Write ((ulong)position, value);
405 [CLSCompliant (false)]
406 public void Write (long position, ushort value)
409 throw new NotSupportedException ();
411 throw new ObjectDisposedException ("buffer");
413 throw new ArgumentOutOfRangeException ();
415 buffer.Write ((ulong)position, value);
418 [CLSCompliant (false)]
419 public void Write (long position, uint value)
422 throw new NotSupportedException ();
424 throw new ObjectDisposedException ("buffer");
426 throw new ArgumentOutOfRangeException ();
428 buffer.Write ((ulong)position, value);
431 [CLSCompliant (false)]
432 public void Write (long position, ulong value)
435 throw new NotSupportedException ();
437 throw new ObjectDisposedException ("buffer");
439 throw new ArgumentOutOfRangeException ();
441 buffer.Write ((ulong)position, value);
444 public void Write<T> (long position, ref T structure) where T : struct
447 throw new NotSupportedException ();
449 throw new ObjectDisposedException ("buffer");
451 throw new ArgumentOutOfRangeException ();
453 buffer.Write<T> ((ulong)position, structure);
456 public void WriteArray<T> (long position, T [] array, int offset, int count) where T : struct
458 buffer.WriteArray ((ulong)position, array, offset, count);
461 public bool CanRead {
462 get { return canread; }
465 public bool CanWrite {
466 get { return canwrite; }
469 public long Capacity {
470 get { return capacity; }
473 protected bool IsOpen {
474 get { return buffer != null; }