* roottypes.cs: Rename from tree.cs.
[mono.git] / mcs / class / corlib / System.IO / IntPtrStream.cs
1 //
2 // System.IO.IntPtrStream: A stream that is backed up by unmanaged memory
3 //
4 // Author:
5 //   Miguel de Icaza (miguel@ximian.com)
6 //
7 // Based on the code for MemoryStream.cs:
8 //
9 // Authors:     Marcin Szczepanski (marcins@zipworld.com.au)
10 //              Patrik Torstensson
11 //              Gonzalo Paniagua Javier (gonzalo@ximian.com)
12 //
13 // (c) 2001,2002 Marcin Szczepanski, Patrik Torstensson
14 // (c) 2003 Ximian, Inc. (http://www.ximian.com)
15 //
16
17 //
18 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
19 //
20 // Permission is hereby granted, free of charge, to any person obtaining
21 // a copy of this software and associated documentation files (the
22 // "Software"), to deal in the Software without restriction, including
23 // without limitation the rights to use, copy, modify, merge, publish,
24 // distribute, sublicense, and/or sell copies of the Software, and to
25 // permit persons to whom the Software is furnished to do so, subject to
26 // the following conditions:
27 // 
28 // The above copyright notice and this permission notice shall be
29 // included in all copies or substantial portions of the Software.
30 // 
31 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
32 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
33 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
34 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
35 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
36 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
37 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38 //
39
40 using System.Runtime.InteropServices;
41 namespace System.IO {
42
43         internal class IntPtrStream : Stream {
44                 unsafe byte *base_address;
45                 int size;
46                 int position;
47                 bool closed;
48
49                 public event EventHandler Closed;
50                 
51                 public IntPtrStream (IntPtr base_address, int size)
52                 {
53                         unsafe {
54                                 this.base_address = (byte*)((void *)base_address);
55                         }
56                         this.size = size;
57                         position = 0;
58                 }
59
60                 public override bool CanRead {
61                         get {
62                                 return true;
63                         }
64                 }
65
66                 public override bool CanSeek {
67                         get {
68                                 return true;
69                         }
70                 }
71
72                 public override bool CanWrite {
73                         get {
74                                 return false;
75                         }
76                 }
77
78                 public override long Position {
79                         get {
80                                 return position;
81                         }
82
83                         set {
84                                 if (position < 0)
85                                         throw new ArgumentOutOfRangeException ("Position", "Can not be negative");
86                                 if (position > size)
87                                         throw new ArgumentOutOfRangeException ("Position", "Pointer falls out of range");
88
89                                 position = (int) value;
90                         }
91                 }
92
93                 public override long Length {
94                         get {
95                                 return size;
96                         }
97                 }
98
99                 public override int Read (byte [] buffer, int offset, int count)
100                 {
101                         if (buffer == null)
102                                 throw new ArgumentNullException ("buffer");
103
104                         if (offset < 0 || count < 0)
105                                 throw new ArgumentOutOfRangeException ("offset or count less than zero.");
106
107                         if (buffer.Length - offset < count )
108                                 throw new ArgumentException ("offset+count",
109                                                               "The size of the buffer is less than offset + count.");
110
111                         if (closed)
112                                 throw new ObjectDisposedException ("Stream has been closed");
113
114                         if (position >= size || count == 0)
115                                 return 0;
116
117                         if (position > size - count)
118                                 count = size - position;
119
120                         unsafe {
121                                 Marshal.Copy ((IntPtr) (base_address + position), buffer, offset, count);
122                         }
123                         position += count;
124                         return count;
125                 }
126
127                 public override int ReadByte ()
128                 {
129                         if (position >= size)
130                                 return -1;
131
132                         if (closed)
133                                 throw new ObjectDisposedException ("Stream has been closed");
134
135                         unsafe {
136                                 return base_address [position++];
137                         }
138                 }
139
140                 public override long Seek (long offset, SeekOrigin loc)
141                 {
142                         // It's funny that they don't throw this exception for < Int32.MinValue
143                         if (offset > (long) Int32.MaxValue)
144                                 throw new ArgumentOutOfRangeException ("Offset out of range. " + offset);
145
146                         if (closed)
147                                 throw new ObjectDisposedException ("Stream has been closed");
148
149                         int ref_point;
150                         switch (loc) {
151                         case SeekOrigin.Begin:
152                                 if (offset < 0)
153                                         throw new IOException ("Attempted to seek before start of MemoryStream.");
154                                 ref_point = 0;
155                                 break;
156                         case SeekOrigin.Current:
157                                 ref_point = position;
158                                 break;
159                         case SeekOrigin.End:
160                                 ref_point = size;
161                                 break;
162                         default:
163                                 throw new ArgumentException ("loc", "Invalid SeekOrigin");
164                         }
165
166                         checked {
167                                 try {
168                                         ref_point += (int) offset;
169                                 } catch {
170                                         throw new ArgumentOutOfRangeException ("Too large seek destination");
171                                 }
172                                 
173                                 if (ref_point < 0)
174                                         throw new IOException ("Attempted to seek before start of MemoryStream.");
175                         }
176
177                         position = ref_point;
178                         return position;
179                 }
180                 
181                 public override void SetLength (long value)
182                 {
183                         throw new NotSupportedException ("This stream can not change its size");
184                 }
185
186                 public override void Write (byte [] buffer, int offset, int count)
187                 {
188                         throw new NotSupportedException ("This stream can not change its size");
189                 }
190
191                 public override void WriteByte (byte value)
192                 {
193                         throw new NotSupportedException ("This stream can not change its size");
194                 }
195                 
196                 public override void Flush ()
197                 {
198                 }
199
200                 public override void Close ()
201                 {
202                         closed = true;
203
204                         if (Closed != null)
205                                 Closed (this, null);
206                 }
207         }
208 }