1 //------------------------------------------------------------------------------
3 // System.IO.UnmanagedMemoryStream.cs
5 // Copyright (C) 2006 Sridhar Kulkarni, All Rights Reserved
7 // Author: Sridhar Kulkarni (sridharkulkarni@gmail.com)
8 // Created: Monday, July 10, 2006
10 //------------------------------------------------------------------------------
13 // Copyright (C) 2005-2006 Novell, Inc (http://www.novell.com)
15 // Permission is hereby granted, free of charge, to any person obtaining
16 // a copy of this software and associated documentation files (the
17 // "Software"), to deal in the Software without restriction, including
18 // without limitation the rights to use, copy, modify, merge, publish,
19 // distribute, sublicense, and/or sell copies of the Software, and to
20 // permit persons to whom the Software is furnished to do so, subject to
21 // the following conditions:
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
26 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
30 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
31 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
32 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
39 using System.Runtime.InteropServices;
44 [CLSCompliantAttribute(false)]
45 public class UnmanagedMemoryStream : Stream
51 FileAccess fileaccess;
52 IntPtr initial_pointer;
53 IntPtr pointer_position;
54 long initial_position;
55 long current_position;
58 protected UnmanagedMemoryStream()
60 fileaccess = FileAccess.Read;
64 current_position = initial_position;
67 public unsafe UnmanagedMemoryStream (byte *pointer, long len)
70 throw new ArgumentNullException("The pointer value is a null reference ");
72 throw new ArgumentOutOfRangeException("The length value is less than zero");
73 fileaccess = FileAccess.Read;
77 current_position = initial_position;
80 initial_pointer = new IntPtr((void*)pointer);
83 public unsafe UnmanagedMemoryStream (byte *pointer, long len, long cap, FileAccess access)
86 throw new ArgumentNullException("The pointer value is a null reference");
88 throw new ArgumentOutOfRangeException("The length value is less than zero");
90 throw new ArgumentOutOfRangeException("The capacity value is less than zero");
92 throw new ArgumentOutOfRangeException("The length value is greater than the capacity value");
97 current_position = initial_position;
99 initial_pointer = new IntPtr ((void*)pointer);
106 public override bool CanRead {
111 return ((fileaccess == FileAccess.Read || fileaccess == FileAccess.ReadWrite)? true:false);
115 public override bool CanSeek {
117 return ((closed) ? false : true);
121 public override bool CanWrite {
126 return ((fileaccess == FileAccess.Write || fileaccess == FileAccess.ReadWrite)? true:false);
129 public long Capacity {
132 throw new ObjectDisposedException("The stream is closed");
137 public override long Length {
140 throw new ObjectDisposedException("The stream is closed");
145 public override long Position {
148 throw new ObjectDisposedException("The stream is closed");
150 return (current_position);
154 throw new ObjectDisposedException("The stream is closed");
155 if (value < 0 || value > (long)Int32.MaxValue || value > capacity)
156 throw new ArgumentOutOfRangeException("value that is less than zero, or the position is larger than Int32.MaxValue or capacity of the stream");
158 current_position = value;
162 public unsafe byte* PositionPointer {
164 throw new NotImplementedException("Error");
167 throw new NotImplementedException("Error");
173 public override int Read ([InAttribute] [OutAttribute] byte[] buffer, int offset, int count)
176 throw new ObjectDisposedException("The stream is closed");
180 throw new ArgumentNullException("The buffer parameter is set to a null reference");
181 if (offset < 0 || count < 0)
182 throw new ArgumentOutOfRangeException("The offset or count parameter is less than zero");
183 if ((buffer.Length - offset) < count)
184 throw new ArgumentException("The length of the buffer array minus the offset parameter is less than the count parameter");
186 if (fileaccess == FileAccess.Write)
187 throw new NotSupportedException("Read property is false");
189 if (current_position == capacity)
193 Marshal.Copy(initial_pointer, buffer, offset,
195 current_position += length;
197 return (buffer.GetLength(0));
201 public override int ReadByte () {
203 throw new ObjectDisposedException("The stream is closed");
204 if (current_position == capacity)
205 throw new NotSupportedException("The current position is at the end of the stream");
209 if (fileaccess== FileAccess.Write)
210 throw new NotSupportedException("The underlying memory does not support reading");
212 if (current_position == length)
216 byteread = (int)Marshal.ReadByte(initial_pointer, (int)current_position);
223 public override long Seek (long offset, SeekOrigin loc) {
225 throw new ObjectDisposedException("The stream is closed");
226 if (offset > capacity)
227 throw new ArgumentOutOfRangeException("The offset value is larger than the maximum size of the stream");
230 case SeekOrigin.Begin:
232 throw new IOException("An attempt was made to seek before the beginning of the stream");
233 refpoint = initial_position;
235 case SeekOrigin.Current:
236 refpoint = current_position;
242 throw new ArgumentException("Invalid SeekOrigin option");
244 refpoint =+ (int)offset;
245 if (refpoint < initial_position)
246 throw new IOException("An attempt was made to seek before the beginning of the stream");
247 current_position = refpoint;
248 return(current_position);
251 public override void SetLength (long value)
254 throw new ObjectDisposedException("The stream is closed");
255 if (value < 0 || value > capacity)
256 throw new ArgumentOutOfRangeException("The specified value is negative or exceeds the capacity of the stream");
257 if (fileaccess == FileAccess.Read)
258 throw new NotSupportedException("write property is set to false");
259 if (fileaccess == FileAccess.Read)
260 throw new NotSupportedException("Length change not supported; see object construction");
265 public override void Flush ()
268 throw new ObjectDisposedException("The stream is closed");
269 //This method performs no action for this class
270 //but is included as part of the Stream base class
273 protected override void Dispose (bool disposing)
279 public override void Write (byte[] buffer, int offset, int count)
282 throw new ObjectDisposedException("The stream is closed");
284 throw new ArgumentNullException("The buffer parameter is a null reference");
285 if (count > capacity)
286 throw new ArgumentOutOfRangeException("The count value is greater than the capacity of the stream");
287 if (offset < 0 || count < 0)
288 throw new ArgumentOutOfRangeException("One of the specified parameters is less than zero");
289 if ((buffer.Length - offset) < count)
290 throw new ArgumentException("The length of the buffer array minus the offset parameter is less than the count parameter");
291 if (fileaccess == FileAccess.Read)
292 throw new NotSupportedException("write property is set to false");
295 //COPY data from managed buffer to unmanaged mem pointer
296 Marshal.Copy(buffer, offset, initial_pointer, (int)length);
297 current_position += length;
302 public override void WriteByte (byte value)
305 throw new ObjectDisposedException("The stream is closed");
307 if (current_position == capacity)
308 throw new NotSupportedException("The current position is at the end of the capacity of the stream");
309 if (fileaccess == FileAccess.Read)
310 throw new NotSupportedException("write property is set to false");
313 Marshal.WriteByte(initial_pointer, (int)current_position, value);