2 // System.IO.UnmanagedMemoryStream.cs
4 // Copyright (C) 2006 Sridhar Kulkarni, All Rights Reserved
7 // Sridhar Kulkarni (sridharkulkarni@gmail.com)
8 // Gert Driesen (drieseng@users.sourceforge.net)
9 // Sebastien Pouliot <sebastien@ximian.com>
11 // Copyright (C) 2005-2006, 2009 Novell, Inc (http://www.novell.com)
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 using System.Runtime.InteropServices;
39 public class UnmanagedMemoryStream : Stream
44 FileAccess fileaccess;
45 IntPtr initial_pointer;
46 long initial_position;
47 long current_position;
49 internal event EventHandler Closed;
52 protected UnmanagedMemoryStream()
57 [CLSCompliantAttribute(false)]
58 public unsafe UnmanagedMemoryStream (byte *pointer, long length)
60 Initialize (pointer, length, length, FileAccess.Read);
63 [CLSCompliantAttribute(false)]
64 public unsafe UnmanagedMemoryStream (byte *pointer, long length, long capacity, FileAccess access)
66 Initialize (pointer, length, capacity, access);
72 public override bool CanRead {
74 return (!closed && (fileaccess != FileAccess.Write));
78 public override bool CanSeek {
84 public override bool CanWrite {
86 return (!closed && (fileaccess != FileAccess.Read));
89 public long Capacity {
92 throw new ObjectDisposedException("The stream is closed");
97 public override long Length {
100 throw new ObjectDisposedException("The stream is closed");
106 [CLSCompliantAttribute(false)]
107 public override long Position {
110 throw new ObjectDisposedException("The stream is closed");
111 return (current_position);
115 throw new ObjectDisposedException("The stream is closed");
117 throw new ArgumentOutOfRangeException("value", "Non-negative number required.");
118 if (value > (long)Int32.MaxValue)
119 throw new ArgumentOutOfRangeException("value", "The position is larger than Int32.MaxValue.");
120 current_position = value;
125 [CLSCompliantAttribute(false)]
127 public unsafe byte* PositionPointer {
130 throw new ObjectDisposedException("The stream is closed");
131 if (current_position >= length)
132 throw new IndexOutOfRangeException ("value");
134 return (byte *) initial_pointer + current_position;
138 throw new ObjectDisposedException("The stream is closed");
140 if (value < (byte *)initial_pointer)
141 throw new IOException ("Address is below the inital address");
143 Position = value - (byte*) initial_pointer;
149 public override int Read ([InAttribute] [OutAttribute] byte[] buffer, int offset, int count)
152 throw new ObjectDisposedException("The stream is closed");
155 throw new ArgumentNullException("buffer");
157 throw new ArgumentOutOfRangeException("offset", "Non-negative number required.");
159 throw new ArgumentOutOfRangeException("count", "Non-negative number required.");
160 if ((buffer.Length - offset) < count)
161 throw new ArgumentException("The length of the buffer array minus the offset parameter is less than the count parameter");
163 if (fileaccess == FileAccess.Write)
164 throw new NotSupportedException("Stream does not support reading");
166 if (current_position >= length)
169 int progress = current_position + count < length ? count : (int) (length - current_position);
170 for (int i = 0; i < progress; i++)
171 buffer [offset + i] = Marshal.ReadByte (initial_pointer, (int) current_position++);
177 public override int ReadByte ()
180 throw new ObjectDisposedException("The stream is closed");
182 if (fileaccess== FileAccess.Write)
183 throw new NotSupportedException("Stream does not support reading");
185 if (current_position >= length)
187 return (int) Marshal.ReadByte(initial_pointer, (int) current_position++);
191 public override long Seek (long offset, SeekOrigin loc)
194 throw new ObjectDisposedException("The stream is closed");
198 case SeekOrigin.Begin:
200 throw new IOException("An attempt was made to seek before the beginning of the stream");
201 refpoint = initial_position;
203 case SeekOrigin.Current:
204 refpoint = current_position;
210 throw new ArgumentException("Invalid SeekOrigin option");
213 if (refpoint < initial_position)
214 throw new IOException("An attempt was made to seek before the beginning of the stream");
215 current_position = refpoint;
216 return(current_position);
219 public override void SetLength (long value)
222 throw new ObjectDisposedException("The stream is closed");
224 throw new ArgumentOutOfRangeException("length", "Non-negative number required.");
225 if (value > capacity)
226 throw new IOException ("Unable to expand length of this stream beyond its capacity.");
227 if (fileaccess == FileAccess.Read)
228 throw new NotSupportedException ("Stream does not support writing.");
230 if (length < current_position)
231 current_position = length;
234 public override void Flush ()
237 throw new ObjectDisposedException("The stream is closed");
238 //This method performs no action for this class
239 //but is included as part of the Stream base class
242 protected override void Dispose (bool disposing)
251 public override void Write (byte[] buffer, int offset, int count)
254 throw new ObjectDisposedException("The stream is closed");
256 throw new ArgumentNullException("The buffer parameter is a null reference");
258 throw new ArgumentOutOfRangeException("offset", "Non-negative number required.");
260 throw new ArgumentOutOfRangeException("count", "Non-negative number required.");
261 if ((buffer.Length - offset) < count)
262 throw new ArgumentException("The length of the buffer array minus the offset parameter is less than the count parameter");
263 if (current_position > capacity - count)
264 throw new NotSupportedException ("Unable to expand length of this stream beyond its capacity.");
265 if (fileaccess == FileAccess.Read)
266 throw new NotSupportedException ("Stream does not support writing.");
269 // use Marshal.WriteByte since that allow us to start writing
270 // from the current position
271 for (int i = 0; i < count; i++)
272 Marshal.WriteByte (initial_pointer, (int) current_position++, buffer [offset + i]);
274 if (current_position > length)
275 length = current_position;
280 public override void WriteByte (byte value)
283 throw new ObjectDisposedException("The stream is closed");
285 if (current_position == capacity)
286 throw new NotSupportedException("The current position is at the end of the capacity of the stream");
287 if (fileaccess == FileAccess.Read)
288 throw new NotSupportedException("Stream does not support writing.");
291 Marshal.WriteByte(initial_pointer, (int)current_position, value);
293 if (current_position > length)
294 length = current_position;
299 protected unsafe void Initialize (byte* pointer, long length,
304 throw new ArgumentNullException("pointer");
306 throw new ArgumentOutOfRangeException("length", "Non-negative number required.");
308 throw new ArgumentOutOfRangeException("capacity", "Non-negative number required.");
309 if (length > capacity)
310 throw new ArgumentOutOfRangeException("length", "The length cannot be greater than the capacity.");
311 if ((access < FileAccess.Read) || (access > FileAccess.ReadWrite))
312 throw new ArgumentOutOfRangeException ("access", "Enum value was out of legal range.");
315 this.length = length;
316 this.capacity = capacity;
317 initial_position = 0;
318 current_position = initial_position;
319 initial_pointer = new IntPtr ((void*)pointer);