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 SafeBuffer safebuffer;
52 internal event EventHandler Closed;
55 protected UnmanagedMemoryStream()
60 [CLSCompliantAttribute(false)]
61 public unsafe UnmanagedMemoryStream (byte *pointer, long length) :
62 this (pointer, length, length, FileAccess.Read)
66 [CLSCompliantAttribute(false)]
67 public unsafe UnmanagedMemoryStream (byte *pointer, long length, long capacity, FileAccess access)
70 Initialize (pointer, length, capacity, access);
74 public UnmanagedMemoryStream (SafeBuffer buffer, long offset, long length) :
75 this (buffer, offset, length, FileAccess.Read)
79 public UnmanagedMemoryStream (SafeBuffer buffer, long offset, long length, FileAccess access)
82 Initialize (buffer, offset, length, access);
88 public override bool CanRead {
90 return (!closed && (fileaccess != FileAccess.Write));
94 public override bool CanSeek {
100 public override bool CanWrite {
102 return (!closed && (fileaccess != FileAccess.Read));
105 public long Capacity {
108 throw new ObjectDisposedException("The stream is closed");
113 public override long Length {
116 throw new ObjectDisposedException("The stream is closed");
122 public override long Position {
125 throw new ObjectDisposedException("The stream is closed");
126 return (current_position);
130 throw new ObjectDisposedException("The stream is closed");
132 throw new ArgumentOutOfRangeException("value", "Non-negative number required.");
133 if (value > (long)Int32.MaxValue)
134 throw new ArgumentOutOfRangeException("value", "The position is larger than Int32.MaxValue.");
135 current_position = value;
139 [CLSCompliantAttribute (false)]
140 public unsafe byte* PositionPointer {
142 #if NET_4_0 || MOBILE
143 if (safebuffer != null)
144 throw new NotSupportedException ("Not supported when using SafeBuffer");
147 throw new ObjectDisposedException("The stream is closed");
148 if (current_position >= length)
149 throw new IndexOutOfRangeException ("value");
151 return (byte *) initial_pointer + current_position;
154 #if NET_4_0 || MOBILE
155 if (safebuffer != null)
156 throw new NotSupportedException ("Not supported when using SafeBuffer");
159 throw new ObjectDisposedException("The stream is closed");
161 if (value < (byte *)initial_pointer)
162 throw new IOException ("Address is below the inital address");
164 Position = value - (byte*) initial_pointer;
170 public override int Read ([InAttribute] [OutAttribute] byte[] buffer, int offset, int count)
173 throw new ObjectDisposedException("The stream is closed");
176 throw new ArgumentNullException("buffer");
178 throw new ArgumentOutOfRangeException("offset", "Non-negative number required.");
180 throw new ArgumentOutOfRangeException("count", "Non-negative number required.");
181 if ((buffer.Length - offset) < count)
182 throw new ArgumentException("The length of the buffer array minus the offset parameter is less than the count parameter");
184 if (fileaccess == FileAccess.Write)
185 throw new NotSupportedException("Stream does not support reading");
187 if (current_position >= length)
190 int progress = current_position + count < length ? count : (int) (length - current_position);
191 #if NET_4_0 || MOBILE
192 if (safebuffer != null) {
196 safebuffer.AcquirePointer (ref ptr);
197 Marshal.Copy (new IntPtr (ptr + current_position), buffer, offset, progress);
200 safebuffer.ReleasePointer ();
206 Marshal.Copy (new IntPtr (initial_pointer.ToInt64 () + current_position), buffer, offset, progress);
208 current_position += progress;
212 public override int ReadByte ()
215 throw new ObjectDisposedException("The stream is closed");
217 if (fileaccess== FileAccess.Write)
218 throw new NotSupportedException("Stream does not support reading");
220 if (current_position >= length)
223 #if NET_4_0 || MOBILE
224 if (safebuffer != null) {
228 safebuffer.AcquirePointer (ref ptr);
229 return (int) Marshal.ReadByte (new IntPtr (ptr), (int) current_position++);
232 safebuffer.ReleasePointer ();
238 return (int) Marshal.ReadByte(initial_pointer, (int) current_position++);
242 public override long Seek (long offset, SeekOrigin loc)
245 throw new ObjectDisposedException("The stream is closed");
249 case SeekOrigin.Begin:
251 throw new IOException("An attempt was made to seek before the beginning of the stream");
252 refpoint = initial_position;
254 case SeekOrigin.Current:
255 refpoint = current_position;
261 throw new ArgumentException("Invalid SeekOrigin option");
264 if (refpoint < initial_position)
265 throw new IOException("An attempt was made to seek before the beginning of the stream");
266 current_position = refpoint;
267 return(current_position);
270 public override void SetLength (long value)
272 #if NET_4_0 || MOBILE
273 if (safebuffer != null)
274 throw new NotSupportedException ("Not supported when using SafeBuffer");
277 throw new ObjectDisposedException("The stream is closed");
279 throw new ArgumentOutOfRangeException("length", "Non-negative number required.");
280 if (value > capacity)
281 throw new IOException ("Unable to expand length of this stream beyond its capacity.");
282 if (fileaccess == FileAccess.Read)
283 throw new NotSupportedException ("Stream does not support writing.");
285 if (length < current_position)
286 current_position = length;
289 public override void Flush ()
292 throw new ObjectDisposedException("The stream is closed");
293 //This method performs no action for this class
294 //but is included as part of the Stream base class
297 protected override void Dispose (bool disposing)
306 public override void Write (byte[] buffer, int offset, int count)
309 throw new ObjectDisposedException("The stream is closed");
311 throw new ArgumentNullException("The buffer parameter is a null reference");
313 throw new ArgumentOutOfRangeException("offset", "Non-negative number required.");
315 throw new ArgumentOutOfRangeException("count", "Non-negative number required.");
316 if ((buffer.Length - offset) < count)
317 throw new ArgumentException("The length of the buffer array minus the offset parameter is less than the count parameter");
318 if (current_position > capacity - count)
319 throw new NotSupportedException ("Unable to expand length of this stream beyond its capacity.");
320 if (fileaccess == FileAccess.Read)
321 throw new NotSupportedException ("Stream does not support writing.");
323 #if NET_4_0 || MOBILE
324 if (safebuffer != null) {
328 safebuffer.AcquirePointer (ref dest);
329 fixed (byte *src = buffer) {
330 dest += current_position;
331 String.memcpy (dest, src + offset, count);
335 safebuffer.ReleasePointer ();
342 fixed (byte *src = buffer) {
343 byte *dest = (byte *) initial_pointer + current_position;
344 String.memcpy (dest, src + offset, count);
348 current_position += count;
349 if (current_position > length)
350 length = current_position;
353 public override void WriteByte (byte value)
356 throw new ObjectDisposedException("The stream is closed");
358 if (current_position == capacity)
359 throw new NotSupportedException("The current position is at the end of the capacity of the stream");
360 if (fileaccess == FileAccess.Read)
361 throw new NotSupportedException("Stream does not support writing.");
363 #if NET_4_0 || MOBILE
364 if (safebuffer != null) {
368 safebuffer.AcquirePointer (ref dest);
369 dest += current_position++;
373 safebuffer.ReleasePointer ();
380 byte *dest = (byte *) initial_pointer + (int) current_position++;
384 if (current_position > length)
385 length = current_position;
388 [CLSCompliant (false)]
389 protected unsafe void Initialize (byte* pointer, long length,
394 throw new ArgumentNullException("pointer");
396 throw new ArgumentOutOfRangeException("length", "Non-negative number required.");
398 throw new ArgumentOutOfRangeException("capacity", "Non-negative number required.");
399 if (length > capacity)
400 throw new ArgumentOutOfRangeException("length", "The length cannot be greater than the capacity.");
401 if ((access < FileAccess.Read) || (access > FileAccess.ReadWrite))
402 throw new ArgumentOutOfRangeException ("access", "Enum value was out of legal range.");
404 throw new InvalidOperationException ("Called Initialize twice");
407 this.length = length;
408 this.capacity = capacity;
409 initial_position = 0;
410 current_position = initial_position;
411 initial_pointer = new IntPtr ((void*)pointer);
415 #if NET_4_0 || MOBILE
416 protected void Initialize (SafeBuffer buffer, long offset, long length, FileAccess access)
419 throw new ArgumentNullException ("buffer");
422 throw new ArgumentOutOfRangeException ("offset");
425 throw new ArgumentOutOfRangeException ("length");
427 ulong blength = buffer.ByteLength;
428 if ((blength - (ulong) length) < (ulong) offset)
429 throw new ArgumentException ("Invalid offset and/or length");
431 if (access < FileAccess.Read || access > FileAccess.ReadWrite)
432 throw new ArgumentOutOfRangeException ("access");
435 throw new InvalidOperationException ("Called Initialize twice");
437 this.length = length;
438 this.capacity = length;
439 this.fileaccess = access;
440 this.safebuffer = buffer;
441 initial_position = offset;
442 current_position = offset;