stub out things to get the beatles xbox site up and running
[moon.git] / class / System.Windows / System.IO / SimpleUnmanagedMemoryStream.cs
blobe9555a08bebcee34c4813da95e8721b8c88e42c7
1 //
2 // System.IO.SimpleUnmanagedMemoryStream.cs
3 //
4 // Copyright (C) 2006 Sridhar Kulkarni, All Rights Reserved
5 //
6 // Authors:
7 // Sridhar Kulkarni (sridharkulkarni@gmail.com)
8 // Gert Driesen (drieseng@users.sourceforge.net)
9 //
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.
32 using System;
33 using System.IO;
34 using System.Runtime.InteropServices;
36 namespace System.IO
38 internal class SimpleUnmanagedMemoryStream : Stream
40 long length;
41 long capacity;
42 IntPtr initial_pointer;
43 long initial_position;
44 long current_position;
46 protected SimpleUnmanagedMemoryStream()
48 initial_position = 0;
49 current_position = initial_position;
52 public unsafe SimpleUnmanagedMemoryStream (byte *pointer, long length)
54 if (pointer == null)
55 throw new ArgumentNullException("pointer");
56 if (length < 0)
57 throw new ArgumentOutOfRangeException("length", "Non-negative number required.");
58 this.length = length;
59 capacity = length;
60 initial_position = 0;
61 current_position = initial_position;
62 initial_pointer = new IntPtr((void*)pointer);
65 public override bool CanRead {
66 get {
67 return true;
71 public override bool CanSeek {
72 get {
73 return true;
77 public override bool CanWrite {
78 get {
79 return false;
83 public override long Length {
84 get {
85 return (length);
88 public override long Position {
89 get {
90 return (current_position);
92 set {
93 if (value < 0)
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)
103 if (buffer == null)
104 throw new ArgumentNullException("buffer");
105 if (offset < 0)
106 throw new ArgumentOutOfRangeException("offset", "Non-negative number required.");
107 if (count < 0)
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)
113 return (0);
114 else {
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++);
118 return progress;
122 public override int ReadByte ()
124 if (current_position >= length)
125 return (-1);
126 return (int) Marshal.ReadByte(initial_pointer, (int) current_position++);
129 public override long Seek (long offset, SeekOrigin loc)
131 long refpoint;
132 switch(loc) {
133 case SeekOrigin.Begin:
134 if (offset < 0)
135 throw new IOException("An attempt was made to seek before the beginning of the stream");
136 refpoint = initial_position;
137 break;
138 case SeekOrigin.Current:
139 refpoint = current_position;
140 break;
141 case SeekOrigin.End:
142 refpoint = length;
143 break;
144 default:
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)
156 if (value < 0)
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.");
160 length = value;
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.");