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)
10 // Copyright (C) 2005-2006 Novell, Inc (http://www.novell.com)
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36 using System.Runtime.InteropServices;
40 [CLSCompliantAttribute(false)]
41 public class UnmanagedMemoryStream : Stream
47 FileAccess fileaccess;
48 IntPtr initial_pointer;
49 long initial_position;
50 long current_position;
52 internal event EventHandler Closed;
55 protected UnmanagedMemoryStream()
57 fileaccess = FileAccess.Read;
61 public unsafe UnmanagedMemoryStream (byte *pointer, long length)
65 throw new ArgumentNullException("pointer");
67 throw new ArgumentOutOfRangeException("length", "Non-negative number required.");
71 initial_pointer = new IntPtr((void*)pointer);
74 public unsafe UnmanagedMemoryStream (byte *pointer, long length, long capacity, FileAccess access)
77 throw new ArgumentNullException("pointer");
79 throw new ArgumentOutOfRangeException("length", "Non-negative number required.");
81 throw new ArgumentOutOfRangeException("capacity", "Non-negative number required.");
82 if (length > capacity)
83 throw new ArgumentOutOfRangeException("length", "The length cannot be greater than the capacity.");
84 if (!Enum.IsDefined (typeof (FileAccess), access))
85 throw new ArgumentOutOfRangeException ("access", "Enum value was out of legal range.");
89 this.capacity = capacity;
91 initial_pointer = new IntPtr ((void*)pointer);
96 public override bool CanRead {
100 return (fileaccess == FileAccess.Read || fileaccess == FileAccess.ReadWrite);
104 public override bool CanSeek {
110 public override bool CanWrite {
114 return (fileaccess == FileAccess.Write || fileaccess == FileAccess.ReadWrite);
117 public long Capacity {
120 throw new ObjectDisposedException("The stream is closed");
125 public override long Length {
128 throw new ObjectDisposedException("The stream is closed");
133 public override long Position {
136 throw new ObjectDisposedException("The stream is closed");
137 return (current_position);
141 throw new ObjectDisposedException("The stream is closed");
143 throw new ArgumentOutOfRangeException("value", "Non-negative number required.");
144 if (value > (long)Int32.MaxValue)
145 throw new ArgumentOutOfRangeException("value", "The position is larger than Int32.MaxValue.");
146 current_position = value;
151 [CLSCompliantAttribute(false)]
153 public unsafe byte* PositionPointer {
155 return (byte *) initial_pointer + current_position;
158 if (value < (byte *)initial_pointer)
159 throw new IOException ("Address is below the inital address");
161 if (value >= (byte *) ((byte *)initial_pointer + length))
162 throw new ArgumentOutOfRangeException ("value");
164 current_position = (long) (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 for (int i = 0; i < progress; i++)
192 buffer [offset + i] = Marshal.ReadByte (initial_pointer, (int) current_position++);
198 public override int ReadByte ()
201 throw new ObjectDisposedException("The stream is closed");
203 if (fileaccess== FileAccess.Write)
204 throw new NotSupportedException("Stream does not support reading");
206 if (current_position >= length)
208 return (int) Marshal.ReadByte(initial_pointer, (int) current_position++);
212 public override long Seek (long offset, SeekOrigin loc)
215 throw new ObjectDisposedException("The stream is closed");
219 case SeekOrigin.Begin:
221 throw new IOException("An attempt was made to seek before the beginning of the stream");
222 refpoint = initial_position;
224 case SeekOrigin.Current:
225 refpoint = current_position;
231 throw new ArgumentException("Invalid SeekOrigin option");
233 refpoint += (int)offset;
234 if (refpoint < initial_position)
235 throw new IOException("An attempt was made to seek before the beginning of the stream");
236 current_position = refpoint;
237 return(current_position);
240 public override void SetLength (long value)
243 throw new ObjectDisposedException("The stream is closed");
245 throw new ArgumentOutOfRangeException("length", "Non-negative number required.");
246 if (value > capacity)
247 throw new IOException ("Unable to expand length of this stream beyond its capacity.");
248 if (fileaccess == FileAccess.Read)
249 throw new NotSupportedException ("Stream does not support writing.");
251 if (length < current_position)
252 current_position = length;
255 public override void Flush ()
258 throw new ObjectDisposedException("The stream is closed");
259 //This method performs no action for this class
260 //but is included as part of the Stream base class
263 protected override void Dispose (bool disposing)
272 public override void Write (byte[] buffer, int offset, int count)
275 throw new ObjectDisposedException("The stream is closed");
277 throw new ArgumentNullException("The buffer parameter is a null reference");
278 if ((current_position + count) > capacity)
279 throw new NotSupportedException ("Unable to expand length of this stream beyond its capacity.");
281 throw new ArgumentOutOfRangeException("offset", "Non-negative number required.");
283 throw new ArgumentOutOfRangeException("count", "Non-negative number required.");
284 if ((buffer.Length - offset) < count)
285 throw new ArgumentException("The length of the buffer array minus the offset parameter is less than the count parameter");
286 if (fileaccess == FileAccess.Read)
287 throw new NotSupportedException ("Stream does not support writing.");
290 // use Marshal.WriteByte since that allow us to start writing
291 // from the current position
292 for (int i = 0; i < count; i++)
293 Marshal.WriteByte (initial_pointer, (int) current_position++, buffer [offset + i]);
295 if (current_position > length)
296 length = current_position;
301 public override void WriteByte (byte value)
304 throw new ObjectDisposedException("The stream is closed");
306 if (current_position == capacity)
307 throw new NotSupportedException("The current position is at the end of the capacity of the stream");
308 if (fileaccess == FileAccess.Read)
309 throw new NotSupportedException("Stream does not support writing.");
312 Marshal.WriteByte(initial_pointer, (int)current_position, value);
314 if (current_position > length)
315 length = current_position;
320 protected unsafe void Initialize (byte* pointer, long length,
325 this.length = length;
326 this.capacity = capacity;
327 initial_position = 0;
328 current_position = initial_position;
330 initial_pointer = new IntPtr ((void *)pointer);