[MSBuild] Fix minor assembly resolution issue
[mono.git] / mcs / class / corlib / System.IO / UnmanagedMemoryAccessor.cs
1 //
2 // System.IO.UnmanagedMemoryAccessor.cs
3 //
4 // Author:
5 //  Zoltan Varga (vargaz@gmail.com)
6 //  Marek Safar (marek.safar@gmail.com)
7 //
8 // Copyright (C) 2009 Novell, Inc (http://www.novell.com)
9 //
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:
17 //
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 //
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.
28 //
29
30
31 using System;
32 using System.Runtime.InteropServices;
33 using System.Security.Permissions;
34
35 namespace System.IO
36 {
37         [MonoTODO ("Offset is ignored")]
38         public class UnmanagedMemoryAccessor : IDisposable {
39                 SafeBuffer buffer;
40 #pragma warning disable 414
41                 long offset;
42 #pragma warning restore
43                 long capacity;
44                 bool canwrite, canread;
45
46                 protected UnmanagedMemoryAccessor ()
47                 {
48                 }
49
50                 public UnmanagedMemoryAccessor (SafeBuffer buffer, long offset, long capacity)
51                 {
52                         Initialize (buffer, offset, capacity, FileAccess.ReadWrite);
53                 }
54
55                 public UnmanagedMemoryAccessor (SafeBuffer buffer, long offset, long capacity, FileAccess access)
56                 {
57                         Initialize (buffer, offset, capacity, access);
58                 }
59
60                 [SecurityPermissionAttribute(SecurityAction.Demand, Flags = SecurityPermissionFlag.UnmanagedCode)]
61                 protected void Initialize(SafeBuffer buffer, long offset, long capacity, FileAccess access)
62                 {
63                         if (buffer == null)
64                                 throw new ArgumentNullException ("buffer");
65                         if (offset < 0)
66                                 throw new ArgumentOutOfRangeException ("offset");
67                         if (capacity < 0)
68                                 throw new ArgumentOutOfRangeException ("capacity");
69
70                         if (access == FileAccess.Read || access == FileAccess.ReadWrite)
71                                 canread = true;
72                         if (access == FileAccess.Write || access == FileAccess.ReadWrite)
73                                 canwrite = true;
74
75                         if (this.buffer != null)
76                                 Dispose (true);
77                                          
78                         this.buffer = buffer;
79                         this.offset = offset;
80                         this.capacity = capacity;
81                 }
82                 
83                 public void Dispose ()
84                 {
85                         Dispose (true);
86                         GC.SuppressFinalize (this);
87                 }
88
89                 protected virtual void Dispose (bool disposing)
90                 {
91                         if (buffer != null){
92                                 if (disposing){
93                                         buffer.Dispose ();
94                                 }
95                         }
96                         buffer = null;
97                 }
98
99                 public byte ReadByte (long position) 
100                 {
101                         if (!canread)
102                                 throw new NotSupportedException ();
103                         if (buffer == null)
104                                 throw new ObjectDisposedException ("buffer");
105                         if (position < 0)
106                                 throw new ArgumentOutOfRangeException ();
107                         
108                         return buffer.Read<byte> ((ulong) position);
109                 }
110
111                 public bool ReadBoolean (long position) 
112                 {
113                         if (!canread)
114                                 throw new NotSupportedException ();
115                         if (buffer == null)
116                                 throw new ObjectDisposedException ("buffer");
117                         if (position < 0)
118                                 throw new ArgumentOutOfRangeException ();
119                         
120                         return buffer.Read<bool> ((ulong) position);
121                 }
122
123                 public char ReadChar (long position) 
124                 {
125                         if (!canread)
126                                 throw new NotSupportedException ();
127                         if (buffer == null)
128                                 throw new ObjectDisposedException ("buffer");
129                         if (position < 0)
130                                 throw new ArgumentOutOfRangeException ();
131                         
132                         return buffer.Read<char> ((ulong) position);
133                 }
134                 
135                 public decimal ReadDecimal (long position) 
136                 {
137                         if (!canread)
138                                 throw new NotSupportedException ();
139                         if (buffer == null)
140                                 throw new ObjectDisposedException ("buffer");
141                         if (position < 0)
142                                 throw new ArgumentOutOfRangeException ();
143                         
144                         return buffer.Read<decimal> ((ulong) position);
145                 }
146                 
147                 public double ReadDouble (long position) 
148                 {
149                         if (!canread)
150                                 throw new NotSupportedException ();
151                         if (buffer == null)
152                                 throw new ObjectDisposedException ("buffer");
153                         if (position < 0)
154                                 throw new ArgumentOutOfRangeException ();
155                         
156                         return buffer.Read<double> ((ulong) position);
157                 }
158
159                 public short ReadInt16 (long position) 
160                 {
161                         if (!canread)
162                                 throw new NotSupportedException ();
163                         if (buffer == null)
164                                 throw new ObjectDisposedException ("buffer");
165                         if (position < 0)
166                                 throw new ArgumentOutOfRangeException ();
167                         
168                         return buffer.Read<short> ((ulong) position);
169                 }
170                 
171                 public int ReadInt32 (long position) 
172                 {
173                         if (!canread)
174                                 throw new NotSupportedException ();
175                         if (buffer == null)
176                                 throw new ObjectDisposedException ("buffer");
177                         if (position < 0)
178                                 throw new ArgumentOutOfRangeException ();
179                         
180                         return buffer.Read<int> ((ulong) position);
181                 }
182                 
183                 public long ReadInt64 (long position) 
184                 {
185                         if (!canread)
186                                 throw new NotSupportedException ();
187                         if (buffer == null)
188                                 throw new ObjectDisposedException ("buffer");
189                         if (position < 0)
190                                 throw new ArgumentOutOfRangeException ();
191                         
192                         return buffer.Read<long> ((ulong) position);
193                 }
194                 
195                 [CLSCompliant (false)]
196                 public sbyte ReadSByte (long position) 
197                 {
198                         if (!canread)
199                                 throw new NotSupportedException ();
200                         if (buffer == null)
201                                 throw new ObjectDisposedException ("buffer");
202                         if (position < 0)
203                                 throw new ArgumentOutOfRangeException ();
204                         
205                         return buffer.Read<sbyte> ((ulong) position);
206                 }
207                 
208                 public float ReadSingle (long position) 
209                 {
210                         if (!canread)
211                                 throw new NotSupportedException ();
212                         if (buffer == null)
213                                 throw new ObjectDisposedException ("buffer");
214                         if (position < 0)
215                                 throw new ArgumentOutOfRangeException ();
216                         
217                         return buffer.Read<float> ((ulong) position);
218                 }
219                 
220                 [CLSCompliant (false)]
221                 public ushort ReadUInt16 (long position) 
222                 {
223                         if (!canread)
224                                 throw new NotSupportedException ();
225                         if (buffer == null)
226                                 throw new ObjectDisposedException ("buffer");
227                         if (position < 0)
228                                 throw new ArgumentOutOfRangeException ();
229                         
230                         return buffer.Read<ushort> ((ulong) position);
231                 }
232
233                 [CLSCompliant (false)]
234                 public uint ReadUInt32 (long position) 
235                 {
236                         if (!canread)
237                                 throw new NotSupportedException ();
238                         if (buffer == null)
239                                 throw new ObjectDisposedException ("buffer");
240                         if (position < 0)
241                                 throw new ArgumentOutOfRangeException ();
242                         
243                         return buffer.Read<uint> ((ulong) position);
244                 }
245
246                 [CLSCompliant (false)]
247                 public ulong ReadUInt64 (long position) 
248                 {
249                         if (!canread)
250                                 throw new NotSupportedException ();
251                         if (buffer == null)
252                                 throw new ObjectDisposedException ("buffer");
253                         if (position < 0)
254                                 throw new ArgumentOutOfRangeException ();
255                         
256                         return buffer.Read<ulong> ((ulong) position);
257                 }
258
259                 public void Read<T> (long position, out T structure) where T : struct
260                 {
261                         if (!canread)
262                                 throw new NotSupportedException ();
263                         if (buffer == null)
264                                 throw new ObjectDisposedException ("buffer");
265                         if (position < 0)
266                                 throw new ArgumentOutOfRangeException ();
267                         
268                         structure = buffer.Read<T> ((ulong) position);
269                 }
270
271                 public int ReadArray<T> (long position, T [] array, int offset, int count) where T : struct
272                 {
273                         if (position < 0)
274                                 throw new ArgumentOutOfRangeException ();
275                         
276                         long left = capacity - position;
277                         var slots = Math.Min (count, (int)(left / Marshal.SizeOf (typeof (T))));
278
279                         buffer.ReadArray ((ulong) position, array, offset, slots);
280                         return slots;
281                 }
282                 
283                 public void Write (long position, bool value) 
284                 {
285                         if (!canwrite)
286                                 throw new NotSupportedException ();
287                         if (buffer == null)
288                                 throw new ObjectDisposedException ("buffer");
289                         if (position < 0)
290                                 throw new ArgumentOutOfRangeException ();
291                         
292                         buffer.Write ((ulong)position, value);
293                 }
294
295                 public void Write (long position, byte value) 
296                 {
297                         if (!canwrite)
298                                 throw new NotSupportedException ();
299                         if (buffer == null)
300                                 throw new ObjectDisposedException ("buffer");
301                         if (position < 0)
302                                 throw new ArgumentOutOfRangeException ();
303                         
304                         buffer.Write ((ulong)position, value);
305                 }
306
307                 public void Write (long position, char value) 
308                 {
309                         if (!canwrite)
310                                 throw new NotSupportedException ();
311                         if (buffer == null)
312                                 throw new ObjectDisposedException ("buffer");
313                         if (position < 0)
314                                 throw new ArgumentOutOfRangeException ();
315                         
316                         buffer.Write ((ulong)position, value);
317                 }
318
319                 public void Write (long position, decimal value) 
320                 {
321                         if (!canwrite)
322                                 throw new NotSupportedException ();
323                         if (buffer == null)
324                                 throw new ObjectDisposedException ("buffer");
325                         if (position < 0)
326                                 throw new ArgumentOutOfRangeException ();
327                         
328                         buffer.Write ((ulong)position, value);
329                 }
330                 
331                 public void Write (long position, double value) 
332                 {
333                         if (!canwrite)
334                                 throw new NotSupportedException ();
335                         if (buffer == null)
336                                 throw new ObjectDisposedException ("buffer");
337                         if (position < 0)
338                                 throw new ArgumentOutOfRangeException ();
339                         
340                         buffer.Write ((ulong)position, value);
341                 }
342
343                 public void Write (long position, short value) 
344                 {
345                         if (!canwrite)
346                                 throw new NotSupportedException ();
347                         if (buffer == null)
348                                 throw new ObjectDisposedException ("buffer");
349                         if (position < 0)
350                                 throw new ArgumentOutOfRangeException ();
351                         
352                         buffer.Write ((ulong)position, value);
353                 }
354
355                 public void Write (long position, int value) 
356                 {
357                         if (!canwrite)
358                                 throw new NotSupportedException ();
359                         if (buffer == null)
360                                 throw new ObjectDisposedException ("buffer");
361                         if (position < 0)
362                                 throw new ArgumentOutOfRangeException ();
363                         
364                         buffer.Write ((ulong)position, value);
365                 }
366
367                 public void Write (long position, long value) 
368                 {
369                         if (!canwrite)
370                                 throw new NotSupportedException ();
371                         if (buffer == null)
372                                 throw new ObjectDisposedException ("buffer");
373                         if (position < 0)
374                                 throw new ArgumentOutOfRangeException ();
375                         
376                         buffer.Write ((ulong)position, value);
377                 }
378
379                 [CLSCompliant (false)]
380                 public void Write (long position, sbyte value) 
381                 {
382                         if (!canwrite)
383                                 throw new NotSupportedException ();
384                         if (buffer == null)
385                                 throw new ObjectDisposedException ("buffer");
386                         if (position < 0)
387                                 throw new ArgumentOutOfRangeException ();
388                         
389                         buffer.Write ((ulong)position, value);
390                 }
391
392                 public void Write (long position, float value) 
393                 {
394                         if (!canwrite)
395                                 throw new NotSupportedException ();
396                         if (buffer == null)
397                                 throw new ObjectDisposedException ("buffer");
398                         if (position < 0)
399                                 throw new ArgumentOutOfRangeException ();
400                         
401                         buffer.Write ((ulong)position, value);
402                 }
403
404                 [CLSCompliant (false)]
405                 public void Write (long position, ushort value) 
406                 {
407                         if (!canwrite)
408                                 throw new NotSupportedException ();
409                         if (buffer == null)
410                                 throw new ObjectDisposedException ("buffer");
411                         if (position < 0)
412                                 throw new ArgumentOutOfRangeException ();
413                         
414                         buffer.Write ((ulong)position, value);
415                 }
416
417                 [CLSCompliant (false)]
418                 public void Write (long position, uint value) 
419                 {
420                         if (!canwrite)
421                                 throw new NotSupportedException ();
422                         if (buffer == null)
423                                 throw new ObjectDisposedException ("buffer");
424                         if (position < 0)
425                                 throw new ArgumentOutOfRangeException ();
426                         
427                         buffer.Write ((ulong)position, value);
428                 }
429
430                 [CLSCompliant (false)]
431                 public void Write (long position, ulong value) 
432                 {
433                         if (!canwrite)
434                                 throw new NotSupportedException ();
435                         if (buffer == null)
436                                 throw new ObjectDisposedException ("buffer");
437                         if (position < 0)
438                                 throw new ArgumentOutOfRangeException ();
439                         
440                         buffer.Write ((ulong)position, value);
441                 }
442
443                 public void Write<T> (long position, ref T structure) where T : struct
444                 {
445                         if (!canwrite)
446                                 throw new NotSupportedException ();
447                         if (buffer == null)
448                                 throw new ObjectDisposedException ("buffer");
449                         if (position < 0)
450                                 throw new ArgumentOutOfRangeException ();
451                         
452                         buffer.Write<T> ((ulong)position, structure);
453                 }
454
455                 public void WriteArray<T> (long position, T [] array, int offset, int count) where T : struct 
456                 {
457                         buffer.WriteArray ((ulong)position, array, offset, count);
458                 }
459         
460                 public bool CanRead {
461                         get { return canread; }
462                 }
463
464                 public bool CanWrite {
465                         get { return canwrite; }
466                 }
467
468                 public long Capacity {
469                         get { return capacity; }
470                 }
471                 
472                 protected bool IsOpen {
473                         get { return buffer != null; }
474                 }
475         }
476 }
477