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.
32 using System.Runtime.InteropServices;
33 using System.Security.Permissions;
37 [MonoTODO ("Offset is ignored")]
38 public class UnmanagedMemoryAccessor : IDisposable {
40 #pragma warning disable 414
42 #pragma warning restore
44 bool canwrite, canread;
46 protected UnmanagedMemoryAccessor ()
50 public UnmanagedMemoryAccessor (SafeBuffer buffer, long offset, long capacity)
52 Initialize (buffer, offset, capacity, FileAccess.ReadWrite);
55 public UnmanagedMemoryAccessor (SafeBuffer buffer, long offset, long capacity, FileAccess access)
57 Initialize (buffer, offset, capacity, access);
60 [SecurityPermissionAttribute(SecurityAction.Demand, Flags = SecurityPermissionFlag.UnmanagedCode)]
61 protected void Initialize(SafeBuffer buffer, long offset, long capacity, FileAccess access)
64 throw new ArgumentNullException ("buffer");
66 throw new ArgumentOutOfRangeException ("offset");
68 throw new ArgumentOutOfRangeException ("capacity");
70 if (access == FileAccess.Read || access == FileAccess.ReadWrite)
72 if (access == FileAccess.Write || access == FileAccess.ReadWrite)
75 if (this.buffer != null)
80 this.capacity = capacity;
83 public void Dispose ()
86 GC.SuppressFinalize (this);
89 protected virtual void Dispose (bool disposing)
99 public byte ReadByte (long position)
102 throw new NotSupportedException ();
104 throw new ObjectDisposedException ("buffer");
106 throw new ArgumentOutOfRangeException ();
108 return buffer.Read<byte> ((ulong) position);
111 public bool ReadBoolean (long position)
114 throw new NotSupportedException ();
116 throw new ObjectDisposedException ("buffer");
118 throw new ArgumentOutOfRangeException ();
120 return buffer.Read<bool> ((ulong) position);
123 public char ReadChar (long position)
126 throw new NotSupportedException ();
128 throw new ObjectDisposedException ("buffer");
130 throw new ArgumentOutOfRangeException ();
132 return buffer.Read<char> ((ulong) position);
135 public decimal ReadDecimal (long position)
138 throw new NotSupportedException ();
140 throw new ObjectDisposedException ("buffer");
142 throw new ArgumentOutOfRangeException ();
144 return buffer.Read<decimal> ((ulong) position);
147 public double ReadDouble (long position)
150 throw new NotSupportedException ();
152 throw new ObjectDisposedException ("buffer");
154 throw new ArgumentOutOfRangeException ();
156 return buffer.Read<double> ((ulong) position);
159 public short ReadInt16 (long position)
162 throw new NotSupportedException ();
164 throw new ObjectDisposedException ("buffer");
166 throw new ArgumentOutOfRangeException ();
168 return buffer.Read<short> ((ulong) position);
171 public int ReadInt32 (long position)
174 throw new NotSupportedException ();
176 throw new ObjectDisposedException ("buffer");
178 throw new ArgumentOutOfRangeException ();
180 return buffer.Read<int> ((ulong) position);
183 public long ReadInt64 (long position)
186 throw new NotSupportedException ();
188 throw new ObjectDisposedException ("buffer");
190 throw new ArgumentOutOfRangeException ();
192 return buffer.Read<long> ((ulong) position);
195 [CLSCompliant (false)]
196 public sbyte ReadSByte (long position)
199 throw new NotSupportedException ();
201 throw new ObjectDisposedException ("buffer");
203 throw new ArgumentOutOfRangeException ();
205 return buffer.Read<sbyte> ((ulong) position);
208 public float ReadSingle (long position)
211 throw new NotSupportedException ();
213 throw new ObjectDisposedException ("buffer");
215 throw new ArgumentOutOfRangeException ();
217 return buffer.Read<float> ((ulong) position);
220 [CLSCompliant (false)]
221 public ushort ReadUInt16 (long position)
224 throw new NotSupportedException ();
226 throw new ObjectDisposedException ("buffer");
228 throw new ArgumentOutOfRangeException ();
230 return buffer.Read<ushort> ((ulong) position);
233 [CLSCompliant (false)]
234 public uint ReadUInt32 (long position)
237 throw new NotSupportedException ();
239 throw new ObjectDisposedException ("buffer");
241 throw new ArgumentOutOfRangeException ();
243 return buffer.Read<uint> ((ulong) position);
246 [CLSCompliant (false)]
247 public ulong ReadUInt64 (long position)
250 throw new NotSupportedException ();
252 throw new ObjectDisposedException ("buffer");
254 throw new ArgumentOutOfRangeException ();
256 return buffer.Read<ulong> ((ulong) position);
259 public void Read<T> (long position, out T structure) where T : struct
262 throw new NotSupportedException ();
264 throw new ObjectDisposedException ("buffer");
266 throw new ArgumentOutOfRangeException ();
268 structure = buffer.Read<T> ((ulong) position);
271 public int ReadArray<T> (long position, T [] array, int offset, int count) where T : struct
274 throw new ArgumentOutOfRangeException ();
276 long left = capacity - position;
277 var slots = Math.Min (count, (int)(left / Marshal.SizeOf (typeof (T))));
279 buffer.ReadArray ((ulong) position, array, offset, slots);
283 public void Write (long position, bool value)
286 throw new NotSupportedException ();
288 throw new ObjectDisposedException ("buffer");
290 throw new ArgumentOutOfRangeException ();
292 buffer.Write ((ulong)position, value);
295 public void Write (long position, byte value)
298 throw new NotSupportedException ();
300 throw new ObjectDisposedException ("buffer");
302 throw new ArgumentOutOfRangeException ();
304 buffer.Write ((ulong)position, value);
307 public void Write (long position, char value)
310 throw new NotSupportedException ();
312 throw new ObjectDisposedException ("buffer");
314 throw new ArgumentOutOfRangeException ();
316 buffer.Write ((ulong)position, value);
319 public void Write (long position, decimal value)
322 throw new NotSupportedException ();
324 throw new ObjectDisposedException ("buffer");
326 throw new ArgumentOutOfRangeException ();
328 buffer.Write ((ulong)position, value);
331 public void Write (long position, double value)
334 throw new NotSupportedException ();
336 throw new ObjectDisposedException ("buffer");
338 throw new ArgumentOutOfRangeException ();
340 buffer.Write ((ulong)position, value);
343 public void Write (long position, short value)
346 throw new NotSupportedException ();
348 throw new ObjectDisposedException ("buffer");
350 throw new ArgumentOutOfRangeException ();
352 buffer.Write ((ulong)position, value);
355 public void Write (long position, int value)
358 throw new NotSupportedException ();
360 throw new ObjectDisposedException ("buffer");
362 throw new ArgumentOutOfRangeException ();
364 buffer.Write ((ulong)position, value);
367 public void Write (long position, long value)
370 throw new NotSupportedException ();
372 throw new ObjectDisposedException ("buffer");
374 throw new ArgumentOutOfRangeException ();
376 buffer.Write ((ulong)position, value);
379 [CLSCompliant (false)]
380 public void Write (long position, sbyte value)
383 throw new NotSupportedException ();
385 throw new ObjectDisposedException ("buffer");
387 throw new ArgumentOutOfRangeException ();
389 buffer.Write ((ulong)position, value);
392 public void Write (long position, float value)
395 throw new NotSupportedException ();
397 throw new ObjectDisposedException ("buffer");
399 throw new ArgumentOutOfRangeException ();
401 buffer.Write ((ulong)position, value);
404 [CLSCompliant (false)]
405 public void Write (long position, ushort value)
408 throw new NotSupportedException ();
410 throw new ObjectDisposedException ("buffer");
412 throw new ArgumentOutOfRangeException ();
414 buffer.Write ((ulong)position, value);
417 [CLSCompliant (false)]
418 public void Write (long position, uint value)
421 throw new NotSupportedException ();
423 throw new ObjectDisposedException ("buffer");
425 throw new ArgumentOutOfRangeException ();
427 buffer.Write ((ulong)position, value);
430 [CLSCompliant (false)]
431 public void Write (long position, ulong value)
434 throw new NotSupportedException ();
436 throw new ObjectDisposedException ("buffer");
438 throw new ArgumentOutOfRangeException ();
440 buffer.Write ((ulong)position, value);
443 public void Write<T> (long position, ref T structure) where T : struct
446 throw new NotSupportedException ();
448 throw new ObjectDisposedException ("buffer");
450 throw new ArgumentOutOfRangeException ();
452 buffer.Write<T> ((ulong)position, structure);
455 public void WriteArray<T> (long position, T [] array, int offset, int count) where T : struct
457 buffer.WriteArray ((ulong)position, array, offset, count);
460 public bool CanRead {
461 get { return canread; }
464 public bool CanWrite {
465 get { return canwrite; }
468 public long Capacity {
469 get { return capacity; }
472 protected bool IsOpen {
473 get { return buffer != null; }