[mono-config] use right type for result of strlen
[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                 internal IntPtr BaseAddress {
61                         get {
62                                 unsafe {
63                                         return new IntPtr ((void*) base_address);
64                                 }
65                         }
66                 }
67
68                 public override bool CanRead {
69                         get {
70                                 return true;
71                         }
72                 }
73
74                 public override bool CanSeek {
75                         get {
76                                 return true;
77                         }
78                 }
79
80                 public override bool CanWrite {
81                         get {
82                                 return false;
83                         }
84                 }
85
86                 public override long Position {
87                         get {
88                                 return position;
89                         }
90
91                         set {
92                                 if (position < 0)
93                                         throw new ArgumentOutOfRangeException ("Position", "Can not be negative");
94                                 if (position > size)
95                                         throw new ArgumentOutOfRangeException ("Position", "Pointer falls out of range");
96
97                                 position = (int) value;
98                         }
99                 }
100
101                 public override long Length {
102                         get {
103                                 return size;
104                         }
105                 }
106
107                 public override int Read (byte [] buffer, int offset, int count)
108                 {
109                         if (buffer == null)
110                                 throw new ArgumentNullException ("buffer");
111
112                         if (offset < 0 || count < 0)
113                                 throw new ArgumentOutOfRangeException ("offset or count less than zero.");
114
115                         if (buffer.Length - offset < count )
116                                 throw new ArgumentException ("offset+count",
117                                                               "The size of the buffer is less than offset + count.");
118
119                         if (closed)
120                                 throw new ObjectDisposedException ("Stream has been closed");
121
122                         if (position >= size || count == 0)
123                                 return 0;
124
125                         if (position > size - count)
126                                 count = size - position;
127
128                         unsafe {
129                                 Marshal.Copy ((IntPtr) (base_address + position), buffer, offset, count);
130                         }
131                         position += count;
132                         return count;
133                 }
134
135                 public override int ReadByte ()
136                 {
137                         if (position >= size)
138                                 return -1;
139
140                         if (closed)
141                                 throw new ObjectDisposedException ("Stream has been closed");
142
143                         unsafe {
144                                 return base_address [position++];
145                         }
146                 }
147
148                 public override long Seek (long offset, SeekOrigin loc)
149                 {
150                         // It's funny that they don't throw this exception for < Int32.MinValue
151                         if (offset > (long) Int32.MaxValue)
152                                 throw new ArgumentOutOfRangeException ("Offset out of range. " + offset);
153
154                         if (closed)
155                                 throw new ObjectDisposedException ("Stream has been closed");
156
157                         int ref_point;
158                         switch (loc) {
159                         case SeekOrigin.Begin:
160                                 if (offset < 0)
161                                         throw new IOException ("Attempted to seek before start of MemoryStream.");
162                                 ref_point = 0;
163                                 break;
164                         case SeekOrigin.Current:
165                                 ref_point = position;
166                                 break;
167                         case SeekOrigin.End:
168                                 ref_point = size;
169                                 break;
170                         default:
171                                 throw new ArgumentException ("loc", "Invalid SeekOrigin");
172                         }
173
174                         checked {
175                                 try {
176                                         ref_point += (int) offset;
177                                 } catch {
178                                         throw new ArgumentOutOfRangeException ("Too large seek destination");
179                                 }
180                                 
181                                 if (ref_point < 0)
182                                         throw new IOException ("Attempted to seek before start of MemoryStream.");
183                         }
184
185                         position = ref_point;
186                         return position;
187                 }
188                 
189                 public override void SetLength (long value)
190                 {
191                         throw new NotSupportedException ("This stream can not change its size");
192                 }
193
194                 public override void Write (byte [] buffer, int offset, int count)
195                 {
196                         throw new NotSupportedException ("This stream can not change its size");
197                 }
198
199                 public override void WriteByte (byte value)
200                 {
201                         throw new NotSupportedException ("This stream can not change its size");
202                 }
203                 
204                 public override void Flush ()
205                 {
206                 }
207
208                 public override void Close ()
209                 {
210                         closed = true;
211
212                         if (Closed != null)
213                                 Closed (this, null);
214                 }
215         }
216 }