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 IntPtr pointer_position;
50 long initial_position;
51 long current_position;
54 protected UnmanagedMemoryStream()
56 fileaccess = FileAccess.Read;
60 current_position = initial_position;
63 public unsafe UnmanagedMemoryStream (byte *pointer, long length)
66 throw new ArgumentNullException("pointer");
68 throw new ArgumentOutOfRangeException("length", "Non-negative number required.");
69 fileaccess = FileAccess.Read;
73 current_position = initial_position;
76 initial_pointer = new IntPtr((void*)pointer);
79 public unsafe UnmanagedMemoryStream (byte *pointer, long length, long capacity, FileAccess access)
82 throw new ArgumentNullException("pointer");
84 throw new ArgumentOutOfRangeException("length", "Non-negative number required.");
86 throw new ArgumentOutOfRangeException("capacity", "Non-negative number required.");
87 if (length > capacity)
88 throw new ArgumentOutOfRangeException("length", "The length cannot be greater than the capacity.");
89 if (!Enum.IsDefined (typeof (FileAccess), access))
90 throw new ArgumentOutOfRangeException ("access", "Enum value was out of legal range.");
93 this.capacity = capacity;
95 current_position = initial_position;
97 initial_pointer = new IntPtr ((void*)pointer);
104 public override bool CanRead {
108 return (fileaccess == FileAccess.Read || fileaccess == FileAccess.ReadWrite);
112 public override bool CanSeek {
118 public override bool CanWrite {
122 return (fileaccess == FileAccess.Write || fileaccess == FileAccess.ReadWrite);
125 public long Capacity {
128 throw new ObjectDisposedException("The stream is closed");
133 public override long Length {
136 throw new ObjectDisposedException("The stream is closed");
141 public override long Position {
144 throw new ObjectDisposedException("The stream is closed");
145 return (current_position);
149 throw new ObjectDisposedException("The stream is closed");
151 throw new ArgumentOutOfRangeException("value", "Non-negative number required.");
152 if (value > (long)Int32.MaxValue)
153 throw new ArgumentOutOfRangeException("value", "The position is larger than Int32.MaxValue.");
154 current_position = value;
158 public unsafe byte* PositionPointer {
160 throw new NotImplementedException("Error");
163 throw new NotImplementedException("Error");
169 public override int Read ([InAttribute] [OutAttribute] byte[] buffer, int offset, int count)
172 throw new ObjectDisposedException("The stream is closed");
175 throw new ArgumentNullException("buffer");
177 throw new ArgumentOutOfRangeException("offset", "Non-negative number required.");
179 throw new ArgumentOutOfRangeException("count", "Non-negative number required.");
180 if ((buffer.Length - offset) < count)
181 throw new ArgumentException("The length of the buffer array minus the offset parameter is less than the count parameter");
183 if (fileaccess == FileAccess.Write)
184 throw new NotSupportedException("Stream does not support reading");
186 if (current_position >= length)
189 int progress = current_position + count < length ? count : (int) (length - current_position);
190 for (int i = 0; i < progress; i++)
191 buffer [offset + i] = Marshal.ReadByte (initial_pointer, (int) current_position++);
196 public override int ReadByte () {
198 throw new ObjectDisposedException("The stream is closed");
200 if (fileaccess== FileAccess.Write)
201 throw new NotSupportedException("Stream does not support reading");
203 if (current_position >= length)
205 return (int) Marshal.ReadByte(initial_pointer, (int) current_position++);
208 public override long Seek (long offset, SeekOrigin loc) {
210 throw new ObjectDisposedException("The stream is closed");
214 case SeekOrigin.Begin:
216 throw new IOException("An attempt was made to seek before the beginning of the stream");
217 refpoint = initial_position;
219 case SeekOrigin.Current:
220 refpoint = current_position;
226 throw new ArgumentException("Invalid SeekOrigin option");
228 refpoint += (int)offset;
229 if (refpoint < initial_position)
230 throw new IOException("An attempt was made to seek before the beginning of the stream");
231 current_position = refpoint;
232 return(current_position);
235 public override void SetLength (long value)
238 throw new ObjectDisposedException("The stream is closed");
240 throw new ArgumentOutOfRangeException("length", "Non-negative number required.");
241 if (value > capacity)
242 throw new IOException ("Unable to expand length of this stream beyond its capacity.");
243 if (fileaccess == FileAccess.Read)
244 throw new NotSupportedException ("Stream does not support writing.");
246 if (length < current_position)
247 current_position = length;
250 public override void Flush ()
253 throw new ObjectDisposedException("The stream is closed");
254 //This method performs no action for this class
255 //but is included as part of the Stream base class
258 protected override void Dispose (bool disposing)
264 public override void Write (byte[] buffer, int offset, int count)
267 throw new ObjectDisposedException("The stream is closed");
269 throw new ArgumentNullException("The buffer parameter is a null reference");
270 if ((current_position + count) > capacity)
271 throw new NotSupportedException ("Unable to expand length of this stream beyond its capacity.");
273 throw new ArgumentOutOfRangeException("offset", "Non-negative number required.");
275 throw new ArgumentOutOfRangeException("count", "Non-negative number required.");
276 if ((buffer.Length - offset) < count)
277 throw new ArgumentException("The length of the buffer array minus the offset parameter is less than the count parameter");
278 if (fileaccess == FileAccess.Read)
279 throw new NotSupportedException ("Stream does not support writing.");
282 // use Marshal.WriteByte since that allow us to start writing
283 // from the current position
284 for (int i = 0; i < count; i++)
285 Marshal.WriteByte (initial_pointer, (int) current_position++, buffer [offset + i]);
287 if (current_position > length)
288 length = current_position;
293 public override void WriteByte (byte value)
296 throw new ObjectDisposedException("The stream is closed");
298 if (current_position == capacity)
299 throw new NotSupportedException("The current position is at the end of the capacity of the stream");
300 if (fileaccess == FileAccess.Read)
301 throw new NotSupportedException("Stream does not support writing.");
304 Marshal.WriteByte(initial_pointer, (int)current_position, value);
306 if (current_position > length)
307 length = current_position;
312 protected unsafe void Initialize (byte* pointer, long length,
317 this.length = length;
318 this.capacity = capacity;
319 initial_position = 0;
320 current_position = initial_position;
322 initial_pointer = new IntPtr ((void *)pointer);