2 // System.IO.SimpleUnmanagedMemoryStream.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.
34 using System
.Runtime
.InteropServices
;
38 internal class SimpleUnmanagedMemoryStream
: Stream
42 IntPtr initial_pointer
;
43 long initial_position
;
44 long current_position
;
46 protected SimpleUnmanagedMemoryStream()
49 current_position
= initial_position
;
52 public unsafe SimpleUnmanagedMemoryStream (byte *pointer
, long length
)
55 throw new ArgumentNullException("pointer");
57 throw new ArgumentOutOfRangeException("length", "Non-negative number required.");
61 current_position
= initial_position
;
62 initial_pointer
= new IntPtr((void*)pointer
);
65 public override bool CanRead
{
71 public override bool CanSeek
{
77 public override bool CanWrite
{
83 public override long Length
{
88 public override long Position
{
90 return (current_position
);
94 throw new ArgumentOutOfRangeException("value", "Non-negative number required.");
95 if (value > (long)Int32
.MaxValue
)
96 throw new ArgumentOutOfRangeException("value", "The position is larger than Int32.MaxValue.");
97 current_position
= value;
101 public override int Read ([InAttribute
] [OutAttribute
] byte[] buffer
, int offset
, int count
)
104 throw new ArgumentNullException("buffer");
106 throw new ArgumentOutOfRangeException("offset", "Non-negative number required.");
108 throw new ArgumentOutOfRangeException("count", "Non-negative number required.");
109 if ((buffer
.Length
- offset
) < count
)
110 throw new ArgumentException("The length of the buffer array minus the offset parameter is less than the count parameter");
112 if (current_position
>= length
)
115 int progress
= current_position
+ count
< length
? count
: (int) (length
- current_position
);
116 for (int i
= 0; i
< progress
; i
++)
117 buffer
[offset
+ i
] = Marshal
.ReadByte (initial_pointer
, (int) current_position
++);
122 public override int ReadByte ()
124 if (current_position
>= length
)
126 return (int) Marshal
.ReadByte(initial_pointer
, (int) current_position
++);
129 public override long Seek (long offset
, SeekOrigin loc
)
133 case SeekOrigin
.Begin
:
135 throw new IOException("An attempt was made to seek before the beginning of the stream");
136 refpoint
= initial_position
;
138 case SeekOrigin
.Current
:
139 refpoint
= current_position
;
145 throw new ArgumentException("Invalid SeekOrigin option");
147 refpoint
+= (int)offset
;
148 if (refpoint
< initial_position
)
149 throw new IOException("An attempt was made to seek before the beginning of the stream");
150 current_position
= refpoint
;
151 return(current_position
);
154 public override void SetLength (long value)
157 throw new ArgumentOutOfRangeException("length", "Non-negative number required.");
158 if (value > capacity
)
159 throw new IOException ("Unable to expand length of this stream beyond its capacity.");
161 if (length
< current_position
)
162 current_position
= length
;
165 public override void Flush ()
169 protected override void Dispose (bool disposing
)
173 public override void Write (byte[] buffer
, int offset
, int count
)
175 throw new NotSupportedException ("Stream does not support writing.");
178 public override void WriteByte (byte value)
180 throw new NotSupportedException("Stream does not support writing.");