12 [__DynamicallyInvokable]
15 private byte[] _buffer;
19 private int _position;
23 private int _capacity;
25 private bool _expandable;
27 private bool _writable;
29 private bool _exposable;
36 private const int MemStreamMaxLength =
int.MaxValue;
41 [__DynamicallyInvokable]
44 [__DynamicallyInvokable]
54 [__DynamicallyInvokable]
57 [__DynamicallyInvokable]
67 [__DynamicallyInvokable]
70 [__DynamicallyInvokable]
83 [__DynamicallyInvokable]
86 [__DynamicallyInvokable]
91 __Error.StreamIsClosed();
93 return _capacity - _origin;
95 [__DynamicallyInvokable]
104 __Error.StreamIsClosed();
106 if (!_expandable && value !=
Capacity)
108 __Error.MemoryStreamNotExpandable();
110 if (!_expandable || value == _capacity)
116 byte[] array =
new byte[value];
119 Buffer.InternalBlockCopy(_buffer, 0, array, 0, _length);
134 [__DynamicallyInvokable]
135 public override long Length 137 [__DynamicallyInvokable]
142 __Error.StreamIsClosed();
144 return _length - _origin;
152 [__DynamicallyInvokable]
155 [__DynamicallyInvokable]
160 __Error.StreamIsClosed();
162 return _position - _origin;
164 [__DynamicallyInvokable]
173 __Error.StreamIsClosed();
175 if (value >
int.MaxValue)
179 _position = _origin + (int)value;
184 [__DynamicallyInvokable]
194 [__DynamicallyInvokable]
201 _buffer =
new byte[capacity];
202 _capacity = capacity;
214 [__DynamicallyInvokable]
216 : this(buffer, writable: true)
225 [__DynamicallyInvokable]
233 _length = (_capacity = buffer.Length);
234 _writable = writable;
249 [__DynamicallyInvokable]
251 : this(buffer, index, count, writable: true, publiclyVisible: false)
265 [__DynamicallyInvokable]
266 public MemoryStream(
byte[] buffer,
int index,
int count,
bool writable)
267 : this(buffer, index, count, writable, publiclyVisible: false)
283 [__DynamicallyInvokable]
284 public MemoryStream(
byte[] buffer,
int index,
int count,
bool writable,
bool publiclyVisible)
298 if (buffer.Length - index < count)
303 _origin = (_position = index);
304 _length = (_capacity = index + count);
305 _writable = writable;
306 _exposable = publiclyVisible;
311 private void EnsureWriteable()
315 __Error.WriteNotSupported();
322 [__DynamicallyInvokable]
323 protected override void Dispose(
bool disposing)
332 _lastReadTask =
null;
341 private bool EnsureCapacity(
int value)
347 if (value > _capacity)
354 if (num < _capacity * 2)
358 if ((uint)(_capacity * 2) > 2147483591u)
360 num = ((value > 2147483591) ? value : 2147483591);
369 [__DynamicallyInvokable]
379 [__DynamicallyInvokable]
380 [HostProtection(
SecurityAction.LinkDemand, ExternalThreading =
true)]
385 return Task.FromCancellation(cancellationToken);
414 [__DynamicallyInvokable]
426 internal byte[] InternalGetBuffer()
431 [FriendAccessAllowed]
432 internal void InternalGetOriginAndLength(out
int origin, out
int length)
436 __Error.StreamIsClosed();
442 internal int InternalGetPosition()
446 __Error.StreamIsClosed();
451 internal int InternalReadInt32()
455 __Error.StreamIsClosed();
457 int num = _position += 4;
463 return _buffer[num - 4] | (_buffer[num - 3] << 8) | (_buffer[num - 2] << 16) | (_buffer[num - 1] << 24);
466 internal int InternalEmulateRead(
int count)
470 __Error.StreamIsClosed();
472 int num = _length - _position;
497 [__DynamicallyInvokable]
498 public override int Read([In] [Out]
byte[] buffer,
int offset,
int count)
512 if (buffer.Length - offset < count)
518 __Error.StreamIsClosed();
520 int num = _length - _position;
534 buffer[offset + num2] = _buffer[_position + num2];
539 Buffer.InternalBlockCopy(_buffer, _position, buffer, offset, num);
560 [__DynamicallyInvokable]
561 [HostProtection(
SecurityAction.LinkDemand, ExternalThreading =
true)]
576 if (buffer.Length - offset < count)
582 return Task.FromCancellation<
int>(cancellationToken);
586 int num =
Read(buffer, offset, count);
588 return (lastReadTask !=
null && lastReadTask.
Result == num) ? lastReadTask : (_lastReadTask =
Task.FromResult(num));
592 return Task.FromCancellation<
int>(exception);
603 [__DynamicallyInvokable]
608 __Error.StreamIsClosed();
610 if (_position >= _length)
614 return _buffer[_position++];
628 [__DynamicallyInvokable]
631 if (destination ==
null)
657 return base.CopyToAsync(destination, bufferSize, cancellationToken);
661 return Task.FromCancellation(cancellationToken);
663 int position = _position;
664 int count = InternalEmulateRead(_length - _position);
666 if (memoryStream ==
null)
668 return destination.
WriteAsync(_buffer, position, count, cancellationToken);
672 memoryStream.
Write(_buffer, position, count);
691 [__DynamicallyInvokable]
696 __Error.StreamIsClosed();
698 if (offset >
int.MaxValue)
706 int num3 = _origin + (int)offset;
707 if (offset < 0 || num3 < _origin)
716 int num2 = _position + (int)offset;
717 if (_position + offset < _origin || num2 < _origin)
726 int num = _length + (int)offset;
727 if (_length + offset < _origin || num < _origin)
745 [__DynamicallyInvokable]
748 if (value < 0 || value >
int.MaxValue)
753 if (value >
int.MaxValue - _origin)
757 int num = _origin + (int)value;
758 if (!EnsureCapacity(num) && num > _length)
771 [__DynamicallyInvokable]
774 byte[] array =
new byte[_length - _origin];
775 Buffer.InternalBlockCopy(_buffer, _origin, array, 0, _length - _origin);
792 [__DynamicallyInvokable]
793 public override void Write(
byte[] buffer,
int offset,
int count)
807 if (buffer.Length - offset < count)
813 __Error.StreamIsClosed();
816 int num = _position + count;
823 bool flag = _position > _length;
824 if (num > _capacity && EnsureCapacity(num))
834 if (count <= 8 && buffer != _buffer)
839 _buffer[_position + num2] = buffer[offset + num2];
844 Buffer.InternalBlockCopy(buffer, offset, _buffer, _position, count);
864 [__DynamicallyInvokable]
865 [HostProtection(
SecurityAction.LinkDemand, ExternalThreading =
true)]
880 if (buffer.Length - offset < count)
886 return Task.FromCancellation(cancellationToken);
890 Write(buffer, offset, count);
895 return Task.FromCancellation<VoidTaskResult>(exception);
907 [__DynamicallyInvokable]
912 __Error.StreamIsClosed();
915 if (_position >= _length)
917 int num = _position + 1;
918 bool flag = _position > _length;
919 if (num >= _capacity && EnsureCapacity(num))
925 Array.
Clear(_buffer, _length, _position - _length);
929 _buffer[_position++] = value;
937 [__DynamicallyInvokable]
946 __Error.StreamIsClosed();
948 stream.
Write(_buffer, _origin, _length - _origin);
override bool CanRead
Gets a value indicating whether the current stream supports reading.
MemoryStream(byte[] buffer)
Initializes a new non-resizable instance of the T:System.IO.MemoryStream class based on the specified...
abstract void Write(byte[] buffer, int offset, int count)
When overridden in a derived class, writes a sequence of bytes to the current stream and advances the...
override void WriteByte(byte value)
Writes a byte to the current stream at the current position.
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
override int Read([In] [Out] byte[] buffer, int offset, int count)
Reads a block of bytes from the current stream and writes the data to a buffer.
override long Length
Gets the length of the stream in bytes.
Propagates notification that operations should be canceled.
override Task CopyToAsync(Stream destination, int bufferSize, CancellationToken cancellationToken)
Asynchronously reads all the bytes from the current stream and writes them to another stream,...
override void SetLength(long value)
Sets the length of the current stream to the specified value.
override bool CanSeek
Gets a value indicating whether the current stream supports seeking.
TResult Result
Gets the result value of this T:System.Threading.Tasks.Task`1.
MemoryStream(int capacity)
Initializes a new instance of the T:System.IO.MemoryStream class with an expandable capacity initiali...
static void Clear(Array array, int index, int length)
Sets a range of elements in an array to the default value of each element type.
The exception that is thrown when the value of an argument is outside the allowable range of values a...
abstract bool CanWrite
When overridden in a derived class, gets a value indicating whether the current stream supports writi...
virtual void WriteTo(Stream stream)
Writes the entire contents of this memory stream to another stream.
override bool CanWrite
Gets a value indicating whether the current stream supports writing.
bool IsCancellationRequested
Gets whether cancellation has been requested for this token.
override void Flush()
Overrides the M:System.IO.Stream.Flush method so that no action is performed.
SeekOrigin
Specifies the position in a stream to use for seeking.
The exception that is thrown when an operation is performed on a disposed object.
virtual byte [] GetBuffer()
Returns the array of unsigned bytes from which this stream was created.
abstract bool CanRead
When overridden in a derived class, gets a value indicating whether the current stream supports readi...
SecurityAction
Specifies the security actions that can be performed using declarative security.
Provides information about, and means to manipulate, the current environment and platform....
MemoryStream(byte[] buffer, int index, int count, bool writable)
Initializes a new non-resizable instance of the T:System.IO.MemoryStream class based on the specified...
override void Write(byte[] buffer, int offset, int count)
Writes a block of bytes to the current stream using data read from a buffer.
Creates a stream whose backing store is memory.To browse the .NET Framework source code for this type...
virtual byte [] ToArray()
Writes the stream contents to a byte array, regardless of the P:System.IO.MemoryStream....
MemoryStream()
Initializes a new instance of the T:System.IO.MemoryStream class with an expandable capacity initiali...
static Task FromException(Exception exception)
Creates a T:System.Threading.Tasks.Task that has completed with a specified exception.
The exception that is thrown when an I/O error occurs.
override Task< int > ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
Asynchronously reads a sequence of bytes from the current stream, advances the position within the st...
Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the ba...
static Task CompletedTask
Gets a task that has already completed successfully.
override void Dispose(bool disposing)
Releases the unmanaged resources used by the T:System.IO.MemoryStream class and optionally releases t...
override Task FlushAsync(CancellationToken cancellationToken)
Asynchronously clears all buffers for this stream, and monitors cancellation requests.
void Dispose()
Releases all resources used by the current instance of the T:System.Threading.Tasks....
override long Seek(long offset, SeekOrigin loc)
Sets the position within the current stream to the specified value.
The exception that is thrown when one of the arguments provided to a method is not valid.
virtual int Capacity
Gets or sets the number of bytes allocated for this stream.
The exception that is thrown in a thread upon cancellation of an operation that the thread was execut...
virtual bool TryGetBuffer(out ArraySegment< byte > buffer)
Returns the array of unsigned bytes from which this stream was created. The return value indicates wh...
Represents errors that occur during application execution.To browse the .NET Framework source code fo...
override int ReadByte()
Reads a byte from the current stream.
MemoryStream(byte[] buffer, int index, int count, bool writable, bool publiclyVisible)
Initializes a new instance of the T:System.IO.MemoryStream class based on the specified region of a b...
Specifies that the class can be serialized.
MemoryStream(byte[] buffer, bool writable)
Initializes a new non-resizable instance of the T:System.IO.MemoryStream class based on the specified...
Task WriteAsync(byte[] buffer, int offset, int count)
Asynchronously writes a sequence of bytes to the current stream and advances the current position wit...
Manipulates arrays of primitive types.
The exception that is thrown when the operating system denies access because of an I/O error or a spe...
MemoryStream(byte[] buffer, int index, int count)
Initializes a new non-resizable instance of the T:System.IO.MemoryStream class based on the specified...
The exception that is thrown when an invoked method is not supported, or when there is an attempt to ...
override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
Asynchronously writes a sequence of bytes to the current stream, advances the current position within...
override long Position
Gets or sets the current position within the stream.
Provides a generic view of a sequence of bytes. This is an abstract class.To browse the ....