1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
|
//
// System.IO.MemoryStream
//
// Author: Marcin Szczepanski (marcins@zipworld.com.au)
//
// TODO: Clarify some of the lamespec issues
//
namespace System.IO {
[Serializable]
public class MemoryStream : Stream {
private bool canRead;
private bool canSeek;
private bool canWrite;
private bool allowGetBuffer;
private int capacity;
private byte[] internalBuffer;
private int initialLength;
private bool expandable;
private bool streamClosed = false;
private long position = 0;
public MemoryStream() {
canRead = true;
canSeek = true;
canWrite = true;
capacity = 0;
internalBuffer = new byte[0];
allowGetBuffer = true;
expandable = true;
}
public MemoryStream( byte[] buffer ) {
InternalConstructor( buffer, 0, buffer.Length, true, false );
}
public MemoryStream( int capacity ) {
canRead = true;
canSeek = true;
canWrite = true;
this.capacity = capacity;
initialLength = 0;
internalBuffer = new byte[ 0 ];
expandable = true;
allowGetBuffer = true;
}
public MemoryStream( byte[] buffer, bool writeable ) {
if( buffer == null ) {
throw new ArgumentNullException();
}
InternalConstructor( buffer, 0, buffer.Length, writeable, true );
}
public MemoryStream( byte[] buffer, int index, int count ) {
if( buffer == null ) {
throw new ArgumentNullException();
}
InternalConstructor( buffer, index, count, true, false );
}
public MemoryStream( byte[] buffer, int index, int count, bool writeable ) {
if( buffer == null ) {
throw new ArgumentNullException();
}
InternalConstructor( buffer, index, count, writeable, true );
}
public MemoryStream( byte[] buffer, int index, int count, bool writeable, bool publicallyVisible ) {
InternalConstructor( buffer, index, count, writeable, publicallyVisible );
}
private void InternalConstructor( byte[] buffer, int index, int count, bool writeable, bool publicallyVisible ) {
if( buffer == null ) {
throw new ArgumentNullException();
} else if ( index < 0 || count < 0 ) {
throw new ArgumentOutOfRangeException();
} else if ( buffer.Length - index < count ) {
throw new ArgumentException();
}
// LAMESPEC: The spec says to throw an UnauthorisedAccessException if
// publicallyVisibile is fale?! Doesn't that defy the point of having
// it there in the first place. I'll leave it out for now.
canRead = true;
canSeek = true;
canWrite = writeable;
initialLength = count;
internalBuffer = new byte[ count ];
capacity = count;
Array.Copy( buffer, index, internalBuffer, 0, count );
allowGetBuffer = publicallyVisible;
expandable = false;
}
public override bool CanRead {
get {
return this.canRead;
}
}
public override bool CanSeek {
get {
return this.canSeek;
}
}
public override bool CanWrite {
get {
return this.canWrite;
}
}
public virtual int Capacity {
get {
return this.capacity;
}
set {
if( value < 0 || value < capacity ) {
throw new ArgumentOutOfRangeException("value",
"New capacity cannot be negative or less than the current capacity" );
} else if( !expandable ) {
throw new NotSupportedException( "Cannot expand this MemoryStream" );
}
byte[] newBuffer = new byte[ value ];
Array.Copy( internalBuffer, 0, newBuffer, 0, capacity );
capacity = value;
}
}
public override long Length {
get {
// LAMESPEC: The spec says to throw an IOException if the
// stream is closed and an ObjectDisposedException if
// "methods were called after the stream was closed". What
// is the difference?
if( streamClosed ) {
throw new IOException( "MemoryStream is closed" );
}
return internalBuffer.Length;
}
}
public override long Position {
get {
if( streamClosed ) {
throw new IOException( "MemoryStream is closed" );
}
return position;
}
set {
if( position < 0 ) {
throw new ArgumentOutOfRangeException ("value", "Position cannot be negative" );
} else if (position > Int32.MaxValue) {
throw new ArgumentOutOfRangeException ("value",
"Length must be non-negative and less than 2^31 - 1 - origin");
} else if( streamClosed ) {
throw new IOException( "MemoryStream is closed" );
}
position = value;
}
}
public override void Close() {
if( streamClosed ) {
return;
}
streamClosed = true;
internalBuffer = null;
}
public override void Flush() { }
public virtual byte[] GetBuffer() {
if( !allowGetBuffer ) {
throw new UnauthorizedAccessException();
}
return internalBuffer;
}
public override int Read( byte[] buffer, int offset, int count ) {
if( buffer == null ) {
throw new ArgumentNullException();
} else if( offset < 0 || count < 0 ) {
throw new ArgumentOutOfRangeException();
} else if( buffer.Length - offset < count ) {
throw new ArgumentException();
} else if ( streamClosed ) {
throw new ObjectDisposedException( "MemoryStream" );
}
long ReadTo;
if( position + count > internalBuffer.Length ) {
ReadTo = internalBuffer.Length;
} else {
ReadTo = position + (long)count;
}
Array.Copy( internalBuffer, (int)position, buffer, offset, (int)(ReadTo - position) );
int bytesRead = (int)(ReadTo - position);
position = ReadTo;
return bytesRead;
}
public override int ReadByte( ) {
if( streamClosed ) {
throw new ObjectDisposedException( "MemoryStream" );
}
// LAMESPEC: What happens if we're at the end of the stream? It's unspecified in the
// docs but tests against the MS impl. show it returns -1
//
if( position >= internalBuffer.Length ) {
return -1;
} else {
return internalBuffer[ position++ ];
}
}
public override long Seek( long offset, SeekOrigin loc ) {
long refPoint;
if( streamClosed ) {
throw new ObjectDisposedException( "MemoryStream" );
}
switch( loc ) {
case SeekOrigin.Begin:
refPoint = 0;
break;
case SeekOrigin.Current:
refPoint = position;
break;
case SeekOrigin.End:
refPoint = internalBuffer.Length;
break;
default:
throw new ArgumentException( "Invalid SeekOrigin" );
}
// LAMESPEC: My goodness, how may LAMESPECs are there in this
// class! :) In the spec for the Position property it's stated
// "The position must not be more than one byte beyond the end of the stream."
// In the spec for seek it says "Seeking to any location beyond the length of the
// stream is supported." That's a contradiction i'd say.
// I guess seek can go anywhere but if you use position it may get moved back.
if( refPoint + offset < 0 ) {
throw new IOException( "Attempted to seek before start of MemoryStream" );
} else if( offset > internalBuffer.Length ) {
throw new ArgumentOutOfRangeException("offset",
"Offset cannot be greater than length of MemoryStream" );
}
position = refPoint + offset;
return position;
}
public override void SetLength( long value ) {
if( streamClosed ) {
throw new ObjectDisposedException( "MemoryStream" );
} else if( !expandable && value > capacity ) {
throw new NotSupportedException( "Expanding this MemoryStream is not supported" );
} else if( !canWrite ) {
throw new IOException( "Cannot write to this MemoryStream" );
} else if( value < 0 ) {
// LAMESPEC: AGAIN! It says to throw this exception if value is
// greater than "the maximum length of the MemoryStream". I haven't
// seen anywhere mention what the maximum length of a MemoryStream is and
// since we're this far this memory stream is expandable.
throw new ArgumentOutOfRangeException();
}
byte[] newBuffer;
newBuffer = new byte[ value ];
if (value < internalBuffer.Length) {
// truncate
Array.Copy( internalBuffer, 0, newBuffer, 0, (int)value );
} else {
// expand
Array.Copy( internalBuffer, 0, newBuffer, 0, internalBuffer.Length );
}
internalBuffer = newBuffer;
capacity = (int)value;
}
public virtual byte[] ToArray() {
if( streamClosed ) {
throw new ArgumentException( "The MemoryStream has been closed" );
}
byte[] outBuffer = new byte[capacity];
Array.Copy( internalBuffer, 0, outBuffer, 0, capacity);
return outBuffer;
}
public override void Write( byte[] buffer, int offset, int count ) {
if( buffer == null ) {
throw new ArgumentNullException();
} else if( !canWrite ) {
throw new NotSupportedException();
} else if( buffer.Length - offset < count ) {
throw new ArgumentException();
} else if( offset < 0 || count < 0 ) {
throw new ArgumentOutOfRangeException();
} else if( streamClosed ) {
throw new ObjectDisposedException( "MemoryStream" );
}
if( position + count > capacity ) {
if( expandable ) {
// expand the buffer
SetLength( position + count );
} else {
// only write as many bytes as will fit
count = (int)((long)capacity - position);
}
}
// internal buffer may not be allocated all the way up to capacity
// count will already be limited to capacity above if non-expandable
if( position + count >= internalBuffer.Length )
SetLength( position + count );
Array.Copy( buffer, offset, internalBuffer, (int)position, count );
position += count;
}
public override void WriteByte( byte value ) {
if ( streamClosed )
throw new ObjectDisposedException( "MemoryStream" );
else if( !canWrite || (position >= capacity && !expandable))
throw new NotSupportedException();
if( position >= internalBuffer.Length )
SetLength ( position + 1 );
internalBuffer[ position++ ] = value;
}
public virtual void WriteTo( Stream stream ) {
if( stream == null ) {
throw new ArgumentNullException();
}
stream.Write( internalBuffer, 0, internalBuffer.Length );
}
}
}
|